From 3f866a07d8d0710ecddf5ad18f7db275871af94a Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 28 Nov 2023 10:11:53 +0100 Subject: [PATCH] Fix docker compose PR location default value --- .../Livewire/Project/Application/General.php | 7 +++- app/Models/Application.php | 41 +++++++++++++------ bootstrap/helpers/shared.php | 34 +++++++++++---- ..._24_080341_add_docker_compose_location.php | 2 +- .../project/application/general.blade.php | 7 ++-- 5 files changed, 65 insertions(+), 26 deletions(-) diff --git a/app/Http/Livewire/Project/Application/General.php b/app/Http/Livewire/Project/Application/General.php index da642ba77..2917fa438 100644 --- a/app/Http/Livewire/Project/Application/General.php +++ b/app/Http/Livewire/Project/Application/General.php @@ -60,7 +60,9 @@ class General extends Component 'application.docker_compose_location' => 'nullable', 'application.docker_compose_pr_location' => 'nullable', 'application.docker_compose' => 'nullable', + 'application.docker_compose_pr' => 'nullable', 'application.docker_compose_raw' => 'nullable', + 'application.docker_compose_pr_raw' => 'nullable', 'application.custom_labels' => 'nullable', 'application.dockerfile_target_build' => 'nullable', 'application.settings.is_static' => 'boolean|required', @@ -88,7 +90,9 @@ class General extends Component 'application.docker_compose_location' => 'Docker compose location', 'application.docker_compose_pr_location' => 'Docker compose location', 'application.docker_compose' => 'Docker compose', + 'application.docker_compose_pr' => 'Docker compose', 'application.docker_compose_raw' => 'Docker compose raw', + 'application.docker_compose_pr_raw' => 'Docker compose raw', 'application.custom_labels' => 'Custom labels', 'application.dockerfile_target_build' => 'Dockerfile target build', 'application.settings.is_static' => 'Is static', @@ -98,7 +102,6 @@ class General extends Component { try { $this->parsedServices = $this->application->parseCompose(); - ray($this->parsedServices); } catch (\Throwable $e) { $this->emit('error', $e->getMessage()); } @@ -196,7 +199,7 @@ class General extends Component public function submit($showToaster = true) { try { - if ($this->initialDockerComposeLocation !== $this->application->docker_compose_location) { + if ($this->initialDockerComposeLocation !== $this->application->docker_compose_location || $this->initialDockerComposePrLocation !== $this->application->docker_compose_pr_location) { $this->loadComposeFile(); } $this->validate(); diff --git a/app/Models/Application.php b/app/Models/Application.php index a7c721800..ea7e0a930 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -153,7 +153,7 @@ class Application extends BaseModel return Attribute::make( set: function ($value) { if (is_null($value) || $value === '') { - return '/docker-compose-pr.yaml'; + return '/docker-compose.yaml'; } else { if ($value !== '/') { return Str::start(Str::replaceEnd('/', '', $value), '/'); @@ -602,7 +602,11 @@ class Application extends BaseModel function parseCompose(int $pull_request_id = 0) { if ($this->docker_compose_raw) { - return parseDockerComposeFile(resource: $this, isNew: false, pull_request_id: $pull_request_id); + $mainCompose = parseDockerComposeFile(resource: $this, isNew: false, pull_request_id: $pull_request_id); + if ($this->getMorphClass() === 'App\Models\Application' && $this->docker_compose_pr_raw) { + parseDockerComposeFile(resource: $this, isNew: false, pull_request_id: $pull_request_id, is_pr: true); + } + return $mainCompose; } else { return collect([]); } @@ -620,11 +624,15 @@ class Application extends BaseModel $workdir = rtrim($this->base_directory, '/'); $composeFile = $this->docker_compose_location; $prComposeFile = $this->docker_compose_pr_location; + $fileList = collect([".$composeFile"]); + if ($composeFile !== $prComposeFile) { + $fileList->push(".$prComposeFile"); + } $commands = collect([ "mkdir -p /tmp/{$uuid} && cd /tmp/{$uuid}", $cloneCommand, "git sparse-checkout init --cone", - "git sparse-checkout set .$workdir$composeFile .$workdir$prComposeFile", + "git sparse-checkout set {$fileList->implode(' ')}", "git read-tree -mu HEAD", "cat .$workdir$composeFile", ]); @@ -632,23 +640,30 @@ class Application extends BaseModel if (!$composeFileContent) { $this->docker_compose_location = $initialDockerComposeLocation; $this->save(); - throw new \Exception("Could not load compose file from $workdir$composeFile"); + throw new \Exception("Could not load base compose file from $workdir$composeFile"); } else { $this->docker_compose_raw = $composeFileContent; $this->save(); } - $commands = collect([ - "cat .$workdir$prComposeFile", - ]); - $composePrFileContent = instant_remote_process($commands, $this->destination->server, false); - if (!$composePrFileContent) { - $this->docker_compose_pr_location = $initialDockerComposePrLocation; + if ($composeFile === $prComposeFile) { + $this->docker_compose_pr_raw = $composeFileContent; $this->save(); - throw new \Exception("Could not load compose file from $workdir$prComposeFile"); } else { - $this->docker_compose_pr_raw = $composePrFileContent; - $this->save(); + $commands = collect([ + "cd /tmp/{$uuid}", + "cat .$workdir$prComposeFile", + ]); + $composePrFileContent = instant_remote_process($commands, $this->destination->server, false); + if (!$composePrFileContent) { + $this->docker_compose_pr_location = $initialDockerComposePrLocation; + $this->save(); + throw new \Exception("Could not load compose file from $workdir$prComposeFile"); + } else { + $this->docker_compose_pr_raw = $composePrFileContent; + $this->save(); + } } + $commands = collect([ "rm -rf /tmp/{$uuid}", ]); diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.php index fcf32d215..2b2f32d33 100644 --- a/bootstrap/helpers/shared.php +++ b/bootstrap/helpers/shared.php @@ -579,7 +579,7 @@ function getTopLevelNetworks(Service|Application $resource) return $topLevelNetworks->keys(); } } -function parseDockerComposeFile(Service|Application $resource, bool $isNew = false, int $pull_request_id) +function parseDockerComposeFile(Service|Application $resource, bool $isNew = false, int $pull_request_id, bool $is_pr = false) { // ray()->clearAll(); if ($resource->getMorphClass() === 'App\Models\Service') { @@ -1089,8 +1089,17 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal return collect([]); } } else if ($resource->getMorphClass() === 'App\Models\Application') { - if ($pull_request_id !== 0 && $resource->dockerComposePrLocation() !== $resource->dockerComposeLocation()) { - + $isSameDockerComposeFile = false; + if ($resource->dockerComposePrLocation() === $resource->dockerComposeLocation()) { + $isSameDockerComposeFile = true; + $is_pr = false; + } + if ($is_pr) { + try { + $yaml = Yaml::parse($resource->docker_compose_pr_raw); + } catch (\Exception $e) { + throw new \Exception($e->getMessage()); + } } else { try { $yaml = Yaml::parse($resource->docker_compose_raw); @@ -1098,7 +1107,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal throw new \Exception($e->getMessage()); } } - + ray($yaml); $server = $resource->destination->server; $topLevelVolumes = collect(data_get($yaml, 'volumes', [])); if ($pull_request_id !== 0) { @@ -1172,7 +1181,6 @@ 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')); @@ -1469,8 +1477,20 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal 'volumes' => $topLevelVolumes->toArray(), 'networks' => $topLevelNetworks->toArray(), ]; - $resource->docker_compose_raw = Yaml::dump($yaml, 10, 2); - $resource->docker_compose = Yaml::dump($finalServices, 10, 2); + if ($isSameDockerComposeFile) { + $resource->docker_compose_pr_raw = Yaml::dump($yaml, 10, 2); + $resource->docker_compose_pr = Yaml::dump($finalServices, 10, 2); + $resource->docker_compose_raw = Yaml::dump($yaml, 10, 2); + $resource->docker_compose = Yaml::dump($finalServices, 10, 2); + } else { + if ($is_pr) { + $resource->docker_compose_pr_raw = Yaml::dump($yaml, 10, 2); + $resource->docker_compose_pr = Yaml::dump($finalServices, 10, 2); + } else { + $resource->docker_compose_raw = Yaml::dump($yaml, 10, 2); + $resource->docker_compose = Yaml::dump($finalServices, 10, 2); + } + } $resource->save(); return collect($finalServices); } diff --git a/database/migrations/2023_11_24_080341_add_docker_compose_location.php b/database/migrations/2023_11_24_080341_add_docker_compose_location.php index eab705ff0..8508181a3 100644 --- a/database/migrations/2023_11_24_080341_add_docker_compose_location.php +++ b/database/migrations/2023_11_24_080341_add_docker_compose_location.php @@ -13,7 +13,7 @@ return new class extends Migration { Schema::table('applications', function (Blueprint $table) { $table->string('docker_compose_location')->nullable()->default('/docker-compose.yaml')->after('dockerfile_location'); - $table->string('docker_compose_pr_location')->nullable()->default('/docker-compose-pr.yaml')->after('docker_compose_location'); + $table->string('docker_compose_pr_location')->nullable()->default('/docker-compose.yaml')->after('docker_compose_location'); $table->longText('docker_compose')->nullable()->after('docker_compose_location'); $table->longText('docker_compose_pr')->nullable()->after('docker_compose_location'); diff --git a/resources/views/livewire/project/application/general.blade.php b/resources/views/livewire/project/application/general.blade.php index b08a9fb60..daeac9710 100644 --- a/resources/views/livewire/project/application/general.blade.php +++ b/resources/views/livewire/project/application/general.blade.php @@ -119,9 +119,9 @@ - + helper="It is calculated together with the Base Directory:
{{ Str::start($application->base_directory . $application->docker_compose_pr_location, '/') }}" /> --}} @endif @if ($application->build_pack === 'dockerfile') build_pack === 'dockercompose') Reload Compose File - + {{-- --}} @endif @if ($application->dockerfile)