From 14d9c06dcdf01178a44e5c6cb6eeb3c7c0186fc3 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 10 Oct 2023 11:16:38 +0200 Subject: [PATCH] feat: able to deploy docker images --- .../Livewire/Project/Application/General.php | 4 ++ app/Http/Livewire/Project/New/DockerImage.php | 7 ++- app/Jobs/ApplicationDeploymentJob.php | 25 +++++++++- app/Models/Application.php | 5 +- .../components/applications/navbar.blade.php | 4 +- .../project/application/general.blade.php | 46 +++++++++++-------- .../project/new/docker-image.blade.php | 2 +- 7 files changed, 66 insertions(+), 27 deletions(-) diff --git a/app/Http/Livewire/Project/Application/General.php b/app/Http/Livewire/Project/Application/General.php index 52212dc9e..93fcd250b 100644 --- a/app/Http/Livewire/Project/Application/General.php +++ b/app/Http/Livewire/Project/Application/General.php @@ -49,6 +49,8 @@ class General extends Component 'application.ports_exposes' => 'required', 'application.ports_mappings' => 'nullable', 'application.dockerfile' => 'nullable', + 'application.docker_registry_image_name' => 'nullable', + 'application.docker_registry_image_tag' => 'nullable', ]; protected $validationAttributes = [ 'application.name' => 'name', @@ -67,6 +69,8 @@ class General extends Component 'application.ports_exposes' => 'Ports exposes', 'application.ports_mappings' => 'Ports mappings', 'application.dockerfile' => 'Dockerfile', + 'application.docker_registry_image_name' => 'Docker registry image name', + 'application.docker_registry_image_tag' => 'Docker registry image tag', ]; diff --git a/app/Http/Livewire/Project/New/DockerImage.php b/app/Http/Livewire/Project/New/DockerImage.php index 5e1107e4d..1081f3401 100644 --- a/app/Http/Livewire/Project/New/DockerImage.php +++ b/app/Http/Livewire/Project/New/DockerImage.php @@ -26,7 +26,11 @@ class DockerImage extends Component 'dockerImage' => 'required' ]); $image = Str::of($this->dockerImage)->before(':'); - $tag = Str::of($this->dockerImage)->after(':') ?: 'latest'; + if (Str::of($this->dockerImage)->contains(':')) { + $tag = Str::of($this->dockerImage)->after(':'); + } else { + $tag = 'latest'; + } $destination_uuid = $this->query['destination']; $destination = StandaloneDocker::where('uuid', $destination_uuid)->first(); if (!$destination) { @@ -39,6 +43,7 @@ class DockerImage extends Component $project = Project::where('uuid', $this->parameters['project_uuid'])->first(); $environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first(); + ray($image,$tag); $application = Application::create([ 'name' => 'docker-image-' . new Cuid2(7), 'repository_project_id' => 0, diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index 57d89d932..a1dff9c93 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -45,6 +45,9 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted private string $commit; private bool $force_rebuild; + private ?string $dockerImage = null; + private ?string $dockerImageTag = null; + private GithubApp|GitlabApp|string $source = 'other'; private StandaloneDocker|SwarmDocker $destination; private Server $server; @@ -135,6 +138,8 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted try { if ($this->application->dockerfile) { $this->deploy_simple_dockerfile(); + } else if ($this->application->build_pack === 'dockerimage') { + $this->deploy_dockerimage(); } else { if ($this->pull_request_id !== 0) { $this->deploy_pull_request(); @@ -245,6 +250,21 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted $this->rolling_update(); } + private function deploy_dockerimage() + { + $this->dockerImage = $this->application->docker_registry_image_name; + $this->dockerImageTag = $this->application->docker_registry_image_tag; + ray("echo 'Starting deployment of {$this->dockerImage}:{$this->dockerImageTag}.'"); + $this->execute_remote_command( + [ + "echo 'Starting deployment of {$this->dockerImage}:{$this->dockerImageTag}.'" + ], + ); + $this->production_image_name = Str::lower("{$this->dockerImage}:{$this->dockerImageTag}"); + $this->prepare_builder_image(); + $this->generate_compose_file(); + $this->rolling_update(); + } private function deploy() { $this->execute_remote_command( @@ -398,7 +418,8 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted ); } - private function set_base_dir() { + private function set_base_dir() + { $this->execute_remote_command( [ "echo -n 'Setting base directory to {$this->workdir}.'" @@ -671,7 +692,7 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted private function generate_healthcheck_commands() { - if ($this->application->dockerfile || $this->application->build_pack === 'dockerfile') { + if ($this->application->dockerfile || $this->application->build_pack === 'dockerfile' || $this->application->build_pack === 'dockerimage') { // TODO: disabled HC because there are several ways to hc a simple docker image, hard to figure out a good way. Like some docker images (pocketbase) does not have curl. return 'exit 0'; } diff --git a/app/Models/Application.php b/app/Models/Application.php index 32eb227ca..cd3b43e05 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -259,13 +259,14 @@ class Application extends BaseModel if ($this->dockerfile) { return false; } - + if ($this->build_pack === 'dockerimage'){ + return false; + } return true; } public function isHealthcheckDisabled(): bool { if (data_get($this, 'dockerfile') || data_get($this, 'build_pack') === 'dockerfile' || data_get($this, 'health_check_enabled') === false) { - ray('dockerfile'); return true; } return false; diff --git a/resources/views/components/applications/navbar.blade.php b/resources/views/components/applications/navbar.blade.php index b84b0bf13..d38790789 100644 --- a/resources/views/components/applications/navbar.blade.php +++ b/resources/views/components/applications/navbar.blade.php @@ -16,7 +16,7 @@ @if ($application->status !== 'exited') -