From c1710c8f7bd24d10e1b50b87b35cef452cea47f7 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Mon, 27 Nov 2023 15:25:15 +0100 Subject: [PATCH] moar fixes --- app/Actions/Application/StopApplication.php | 10 ++ .../Livewire/Project/Application/General.php | 4 + .../Livewire/Project/Application/Heading.php | 2 +- .../Livewire/Project/Application/Previews.php | 2 +- app/Jobs/ApplicationDeploymentJob.php | 1 - app/Models/Application.php | 5 - app/Models/ApplicationPreview.php | 21 ++- bootstrap/helpers/shared.php | 15 +- .../components/applications/links.blade.php | 167 +++++++++--------- .../project/application/previews.blade.php | 7 +- 10 files changed, 138 insertions(+), 96 deletions(-) diff --git a/app/Actions/Application/StopApplication.php b/app/Actions/Application/StopApplication.php index a98384f45..1d09f0daf 100644 --- a/app/Actions/Application/StopApplication.php +++ b/app/Actions/Application/StopApplication.php @@ -25,5 +25,15 @@ class StopApplication // TODO: make notification for application // $application->environment->project->team->notify(new StatusChanged($application)); } + // Delete Preview Deployments + $previewDeployments = $application->previews; + foreach ($previewDeployments as $previewDeployment) { + $containers = getCurrentApplicationContainerStatus($server, $application->id, $previewDeployment->pull_request_id); + foreach ($containers as $container) { + $name = str_replace('/', '', $container['Names']); + instant_remote_process(["docker rm -f $name"], $application->destination->server, throwError: false); + } + } + } } diff --git a/app/Http/Livewire/Project/Application/General.php b/app/Http/Livewire/Project/Application/General.php index d108bca14..0284b40a9 100644 --- a/app/Http/Livewire/Project/Application/General.php +++ b/app/Http/Livewire/Project/Application/General.php @@ -152,6 +152,10 @@ class General extends Component $this->application->settings->is_static = $this->is_static = false; $this->application->settings->save(); } + if ($this->application->build_pack === 'dockercompose') { + $this->application->fqdn = null; + $this->application->settings->save(); + } $this->submit(); } public function checkLabelUpdates() diff --git a/app/Http/Livewire/Project/Application/Heading.php b/app/Http/Livewire/Project/Application/Heading.php index dae105cd6..14bdf1db0 100644 --- a/app/Http/Livewire/Project/Application/Heading.php +++ b/app/Http/Livewire/Project/Application/Heading.php @@ -41,7 +41,7 @@ class Heading extends Component public function deploy(bool $force_rebuild = false) { - if (!$this->application->deployableComposeBuildPack()) { + if ($this->application->build_pack === 'dockercompose' && is_null($this->application->docker_compose_raw)) { $this->emit('error', 'Please load a Compose file first.'); return; } diff --git a/app/Http/Livewire/Project/Application/Previews.php b/app/Http/Livewire/Project/Application/Previews.php index fd471a007..effdd7f7c 100644 --- a/app/Http/Livewire/Project/Application/Previews.php +++ b/app/Http/Livewire/Project/Application/Previews.php @@ -77,7 +77,7 @@ class Previews extends Component $name = str_replace('/', '', $container['Names']); instant_remote_process(["docker rm -f $name"], $this->application->destination->server, throwError: false); } - ApplicationPreview::where('application_id', $this->application->id)->where('pull_request_id', $pull_request_id)->delete(); + ApplicationPreview::where('application_id', $this->application->id)->where('pull_request_id', $pull_request_id)->first()->delete(); $this->application->refresh(); } catch (\Throwable $e) { return handleError($e, $this); diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index 6dca962bb..36907a917 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -949,7 +949,6 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted ]; } if ($this->application->settings->is_gpu_enabled) { - ray('asd'); $docker_compose['services'][$this->container_name]['deploy']['resources']['reservations']['devices'] = [ [ 'driver' => data_get($this->application, 'settings.gpu_driver', 'nvidia'), diff --git a/app/Models/Application.php b/app/Models/Application.php index 809d5ff6f..2e58d1891 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -48,10 +48,6 @@ class Application extends BaseModel $application->environment_variables_preview()->delete(); }); } - public function deployableComposeBuildPack() - { - return $this->build_pack === 'dockercompose' && $this->docker_compose_raw; - } public function link() { return route('project.application.configuration', [ @@ -262,7 +258,6 @@ class Application extends BaseModel { return $this->morphTo(); } - public function isDeploymentInprogress() { $deployments = ApplicationDeploymentQueue::where('application_id', $this->id)->where('status', 'in_progress')->count(); diff --git a/app/Models/ApplicationPreview.php b/app/Models/ApplicationPreview.php index 13775abae..87dce056e 100644 --- a/app/Models/ApplicationPreview.php +++ b/app/Models/ApplicationPreview.php @@ -5,7 +5,26 @@ namespace App\Models; class ApplicationPreview extends BaseModel { protected $guarded = []; - + protected static function booted() + { + static::deleting(function ($preview) { + if ($preview->application->build_pack === 'dockercompose') { + $server = $preview->application->destination->server; + $composeFile = $preview->application->parseCompose(pull_request_id: $preview->pull_request_id); + $volumes = data_get($composeFile, 'volumes'); + $networks = data_get($composeFile, 'networks'); + $networkKeys = collect($networks)->keys(); + $volumeKeys = collect($volumes)->keys(); + $volumeKeys->each(function ($key) use ($server) { + instant_remote_process(["docker volume rm -f $key"], $server, false); + }); + $networkKeys->each(function ($key) use ($server) { + instant_remote_process(["docker network disconnect $key coolify-proxy"], $server, false); + instant_remote_process(["docker network rm $key"], $server, false); + }); + } + }); + } static function findPreviewByApplicationAndPullId(int $application_id, int $pull_request_id) { return self::where('application_id', $application_id)->where('pull_request_id', $pull_request_id)->firstOrFail(); diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.php index df562d5ff..ecb67cd0c 100644 --- a/bootstrap/helpers/shared.php +++ b/bootstrap/helpers/shared.php @@ -1144,14 +1144,21 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal if ($volume->contains(':')) { $name = $volume->before(':'); $mount = $volume->after(':'); - $newName = $name . "-{$resource->uuid}-$pull_request_id"; + $newName = $resource->uuid . "-{$name}-pr-$pull_request_id"; $volume = str("$newName:$mount"); $topLevelVolumes->put($newName, [ 'name' => $newName, ]); } } else if (is_array($volume)) { - $volume['source'] = str($volume['source'])->append("-{$resource->uuid}-$pull_request_id"); + $source = data_get($volume, 'source'); + if ($source) { + $newSource = $resource->uuid . "-{$source}-pr-$pull_request_id"; + data_set($volume, 'source', $newSource); + $topLevelVolumes->put($newSource, [ + 'name' => $newSource, + ]); + } } @@ -1159,6 +1166,8 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal }); data_set($service, 'volumes', $serviceVolumes->toArray()); } + } else { + } // Decide if the service is a database $isDatabase = isDatabaseImage(data_get_str($service, 'image')); @@ -1202,7 +1211,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal if ($pull_request_id !== 0) { $topLevelNetworks->put($network, [ 'name' => $network, - 'external' => false + 'external' => true ]); } else { $topLevelNetworks->put($network, [ diff --git a/resources/views/components/applications/links.blade.php b/resources/views/components/applications/links.blade.php index 44d69df62..c5bdc4a48 100644 --- a/resources/views/components/applications/links.blade.php +++ b/resources/views/components/applications/links.blade.php @@ -1,21 +1,25 @@
- + @if (data_get($application, 'fqdn') || + collect(json_decode($this->application->docker_compose_domains))->count() > 0 || + data_get($application, 'previews', collect([]))->count() > 0 || + data_get($application, 'ports_mappings_array')) + - + @endif
diff --git a/resources/views/livewire/project/application/previews.blade.php b/resources/views/livewire/project/application/previews.blade.php index b02acc4ab..6663c71d3 100644 --- a/resources/views/livewire/project/application/previews.blade.php +++ b/resources/views/livewire/project/application/previews.blade.php @@ -71,15 +71,16 @@
- + @if (data_get($preview, 'status') === 'exited') Deploy @else Redeploy @endif - Remove - Preview + Remove Preview