2023-12-07 12:07:16 +00:00
|
|
|
<?php
|
|
|
|
|
2023-12-07 18:06:32 +00:00
|
|
|
namespace App\Livewire\Project\Shared;
|
2023-12-07 12:07:16 +00:00
|
|
|
|
|
|
|
use App\Models\Application;
|
|
|
|
use App\Models\Server;
|
|
|
|
use App\Models\Service;
|
|
|
|
use App\Models\StandaloneMariadb;
|
|
|
|
use App\Models\StandaloneMongodb;
|
|
|
|
use App\Models\StandaloneMysql;
|
|
|
|
use App\Models\StandalonePostgresql;
|
|
|
|
use App\Models\StandaloneRedis;
|
2024-03-02 13:55:39 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2023-12-07 12:07:16 +00:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
class ExecuteContainerCommand extends Component
|
|
|
|
{
|
|
|
|
public string $command;
|
|
|
|
public string $container;
|
2024-03-02 13:55:39 +00:00
|
|
|
public Collection $containers;
|
2023-12-07 12:07:16 +00:00
|
|
|
public $parameters;
|
|
|
|
public $resource;
|
|
|
|
public string $type;
|
|
|
|
public string $workDir = '';
|
|
|
|
public Server $server;
|
2024-03-02 13:55:39 +00:00
|
|
|
public Collection $servers;
|
|
|
|
|
2023-12-07 12:07:16 +00:00
|
|
|
protected $rules = [
|
|
|
|
'server' => 'required',
|
|
|
|
'container' => 'required',
|
|
|
|
'command' => 'required',
|
|
|
|
'workDir' => 'nullable',
|
|
|
|
];
|
|
|
|
|
|
|
|
public function mount()
|
|
|
|
{
|
|
|
|
$this->parameters = get_route_parameters();
|
2023-12-08 12:55:55 +00:00
|
|
|
$this->containers = collect();
|
2024-03-02 13:55:39 +00:00
|
|
|
$this->servers = collect();
|
2023-12-07 12:07:16 +00:00
|
|
|
if (data_get($this->parameters, 'application_uuid')) {
|
|
|
|
$this->type = 'application';
|
|
|
|
$this->resource = Application::where('uuid', $this->parameters['application_uuid'])->firstOrFail();
|
2024-03-02 13:55:39 +00:00
|
|
|
if ($this->resource->destination->server->isFunctional()) {
|
|
|
|
$this->servers = $this->servers->push($this->resource->destination->server);
|
|
|
|
}
|
|
|
|
foreach ($this->resource->additional_servers as $server) {
|
|
|
|
if ($server->isFunctional()) {
|
|
|
|
$this->servers = $this->servers->push($server);
|
|
|
|
}
|
2023-12-07 12:07:16 +00:00
|
|
|
}
|
|
|
|
} else if (data_get($this->parameters, 'database_uuid')) {
|
|
|
|
$this->type = 'database';
|
|
|
|
$resource = StandalonePostgresql::where('uuid', $this->parameters['database_uuid'])->first();
|
|
|
|
if (is_null($resource)) {
|
|
|
|
$resource = StandaloneRedis::where('uuid', $this->parameters['database_uuid'])->first();
|
|
|
|
if (is_null($resource)) {
|
|
|
|
$resource = StandaloneMongodb::where('uuid', $this->parameters['database_uuid'])->first();
|
|
|
|
if (is_null($resource)) {
|
|
|
|
$resource = StandaloneMysql::where('uuid', $this->parameters['database_uuid'])->first();
|
|
|
|
if (is_null($resource)) {
|
|
|
|
$resource = StandaloneMariadb::where('uuid', $this->parameters['database_uuid'])->first();
|
|
|
|
if (is_null($resource)) {
|
|
|
|
abort(404);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->resource = $resource;
|
2024-03-02 13:55:39 +00:00
|
|
|
if ($this->resource->server->isFunctional()) {
|
|
|
|
$this->servers = $this->servers->push($this->resource->server);
|
|
|
|
}
|
2023-12-07 12:07:16 +00:00
|
|
|
$this->container = $this->resource->uuid;
|
2024-03-02 13:55:39 +00:00
|
|
|
$this->containers->push($this->container);
|
2023-12-07 12:07:16 +00:00
|
|
|
} else if (data_get($this->parameters, 'service_uuid')) {
|
|
|
|
$this->type = 'service';
|
|
|
|
$this->resource = Service::where('uuid', $this->parameters['service_uuid'])->firstOrFail();
|
|
|
|
$this->resource->applications()->get()->each(function ($application) {
|
2024-03-02 13:55:39 +00:00
|
|
|
$this->containers->push(data_get($application, 'name') . '-' . data_get($this->resource, 'uuid'));
|
2023-12-07 12:07:16 +00:00
|
|
|
});
|
|
|
|
$this->resource->databases()->get()->each(function ($database) {
|
2024-03-02 13:55:39 +00:00
|
|
|
$this->containers->push(data_get($database, 'name') . '-' . data_get($this->resource, 'uuid'));
|
2023-12-07 12:07:16 +00:00
|
|
|
});
|
2024-03-02 13:55:39 +00:00
|
|
|
if ($this->resource->server->isFunctional()) {
|
|
|
|
$this->servers = $this->servers->push($this->resource->server);
|
|
|
|
}
|
2023-12-07 12:07:16 +00:00
|
|
|
}
|
2023-12-07 12:48:23 +00:00
|
|
|
if ($this->containers->count() > 0) {
|
2023-12-07 12:07:16 +00:00
|
|
|
$this->container = $this->containers->first();
|
|
|
|
}
|
|
|
|
}
|
2024-03-02 13:55:39 +00:00
|
|
|
public function loadContainers()
|
|
|
|
{
|
|
|
|
foreach ($this->servers as $server) {
|
|
|
|
if (data_get($this->parameters, 'application_uuid')) {
|
|
|
|
if ($server->isSwarm()) {
|
|
|
|
$containers = collect([
|
|
|
|
[
|
|
|
|
'Names' => $this->resource->uuid . '_' . $this->resource->uuid,
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
$containers = getCurrentApplicationContainerStatus($server, $this->resource->id, includePullrequests: true);
|
|
|
|
}
|
|
|
|
foreach ($containers as $container) {
|
|
|
|
$payload = [
|
|
|
|
'server' => $server,
|
|
|
|
'container' => $container,
|
|
|
|
];
|
|
|
|
$this->containers = $this->containers->push($payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($containers->count() > 0) {
|
|
|
|
if (data_get($this->parameters, 'application_uuid')) {
|
|
|
|
$this->container = data_get($this->containers->first(), 'container.Names');
|
|
|
|
} elseif (data_get($this->parameters, 'database_uuid')) {
|
|
|
|
$this->container = $this->containers->first();
|
|
|
|
} elseif (data_get($this->parameters, 'service_uuid')) {
|
|
|
|
$this->container = $this->containers->first();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-07 12:07:16 +00:00
|
|
|
|
|
|
|
public function runCommand()
|
|
|
|
{
|
|
|
|
try {
|
2024-03-02 13:55:39 +00:00
|
|
|
if (data_get($this->parameters, 'application_uuid')) {
|
|
|
|
$container = $this->containers->where('container.Names', $this->container)->first();
|
|
|
|
$container_name = data_get($container, 'container.Names');
|
|
|
|
if (is_null($container)) {
|
|
|
|
throw new \RuntimeException('Container not found.');
|
|
|
|
}
|
|
|
|
$server = data_get($container, 'server');
|
|
|
|
} else {
|
|
|
|
$container_name = $this->container;
|
|
|
|
$server = $this->servers->first();
|
|
|
|
}
|
|
|
|
if ($server->isForceDisabled()) {
|
2024-02-26 10:25:38 +00:00
|
|
|
throw new \RuntimeException('Server is disabled.');
|
|
|
|
}
|
2024-01-11 13:13:43 +00:00
|
|
|
$cmd = 'sh -c "if [ -f ~/.profile ]; then . ~/.profile; fi; ' . str_replace('"', '\"', $this->command) . '"';
|
2023-12-07 12:07:16 +00:00
|
|
|
if (!empty($this->workDir)) {
|
2024-03-02 13:55:39 +00:00
|
|
|
$exec = "docker exec -w {$this->workDir} {$container_name} {$cmd}";
|
2023-12-07 12:07:16 +00:00
|
|
|
} else {
|
2024-03-02 13:55:39 +00:00
|
|
|
$exec = "docker exec {$container_name} {$cmd}";
|
2023-12-07 12:07:16 +00:00
|
|
|
}
|
2024-03-02 13:55:39 +00:00
|
|
|
$activity = remote_process([$exec], $server, ignore_errors: true);
|
2024-02-05 13:40:54 +00:00
|
|
|
$this->dispatch('activityMonitor', $activity->id);
|
2023-12-07 12:07:16 +00:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
return handleError($e, $this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
return view('livewire.project.shared.execute-container-command');
|
|
|
|
}
|
|
|
|
}
|