Skip to content

Commit

Permalink
feat: rolling update
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasbacsai committed Aug 21, 2023
1 parent a3f3470 commit bed959f
Show file tree
Hide file tree
Showing 19 changed files with 263 additions and 118 deletions.
4 changes: 2 additions & 2 deletions app/Actions/Database/StartPostgresql.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class StartPostgresql
public function __invoke(Server $server, StandalonePostgresql $database)
{
$this->database = $database;
$container_name = generate_container_name($this->database->uuid);
$container_name = $this->database->uuid;
$this->configuration_dir = database_configuration_dir() . '/' . $container_name;

$this->commands = [
Expand All @@ -36,7 +36,7 @@ public function __invoke(Server $server, StandalonePostgresql $database)
'image' => $this->database->image,
'container_name' => $container_name,
'environment' => $environment_variables,
'restart' => 'always',
'restart' => RESTART_MODE,
'networks' => [
$this->database->destination->network,
],
Expand Down
11 changes: 7 additions & 4 deletions app/Http/Livewire/Project/Application/General.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,26 @@ public function generateServerRandomDomain()

public function submit()
{
ray($this->application);
try {
$this->validate();

$domains = Str::of($this->application->fqdn)->trim()->explode(',')->map(function ($domain) {
return Str::of($domain)->trim()->lower();
});
$port = get_port_from_dockerfile($this->application->dockerfile);
if ($port) {
$this->application->ports_exposes = $port;
if ($this->application->dockerfile) {
$port = get_port_from_dockerfile($this->application->dockerfile);
if ($port) {
$this->application->ports_exposes = $port;
}
}
if ($this->application->base_directory && $this->application->base_directory !== '/') {
$this->application->base_directory = rtrim($this->application->base_directory, '/');
}
if ($this->application->publish_directory && $this->application->publish_directory !== '/') {
$this->application->publish_directory = rtrim($this->application->publish_directory, '/');
}
$this->application->fqdn = data_get($domains->implode(','), '', null);
$this->application->fqdn = $domains->implode(',');
$this->application->save();
$this->emit('success', 'Application settings updated!');
} catch (\Exception $e) {
Expand Down
30 changes: 19 additions & 11 deletions app/Http/Livewire/Project/Application/Heading.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Http\Livewire\Project\Application;

use App\Jobs\ContainerStatusJob;
use App\Jobs\ApplicationContainerStatusJob;
use App\Models\Application;
use App\Notifications\Application\StatusChanged;
use Livewire\Component;
Expand All @@ -22,9 +22,8 @@ public function mount()

public function check_status()
{
dispatch_sync(new ContainerStatusJob(
resource: $this->application,
container_name: generate_container_name($this->application->uuid),
dispatch_sync(new ApplicationContainerStatusJob(
application: $this->application,
));
$this->application->refresh();
}
Expand Down Expand Up @@ -58,12 +57,21 @@ protected function setDeploymentUuid()

public function stop()
{
remote_process(
["docker rm -f {$this->application->uuid}"],
$this->application->destination->server
);
$this->application->status = 'stopped';
$this->application->save();
$this->application->environment->project->team->notify(new StatusChanged($this->application));
$containers = getCurrentApplicationContainerStatus($this->application->destination->server, $this->application->id);
if ($containers->count() === 0) {
return;
}
foreach ($containers as $container) {
$containerName = data_get($container, 'Names');
if ($containerName) {
remote_process(
["docker rm -f {$containerName}"],
$this->application->destination->server
);
$this->application->status = 'stopped';
$this->application->save();
$this->application->environment->project->team->notify(new StatusChanged($this->application));
}
}
}
}
11 changes: 5 additions & 6 deletions app/Http/Livewire/Project/Application/Previews.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Http\Livewire\Project\Application;

use App\Jobs\ContainerStatusJob;
use App\Jobs\ApplicationContainerStatusJob;
use App\Models\Application;
use App\Models\ApplicationPreview;
use Illuminate\Support\Collection;
Expand All @@ -25,10 +25,9 @@ public function mount()

public function loadStatus($pull_request_id)
{
dispatch(new ContainerStatusJob(
resource: $this->application,
container_name: generate_container_name($this->application->uuid, $pull_request_id),
pull_request_id: $pull_request_id
dispatch(new ApplicationContainerStatusJob(
application: $this->application,
pullRequestId: $pull_request_id
));
}

Expand Down Expand Up @@ -82,7 +81,7 @@ protected function setDeploymentUuid()
public function stop(int $pull_request_id)
{
try {
$container_name = generate_container_name($this->application->uuid, $pull_request_id);
$container_name = generateApplicationContainerName($this->application->uuid, $pull_request_id);
ray('Stopping container: ' . $container_name);

instant_remote_process(["docker rm -f $container_name"], $this->application->destination->server, throwError: false);
Expand Down
7 changes: 3 additions & 4 deletions app/Http/Livewire/Project/Database/Heading.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Http\Livewire\Project\Database;

use App\Actions\Database\StartPostgresql;
use App\Jobs\ContainerStatusJob;
use App\Jobs\DatabaseContainerStatusJob;
use App\Notifications\Application\StatusChanged;
use Livewire\Component;

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

public function check_status()
{
dispatch_sync(new ContainerStatusJob(
resource: $this->database,
container_name: generate_container_name($this->database->uuid),
dispatch_sync(new DatabaseContainerStatusJob(
database: $this->database,
));
$this->database->refresh();
}
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Livewire/Project/New/SimpleDockerfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public function submit()
'source_id' => 0,
'source_type' => GithubApp::class
]);
$application->update([
'name' => 'dockerfile-' . $application->id
]);

redirect()->route('project.application.configuration', [
'application_uuid' => $application->uuid,
'environment_name' => $environment->name,
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Livewire/Server/Proxy/Deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function start_proxy()
$this->server->proxy->last_applied_settings &&
$this->server->proxy->last_saved_settings !== $this->server->proxy->last_applied_settings
) {
$this->emit('saveConfiguration', $server);
$this->emit('saveConfiguration', $this->server);
}
$activity = resolve(StartProxy::class)($this->server);
$this->emit('newMonitorActivity', $activity->id);
Expand Down
53 changes: 53 additions & 0 deletions app/Jobs/ApplicationContainerStatusJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Jobs;

use App\Models\Application;
use App\Models\ApplicationPreview;
use App\Notifications\Application\StatusChanged;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ApplicationContainerStatusJob implements ShouldQueue, ShouldBeUnique
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public string $containerName;

public function __construct(
public Application $application,
public int $pullRequestId = 0)
{
$this->containerName = generateApplicationContainerName($application->uuid, $pullRequestId);
}

public function uniqueId(): string
{
return $this->containerName;
}

public function handle(): void
{
try {
$status = getApplicationContainerStatus(application: $this->application);
if ($this->application->status === 'running' && $status !== 'running') {
$this->application->environment->project->team->notify(new StatusChanged($this->application));
}

if ($this->pullRequestId !== 0) {
$preview = ApplicationPreview::findPreviewByApplicationAndPullId($this->application->id, $this->pullRequestId);
$preview->status = $status;
$preview->save();
} else {
$this->application->status = $status;
$this->application->save();
}
} catch (\Exception $e) {
ray($e->getMessage());
}
}
}
86 changes: 67 additions & 19 deletions app/Jobs/ApplicationDeploymentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ApplicationDeploymentJob implements ShouldQueue
private ApplicationPreview|null $preview = null;

private string $container_name;
private string|null $currently_running_container_name = null;
private string $workdir;
private string $configuration_dir;
private string $build_workdir;
Expand Down Expand Up @@ -86,7 +87,7 @@ public function __construct(int $application_deployment_queue_id)
$this->build_workdir = "{$this->workdir}" . rtrim($this->application->base_directory, '/');
$this->is_debug_enabled = $this->application->settings->is_debug_enabled;

$this->container_name = generate_container_name($this->application->uuid, $this->pull_request_id);
$this->container_name = generateApplicationContainerName($this->application->uuid, $this->pull_request_id);
$this->private_key_location = save_private_key_for_server($this->server);
$this->saved_outputs = collect();

Expand All @@ -113,6 +114,10 @@ public function __construct(int $application_deployment_queue_id)
public function handle(): void
{
// ray()->measure();
$containers = getCurrentApplicationContainerStatus($this->server, $this->application->id);
if ($containers->count() > 0) {
$this->currently_running_container_name = data_get($containers[0], 'Names');
}
$this->application_deployment_queue->update([
'status' => ApplicationDeploymentStatus::IN_PROGRESS->value,
]);
Expand Down Expand Up @@ -175,9 +180,9 @@ private function deploy_simple_dockerfile()
$this->generate_build_env_variables();
$this->add_build_env_variables_to_dockerfile();
$this->build_image();
$this->stop_running_container();
$this->start_by_compose_file();
$this->rolling_update();
}

private function deploy()
{
$this->execute_remote_command(
Expand Down Expand Up @@ -206,8 +211,7 @@ private function deploy()
"echo 'Docker Image found locally with the same Git Commit SHA {$this->application->uuid}:{$this->commit}. Build step skipped...'"
]);
$this->generate_compose_file();
$this->stop_running_container();
$this->start_by_compose_file();
$this->rolling_update();
return;
}
}
Expand All @@ -219,8 +223,54 @@ private function deploy()
$this->generate_build_env_variables();
$this->add_build_env_variables_to_dockerfile();
$this->build_image();
$this->stop_running_container();
$this->rolling_update();
}

private function rolling_update()
{
$this->start_by_compose_file();
$this->health_check();
$this->stop_running_container();
}
private function health_check()
{
ray('New container name: ',$this->container_name);
if ($this->container_name) {
$counter = 0;
$this->execute_remote_command(
[
"echo 'Waiting for health check to pass on the new version of your application.'"
],
);
while ($counter < $this->application->health_check_retries) {
$this->execute_remote_command(
[
"echo 'Attempt {$counter} of {$this->application->health_check_retries}'"
],
[
"docker inspect --format='{{json .State.Health.Status}}' {$this->container_name}",
"hidden" => true,
"save" => "health_check"
],

);
$this->execute_remote_command(
[
"echo 'New application version health check status: {$this->saved_outputs->get('health_check')}'"
],
);
if (Str::of($this->saved_outputs->get('health_check'))->contains('healthy')) {
$this->execute_remote_command(
[
"echo 'Rolling update completed.'"
],
);
break;
}
$counter++;
sleep($this->application->health_check_interval);
}
}
}
private function deploy_pull_request()
{
Expand All @@ -241,8 +291,7 @@ private function deploy_pull_request()
// $this->generate_build_env_variables();
// $this->add_build_env_variables_to_dockerfile();
$this->build_image();
$this->stop_running_container();
$this->start_by_compose_file();
$this->rolling_update();
}

private function prepare_builder_image()
Expand Down Expand Up @@ -409,7 +458,7 @@ private function generate_compose_file()
$this->container_name => [
'image' => $this->production_image_name,
'container_name' => $this->container_name,
'restart' => 'always',
'restart' => RESTART_MODE,
'environment' => $environment_variables,
'labels' => $this->set_labels_for_applications(),
'expose' => $ports,
Expand Down Expand Up @@ -539,8 +588,8 @@ private function set_labels_for_applications()
$schema = $url->getScheme();
$slug = Str::slug($host . $path);

$http_label = "{$this->application->uuid}-{$slug}-http";
$https_label = "{$this->application->uuid}-{$slug}-https";
$http_label = "{$this->container_name}-{$slug}-http";
$https_label = "{$this->container_name}-{$slug}-https";

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

private function stop_running_container()
{
$this->execute_remote_command(
["echo -n 'Removing old running application.'"],
[$this->execute_in_builder("docker rm -f $this->container_name >/dev/null 2>&1"), "hidden" => true],
);
if ($this->currently_running_container_name) {
$this->execute_remote_command(
["echo -n 'Removing old application version.'"],
[$this->execute_in_builder("docker rm -f $this->currently_running_container_name >/dev/null 2>&1"), "hidden" => true],
);
}
}

private function start_by_compose_file()
{
$this->execute_remote_command(
["echo -n 'Starting new application... '"],
["echo -n 'Rolling update started.'"],
[$this->execute_in_builder("docker compose --project-directory {$this->workdir} up -d >/dev/null"), "hidden" => true],
["echo 'Done. 🎉'"],
);
}



private function generate_build_env_variables()
{
$this->build_args = collect(["--build-arg SOURCE_COMMIT={$this->commit}"]);
Expand Down
Loading

0 comments on commit bed959f

Please sign in to comment.