coolify/app/Livewire/Project/Shared/ExecuteContainerCommand.php

131 lines
5.0 KiB
PHP
Raw Normal View History

2023-12-07 13:07:16 +01:00
<?php
2023-12-07 19:06:32 +01:00
namespace App\Livewire\Project\Shared;
2023-12-07 13:07:16 +01: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;
use Livewire\Component;
class ExecuteContainerCommand extends Component
{
public string $command;
public string $container;
public $containers;
public $parameters;
public $resource;
public string $type;
public string $workDir = '';
public Server $server;
public $servers = [];
2023-12-08 13:55:55 +01:00
public function getListeners()
{
return [
"serviceStatusChanged",
];
}
public function serviceStatusChanged()
{
$this->getContainers();
}
2023-12-07 13:07:16 +01:00
protected $rules = [
'server' => 'required',
'container' => 'required',
'command' => 'required',
'workDir' => 'nullable',
];
public function mount()
{
$this->parameters = get_route_parameters();
2023-12-08 13:55:55 +01:00
$this->getContainers();
}
public function getContainers()
{
$this->containers = collect();
2023-12-07 13:07:16 +01:00
if (data_get($this->parameters, 'application_uuid')) {
$this->type = 'application';
$this->resource = Application::where('uuid', $this->parameters['application_uuid'])->firstOrFail();
$this->server = $this->resource->destination->server;
$containers = getCurrentApplicationContainerStatus($this->server, $this->resource->id, 0);
if ($containers->count() > 0) {
$containers->each(function ($container) {
$this->containers->push(str_replace('/', '', $container['Names']));
});
}
} 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;
$this->server = $this->resource->destination->server;
$this->container = $this->resource->uuid;
// if (!str(data_get($this,'resource.status'))->startsWith('exited')) {
2023-12-11 10:29:03 +01:00
$this->containers->push($this->container);
// }
2023-12-07 13:07:16 +01: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) {
// if (str(data_get($application, 'status'))->contains('running')) {
2023-12-07 13:07:16 +01:00
$this->containers->push(data_get($application, 'name') . '-' . data_get($this->resource, 'uuid'));
// }
2023-12-07 13:07:16 +01:00
});
$this->resource->databases()->get()->each(function ($database) {
// if (str(data_get($database, 'status'))->contains('running')) {
2023-12-07 13:07:16 +01:00
$this->containers->push(data_get($database, 'name') . '-' . data_get($this->resource, 'uuid'));
// }
2023-12-07 13:07:16 +01:00
});
$this->server = $this->resource->server;
}
2023-12-07 13:48:23 +01:00
if ($this->containers->count() > 0) {
2023-12-07 13:07:16 +01:00
$this->container = $this->containers->first();
}
}
public function runCommand()
{
$this->validate();
try {
if ($this->server->isForceDisabled()) {
throw new \RuntimeException('Server is disabled.');
}
// Wrap command to prevent escaped execution in the host.
2024-01-11 14:13:43 +01:00
$cmd = 'sh -c "if [ -f ~/.profile ]; then . ~/.profile; fi; ' . str_replace('"', '\"', $this->command) . '"';
2023-12-07 13:07:16 +01:00
if (!empty($this->workDir)) {
$exec = "docker exec -w {$this->workDir} {$this->container} {$cmd}";
2023-12-07 13:07:16 +01:00
} else {
$exec = "docker exec {$this->container} {$cmd}";
2023-12-07 13:07:16 +01:00
}
$activity = remote_process([$exec], $this->server, ignore_errors: true);
$this->dispatch('activityMonitor', $activity->id);
2023-12-07 13:07:16 +01:00
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
public function render()
{
return view('livewire.project.shared.execute-container-command');
}
}