From 022762c0c912778ab405886e56c5f55f62ef8b47 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Wed, 29 May 2024 15:11:17 +0200 Subject: [PATCH] refactor: applicationdeploymentjob --- app/Jobs/ApplicationDeploymentJob.php | 124 +++++++++++++------------- 1 file changed, 62 insertions(+), 62 deletions(-) diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index bc109abc6..e89fdb2e9 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -17,7 +17,6 @@ use App\Notifications\Application\DeploymentFailed; use App\Notifications\Application\DeploymentSuccess; use App\Traits\ExecuteRemoteCommand; -use Exception; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldBeEncrypted; use Illuminate\Contracts\Queue\ShouldQueue; @@ -27,12 +26,13 @@ use Illuminate\Support\Collection; use Illuminate\Support\Sleep; use Illuminate\Support\Str; -use RuntimeException; -use Spatie\Url\Url; use Symfony\Component\Yaml\Yaml; -use Throwable; use Visus\Cuid2\Cuid2; +use Spatie\Url\Url; use Yosymfony\Toml\Toml; +use Exception; +use RuntimeException; +use Throwable; class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted { @@ -44,7 +44,7 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted private int $application_deployment_queue_id; - private bool $newVersionIsHealthy = false; + private bool $is_new_version_healthy = false; private ApplicationDeploymentQueue $application_deployment_queue; private Application $application; private string $deployment_uuid; @@ -54,8 +54,8 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted private bool $force_rebuild; private bool $restart_only; - private ?string $dockerImage = null; - private ?string $dockerImageTag = null; + private ?string $docker_image = null; + private ?string $docker_image_tag = null; private GithubApp|GitlabApp|string $source = 'other'; private StandaloneDocker|SwarmDocker $destination; @@ -66,7 +66,7 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted private bool $use_build_server = false; // Save original server between phases private Server $original_server; - private Server $mainServer; + private Server $main_server; private ?ApplicationPreview $preview = null; private ?string $git_type = null; private bool $only_this_server = false; @@ -92,19 +92,19 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted private string $docker_compose_location = '/docker-compose.yml'; private ?string $docker_compose_custom_start_command = null; private ?string $docker_compose_custom_build_command = null; - private ?string $addHosts = null; - private ?string $buildTarget = null; + private ?string $add_hosts = null; + private ?string $build_target = null; private Collection $saved_outputs; private ?string $full_healthcheck_url = null; - private string $serverUser = 'root'; - private string $serverUserHomeDir = '/root'; - private string $dockerConfigFileExists = 'NOK'; + private string $server_user = 'root'; + private string $server_user_home_dir = '/root'; + private string $is_docker_config_file_exists = 'NOK'; - private int $customPort = 22; - private ?string $customRepository = null; + private int $custom_port = 22; + private ?string $custom_repository = null; - private ?string $fullRepoUrl = null; + private ?string $full_repository_url = null; private ?string $branch = null; private ?string $coolify_variables = null; @@ -134,8 +134,8 @@ public function __construct(int $application_deployment_queue_id) $this->server = Server::find($this->application_deployment_queue->server_id); $this->timeout = $this->server->settings->dynamic_timeout; $this->destination = $this->server->destinations()->where('id', $this->application_deployment_queue->destination_id)->first(); - $this->server = $this->mainServer = $this->destination->server; - $this->serverUser = $this->server->user; + $this->server = $this->main_server = $this->destination->server; + $this->server_user = $this->server->user; $this->basedir = $this->application->generateBaseDir($this->deployment_uuid); $this->workdir = "{$this->basedir}" . rtrim($this->application->base_directory, '/'); $this->configuration_dir = application_configuration_dir() . "/{$this->application->uuid}"; @@ -217,17 +217,17 @@ public function handle(): void } } } - $this->addHosts = $ips->map(function ($ip, $name) { + $this->add_hosts = $ips->map(function ($ip, $name) { return "--add-host $name:$ip"; })->implode(' '); } if ($this->application->dockerfile_target_build) { - $this->buildTarget = " --target {$this->application->dockerfile_target_build} "; + $this->build_target = " --target {$this->application->dockerfile_target_build} "; } // Check custom port - ['repository' => $this->customRepository, 'port' => $this->customPort] = $this->application->customRepository(); + ['repository' => $this->custom_repository, 'port' => $this->custom_port] = $this->application->custom_repository(); if (data_get($this->application, 'settings.is_build_server_enabled')) { $teamId = data_get($this->application, 'environment.project.team.id'); @@ -355,14 +355,14 @@ private function deploy_simple_dockerfile() } private function deploy_dockerimage_buildpack() { - $this->dockerImage = $this->application->docker_registry_image_name; + $this->docker_image = $this->application->docker_registry_image_name; if (str($this->application->docker_registry_image_tag)->isEmpty()) { - $this->dockerImageTag = 'latest'; + $this->docker_image_tag = 'latest'; } else { - $this->dockerImageTag = $this->application->docker_registry_image_tag; + $this->docker_image_tag = $this->application->docker_registry_image_tag; } - ray("echo 'Starting deployment of {$this->dockerImage}:{$this->dockerImageTag} to {$this->server->name}.'"); - $this->application_deployment_queue->addLogEntry("Starting deployment of {$this->dockerImage}:{$this->dockerImageTag} to {$this->server->name}."); + ray("echo 'Starting deployment of {$this->docker_image}:{$this->docker_image_tag} to {$this->server->name}.'"); + $this->application_deployment_queue->addLogEntry("Starting deployment of {$this->docker_image}:{$this->docker_image_tag} to {$this->server->name}."); $this->generate_image_names(); $this->prepare_builder_image(); $this->generate_compose_file(); @@ -382,7 +382,7 @@ private function deploy_docker_compose_buildpack() if ($this->pull_request_id === 0) { $this->application_deployment_queue->addLogEntry("Starting deployment of {$this->application->name} to {$this->server->name}."); } else { - $this->application_deployment_queue->addLogEntry("Starting pull request (#{$this->pull_request_id}) deployment of {$this->customRepository}:{$this->application->git_branch} to {$this->server->name}."); + $this->application_deployment_queue->addLogEntry("Starting pull request (#{$this->pull_request_id}) deployment of {$this->custom_repository}:{$this->application->git_branch} to {$this->server->name}."); } $this->prepare_builder_image(); $this->check_git_if_build_needed(); @@ -464,7 +464,7 @@ private function deploy_docker_compose_buildpack() } private function deploy_dockerfile_buildpack() { - $this->application_deployment_queue->addLogEntry("Starting deployment of {$this->customRepository}:{$this->application->git_branch} to {$this->server->name}."); + $this->application_deployment_queue->addLogEntry("Starting deployment of {$this->custom_repository}:{$this->application->git_branch} to {$this->server->name}."); if ($this->use_build_server) { $this->server = $this->build_server; } @@ -499,7 +499,7 @@ private function deploy_nixpacks_buildpack() if ($this->use_build_server) { $this->server = $this->build_server; } - $this->application_deployment_queue->addLogEntry("Starting deployment of {$this->customRepository}:{$this->application->git_branch} to {$this->server->name}."); + $this->application_deployment_queue->addLogEntry("Starting deployment of {$this->custom_repository}:{$this->application->git_branch} to {$this->server->name}."); $this->prepare_builder_image(); $this->check_git_if_build_needed(); $this->set_base_dir(); @@ -532,7 +532,7 @@ private function deploy_static_buildpack() if ($this->use_build_server) { $this->server = $this->build_server; } - $this->application_deployment_queue->addLogEntry("Starting deployment of {$this->customRepository}:{$this->application->git_branch} to {$this->server->name}."); + $this->application_deployment_queue->addLogEntry("Starting deployment of {$this->custom_repository}:{$this->application->git_branch} to {$this->server->name}."); $this->prepare_builder_image(); $this->check_git_if_build_needed(); $this->set_base_dir(); @@ -657,7 +657,7 @@ private function generate_image_names() $this->production_image_name = "{$this->application->uuid}:latest"; } } else if ($this->application->build_pack === 'dockerimage') { - $this->production_image_name = "{$this->dockerImage}:{$this->dockerImageTag}"; + $this->production_image_name = "{$this->docker_image}:{$this->docker_image_tag}"; } else if ($this->pull_request_id !== 0) { if ($this->application->docker_registry_image_name) { $this->build_image_name = "{$this->application->docker_registry_image_name}:pr-{$this->pull_request_id}-build"; @@ -667,22 +667,22 @@ private function generate_image_names() $this->production_image_name = "{$this->application->uuid}:pr-{$this->pull_request_id}"; } } else { - $this->dockerImageTag = str($this->commit)->substr(0, 128); + $this->docker_image_tag = str($this->commit)->substr(0, 128); if ($this->application->docker_registry_image_tag) { - $this->dockerImageTag = $this->application->docker_registry_image_tag; + $this->docker_image_tag = $this->application->docker_registry_image_tag; } if ($this->application->docker_registry_image_name) { - $this->build_image_name = "{$this->application->docker_registry_image_name}:{$this->dockerImageTag}-build"; - $this->production_image_name = "{$this->application->docker_registry_image_name}:{$this->dockerImageTag}"; + $this->build_image_name = "{$this->application->docker_registry_image_name}:{$this->docker_image_tag}-build"; + $this->production_image_name = "{$this->application->docker_registry_image_name}:{$this->docker_image_tag}"; } else { - $this->build_image_name = "{$this->application->uuid}:{$this->dockerImageTag}-build"; - $this->production_image_name = "{$this->application->uuid}:{$this->dockerImageTag}"; + $this->build_image_name = "{$this->application->uuid}:{$this->docker_image_tag}-build"; + $this->production_image_name = "{$this->application->uuid}:{$this->docker_image_tag}"; } } } private function just_restart() { - $this->application_deployment_queue->addLogEntry("Restarting {$this->customRepository}:{$this->application->git_branch} on {$this->server->name}."); + $this->application_deployment_queue->addLogEntry("Restarting {$this->custom_repository}:{$this->application->git_branch} on {$this->server->name}."); $this->prepare_builder_image(); $this->check_git_if_build_needed(); $this->set_base_dir(); @@ -879,7 +879,7 @@ private function framework_based_notification() $nixpacks_php_fallback_path = $this->application->environment_variables_preview->where('key', 'NIXPACKS_PHP_FALLBACK_PATH')->first(); $nixpacks_php_root_dir = $this->application->environment_variables_preview->where('key', 'NIXPACKS_PHP_ROOT_DIR')->first(); } - if ($nixpacks_php_fallback_path?->value === '/index.php' && $nixpacks_php_root_dir?->value === '/app/public' && $this->newVersionIsHealthy === false) { + if ($nixpacks_php_fallback_path?->value === '/index.php' && $nixpacks_php_root_dir?->value === '/app/public' && $this->is_new_version_healthy === false) { $this->application_deployment_queue->addLogEntry("There was a change in how Laravel is deployed. Please update your environment variables to match the new deployment method. More details here: https://coolify.io/docs/resources/laravel", 'stderr'); } } @@ -935,7 +935,7 @@ private function health_check() // Implement healthcheck for swarm } else { if ($this->application->isHealthcheckDisabled() && $this->application->custom_healthcheck_found === false) { - $this->newVersionIsHealthy = true; + $this->is_new_version_healthy = true; return; } if ($this->application->custom_healthcheck_found) { @@ -980,13 +980,13 @@ private function health_check() } if (Str::of($this->saved_outputs->get('health_check'))->replace('"', '')->value() === 'healthy') { - $this->newVersionIsHealthy = true; + $this->is_new_version_healthy = true; $this->application->update(['status' => 'running']); $this->application_deployment_queue->addLogEntry("New container is healthy."); break; } if (Str::of($this->saved_outputs->get('health_check'))->replace('"', '')->value() === 'unhealthy') { - $this->newVersionIsHealthy = false; + $this->is_new_version_healthy = false; $this->query_logs(); break; } @@ -1025,9 +1025,9 @@ private function deploy_pull_request() if ($this->use_build_server) { $this->server = $this->build_server; } - $this->newVersionIsHealthy = true; + $this->is_new_version_healthy = true; $this->generate_image_names(); - $this->application_deployment_queue->addLogEntry("Starting pull request (#{$this->pull_request_id}) deployment of {$this->customRepository}:{$this->application->git_branch}."); + $this->application_deployment_queue->addLogEntry("Starting pull request (#{$this->pull_request_id}) deployment of {$this->custom_repository}:{$this->application->git_branch}."); $this->prepare_builder_image(); $this->check_git_if_build_needed(); $this->clone_repository(); @@ -1079,16 +1079,16 @@ private function prepare_builder_image() { $helperImage = config('coolify.helper_image'); // Get user home directory - $this->serverUserHomeDir = instant_remote_process(["echo \$HOME"], $this->server); - $this->dockerConfigFileExists = instant_remote_process(["test -f {$this->serverUserHomeDir}/.docker/config.json && echo 'OK' || echo 'NOK'"], $this->server); + $this->server_user_home_dir = instant_remote_process(["echo \$HOME"], $this->server); + $this->is_docker_config_file_exists = instant_remote_process(["test -f {$this->server_user_home_dir}/.docker/config.json && echo 'OK' || echo 'NOK'"], $this->server); if ($this->use_build_server) { - if ($this->dockerConfigFileExists === 'NOK') { + if ($this->is_docker_config_file_exists === 'NOK') { throw new RuntimeException('Docker config file (~/.docker/config.json) not found on the build server. Please run "docker login" to login to the docker registry on the server.'); } - $runCommand = "docker run -d --name {$this->deployment_uuid} --rm -v {$this->serverUserHomeDir}/.docker/config.json:/root/.docker/config.json:ro -v /var/run/docker.sock:/var/run/docker.sock {$helperImage}"; + $runCommand = "docker run -d --name {$this->deployment_uuid} --rm -v {$this->server_user_home_dir}/.docker/config.json:/root/.docker/config.json:ro -v /var/run/docker.sock:/var/run/docker.sock {$helperImage}"; } else { - if ($this->dockerConfigFileExists === 'OK') { - $runCommand = "docker run -d --network {$this->destination->network} --name {$this->deployment_uuid} --rm -v {$this->serverUserHomeDir}/.docker/config.json:/root/.docker/config.json:ro -v /var/run/docker.sock:/var/run/docker.sock {$helperImage}"; + if ($this->is_docker_config_file_exists === 'OK') { + $runCommand = "docker run -d --network {$this->destination->network} --name {$this->deployment_uuid} --rm -v {$this->server_user_home_dir}/.docker/config.json:/root/.docker/config.json:ro -v /var/run/docker.sock:/var/run/docker.sock {$helperImage}"; } else { $runCommand = "docker run -d --network {$this->destination->network} --name {$this->deployment_uuid} --rm -v /var/run/docker.sock:/var/run/docker.sock {$helperImage}"; } @@ -1132,7 +1132,7 @@ private function deploy_to_additional_destinations() foreach ($destination_ids as $destination_id) { $destination = StandaloneDocker::find($destination_id); $server = $destination->server; - if ($server->team_id !== $this->mainServer->team_id) { + if ($server->team_id !== $this->main_server->team_id) { $this->application_deployment_queue->addLogEntry("Skipping deployment to {$server->name}. Not in the same team?!"); continue; } @@ -1195,7 +1195,7 @@ private function check_git_if_build_needed() executeInDocker($this->deployment_uuid, "chmod 600 /root/.ssh/id_rsa") ], [ - executeInDocker($this->deployment_uuid, "GIT_SSH_COMMAND=\"ssh -o ConnectTimeout=30 -p {$this->customPort} -o Port={$this->customPort} -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa\" git ls-remote {$this->fullRepoUrl} {$local_branch}"), + executeInDocker($this->deployment_uuid, "GIT_SSH_COMMAND=\"ssh -o ConnectTimeout=30 -p {$this->custom_port} -o Port={$this->custom_port} -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa\" git ls-remote {$this->full_repository_url} {$local_branch}"), "hidden" => true, "save" => "git_commit_sha" ], @@ -1203,7 +1203,7 @@ private function check_git_if_build_needed() } else { $this->execute_remote_command( [ - executeInDocker($this->deployment_uuid, "GIT_SSH_COMMAND=\"ssh -o ConnectTimeout=30 -p {$this->customPort} -o Port={$this->customPort} -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" git ls-remote {$this->fullRepoUrl} {$local_branch}"), + executeInDocker($this->deployment_uuid, "GIT_SSH_COMMAND=\"ssh -o ConnectTimeout=30 -p {$this->custom_port} -o Port={$this->custom_port} -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" git ls-remote {$this->full_repository_url} {$local_branch}"), "hidden" => true, "save" => "git_commit_sha" ], @@ -1220,7 +1220,7 @@ private function clone_repository() { $importCommands = $this->generate_git_import_commands(); $this->application_deployment_queue->addLogEntry("\n----------------------------------------"); - $this->application_deployment_queue->addLogEntry("Importing {$this->customRepository}:{$this->application->git_branch} (commit sha {$this->application->git_commit_sha}) to {$this->basedir}."); + $this->application_deployment_queue->addLogEntry("Importing {$this->custom_repository}:{$this->application->git_branch} (commit sha {$this->application->git_commit_sha}) to {$this->basedir}."); if ($this->pull_request_id !== 0) { $this->application_deployment_queue->addLogEntry("Checking out tag pull/{$this->pull_request_id}/head."); } @@ -1248,7 +1248,7 @@ private function clone_repository() private function generate_git_import_commands() { - ['commands' => $commands, 'branch' => $this->branch, 'fullRepoUrl' => $this->fullRepoUrl] = $this->application->generateGitImportCommands( + ['commands' => $commands, 'branch' => $this->branch, 'full_repository_url' => $this->full_repository_url] = $this->application->generateGitImportCommands( deployment_uuid: $this->deployment_uuid, pull_request_id: $this->pull_request_id, git_type: $this->git_type, @@ -1755,10 +1755,10 @@ private function build_image() $this->execute_remote_command([executeInDocker($this->deployment_uuid, "rm /artifacts/thegameplan.json"), "hidden" => true]); } else { if ($this->force_rebuild) { - $build_command = "docker build --no-cache {$this->buildTarget} --network {$this->destination->network} -f {$this->workdir}{$this->dockerfile_location} {$this->build_args} --progress plain -t $this->build_image_name {$this->workdir}"; + $build_command = "docker build --no-cache {$this->build_target} --network {$this->destination->network} -f {$this->workdir}{$this->dockerfile_location} {$this->build_args} --progress plain -t $this->build_image_name {$this->workdir}"; $base64_build_command = base64_encode($build_command); } else { - $build_command = "docker build {$this->buildTarget} --network {$this->destination->network} -f {$this->workdir}{$this->dockerfile_location} {$this->build_args} --progress plain -t $this->build_image_name {$this->workdir}"; + $build_command = "docker build {$this->build_target} --network {$this->destination->network} -f {$this->workdir}{$this->dockerfile_location} {$this->build_args} --progress plain -t $this->build_image_name {$this->workdir}"; $base64_build_command = base64_encode($build_command); } $this->execute_remote_command( @@ -1794,7 +1794,7 @@ private function build_image() } }"); } - $build_command = "docker build {$this->addHosts} --network host -f {$this->workdir}/Dockerfile {$this->build_args} --progress plain -t {$this->production_image_name} {$this->workdir}"; + $build_command = "docker build {$this->add_hosts} --network host -f {$this->workdir}/Dockerfile {$this->build_args} --progress plain -t {$this->production_image_name} {$this->workdir}"; $base64_build_command = base64_encode($build_command); $this->execute_remote_command( [ @@ -1813,7 +1813,7 @@ private function build_image() } else { // Pure Dockerfile based deployment if ($this->application->dockerfile) { - $build_command = "docker build --pull {$this->buildTarget} {$this->addHosts} --network host -f {$this->workdir}{$this->dockerfile_location} {$this->build_args} --progress plain -t {$this->production_image_name} {$this->workdir}"; + $build_command = "docker build --pull {$this->build_target} {$this->add_hosts} --network host -f {$this->workdir}{$this->dockerfile_location} {$this->build_args} --progress plain -t {$this->production_image_name} {$this->workdir}"; $base64_build_command = base64_encode($build_command); $this->execute_remote_command( [ @@ -1839,10 +1839,10 @@ private function build_image() $this->execute_remote_command([executeInDocker($this->deployment_uuid, "rm /artifacts/thegameplan.json"), "hidden" => true]); } else { if ($this->force_rebuild) { - $build_command = "docker build --no-cache {$this->buildTarget} {$this->addHosts} --network host -f {$this->workdir}{$this->dockerfile_location} {$this->build_args} --progress plain -t {$this->production_image_name} {$this->workdir}"; + $build_command = "docker build --no-cache {$this->build_target} {$this->add_hosts} --network host -f {$this->workdir}{$this->dockerfile_location} {$this->build_args} --progress plain -t {$this->production_image_name} {$this->workdir}"; $base64_build_command = base64_encode($build_command); } else { - $build_command = "docker build {$this->buildTarget} {$this->addHosts} --network host -f {$this->workdir}{$this->dockerfile_location} {$this->build_args} --progress plain -t {$this->production_image_name} {$this->workdir}"; + $build_command = "docker build {$this->build_target} {$this->add_hosts} --network host -f {$this->workdir}{$this->dockerfile_location} {$this->build_args} --progress plain -t {$this->production_image_name} {$this->workdir}"; $base64_build_command = base64_encode($build_command); } $this->execute_remote_command( @@ -1862,7 +1862,7 @@ private function build_image() private function stop_running_container(bool $force = false) { $this->application_deployment_queue->addLogEntry("Removing old containers."); - if ($this->newVersionIsHealthy || $force) { + if ($this->is_new_version_healthy || $force) { $containers = getCurrentApplicationContainerStatus($this->server, $this->application->id, $this->pull_request_id); if ($this->pull_request_id === 0) { $containers = $containers->filter(function ($container) {