fix/feat: able to open terminal to any containers

This commit is contained in:
Andras Bacsai
2024-09-17 11:54:25 +02:00
parent d92819ab60
commit 35b9b7fdf2
2 changed files with 27 additions and 52 deletions

View File

@@ -15,6 +15,9 @@ class RunCommand extends Component
public function mount($servers) public function mount($servers)
{ {
if (! auth()->user()->isAdmin()) {
abort(403);
}
$this->servers = $servers; $this->servers = $servers;
$this->containers = $this->getAllActiveContainers(); $this->containers = $this->getAllActiveContainers();
} }
@@ -26,63 +29,25 @@ class RunCommand extends Component
return []; return [];
} }
return $server->definedResources() return $server->loadAllContainers()->map(function ($container) use ($server) {
->filter(function ($resource) { $state = data_get_str($container, 'State')->lower();
$status = method_exists($resource, 'realStatus') ? $resource->realStatus() : (method_exists($resource, 'status') ? $resource->status() : 'exited'); if ($state->contains('running')) {
return str_starts_with($status, 'running:');
})
->map(function ($resource) use ($server) {
if (isDev()) {
if (data_get($resource, 'name') === 'coolify-db') {
$container_name = 'coolify-db';
return [ return [
'name' => $resource->name, 'name' => data_get($container, 'Names'),
'connection_name' => $container_name, 'connection_name' => data_get($container, 'Names'),
'uuid' => $resource->uuid, 'uuid' => data_get($container, 'Names'),
'status' => 'running', 'status' => data_get_str($container, 'State')->lower(),
'server' => $server, 'server' => $server,
'server_uuid' => $server->uuid, 'server_uuid' => $server->uuid,
]; ];
} }
}
if (class_basename($resource) === 'Application') { return null;
if (! $server->isSwarm()) { })->filter();
$current_containers = getCurrentApplicationContainerStatus($server, $resource->id, includePullrequests: true);
}
$status = $resource->status;
} elseif (class_basename($resource) === 'Service') {
$current_containers = getCurrentServiceContainerStatus($server, $resource->id);
$status = $resource->status();
} else {
$status = getContainerStatus($server, $resource->uuid);
if ($status === 'running') {
$current_containers = collect([
'Names' => $resource->name,
]);
}
}
if ($server->isSwarm()) {
$container_name = $resource->uuid.'_'.$resource->uuid;
} else {
$container_name = data_get($current_containers->first(), 'Names');
}
return [
'name' => $resource->name,
'connection_name' => $container_name,
'uuid' => $resource->uuid,
'status' => $status,
'server' => $server,
'server_uuid' => $server->uuid,
];
});
}); });
} }
public function updatedSelectedUuid($value) public function updatedSelectedUuid()
{ {
$this->connectToContainer(); $this->connectToContainer();
} }
@@ -95,9 +60,7 @@ class RunCommand extends Component
return; return;
} }
$container = collect($this->containers)->firstWhere('uuid', $this->selected_uuid); $container = collect($this->containers)->firstWhere('uuid', $this->selected_uuid);
$this->dispatch('send-terminal-command', $this->dispatch('send-terminal-command',
isset($container), isset($container),
$container['connection_name'] ?? $this->selected_uuid, $container['connection_name'] ?? $this->selected_uuid,

View File

@@ -775,6 +775,18 @@ $schema://$host {
} }
} }
public function loadAllContainers(): Collection
{
if ($this->isFunctional()) {
$containers = instant_remote_process(["docker ps -a --format '{{json .}}'"], $this);
$containers = format_docker_command_output_to_json($containers);
return collect($containers);
}
return collect([]);
}
public function loadUnmanagedContainers(): Collection public function loadUnmanagedContainers(): Collection
{ {
if ($this->isFunctional()) { if ($this->isFunctional()) {