feat: rolling update
This commit is contained in:
parent
a3f3470137
commit
bed959f1cd
@ -17,7 +17,7 @@ class StartPostgresql
|
|||||||
public function __invoke(Server $server, StandalonePostgresql $database)
|
public function __invoke(Server $server, StandalonePostgresql $database)
|
||||||
{
|
{
|
||||||
$this->database = $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->configuration_dir = database_configuration_dir() . '/' . $container_name;
|
||||||
|
|
||||||
$this->commands = [
|
$this->commands = [
|
||||||
@ -36,7 +36,7 @@ public function __invoke(Server $server, StandalonePostgresql $database)
|
|||||||
'image' => $this->database->image,
|
'image' => $this->database->image,
|
||||||
'container_name' => $container_name,
|
'container_name' => $container_name,
|
||||||
'environment' => $environment_variables,
|
'environment' => $environment_variables,
|
||||||
'restart' => 'always',
|
'restart' => RESTART_MODE,
|
||||||
'networks' => [
|
'networks' => [
|
||||||
$this->database->destination->network,
|
$this->database->destination->network,
|
||||||
],
|
],
|
||||||
|
@ -136,15 +136,18 @@ public function generateServerRandomDomain()
|
|||||||
|
|
||||||
public function submit()
|
public function submit()
|
||||||
{
|
{
|
||||||
|
ray($this->application);
|
||||||
try {
|
try {
|
||||||
$this->validate();
|
$this->validate();
|
||||||
|
|
||||||
$domains = Str::of($this->application->fqdn)->trim()->explode(',')->map(function ($domain) {
|
$domains = Str::of($this->application->fqdn)->trim()->explode(',')->map(function ($domain) {
|
||||||
return Str::of($domain)->trim()->lower();
|
return Str::of($domain)->trim()->lower();
|
||||||
});
|
});
|
||||||
$port = get_port_from_dockerfile($this->application->dockerfile);
|
if ($this->application->dockerfile) {
|
||||||
if ($port) {
|
$port = get_port_from_dockerfile($this->application->dockerfile);
|
||||||
$this->application->ports_exposes = $port;
|
if ($port) {
|
||||||
|
$this->application->ports_exposes = $port;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if ($this->application->base_directory && $this->application->base_directory !== '/') {
|
if ($this->application->base_directory && $this->application->base_directory !== '/') {
|
||||||
$this->application->base_directory = rtrim($this->application->base_directory, '/');
|
$this->application->base_directory = rtrim($this->application->base_directory, '/');
|
||||||
@ -152,7 +155,7 @@ public function submit()
|
|||||||
if ($this->application->publish_directory && $this->application->publish_directory !== '/') {
|
if ($this->application->publish_directory && $this->application->publish_directory !== '/') {
|
||||||
$this->application->publish_directory = rtrim($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->application->save();
|
||||||
$this->emit('success', 'Application settings updated!');
|
$this->emit('success', 'Application settings updated!');
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Http\Livewire\Project\Application;
|
namespace App\Http\Livewire\Project\Application;
|
||||||
|
|
||||||
use App\Jobs\ContainerStatusJob;
|
use App\Jobs\ApplicationContainerStatusJob;
|
||||||
use App\Models\Application;
|
use App\Models\Application;
|
||||||
use App\Notifications\Application\StatusChanged;
|
use App\Notifications\Application\StatusChanged;
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
@ -22,9 +22,8 @@ public function mount()
|
|||||||
|
|
||||||
public function check_status()
|
public function check_status()
|
||||||
{
|
{
|
||||||
dispatch_sync(new ContainerStatusJob(
|
dispatch_sync(new ApplicationContainerStatusJob(
|
||||||
resource: $this->application,
|
application: $this->application,
|
||||||
container_name: generate_container_name($this->application->uuid),
|
|
||||||
));
|
));
|
||||||
$this->application->refresh();
|
$this->application->refresh();
|
||||||
}
|
}
|
||||||
@ -58,12 +57,21 @@ protected function setDeploymentUuid()
|
|||||||
|
|
||||||
public function stop()
|
public function stop()
|
||||||
{
|
{
|
||||||
remote_process(
|
$containers = getCurrentApplicationContainerStatus($this->application->destination->server, $this->application->id);
|
||||||
["docker rm -f {$this->application->uuid}"],
|
if ($containers->count() === 0) {
|
||||||
$this->application->destination->server
|
return;
|
||||||
);
|
}
|
||||||
$this->application->status = 'stopped';
|
foreach ($containers as $container) {
|
||||||
$this->application->save();
|
$containerName = data_get($container, 'Names');
|
||||||
$this->application->environment->project->team->notify(new StatusChanged($this->application));
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Http\Livewire\Project\Application;
|
namespace App\Http\Livewire\Project\Application;
|
||||||
|
|
||||||
use App\Jobs\ContainerStatusJob;
|
use App\Jobs\ApplicationContainerStatusJob;
|
||||||
use App\Models\Application;
|
use App\Models\Application;
|
||||||
use App\Models\ApplicationPreview;
|
use App\Models\ApplicationPreview;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
@ -25,10 +25,9 @@ public function mount()
|
|||||||
|
|
||||||
public function loadStatus($pull_request_id)
|
public function loadStatus($pull_request_id)
|
||||||
{
|
{
|
||||||
dispatch(new ContainerStatusJob(
|
dispatch(new ApplicationContainerStatusJob(
|
||||||
resource: $this->application,
|
application: $this->application,
|
||||||
container_name: generate_container_name($this->application->uuid, $pull_request_id),
|
pullRequestId: $pull_request_id
|
||||||
pull_request_id: $pull_request_id
|
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,7 +81,7 @@ protected function setDeploymentUuid()
|
|||||||
public function stop(int $pull_request_id)
|
public function stop(int $pull_request_id)
|
||||||
{
|
{
|
||||||
try {
|
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);
|
ray('Stopping container: ' . $container_name);
|
||||||
|
|
||||||
instant_remote_process(["docker rm -f $container_name"], $this->application->destination->server, throwError: false);
|
instant_remote_process(["docker rm -f $container_name"], $this->application->destination->server, throwError: false);
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
namespace App\Http\Livewire\Project\Database;
|
namespace App\Http\Livewire\Project\Database;
|
||||||
|
|
||||||
use App\Actions\Database\StartPostgresql;
|
use App\Actions\Database\StartPostgresql;
|
||||||
use App\Jobs\ContainerStatusJob;
|
use App\Jobs\DatabaseContainerStatusJob;
|
||||||
use App\Notifications\Application\StatusChanged;
|
use App\Notifications\Application\StatusChanged;
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
|
|
||||||
@ -25,9 +25,8 @@ public function activityFinished()
|
|||||||
|
|
||||||
public function check_status()
|
public function check_status()
|
||||||
{
|
{
|
||||||
dispatch_sync(new ContainerStatusJob(
|
dispatch_sync(new DatabaseContainerStatusJob(
|
||||||
resource: $this->database,
|
database: $this->database,
|
||||||
container_name: generate_container_name($this->database->uuid),
|
|
||||||
));
|
));
|
||||||
$this->database->refresh();
|
$this->database->refresh();
|
||||||
}
|
}
|
||||||
|
@ -59,6 +59,10 @@ public function submit()
|
|||||||
'source_id' => 0,
|
'source_id' => 0,
|
||||||
'source_type' => GithubApp::class
|
'source_type' => GithubApp::class
|
||||||
]);
|
]);
|
||||||
|
$application->update([
|
||||||
|
'name' => 'dockerfile-' . $application->id
|
||||||
|
]);
|
||||||
|
|
||||||
redirect()->route('project.application.configuration', [
|
redirect()->route('project.application.configuration', [
|
||||||
'application_uuid' => $application->uuid,
|
'application_uuid' => $application->uuid,
|
||||||
'environment_name' => $environment->name,
|
'environment_name' => $environment->name,
|
||||||
|
@ -17,7 +17,7 @@ public function start_proxy()
|
|||||||
$this->server->proxy->last_applied_settings &&
|
$this->server->proxy->last_applied_settings &&
|
||||||
$this->server->proxy->last_saved_settings !== $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);
|
$activity = resolve(StartProxy::class)($this->server);
|
||||||
$this->emit('newMonitorActivity', $activity->id);
|
$this->emit('newMonitorActivity', $activity->id);
|
||||||
|
53
app/Jobs/ApplicationContainerStatusJob.php
Normal file
53
app/Jobs/ApplicationContainerStatusJob.php
Normal file
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -50,6 +50,7 @@ class ApplicationDeploymentJob implements ShouldQueue
|
|||||||
private ApplicationPreview|null $preview = null;
|
private ApplicationPreview|null $preview = null;
|
||||||
|
|
||||||
private string $container_name;
|
private string $container_name;
|
||||||
|
private string|null $currently_running_container_name = null;
|
||||||
private string $workdir;
|
private string $workdir;
|
||||||
private string $configuration_dir;
|
private string $configuration_dir;
|
||||||
private string $build_workdir;
|
private string $build_workdir;
|
||||||
@ -86,7 +87,7 @@ public function __construct(int $application_deployment_queue_id)
|
|||||||
$this->build_workdir = "{$this->workdir}" . rtrim($this->application->base_directory, '/');
|
$this->build_workdir = "{$this->workdir}" . rtrim($this->application->base_directory, '/');
|
||||||
$this->is_debug_enabled = $this->application->settings->is_debug_enabled;
|
$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->private_key_location = save_private_key_for_server($this->server);
|
||||||
$this->saved_outputs = collect();
|
$this->saved_outputs = collect();
|
||||||
|
|
||||||
@ -113,6 +114,10 @@ public function __construct(int $application_deployment_queue_id)
|
|||||||
public function handle(): void
|
public function handle(): void
|
||||||
{
|
{
|
||||||
// ray()->measure();
|
// 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([
|
$this->application_deployment_queue->update([
|
||||||
'status' => ApplicationDeploymentStatus::IN_PROGRESS->value,
|
'status' => ApplicationDeploymentStatus::IN_PROGRESS->value,
|
||||||
]);
|
]);
|
||||||
@ -175,9 +180,9 @@ private function deploy_simple_dockerfile()
|
|||||||
$this->generate_build_env_variables();
|
$this->generate_build_env_variables();
|
||||||
$this->add_build_env_variables_to_dockerfile();
|
$this->add_build_env_variables_to_dockerfile();
|
||||||
$this->build_image();
|
$this->build_image();
|
||||||
$this->stop_running_container();
|
$this->rolling_update();
|
||||||
$this->start_by_compose_file();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function deploy()
|
private function deploy()
|
||||||
{
|
{
|
||||||
$this->execute_remote_command(
|
$this->execute_remote_command(
|
||||||
@ -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...'"
|
"echo 'Docker Image found locally with the same Git Commit SHA {$this->application->uuid}:{$this->commit}. Build step skipped...'"
|
||||||
]);
|
]);
|
||||||
$this->generate_compose_file();
|
$this->generate_compose_file();
|
||||||
$this->stop_running_container();
|
$this->rolling_update();
|
||||||
$this->start_by_compose_file();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -219,8 +223,54 @@ private function deploy()
|
|||||||
$this->generate_build_env_variables();
|
$this->generate_build_env_variables();
|
||||||
$this->add_build_env_variables_to_dockerfile();
|
$this->add_build_env_variables_to_dockerfile();
|
||||||
$this->build_image();
|
$this->build_image();
|
||||||
$this->stop_running_container();
|
$this->rolling_update();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function rolling_update()
|
||||||
|
{
|
||||||
$this->start_by_compose_file();
|
$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()
|
private function deploy_pull_request()
|
||||||
{
|
{
|
||||||
@ -241,8 +291,7 @@ private function deploy_pull_request()
|
|||||||
// $this->generate_build_env_variables();
|
// $this->generate_build_env_variables();
|
||||||
// $this->add_build_env_variables_to_dockerfile();
|
// $this->add_build_env_variables_to_dockerfile();
|
||||||
$this->build_image();
|
$this->build_image();
|
||||||
$this->stop_running_container();
|
$this->rolling_update();
|
||||||
$this->start_by_compose_file();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function prepare_builder_image()
|
private function prepare_builder_image()
|
||||||
@ -409,7 +458,7 @@ private function generate_compose_file()
|
|||||||
$this->container_name => [
|
$this->container_name => [
|
||||||
'image' => $this->production_image_name,
|
'image' => $this->production_image_name,
|
||||||
'container_name' => $this->container_name,
|
'container_name' => $this->container_name,
|
||||||
'restart' => 'always',
|
'restart' => RESTART_MODE,
|
||||||
'environment' => $environment_variables,
|
'environment' => $environment_variables,
|
||||||
'labels' => $this->set_labels_for_applications(),
|
'labels' => $this->set_labels_for_applications(),
|
||||||
'expose' => $ports,
|
'expose' => $ports,
|
||||||
@ -539,8 +588,8 @@ private function set_labels_for_applications()
|
|||||||
$schema = $url->getScheme();
|
$schema = $url->getScheme();
|
||||||
$slug = Str::slug($host . $path);
|
$slug = Str::slug($host . $path);
|
||||||
|
|
||||||
$http_label = "{$this->application->uuid}-{$slug}-http";
|
$http_label = "{$this->container_name}-{$slug}-http";
|
||||||
$https_label = "{$this->application->uuid}-{$slug}-https";
|
$https_label = "{$this->container_name}-{$slug}-https";
|
||||||
|
|
||||||
if ($schema === 'https') {
|
if ($schema === 'https') {
|
||||||
// Set labels for https
|
// Set labels for https
|
||||||
@ -647,23 +696,22 @@ private function build_image()
|
|||||||
|
|
||||||
private function stop_running_container()
|
private function stop_running_container()
|
||||||
{
|
{
|
||||||
$this->execute_remote_command(
|
if ($this->currently_running_container_name) {
|
||||||
["echo -n 'Removing old running application.'"],
|
$this->execute_remote_command(
|
||||||
[$this->execute_in_builder("docker rm -f $this->container_name >/dev/null 2>&1"), "hidden" => true],
|
["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()
|
private function start_by_compose_file()
|
||||||
{
|
{
|
||||||
$this->execute_remote_command(
|
$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],
|
[$this->execute_in_builder("docker compose --project-directory {$this->workdir} up -d >/dev/null"), "hidden" => true],
|
||||||
["echo 'Done. 🎉'"],
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private function generate_build_env_variables()
|
private function generate_build_env_variables()
|
||||||
{
|
{
|
||||||
$this->build_args = collect(["--build-arg SOURCE_COMMIT={$this->commit}"]);
|
$this->build_args = collect(["--build-arg SOURCE_COMMIT={$this->commit}"]);
|
||||||
|
@ -1,54 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Jobs;
|
|
||||||
|
|
||||||
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 ContainerStatusJob implements ShouldQueue, ShouldBeUnique
|
|
||||||
{
|
|
||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
||||||
|
|
||||||
public string $container_name;
|
|
||||||
public string|null $pull_request_id;
|
|
||||||
public $resource;
|
|
||||||
|
|
||||||
public function __construct($resource, string $container_name, string|null $pull_request_id = null)
|
|
||||||
{
|
|
||||||
$this->resource = $resource;
|
|
||||||
$this->container_name = $container_name;
|
|
||||||
$this->pull_request_id = $pull_request_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function uniqueId(): string
|
|
||||||
{
|
|
||||||
return $this->container_name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function handle(): void
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
$status = get_container_status(server: $this->resource->destination->server, container_id: $this->container_name, throwError: false);
|
|
||||||
if ($this->resource->status === 'running' && $status !== 'running') {
|
|
||||||
$this->resource->environment->project->team->notify(new StatusChanged($this->resource));
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->pull_request_id) {
|
|
||||||
$preview = ApplicationPreview::findPreviewByApplicationAndPullId($this->resource->id, $this->pull_request_id);
|
|
||||||
$preview->status = $status;
|
|
||||||
$preview->save();
|
|
||||||
} else {
|
|
||||||
$this->resource->status = $status;
|
|
||||||
$this->resource->save();
|
|
||||||
}
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
ray($e->getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
52
app/Jobs/DatabaseContainerStatusJob.php
Normal file
52
app/Jobs/DatabaseContainerStatusJob.php
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Jobs;
|
||||||
|
|
||||||
|
use App\Models\ApplicationPreview;
|
||||||
|
use App\Models\StandalonePostgresql;
|
||||||
|
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 DatabaseContainerStatusJob implements ShouldQueue, ShouldBeUnique
|
||||||
|
{
|
||||||
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
|
public string $containerName;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
public StandalonePostgresql $database,
|
||||||
|
) {
|
||||||
|
$this->containerName = $database->uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function uniqueId(): string
|
||||||
|
{
|
||||||
|
return $this->containerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handle(): void
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$status = getContainerStatus(
|
||||||
|
server: $this->database->destination->server,
|
||||||
|
container_id: $this->containerName,
|
||||||
|
throwError: false
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($this->database->status === 'running' && $status !== 'running') {
|
||||||
|
$this->database->environment->project->team->notify(new StatusChanged($this->database));
|
||||||
|
}
|
||||||
|
if ($this->database->status !== $status) {
|
||||||
|
$this->database->status = $status;
|
||||||
|
$this->database->save();
|
||||||
|
}
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
ray($e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -24,7 +24,7 @@ public function handle()
|
|||||||
$container_name = 'coolify-proxy';
|
$container_name = 'coolify-proxy';
|
||||||
$servers = Server::isUsable()->whereNotNull('proxy')->get();
|
$servers = Server::isUsable()->whereNotNull('proxy')->get();
|
||||||
foreach ($servers as $server) {
|
foreach ($servers as $server) {
|
||||||
$status = get_container_status(server: $server, container_id: $container_name);
|
$status = getContainerStatus(server: $server, container_id: $container_name);
|
||||||
if ($status === 'running') {
|
if ($status === 'running') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ public function uniqueId(): int
|
|||||||
public function handle(): void
|
public function handle(): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$container = get_container_status(server: $this->server, all_data: true, container_id: 'coolify-proxy', throwError: true);
|
$container = getContainerStatus(server: $this->server, all_data: true, container_id: 'coolify-proxy', throwError: true);
|
||||||
$status = $container['State']['Status'];
|
$status = $container['State']['Status'];
|
||||||
if ($this->server->proxy->status !== $status) {
|
if ($this->server->proxy->status !== $status) {
|
||||||
$this->server->proxy->status = $status;
|
$this->server->proxy->status = $status;
|
||||||
|
@ -23,7 +23,7 @@ public function handle()
|
|||||||
try {
|
try {
|
||||||
$container_name = 'coolify-proxy';
|
$container_name = 'coolify-proxy';
|
||||||
ray('Starting proxy for server: ' . $this->server->name);
|
ray('Starting proxy for server: ' . $this->server->name);
|
||||||
$status = get_container_status(server: $this->server, container_id: $container_name);
|
$status = getContainerStatus(server: $this->server, container_id: $container_name);
|
||||||
if ($status === 'running') {
|
if ($status === 'running') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace App\Jobs;
|
namespace App\Jobs;
|
||||||
|
|
||||||
use App\Models\Application;
|
use App\Models\Application;
|
||||||
|
use App\Models\StandalonePostgresql;
|
||||||
use Illuminate\Bus\Queueable;
|
use Illuminate\Bus\Queueable;
|
||||||
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
@ -15,19 +16,25 @@ class ResourceStatusJob implements ShouldQueue, ShouldBeUnique
|
|||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
public $applications;
|
public $applications;
|
||||||
|
public $postgresqls;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->applications = Application::all();
|
$this->applications = Application::all();
|
||||||
|
$this->postgresqls = StandalonePostgresql::all();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handle(): void
|
public function handle(): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
foreach ($this->applications as $application) {
|
foreach ($this->applications as $application) {
|
||||||
dispatch(new ContainerStatusJob(
|
dispatch(new ApplicationContainerStatusJob(
|
||||||
resource: $application,
|
application: $application,
|
||||||
container_name: generate_container_name($application->uuid),
|
));
|
||||||
|
}
|
||||||
|
foreach ($this->postgresqls as $postgresql) {
|
||||||
|
dispatch(new DatabaseContainerStatusJob(
|
||||||
|
database: $postgresql,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
|
@ -9,3 +9,4 @@
|
|||||||
'monthly' => '0 0 1 * *',
|
'monthly' => '0 0 1 * *',
|
||||||
'yearly' => '0 0 1 1 *',
|
'yearly' => '0 0 1 1 *',
|
||||||
];
|
];
|
||||||
|
const RESTART_MODE = 'unless-stopped';
|
||||||
|
@ -1,14 +1,27 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use App\Models\Application;
|
||||||
use App\Models\Server;
|
use App\Models\Server;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
function getCurrentApplicationContainerStatus(Server $server, int $id): Collection {
|
||||||
|
$containers = instant_remote_process(["docker ps -a --filter='label=coolify.applicationId={$id}' --format '{{json .}}' "], $server);
|
||||||
|
if (!$containers) {
|
||||||
|
return collect([]);
|
||||||
|
}
|
||||||
|
return format_docker_command_output_to_json($containers);
|
||||||
|
}
|
||||||
|
|
||||||
function format_docker_command_output_to_json($rawOutput): Collection
|
function format_docker_command_output_to_json($rawOutput): Collection
|
||||||
{
|
{
|
||||||
$outputLines = explode(PHP_EOL, $rawOutput);
|
$outputLines = explode(PHP_EOL, $rawOutput);
|
||||||
|
if (count($outputLines) === 1) {
|
||||||
return collect($outputLines)
|
$outputLines = collect($outputLines[0]);
|
||||||
|
} else {
|
||||||
|
$outputLines = collect($outputLines);
|
||||||
|
}
|
||||||
|
return $outputLines
|
||||||
->reject(fn ($line) => empty($line))
|
->reject(fn ($line) => empty($line))
|
||||||
->map(fn ($outputLine) => json_decode($outputLine, true, flags: JSON_THROW_ON_ERROR));
|
->map(fn ($outputLine) => json_decode($outputLine, true, flags: JSON_THROW_ON_ERROR));
|
||||||
}
|
}
|
||||||
@ -44,26 +57,38 @@ function format_docker_envs_to_json($rawOutput)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_container_status(Server $server, string $container_id, bool $all_data = false, bool $throwError = false)
|
function getApplicationContainerStatus(Application $application) {
|
||||||
|
$server = $application->destination->server;
|
||||||
|
$id = $application->id;
|
||||||
|
|
||||||
|
$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)
|
||||||
{
|
{
|
||||||
check_server_connection($server);
|
// check_server_connection($server);
|
||||||
$container = instant_remote_process(["docker inspect --format '{{json .}}' {$container_id}"], $server, $throwError);
|
$container = instant_remote_process(["docker inspect --format '{{json .}}' {$container_id}"], $server, $throwError);
|
||||||
if (!$container) {
|
if (!$container) {
|
||||||
return 'exited';
|
return 'exited';
|
||||||
}
|
}
|
||||||
$container = format_docker_command_output_to_json($container);
|
$container = format_docker_command_output_to_json($container);
|
||||||
if ($all_data) {
|
if ($all_data) {
|
||||||
return $container[0];
|
return $container;
|
||||||
}
|
}
|
||||||
return data_get($container[0], 'State.Status', 'exited');
|
return data_get($container[0], 'State.Status', 'exited');
|
||||||
}
|
}
|
||||||
|
|
||||||
function generate_container_name(string $uuid, int $pull_request_id = 0)
|
function generateApplicationContainerName(string $uuid, int $pull_request_id = 0)
|
||||||
{
|
{
|
||||||
if ($pull_request_id !== 0) {
|
$now = now()->format('YmdHis');
|
||||||
return $uuid . '-pr-' . $pull_request_id;
|
if ($pull_request_id !== 0 && $pull_request_id !== null) {
|
||||||
|
return $uuid . '-pr-' . $pull_request_id . '-' . $now;
|
||||||
} else {
|
} else {
|
||||||
return $uuid;
|
return $uuid . '-' . $now;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function get_port_from_dockerfile($dockerfile): int
|
function get_port_from_dockerfile($dockerfile): int
|
||||||
|
@ -32,7 +32,7 @@ function generate_default_proxy_configuration(Server $server)
|
|||||||
"traefik" => [
|
"traefik" => [
|
||||||
"container_name" => "coolify-proxy",
|
"container_name" => "coolify-proxy",
|
||||||
"image" => "traefik:v2.10",
|
"image" => "traefik:v2.10",
|
||||||
"restart" => "always",
|
"restart" => RESTART_MODE,
|
||||||
"extra_hosts" => [
|
"extra_hosts" => [
|
||||||
"host.docker.internal:host-gateway",
|
"host.docker.internal:host-gateway",
|
||||||
],
|
],
|
||||||
|
@ -161,7 +161,7 @@
|
|||||||
$found = ApplicationPreview::where('application_id', $application->id)->where('pull_request_id', $pull_request_id)->first();
|
$found = ApplicationPreview::where('application_id', $application->id)->where('pull_request_id', $pull_request_id)->first();
|
||||||
if ($found) {
|
if ($found) {
|
||||||
$found->delete();
|
$found->delete();
|
||||||
$container_name = generate_container_name($application->uuid, $pull_request_id);
|
$container_name = generateApplicationContainerName($application->uuid, $pull_request_id);
|
||||||
ray('Stopping container: ' . $container_name);
|
ray('Stopping container: ' . $container_name);
|
||||||
remote_process(["docker rm -f $container_name"], $application->destination->server);
|
remote_process(["docker rm -f $container_name"], $application->destination->server);
|
||||||
return response('Preview Deployment closed.');
|
return response('Preview Deployment closed.');
|
||||||
|
Loading…
Reference in New Issue
Block a user