From 7d6bd10ccafbf1573cb4455026899618de3739f9 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 16 Feb 2024 23:09:35 +0100 Subject: [PATCH] Add Docker container management methods and update Livewire component --- app/Livewire/Server/Resources.php | 23 +++ app/Models/Server.php | 25 ++- .../views/livewire/server/resources.blade.php | 180 +++++++++++++----- 3 files changed, 181 insertions(+), 47 deletions(-) diff --git a/app/Livewire/Server/Resources.php b/app/Livewire/Server/Resources.php index c3ef7f692..99d988271 100644 --- a/app/Livewire/Server/Resources.php +++ b/app/Livewire/Server/Resources.php @@ -4,6 +4,7 @@ use App\Models\Server; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; +use Illuminate\Support\Collection; use Livewire\Component; class Resources extends Component @@ -11,6 +12,7 @@ class Resources extends Component use AuthorizesRequests; public ?Server $server = null; public $parameters = []; + public Collection $unmanagedContainers; public function getListeners() { $teamId = auth()->user()->currentTeam()->id; @@ -19,11 +21,31 @@ public function getListeners() ]; } + public function startUnmanaged($id) { + $this->server->startUnmanaged($id); + $this->dispatch('success', 'Container started.'); + $this->loadUnmanagedContainers(); + } + public function restartUnmanaged($id) { + $this->server->restartUnmanaged($id); + $this->dispatch('success', 'Container restarted.'); + $this->loadUnmanagedContainers(); + } + public function stopUnmanaged($id) { + $this->server->stopUnmanaged($id); + $this->dispatch('success', 'Container stopped.'); + $this->loadUnmanagedContainers(); + } public function refreshStatus() { $this->server->refresh(); + $this->loadUnmanagedContainers(); $this->dispatch('success', 'Resource statuses refreshed.'); } + public function loadUnmanagedContainers() { + $this->unmanagedContainers = $this->server->loadUnmanagedContainers(); + } public function mount() { + $this->unmanagedContainers = collect(); $this->parameters = get_route_parameters(); try { $this->server = Server::ownedByCurrentTeam()->whereUuid(request()->server_uuid)->first(); @@ -33,6 +55,7 @@ public function mount() { } catch (\Throwable $e) { return handleError($e, $this); } + $this->loadUnmanagedContainers(); } public function render() { diff --git a/app/Models/Server.php b/app/Models/Server.php index ce34486ed..24799fe6e 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -225,6 +225,29 @@ public function definedResources() $services = $this->services(); return $applications->concat($databases)->concat($services->get()); } + public function stopUnmanaged($id) { + return instant_remote_process(["docker stop -t 0 $id"], $this); + } + public function restartUnmanaged($id) { + return instant_remote_process(["docker restart $id"], $this); + } + public function startUnmanaged($id) { + return instant_remote_process(["docker start $id"], $this); + } + public function loadUnmanagedContainers() + { + $containers = instant_remote_process(["docker ps -a --format '{{json .}}' "], $this); + $containers = format_docker_command_output_to_json($containers); + $containers = $containers->map(function ($container) { + $labels = data_get($container, 'Labels'); + if (!str($labels)->contains("coolify.managed")) { + return $container; + } + return null; + }); + $containers = $containers->filter(); + return collect($containers); + } public function hasDefinedResources() { $applications = $this->applications()->count() > 0; @@ -246,7 +269,7 @@ public function databases() $mariadbs = data_get($standaloneDocker, 'mariadbs', collect([])); return $postgresqls->concat($redis)->concat($mongodbs)->concat($mysqls)->concat($mariadbs); })->filter(function ($item) { - return data_get($item,'name') === 'coolify-db'; + return data_get($item, 'name') === 'coolify-db'; })->flatten(); } public function applications() diff --git a/resources/views/livewire/server/resources.blade.php b/resources/views/livewire/server/resources.blade.php index 964adc4b6..113ce4e63 100644 --- a/resources/views/livewire/server/resources.blade.php +++ b/resources/views/livewire/server/resources.blade.php @@ -1,55 +1,143 @@
-
-
-

Resources

- Refresh + +
+ -
Here you can find all resources for this server.
-
-
-
-
-
-
- - - - - - - - - - - - @forelse ($server->definedResources()->sortBy('name',SORT_NATURAL) as $resource) - - - - - - - - @empty - @endforelse - -
ProjectEnvironmentNameTypeStatus
- {{ data_get($resource->project(), 'name') }} - - {{ data_get($resource, 'environment.name') }} - {{ $resource->name }} - {{ str($resource->type())->headline() }} - @if ($resource->type() === 'service') - - @else - - @endif -
+
+
+
+
+

Resources

+ Refresh +
+
Here you can find all resources that are managed by Coolify.
+
+
+
+
+
+
+ + + + + + + + + + + + @forelse ($server->definedResources()->sortBy('name',SORT_NATURAL) as $resource) + + + + + + + + @empty + @endforelse + +
Project + + EnvironmentNameTypeStatus +
+ {{ data_get($resource->project(), 'name') }} + + {{ data_get($resource, 'environment.name') }} + {{ $resource->name }} + + {{ str($resource->type())->headline() }} + @if ($resource->type() === 'service') + + @else + + @endif +
+
+
+
+
+
+
+

Resources

+ Refresh +
+
Here you can find all other containers running on the server.
+
+ @if ($unmanagedContainers->count() > 0) +
+
+
+
+
+ + + + + + + + + + + @forelse ($unmanagedContainers->sortBy('name',SORT_NATURAL) as $resource) + + + + + + + @empty + @endforelse + +
Name + Image + Status + Action +
+ {{ data_get($resource, 'Names') }} + + {{ data_get($resource, 'Image') }} + + {{ data_get($resource, 'State') }} + + @if (data_get($resource, 'State') === 'running') + Restart + Stop + @elseif (data_get($resource, 'State') === 'exited') + Start + @elseif (data_get($resource, 'State') === 'restarting') + Stop + @endif +
+
+
+
+
+
+ @endif +
+ +