From d38d50dca250329616e087602949e1ccb8ffbbc3 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 17 Nov 2023 14:14:13 +0100 Subject: [PATCH 1/3] Fix server readiness check and update version number --- app/Jobs/ApplicationDeploymentJob.php | 4 ---- app/Jobs/ContainerStatusJob.php | 3 --- app/Jobs/ServerStatusJob.php | 5 +---- app/Models/Server.php | 5 +---- config/sentry.php | 2 +- config/version.php | 2 +- versions.json | 2 +- 7 files changed, 5 insertions(+), 18 deletions(-) diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index 4c0450920..4e4f66301 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -1022,10 +1022,6 @@ COPY ./nginx.conf /etc/nginx/conf.d/default.conf"); listen [::]:80; server_name localhost; - // real_ip_header X-Forwarded-For; - // proxy_set_header X-Real-IP \$remote_addr; - // proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; - location / { root /usr/share/nginx/html; index index.html; diff --git a/app/Jobs/ContainerStatusJob.php b/app/Jobs/ContainerStatusJob.php index 20acb4bc4..dd7247c2b 100644 --- a/app/Jobs/ContainerStatusJob.php +++ b/app/Jobs/ContainerStatusJob.php @@ -39,9 +39,6 @@ class ContainerStatusJob implements ShouldQueue, ShouldBeEncrypted { // ray("checking server status for {$this->server->id}"); try { - if (!$this->server->checkServerRediness()) { - return; - } $containers = instant_remote_process(["docker container ls -q"], $this->server); if (!$containers) { return; diff --git a/app/Jobs/ServerStatusJob.php b/app/Jobs/ServerStatusJob.php index 80ebf370e..7d591bc83 100644 --- a/app/Jobs/ServerStatusJob.php +++ b/app/Jobs/ServerStatusJob.php @@ -34,10 +34,7 @@ class ServerStatusJob implements ShouldQueue, ShouldBeEncrypted { ray("checking server status for {$this->server->id}"); try { - if (!$this->server->checkServerRediness()) { - ray('server not ready'); - return; - } + $this->server->checkServerRediness(); $this->cleanup(notify: false); } catch (\Throwable $e) { send_internal_notification('ServerStatusJob failed with: ' . $e->getMessage()); diff --git a/app/Models/Server.php b/app/Models/Server.php index 9934b0620..8cd4dc957 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -130,9 +130,6 @@ class Server extends BaseModel } public function checkServerRediness() { - if ($this->skipServer()) { - return false; - } $serverUptimeCheckNumber = $this->unreachable_count; $serverUptimeCheckNumberMax = 5; @@ -168,7 +165,7 @@ class Server extends BaseModel $db->update(['status' => 'exited']); } } - return false; + throw new \Exception('Server is not reachable.'); } $result = $this->validateConnection(); ray('validateConnection: ' . $result); diff --git a/config/sentry.php b/config/sentry.php index bd908c402..5655b866b 100644 --- a/config/sentry.php +++ b/config/sentry.php @@ -7,7 +7,7 @@ return [ // The release version of your application // Example with dynamic git hash: trim(exec('git --git-dir ' . base_path('.git') . ' log --pretty="%h" -n1 HEAD')) - 'release' => '4.0.0-beta.141', + 'release' => '4.0.0-beta.142', // When left empty or `null` the Laravel environment will be used 'environment' => config('app.env'), diff --git a/config/version.php b/config/version.php index 37f9d819e..8b7d1633c 100644 --- a/config/version.php +++ b/config/version.php @@ -1,3 +1,3 @@ Date: Fri, 17 Nov 2023 14:22:05 +0100 Subject: [PATCH 2/3] Fix server status check and add new job --- .../Livewire/Project/Application/Heading.php | 3 ++ .../Livewire/Project/Application/Rollback.php | 45 ++++++++++--------- app/Jobs/ContainerStatusJob.php | 3 +- app/Models/Server.php | 4 +- app/Models/User.php | 1 - 5 files changed, 31 insertions(+), 25 deletions(-) diff --git a/app/Http/Livewire/Project/Application/Heading.php b/app/Http/Livewire/Project/Application/Heading.php index be96d4ffa..e09e469a9 100644 --- a/app/Http/Livewire/Project/Application/Heading.php +++ b/app/Http/Livewire/Project/Application/Heading.php @@ -4,6 +4,7 @@ namespace App\Http\Livewire\Project\Application; use App\Actions\Application\StopApplication; use App\Jobs\ContainerStatusJob; +use App\Jobs\ServerStatusJob; use App\Models\Application; use Livewire\Component; use Visus\Cuid2\Cuid2; @@ -28,6 +29,8 @@ class Heading extends Component $this->application->previews->each(function ($preview) { $preview->refresh(); }); + } else { + dispatch(new ServerStatusJob($this->application->destination->server)); } } diff --git a/app/Http/Livewire/Project/Application/Rollback.php b/app/Http/Livewire/Project/Application/Rollback.php index 1d73b7c85..dcebd4d93 100644 --- a/app/Http/Livewire/Project/Application/Rollback.php +++ b/app/Http/Livewire/Project/Application/Rollback.php @@ -42,28 +42,31 @@ class Rollback extends Component { try { $image = $this->application->uuid; - $output = instant_remote_process([ - "docker inspect --format='{{.Config.Image}}' {$this->application->uuid}", - ], $this->application->destination->server, throwError: false); - $current_tag = Str::of($output)->trim()->explode(":"); - $this->current = data_get($current_tag, 1); + if ($this->application->destination->server->isFunctional()) { + $output = instant_remote_process([ + "docker inspect --format='{{.Config.Image}}' {$this->application->uuid}", + ], $this->application->destination->server, throwError: false); + $current_tag = Str::of($output)->trim()->explode(":"); + $this->current = data_get($current_tag, 1); - $output = instant_remote_process([ - "docker images --format '{{.Repository}}#{{.Tag}}#{{.CreatedAt}}'", - ], $this->application->destination->server); - $this->images = Str::of($output)->trim()->explode("\n")->filter(function ($item) use ($image) { - return Str::of($item)->contains($image); - })->map(function ($item) { - $item = Str::of($item)->explode('#'); - if ($item[1] === $this->current) { - // $is_current = true; - } - return [ - 'tag' => $item[1], - 'created_at' => $item[2], - 'is_current' => $is_current ?? null, - ]; - })->toArray(); + $output = instant_remote_process([ + "docker images --format '{{.Repository}}#{{.Tag}}#{{.CreatedAt}}'", + ], $this->application->destination->server); + $this->images = Str::of($output)->trim()->explode("\n")->filter(function ($item) use ($image) { + return Str::of($item)->contains($image); + })->map(function ($item) { + $item = Str::of($item)->explode('#'); + if ($item[1] === $this->current) { + // $is_current = true; + } + return [ + 'tag' => $item[1], + 'created_at' => $item[2], + 'is_current' => $is_current ?? null, + ]; + })->toArray(); + } + return []; } catch (\Throwable $e) { return handleError($e, $this); } diff --git a/app/Jobs/ContainerStatusJob.php b/app/Jobs/ContainerStatusJob.php index dd7247c2b..7f991aee1 100644 --- a/app/Jobs/ContainerStatusJob.php +++ b/app/Jobs/ContainerStatusJob.php @@ -37,8 +37,9 @@ class ContainerStatusJob implements ShouldQueue, ShouldBeEncrypted public function handle(): void { - // ray("checking server status for {$this->server->id}"); + ray("checking container statuses for {$this->server->id}"); try { + $this->server->checkServerRediness(); $containers = instant_remote_process(["docker container ls -q"], $this->server); if (!$containers) { return; diff --git a/app/Models/Server.php b/app/Models/Server.php index 8cd4dc957..77b80e680 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -131,11 +131,11 @@ class Server extends BaseModel public function checkServerRediness() { $serverUptimeCheckNumber = $this->unreachable_count; - $serverUptimeCheckNumberMax = 5; + $serverUptimeCheckNumberMax = 3; $currentTime = now()->timestamp; $runtime5Minutes = 1 * 60; - // Run for 1 minutes max and check every 5 seconds + // Run for 1 minutes max and check every 5 seconds for 3 times while ($currentTime + $runtime5Minutes > now()->timestamp) { if ($serverUptimeCheckNumber >= $serverUptimeCheckNumberMax) { if ($this->unreachable_notification_sent === false) { diff --git a/app/Models/User.php b/app/Models/User.php index 640de6381..ee963a523 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -52,7 +52,6 @@ class User extends Authenticatable implements SendsEmail } public function createToken(string $name, array $abilities = ['*'], DateTimeInterface $expiresAt = null) { - ray('asd'); $plainTextToken = sprintf( '%s%s%s', config('sanctum.token_prefix', ''), From 307ee52ac094aa18332c1664742027fe19c98169 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 17 Nov 2023 14:29:37 +0100 Subject: [PATCH 3/3] wtf --- app/Jobs/ContainerStatusJob.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Jobs/ContainerStatusJob.php b/app/Jobs/ContainerStatusJob.php index 7f991aee1..2762b8b54 100644 --- a/app/Jobs/ContainerStatusJob.php +++ b/app/Jobs/ContainerStatusJob.php @@ -23,7 +23,7 @@ class ContainerStatusJob implements ShouldQueue, ShouldBeEncrypted public function __construct(public Server $server) { - // $this->handle(); + $this->handle(); } public function middleware(): array {