From 14919980c2e1cf7a40508770a198d720c149b49a Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 14 Apr 2023 13:18:55 +0200 Subject: [PATCH] feat: debuggable executeNow commands --- app/Http/Livewire/DeployApplication.php | 8 +++++ app/Jobs/ContainerStatusJob.php | 16 +++++----- app/Jobs/DeployApplicationJob.php | 32 +++++++++++++------ .../seeders/ApplicationSettingsSeeder.php | 8 ++--- .../livewire/deploy-application.blade.php | 1 + 5 files changed, 42 insertions(+), 23 deletions(-) diff --git a/app/Http/Livewire/DeployApplication.php b/app/Http/Livewire/DeployApplication.php index 8533d3357..a18d85f6b 100644 --- a/app/Http/Livewire/DeployApplication.php +++ b/app/Http/Livewire/DeployApplication.php @@ -53,6 +53,14 @@ public function stop() $this->application->status = 'stopped'; $this->application->save(); } + public function kill() + { + runRemoteCommandSync($this->destination->server, ["docker rm -f {$this->application_uuid}"]); + if ($this->application->status != 'exited') { + $this->application->status = 'exited'; + $this->application->save(); + } + } public function pollingStatus() { diff --git a/app/Jobs/ContainerStatusJob.php b/app/Jobs/ContainerStatusJob.php index e22a55aab..b66d9fd78 100644 --- a/app/Jobs/ContainerStatusJob.php +++ b/app/Jobs/ContainerStatusJob.php @@ -19,6 +19,14 @@ public function __construct( public string|null $container_id = null, ) { } + public function handle(): void + { + if ($this->container_id) { + $this->checkContainerStatus(); + } else { + $this->checkAllServers(); + } + } protected function checkAllServers() { try { @@ -69,12 +77,4 @@ protected function checkContainerStatus() Log::error($e->getMessage()); } } - public function handle(): void - { - if ($this->container_id) { - $this->checkContainerStatus(); - } else { - $this->checkAllServers(); - } - } } diff --git a/app/Jobs/DeployApplicationJob.php b/app/Jobs/DeployApplicationJob.php index b64857222..86a998d3b 100644 --- a/app/Jobs/DeployApplicationJob.php +++ b/app/Jobs/DeployApplicationJob.php @@ -97,7 +97,7 @@ public function handle(): void if ($this->activity->properties->get('stopped_container_check') == 0) { $this->executeNow([ - "echo 'Container {$this->application->uuid} was stopped, starting it...'" + "echo -n 'Container {$this->application->uuid} was stopped, starting it...'" ]); $this->executeNow([ "docker start {$this->application->uuid}" @@ -144,30 +144,39 @@ public function handle(): void $this->executeNow([ "echo -n 'Generating nixpacks configuration... '", + ]); + $this->executeNow([ $this->nixpacks_build_cmd(), $this->execute_in_builder("cp {$this->workdir}/.nixpacks/Dockerfile {$this->workdir}/Dockerfile"), $this->execute_in_builder("rm -f {$this->workdir}/.nixpacks/Dockerfile"), - "echo 'Done.'", - ]); + ], isDebuggable: true); + $this->executeNow([ + "echo 'Done.'", "echo -n 'Building image... '", - $this->execute_in_builder("docker build -f {$this->workdir}/Dockerfile --build-arg SOURCE_COMMIT={$this->git_commit} --progress plain -t {$this->application->uuid}:{$this->git_commit} {$this->workdir}"), - "echo 'Done.'", ]); + $this->executeNow([ - "echo -n 'Removing old container... '", + $this->execute_in_builder("docker build -f {$this->workdir}/Dockerfile --build-arg SOURCE_COMMIT={$this->git_commit} --progress plain -t {$this->application->uuid}:{$this->git_commit} {$this->workdir}"), + ], isDebuggable: true); + + $this->executeNow([ + "echo 'Done.'", + "echo -n 'Removing old instance... '", $this->execute_in_builder("docker rm -f {$this->application->uuid} >/dev/null 2>&1"), "echo 'Done.'", + "echo -n 'Starting your application... '", ]); $this->executeNow([ - "echo -n 'Starting new container... '", $this->execute_in_builder("docker compose --project-directory {$this->workdir} up -d >/dev/null"), + ], isDebuggable: true); + + $this->executeNow([ "echo 'Done. 🎉'", "docker stop -t 0 {$this->deployment_uuid} >/dev/null" ], setStatus: true); } - dispatch(new ContainerStatusJob($this->application_uuid)); // Saving docker-compose.yml @@ -305,9 +314,10 @@ private function set_labels_for_applications() return $labels; } - private function executeNow(array|Collection $command, string $propertyName = null, bool $hideFromOutput = false, $setStatus = false) + private function executeNow(array|Collection $command, string $propertyName = null, bool $hideFromOutput = false, $setStatus = false, bool $isDebuggable = false) { static::$batch_counter++; + if ($command instanceof Collection) { $commandText = $command->implode("\n"); } else { @@ -318,7 +328,9 @@ private function executeNow(array|Collection $command, string $propertyName = nu 'command' => $commandText, ]); $this->activity->save(); - + if ($isDebuggable && !$this->application->settings->is_debug) { + $hideFromOutput = true; + } $remoteProcess = resolve(RunRemoteProcess::class, [ 'activity' => $this->activity, 'hideFromOutput' => $hideFromOutput, diff --git a/database/seeders/ApplicationSettingsSeeder.php b/database/seeders/ApplicationSettingsSeeder.php index fceb32954..1b6068ad5 100644 --- a/database/seeders/ApplicationSettingsSeeder.php +++ b/database/seeders/ApplicationSettingsSeeder.php @@ -17,10 +17,8 @@ class ApplicationSettingsSeeder extends Seeder */ public function run(): void { - // $application_1 = Application::find(1); - // ApplicationSetting::create([ - // 'id' => 1, - // 'application_id' => $application_1->id, - // ]); + $application_1 = Application::find(1)->load(['settings']); + $application_1->settings->is_debug = false; + $application_1->settings->save(); } } diff --git a/resources/views/livewire/deploy-application.blade.php b/resources/views/livewire/deploy-application.blade.php index ecaf3119c..f10497bc1 100644 --- a/resources/views/livewire/deploy-application.blade.php +++ b/resources/views/livewire/deploy-application.blade.php @@ -4,5 +4,6 @@ @else @endif + status: {{ $application->status }}