From 08383438419ba4ea9b18c7c887ba98824e46deb5 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Wed, 1 Nov 2023 20:55:21 +0100 Subject: [PATCH] fix: pull requests feat: add follow for full screen logs --- app/Jobs/ApplicationDeploymentJob.php | 26 ++++++-- app/Jobs/ContainerStatusJob.php | 2 +- bootstrap/helpers/docker.php | 31 +++------ resources/css/app.css | 2 +- .../application/deployment-logs.blade.php | 66 +++++++++++++++---- .../project/application/previews.blade.php | 8 +-- .../project/shared/get-logs.blade.php | 15 +++-- 7 files changed, 98 insertions(+), 52 deletions(-) diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index fcd07d49f..6fb829ef2 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -138,9 +138,16 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted public function handle(): void { // ray()->measure(); - $containers = getCurrentApplicationContainerStatus($this->server, $this->application->id); - if ($containers->count() > 0) { + $containers = getCurrentApplicationContainerStatus($this->server, $this->application->id, $this->pull_request_id); + if ($containers->count() === 1) { $this->currently_running_container_name = data_get($containers[0], 'Names'); + } else { + $foundContainer = $containers->filter(function ($container) { + return !str(data_get($container, 'Names'))->startsWith("{$this->application->uuid}-pr-"); + })->first(); + if ($foundContainer) { + $this->currently_running_container_name = data_get($foundContainer, 'Names'); + } } if ($this->pull_request_id !== 0 && $this->pull_request_id !== null) { $this->currently_running_container_name = $this->container_name; @@ -747,14 +754,21 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted $labels = collect(generateLabelsApplication($this->application, $this->preview)); } if ($this->pull_request_id !== 0) { + $newLabels = collect(generateLabelsApplication($this->application, $this->preview)); + $newHostLabel = $newLabels->filter(function ($label) { + return str($label)->contains('Host'); + }); $labels = $labels->reject(function ($label) { return str($label)->contains('Host'); }); - $newLabels = collect(generateLabelsApplication($this->application, $this->preview)); - $hostLabels = $newLabels->filter(function ($label) { - return str($label)->contains('Host'); + + $labels = $labels->map(function ($label) { + $pattern = '/([a-zA-Z0-9]+)-(\d+)-(http|https)/'; + $replacement = "$1-pr-{$this->pull_request_id}-$2-$3"; + $newLabel = preg_replace($pattern, $replacement, $label); + return $newLabel; }); - $labels = $labels->merge($hostLabels); + $labels = $labels->merge($newHostLabel); } $labels = $labels->merge(defaultLabels($this->application->id, $this->application->uuid, $this->pull_request_id))->toArray(); $docker_compose = [ diff --git a/app/Jobs/ContainerStatusJob.php b/app/Jobs/ContainerStatusJob.php index 99a1c561b..545d92bd5 100644 --- a/app/Jobs/ContainerStatusJob.php +++ b/app/Jobs/ContainerStatusJob.php @@ -18,7 +18,6 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\Middleware\WithoutOverlapping; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Arr; -use Illuminate\Support\Str; class ContainerStatusJob implements ShouldQueue, ShouldBeEncrypted { @@ -26,6 +25,7 @@ class ContainerStatusJob implements ShouldQueue, ShouldBeEncrypted public function __construct(public Server $server) { + $this->handle(); } public function middleware(): array { diff --git a/bootstrap/helpers/docker.php b/bootstrap/helpers/docker.php index a61bb177c..8a01ec549 100644 --- a/bootstrap/helpers/docker.php +++ b/bootstrap/helpers/docker.php @@ -6,11 +6,14 @@ use App\Models\Server; use Illuminate\Support\Collection; use Illuminate\Support\Str; use Spatie\Url\Url; -use Visus\Cuid2\Cuid2; -function getCurrentApplicationContainerStatus(Server $server, int $id): Collection +function getCurrentApplicationContainerStatus(Server $server, int $id, ?int $pullRequestId = null): Collection { - $containers = instant_remote_process(["docker ps -a --filter='label=coolify.applicationId={$id}' --format '{{json .}}' "], $server); + if ($pullRequestId) { + $containers = instant_remote_process(["docker ps -a --filter='label=coolify.applicationId={$id}' --filter='label=coolify.pullRequestId={$pullRequestId}' --format '{{json .}}' "], $server); + } else { + $containers = instant_remote_process(["docker ps -a --filter='label=coolify.applicationId={$id}' --format '{{json .}}'"], $server); + } if (!$containers) { return collect([]); } @@ -77,20 +80,6 @@ function executeInDocker(string $containerId, string $command) // return "docker exec {$this->deployment_uuid} bash -c '{$command} |& tee -a /proc/1/fd/1; [ \$PIPESTATUS -eq 0 ] || exit \$PIPESTATUS'"; } -function getApplicationContainerStatus(Application $application) -{ - $server = data_get($application, 'destination.server'); - $id = $application->id; - if (!$server) { - return 'exited'; - } - $containers = getCurrentApplicationContainerStatus($server, $id); - if ($containers->count() > 0) { - $status = data_get($containers[0], 'State', 'exited'); - return $status; - } - return 'exited'; -} function getContainerStatus(Server $server, string $container_id, bool $all_data = false, bool $throwError = false) { $container = instant_remote_process(["docker inspect --format '{{json .}}' {$container_id}"], $server, $throwError); @@ -212,9 +201,9 @@ function generateLabelsApplication(Application $application, ?ApplicationPreview $onlyPort = $ports[0]; } $pull_request_id = data_get($preview, 'pull_request_id', 0); - $appId = $application->id; - if ($pull_request_id !== 0 && $pull_request_id !== null) { - $appId = $appId . '-pr-' . $pull_request_id; + $appUuid = $application->uuid; + if ($pull_request_id !== 0) { + $appUuid = $appUuid . '-pr-' . $pull_request_id; } $labels = collect([]); if ($application->fqdn) { @@ -224,7 +213,7 @@ function generateLabelsApplication(Application $application, ?ApplicationPreview $domains = Str::of(data_get($application, 'fqdn'))->explode(','); } // Add Traefik labels no matter which proxy is selected - $labels = $labels->merge(fqdnLabelsForTraefik($application->uuid, $domains, $application->settings->is_force_https_enabled, $onlyPort)); + $labels = $labels->merge(fqdnLabelsForTraefik($appUuid, $domains, $application->settings->is_force_https_enabled, $onlyPort)); } return $labels->all(); } diff --git a/resources/css/app.css b/resources/css/app.css index 246dc98df..05769567a 100644 --- a/resources/css/app.css +++ b/resources/css/app.css @@ -122,5 +122,5 @@ tr td:first-child { @apply pt-2 pb-10; } .fullscreen { - @apply fixed top-0 left-0 w-full h-full z-[9999] bg-coolgray-200 overflow-y-auto scrollbar ; + @apply fixed top-0 left-0 w-full h-full z-[9999] bg-coolgray-100 overflow-y-auto scrollbar pb-4 ; } diff --git a/resources/views/livewire/project/application/deployment-logs.blade.php b/resources/views/livewire/project/application/deployment-logs.blade.php index b47d6dc38..3de40e4e8 100644 --- a/resources/views/livewire/project/application/deployment-logs.blade.php +++ b/resources/views/livewire/project/application/deployment-logs.blade.php @@ -1,4 +1,4 @@ -
+
@if (data_get($application_deployment_queue, 'status') === 'in_progress')
Deployment is @@ -6,24 +6,35 @@
-
Logs will be updated automatically.
+ {{--
Logs will be updated automatically.
--}} @else
Deployment is {{ Str::headline(data_get($application_deployment_queue, 'status')) }}.
@endif -
+
-
+
diff --git a/resources/views/livewire/project/application/previews.blade.php b/resources/views/livewire/project/application/previews.blade.php index 19e61376b..8f78ea517 100644 --- a/resources/views/livewire/project/application/previews.blade.php +++ b/resources/views/livewire/project/application/previews.blade.php @@ -78,11 +78,9 @@ Redeploy @endif - @if (data_get($preview, 'status') !== 'exited') - Remove - Preview - - @endif + Remove + Preview + diff --git a/resources/views/livewire/project/shared/get-logs.blade.php b/resources/views/livewire/project/shared/get-logs.blade.php index 137bd9dbb..16b2853cf 100644 --- a/resources/views/livewire/project/shared/get-logs.blade.php +++ b/resources/views/livewire/project/shared/get-logs.blade.php @@ -13,17 +13,18 @@ Refresh -
-
- -