Skip to content

Commit 35b9b7f

Browse files
committed
fix/feat: able to open terminal to any containers
1 parent d92819a commit 35b9b7f

File tree

2 files changed

+27
-52
lines changed

2 files changed

+27
-52
lines changed

app/Livewire/RunCommand.php

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class RunCommand extends Component
1515

1616
public function mount($servers)
1717
{
18+
if (! auth()->user()->isAdmin()) {
19+
abort(403);
20+
}
1821
$this->servers = $servers;
1922
$this->containers = $this->getAllActiveContainers();
2023
}
@@ -26,63 +29,25 @@ private function getAllActiveContainers()
2629
return [];
2730
}
2831

29-
return $server->definedResources()
30-
->filter(function ($resource) {
31-
$status = method_exists($resource, 'realStatus') ? $resource->realStatus() : (method_exists($resource, 'status') ? $resource->status() : 'exited');
32-
33-
return str_starts_with($status, 'running:');
34-
})
35-
->map(function ($resource) use ($server) {
36-
if (isDev()) {
37-
if (data_get($resource, 'name') === 'coolify-db') {
38-
$container_name = 'coolify-db';
39-
40-
return [
41-
'name' => $resource->name,
42-
'connection_name' => $container_name,
43-
'uuid' => $resource->uuid,
44-
'status' => 'running',
45-
'server' => $server,
46-
'server_uuid' => $server->uuid,
47-
];
48-
}
49-
}
50-
51-
if (class_basename($resource) === 'Application') {
52-
if (! $server->isSwarm()) {
53-
$current_containers = getCurrentApplicationContainerStatus($server, $resource->id, includePullrequests: true);
54-
}
55-
$status = $resource->status;
56-
} elseif (class_basename($resource) === 'Service') {
57-
$current_containers = getCurrentServiceContainerStatus($server, $resource->id);
58-
$status = $resource->status();
59-
} else {
60-
$status = getContainerStatus($server, $resource->uuid);
61-
if ($status === 'running') {
62-
$current_containers = collect([
63-
'Names' => $resource->name,
64-
]);
65-
}
66-
}
67-
if ($server->isSwarm()) {
68-
$container_name = $resource->uuid.'_'.$resource->uuid;
69-
} else {
70-
$container_name = data_get($current_containers->first(), 'Names');
71-
}
72-
32+
return $server->loadAllContainers()->map(function ($container) use ($server) {
33+
$state = data_get_str($container, 'State')->lower();
34+
if ($state->contains('running')) {
7335
return [
74-
'name' => $resource->name,
75-
'connection_name' => $container_name,
76-
'uuid' => $resource->uuid,
77-
'status' => $status,
36+
'name' => data_get($container, 'Names'),
37+
'connection_name' => data_get($container, 'Names'),
38+
'uuid' => data_get($container, 'Names'),
39+
'status' => data_get_str($container, 'State')->lower(),
7840
'server' => $server,
7941
'server_uuid' => $server->uuid,
8042
];
81-
});
43+
}
44+
45+
return null;
46+
})->filter();
8247
});
8348
}
8449

85-
public function updatedSelectedUuid($value)
50+
public function updatedSelectedUuid()
8651
{
8752
$this->connectToContainer();
8853
}
@@ -95,9 +60,7 @@ public function connectToContainer()
9560

9661
return;
9762
}
98-
9963
$container = collect($this->containers)->firstWhere('uuid', $this->selected_uuid);
100-
10164
$this->dispatch('send-terminal-command',
10265
isset($container),
10366
$container['connection_name'] ?? $this->selected_uuid,

app/Models/Server.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,18 @@ public function getContainersWithSentinel(): Collection
775775
}
776776
}
777777

778+
public function loadAllContainers(): Collection
779+
{
780+
if ($this->isFunctional()) {
781+
$containers = instant_remote_process(["docker ps -a --format '{{json .}}'"], $this);
782+
$containers = format_docker_command_output_to_json($containers);
783+
784+
return collect($containers);
785+
}
786+
787+
return collect([]);
788+
}
789+
778790
public function loadUnmanagedContainers(): Collection
779791
{
780792
if ($this->isFunctional()) {

0 commit comments

Comments
 (0)