Skip to content

Commit

Permalink
fix/feat: able to open terminal to any containers
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasbacsai committed Sep 17, 2024
1 parent d92819a commit 35b9b7f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 52 deletions.
67 changes: 15 additions & 52 deletions app/Livewire/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class RunCommand extends Component

public function mount($servers)
{
if (! auth()->user()->isAdmin()) {
abort(403);
}
$this->servers = $servers;
$this->containers = $this->getAllActiveContainers();
}
Expand All @@ -26,63 +29,25 @@ private function getAllActiveContainers()
return [];
}

return $server->definedResources()
->filter(function ($resource) {
$status = method_exists($resource, 'realStatus') ? $resource->realStatus() : (method_exists($resource, 'status') ? $resource->status() : 'exited');

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 [
'name' => $resource->name,
'connection_name' => $container_name,
'uuid' => $resource->uuid,
'status' => 'running',
'server' => $server,
'server_uuid' => $server->uuid,
];
}
}

if (class_basename($resource) === 'Application') {
if (! $server->isSwarm()) {
$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 $server->loadAllContainers()->map(function ($container) use ($server) {
$state = data_get_str($container, 'State')->lower();
if ($state->contains('running')) {
return [
'name' => $resource->name,
'connection_name' => $container_name,
'uuid' => $resource->uuid,
'status' => $status,
'name' => data_get($container, 'Names'),
'connection_name' => data_get($container, 'Names'),
'uuid' => data_get($container, 'Names'),
'status' => data_get_str($container, 'State')->lower(),
'server' => $server,
'server_uuid' => $server->uuid,
];
});
}

return null;
})->filter();
});
}

public function updatedSelectedUuid($value)
public function updatedSelectedUuid()
{
$this->connectToContainer();
}
Expand All @@ -95,9 +60,7 @@ public function connectToContainer()

return;
}

$container = collect($this->containers)->firstWhere('uuid', $this->selected_uuid);

$this->dispatch('send-terminal-command',
isset($container),
$container['connection_name'] ?? $this->selected_uuid,
Expand Down
12 changes: 12 additions & 0 deletions app/Models/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,18 @@ public function getContainersWithSentinel(): Collection
}
}

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
{
if ($this->isFunctional()) {
Expand Down

0 comments on commit 35b9b7f

Please sign in to comment.