Skip to content

Commit bed959f

Browse files
committed
feat: rolling update
1 parent a3f3470 commit bed959f

19 files changed

+263
-118
lines changed

app/Actions/Database/StartPostgresql.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class StartPostgresql
1717
public function __invoke(Server $server, StandalonePostgresql $database)
1818
{
1919
$this->database = $database;
20-
$container_name = generate_container_name($this->database->uuid);
20+
$container_name = $this->database->uuid;
2121
$this->configuration_dir = database_configuration_dir() . '/' . $container_name;
2222

2323
$this->commands = [
@@ -36,7 +36,7 @@ public function __invoke(Server $server, StandalonePostgresql $database)
3636
'image' => $this->database->image,
3737
'container_name' => $container_name,
3838
'environment' => $environment_variables,
39-
'restart' => 'always',
39+
'restart' => RESTART_MODE,
4040
'networks' => [
4141
$this->database->destination->network,
4242
],

app/Http/Livewire/Project/Application/General.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,26 @@ public function generateServerRandomDomain()
136136

137137
public function submit()
138138
{
139+
ray($this->application);
139140
try {
140141
$this->validate();
141142

142143
$domains = Str::of($this->application->fqdn)->trim()->explode(',')->map(function ($domain) {
143144
return Str::of($domain)->trim()->lower();
144145
});
145-
$port = get_port_from_dockerfile($this->application->dockerfile);
146-
if ($port) {
147-
$this->application->ports_exposes = $port;
146+
if ($this->application->dockerfile) {
147+
$port = get_port_from_dockerfile($this->application->dockerfile);
148+
if ($port) {
149+
$this->application->ports_exposes = $port;
150+
}
148151
}
149152
if ($this->application->base_directory && $this->application->base_directory !== '/') {
150153
$this->application->base_directory = rtrim($this->application->base_directory, '/');
151154
}
152155
if ($this->application->publish_directory && $this->application->publish_directory !== '/') {
153156
$this->application->publish_directory = rtrim($this->application->publish_directory, '/');
154157
}
155-
$this->application->fqdn = data_get($domains->implode(','), '', null);
158+
$this->application->fqdn = $domains->implode(',');
156159
$this->application->save();
157160
$this->emit('success', 'Application settings updated!');
158161
} catch (\Exception $e) {

app/Http/Livewire/Project/Application/Heading.php

+19-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\Http\Livewire\Project\Application;
44

5-
use App\Jobs\ContainerStatusJob;
5+
use App\Jobs\ApplicationContainerStatusJob;
66
use App\Models\Application;
77
use App\Notifications\Application\StatusChanged;
88
use Livewire\Component;
@@ -22,9 +22,8 @@ public function mount()
2222

2323
public function check_status()
2424
{
25-
dispatch_sync(new ContainerStatusJob(
26-
resource: $this->application,
27-
container_name: generate_container_name($this->application->uuid),
25+
dispatch_sync(new ApplicationContainerStatusJob(
26+
application: $this->application,
2827
));
2928
$this->application->refresh();
3029
}
@@ -58,12 +57,21 @@ protected function setDeploymentUuid()
5857

5958
public function stop()
6059
{
61-
remote_process(
62-
["docker rm -f {$this->application->uuid}"],
63-
$this->application->destination->server
64-
);
65-
$this->application->status = 'stopped';
66-
$this->application->save();
67-
$this->application->environment->project->team->notify(new StatusChanged($this->application));
60+
$containers = getCurrentApplicationContainerStatus($this->application->destination->server, $this->application->id);
61+
if ($containers->count() === 0) {
62+
return;
63+
}
64+
foreach ($containers as $container) {
65+
$containerName = data_get($container, 'Names');
66+
if ($containerName) {
67+
remote_process(
68+
["docker rm -f {$containerName}"],
69+
$this->application->destination->server
70+
);
71+
$this->application->status = 'stopped';
72+
$this->application->save();
73+
$this->application->environment->project->team->notify(new StatusChanged($this->application));
74+
}
75+
}
6876
}
6977
}

app/Http/Livewire/Project/Application/Previews.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\Http\Livewire\Project\Application;
44

5-
use App\Jobs\ContainerStatusJob;
5+
use App\Jobs\ApplicationContainerStatusJob;
66
use App\Models\Application;
77
use App\Models\ApplicationPreview;
88
use Illuminate\Support\Collection;
@@ -25,10 +25,9 @@ public function mount()
2525

2626
public function loadStatus($pull_request_id)
2727
{
28-
dispatch(new ContainerStatusJob(
29-
resource: $this->application,
30-
container_name: generate_container_name($this->application->uuid, $pull_request_id),
31-
pull_request_id: $pull_request_id
28+
dispatch(new ApplicationContainerStatusJob(
29+
application: $this->application,
30+
pullRequestId: $pull_request_id
3231
));
3332
}
3433

@@ -82,7 +81,7 @@ protected function setDeploymentUuid()
8281
public function stop(int $pull_request_id)
8382
{
8483
try {
85-
$container_name = generate_container_name($this->application->uuid, $pull_request_id);
84+
$container_name = generateApplicationContainerName($this->application->uuid, $pull_request_id);
8685
ray('Stopping container: ' . $container_name);
8786

8887
instant_remote_process(["docker rm -f $container_name"], $this->application->destination->server, throwError: false);

app/Http/Livewire/Project/Database/Heading.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace App\Http\Livewire\Project\Database;
44

55
use App\Actions\Database\StartPostgresql;
6-
use App\Jobs\ContainerStatusJob;
6+
use App\Jobs\DatabaseContainerStatusJob;
77
use App\Notifications\Application\StatusChanged;
88
use Livewire\Component;
99

@@ -25,9 +25,8 @@ public function activityFinished()
2525

2626
public function check_status()
2727
{
28-
dispatch_sync(new ContainerStatusJob(
29-
resource: $this->database,
30-
container_name: generate_container_name($this->database->uuid),
28+
dispatch_sync(new DatabaseContainerStatusJob(
29+
database: $this->database,
3130
));
3231
$this->database->refresh();
3332
}

app/Http/Livewire/Project/New/SimpleDockerfile.php

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public function submit()
5959
'source_id' => 0,
6060
'source_type' => GithubApp::class
6161
]);
62+
$application->update([
63+
'name' => 'dockerfile-' . $application->id
64+
]);
65+
6266
redirect()->route('project.application.configuration', [
6367
'application_uuid' => $application->uuid,
6468
'environment_name' => $environment->name,

app/Http/Livewire/Server/Proxy/Deploy.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function start_proxy()
1717
$this->server->proxy->last_applied_settings &&
1818
$this->server->proxy->last_saved_settings !== $this->server->proxy->last_applied_settings
1919
) {
20-
$this->emit('saveConfiguration', $server);
20+
$this->emit('saveConfiguration', $this->server);
2121
}
2222
$activity = resolve(StartProxy::class)($this->server);
2323
$this->emit('newMonitorActivity', $activity->id);
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\Models\Application;
6+
use App\Models\ApplicationPreview;
7+
use App\Notifications\Application\StatusChanged;
8+
use Illuminate\Bus\Queueable;
9+
use Illuminate\Contracts\Queue\ShouldBeUnique;
10+
use Illuminate\Contracts\Queue\ShouldQueue;
11+
use Illuminate\Foundation\Bus\Dispatchable;
12+
use Illuminate\Queue\InteractsWithQueue;
13+
use Illuminate\Queue\SerializesModels;
14+
15+
class ApplicationContainerStatusJob implements ShouldQueue, ShouldBeUnique
16+
{
17+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
18+
19+
public string $containerName;
20+
21+
public function __construct(
22+
public Application $application,
23+
public int $pullRequestId = 0)
24+
{
25+
$this->containerName = generateApplicationContainerName($application->uuid, $pullRequestId);
26+
}
27+
28+
public function uniqueId(): string
29+
{
30+
return $this->containerName;
31+
}
32+
33+
public function handle(): void
34+
{
35+
try {
36+
$status = getApplicationContainerStatus(application: $this->application);
37+
if ($this->application->status === 'running' && $status !== 'running') {
38+
$this->application->environment->project->team->notify(new StatusChanged($this->application));
39+
}
40+
41+
if ($this->pullRequestId !== 0) {
42+
$preview = ApplicationPreview::findPreviewByApplicationAndPullId($this->application->id, $this->pullRequestId);
43+
$preview->status = $status;
44+
$preview->save();
45+
} else {
46+
$this->application->status = $status;
47+
$this->application->save();
48+
}
49+
} catch (\Exception $e) {
50+
ray($e->getMessage());
51+
}
52+
}
53+
}

app/Jobs/ApplicationDeploymentJob.php

+67-19
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class ApplicationDeploymentJob implements ShouldQueue
5050
private ApplicationPreview|null $preview = null;
5151

5252
private string $container_name;
53+
private string|null $currently_running_container_name = null;
5354
private string $workdir;
5455
private string $configuration_dir;
5556
private string $build_workdir;
@@ -86,7 +87,7 @@ public function __construct(int $application_deployment_queue_id)
8687
$this->build_workdir = "{$this->workdir}" . rtrim($this->application->base_directory, '/');
8788
$this->is_debug_enabled = $this->application->settings->is_debug_enabled;
8889

89-
$this->container_name = generate_container_name($this->application->uuid, $this->pull_request_id);
90+
$this->container_name = generateApplicationContainerName($this->application->uuid, $this->pull_request_id);
9091
$this->private_key_location = save_private_key_for_server($this->server);
9192
$this->saved_outputs = collect();
9293

@@ -113,6 +114,10 @@ public function __construct(int $application_deployment_queue_id)
113114
public function handle(): void
114115
{
115116
// ray()->measure();
117+
$containers = getCurrentApplicationContainerStatus($this->server, $this->application->id);
118+
if ($containers->count() > 0) {
119+
$this->currently_running_container_name = data_get($containers[0], 'Names');
120+
}
116121
$this->application_deployment_queue->update([
117122
'status' => ApplicationDeploymentStatus::IN_PROGRESS->value,
118123
]);
@@ -175,9 +180,9 @@ private function deploy_simple_dockerfile()
175180
$this->generate_build_env_variables();
176181
$this->add_build_env_variables_to_dockerfile();
177182
$this->build_image();
178-
$this->stop_running_container();
179-
$this->start_by_compose_file();
183+
$this->rolling_update();
180184
}
185+
181186
private function deploy()
182187
{
183188
$this->execute_remote_command(
@@ -206,8 +211,7 @@ private function deploy()
206211
"echo 'Docker Image found locally with the same Git Commit SHA {$this->application->uuid}:{$this->commit}. Build step skipped...'"
207212
]);
208213
$this->generate_compose_file();
209-
$this->stop_running_container();
210-
$this->start_by_compose_file();
214+
$this->rolling_update();
211215
return;
212216
}
213217
}
@@ -219,8 +223,54 @@ private function deploy()
219223
$this->generate_build_env_variables();
220224
$this->add_build_env_variables_to_dockerfile();
221225
$this->build_image();
222-
$this->stop_running_container();
226+
$this->rolling_update();
227+
}
228+
229+
private function rolling_update()
230+
{
223231
$this->start_by_compose_file();
232+
$this->health_check();
233+
$this->stop_running_container();
234+
}
235+
private function health_check()
236+
{
237+
ray('New container name: ',$this->container_name);
238+
if ($this->container_name) {
239+
$counter = 0;
240+
$this->execute_remote_command(
241+
[
242+
"echo 'Waiting for health check to pass on the new version of your application.'"
243+
],
244+
);
245+
while ($counter < $this->application->health_check_retries) {
246+
$this->execute_remote_command(
247+
[
248+
"echo 'Attempt {$counter} of {$this->application->health_check_retries}'"
249+
],
250+
[
251+
"docker inspect --format='{{json .State.Health.Status}}' {$this->container_name}",
252+
"hidden" => true,
253+
"save" => "health_check"
254+
],
255+
256+
);
257+
$this->execute_remote_command(
258+
[
259+
"echo 'New application version health check status: {$this->saved_outputs->get('health_check')}'"
260+
],
261+
);
262+
if (Str::of($this->saved_outputs->get('health_check'))->contains('healthy')) {
263+
$this->execute_remote_command(
264+
[
265+
"echo 'Rolling update completed.'"
266+
],
267+
);
268+
break;
269+
}
270+
$counter++;
271+
sleep($this->application->health_check_interval);
272+
}
273+
}
224274
}
225275
private function deploy_pull_request()
226276
{
@@ -241,8 +291,7 @@ private function deploy_pull_request()
241291
// $this->generate_build_env_variables();
242292
// $this->add_build_env_variables_to_dockerfile();
243293
$this->build_image();
244-
$this->stop_running_container();
245-
$this->start_by_compose_file();
294+
$this->rolling_update();
246295
}
247296

248297
private function prepare_builder_image()
@@ -409,7 +458,7 @@ private function generate_compose_file()
409458
$this->container_name => [
410459
'image' => $this->production_image_name,
411460
'container_name' => $this->container_name,
412-
'restart' => 'always',
461+
'restart' => RESTART_MODE,
413462
'environment' => $environment_variables,
414463
'labels' => $this->set_labels_for_applications(),
415464
'expose' => $ports,
@@ -539,8 +588,8 @@ private function set_labels_for_applications()
539588
$schema = $url->getScheme();
540589
$slug = Str::slug($host . $path);
541590

542-
$http_label = "{$this->application->uuid}-{$slug}-http";
543-
$https_label = "{$this->application->uuid}-{$slug}-https";
591+
$http_label = "{$this->container_name}-{$slug}-http";
592+
$https_label = "{$this->container_name}-{$slug}-https";
544593

545594
if ($schema === 'https') {
546595
// Set labels for https
@@ -647,23 +696,22 @@ private function build_image()
647696

648697
private function stop_running_container()
649698
{
650-
$this->execute_remote_command(
651-
["echo -n 'Removing old running application.'"],
652-
[$this->execute_in_builder("docker rm -f $this->container_name >/dev/null 2>&1"), "hidden" => true],
653-
);
699+
if ($this->currently_running_container_name) {
700+
$this->execute_remote_command(
701+
["echo -n 'Removing old application version.'"],
702+
[$this->execute_in_builder("docker rm -f $this->currently_running_container_name >/dev/null 2>&1"), "hidden" => true],
703+
);
704+
}
654705
}
655706

656707
private function start_by_compose_file()
657708
{
658709
$this->execute_remote_command(
659-
["echo -n 'Starting new application... '"],
710+
["echo -n 'Rolling update started.'"],
660711
[$this->execute_in_builder("docker compose --project-directory {$this->workdir} up -d >/dev/null"), "hidden" => true],
661-
["echo 'Done. 🎉'"],
662712
);
663713
}
664714

665-
666-
667715
private function generate_build_env_variables()
668716
{
669717
$this->build_args = collect(["--build-arg SOURCE_COMMIT={$this->commit}"]);

0 commit comments

Comments
 (0)