From b37dc4c73e408e908286d59fc6774b651d1e6450 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Thu, 11 Jul 2024 10:16:56 +0200 Subject: [PATCH] fix: remove networks when deleting a docker compose based app --- app/Actions/Application/StopApplication.php | 5 +++ app/Actions/Service/StopService.php | 10 +++--- bootstrap/helpers/proxy.php | 34 ++++++++++++++++++--- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/app/Actions/Application/StopApplication.php b/app/Actions/Application/StopApplication.php index 446659e5b..493f7eee4 100644 --- a/app/Actions/Application/StopApplication.php +++ b/app/Actions/Application/StopApplication.php @@ -38,6 +38,11 @@ public function handle(Application $application) } } } + if ($application->build_pack === 'dockercompose') { + $uuid = $application->uuid; + instant_remote_process(["docker network disconnect {$uuid} coolify-proxy"], $server); + instant_remote_process(["docker network rm {$uuid}"], $server); + } } } } diff --git a/app/Actions/Service/StopService.php b/app/Actions/Service/StopService.php index 4c0042ebd..c7b1170a7 100644 --- a/app/Actions/Service/StopService.php +++ b/app/Actions/Service/StopService.php @@ -19,18 +19,16 @@ public function handle(Service $service) ray('Stopping service: '.$service->name); $applications = $service->applications()->get(); foreach ($applications as $application) { - instant_remote_process(["docker rm -f {$application->name}-{$service->uuid}"], $service->server); + instant_remote_process(["docker rm -f {$application->name}-{$service->uuid}"], $service->server, false); $application->update(['status' => 'exited']); } $dbs = $service->databases()->get(); foreach ($dbs as $db) { - instant_remote_process(["docker rm -f {$db->name}-{$service->uuid}"], $service->server); + instant_remote_process(["docker rm -f {$db->name}-{$service->uuid}"], $service->server, false); $db->update(['status' => 'exited']); } - instant_remote_process(["docker network disconnect {$service->uuid} coolify-proxy 2>/dev/null"], $service->server, false); - instant_remote_process(["docker network rm {$service->uuid} 2>/dev/null"], $service->server, false); - // TODO: make notification for databases - // $service->environment->project->team->notify(new StatusChanged($service)); + instant_remote_process(["docker network disconnect {$service->uuid} coolify-proxy"], $service->server); + instant_remote_process(["docker network rm {$service->uuid}"], $service->server); } catch (\Exception $e) { echo $e->getMessage(); ray($e->getMessage()); diff --git a/bootstrap/helpers/proxy.php b/bootstrap/helpers/proxy.php index 2bf230c20..44352ea55 100644 --- a/bootstrap/helpers/proxy.php +++ b/bootstrap/helpers/proxy.php @@ -5,7 +5,24 @@ use App\Models\Server; use Symfony\Component\Yaml\Yaml; -function connectProxyToNetworks(Server $server) +function collectProxyDockerNetworksByServer(Server $server) +{ + if (! $server->isFunctional()) { + return collect(); + } + $proxyType = $server->proxyType(); + if (is_null($proxyType) || $proxyType === 'NONE') { + return collect(); + } + $networks = instant_remote_process(['docker inspect --format="{{json .NetworkSettings.Networks }}" coolify-proxy'], $server, false); + $networks = collect($networks)->map(function ($network) { + return collect(json_decode($network))->keys(); + })->flatten()->unique(); + + return $networks; + +} +function collectDockerNetworksByServer(Server $server) { if ($server->isSwarm()) { $networks = collect($server->swarmDockers)->map(function ($docker) { @@ -43,6 +60,18 @@ function connectProxyToNetworks(Server $server) if ($networks->count() === 0) { $networks = collect(['coolify-overlay']); } + } else { + if ($networks->count() === 0) { + $networks = collect(['coolify']); + } + } + + return $networks; +} +function connectProxyToNetworks(Server $server) +{ + $networks = collectDockerNetworksByServer($server); + if ($server->isSwarm()) { $commands = $networks->map(function ($network) { return [ "echo 'Connecting coolify-proxy to $network network...'", @@ -51,9 +80,6 @@ function connectProxyToNetworks(Server $server) ]; }); } else { - if ($networks->count() === 0) { - $networks = collect(['coolify']); - } $commands = $networks->map(function ($network) { return [ "echo 'Connecting coolify-proxy to $network network...'",