rollback job

This commit is contained in:
Andras Bacsai 2023-05-22 10:34:00 +02:00
parent 828ecc6803
commit ecf0b30513
8 changed files with 443 additions and 36 deletions

View File

@ -2,18 +2,36 @@
namespace App\Http\Livewire\Project\Application; namespace App\Http\Livewire\Project\Application;
use App\Jobs\DeployApplicationJob;
use App\Jobs\RollbackApplicationJob;
use App\Models\Application; use App\Models\Application;
use Livewire\Component; use Livewire\Component;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Visus\Cuid2\Cuid2;
class Rollback extends Component class Rollback extends Component
{ {
public Application $application; public Application $application;
public $images = []; public $images = [];
public string|null $current; public string|null $current;
public function revertImage($tag) public array $parameters;
public function mount()
{ {
dd("Reverting to {$this->application->uuid}:{$tag}"); $this->parameters = getParameters();
}
public function rollbackImage($tag)
{
$deployment_uuid = new Cuid2(7);
dispatch(new RollbackApplicationJob(
deployment_uuid: $deployment_uuid,
application_uuid: $this->application->uuid,
commit: $tag,
));
$this->parameters['deployment_uuid'] = $deployment_uuid;
return redirect()->route('project.application.deployment', $this->parameters);
} }
public function loadImages() public function loadImages()
{ {

View File

@ -0,0 +1,379 @@
<?php
namespace App\Jobs;
use App\Actions\CoolifyTask\RunRemoteProcess;
use App\Data\CoolifyTaskArgs;
use App\Enums\ActivityTypes;
use App\Models\Application;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;
use Spatie\Activitylog\Models\Activity;
use Symfony\Component\Yaml\Yaml;
use Illuminate\Support\Str;
use Spatie\Url\Url;
class RollbackApplicationJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $application;
protected $destination;
protected $source;
protected Activity $activity;
protected string $git_commit;
protected string $workdir;
protected string $docker_compose;
protected $build_args;
protected $env_args;
public static int $batch_counter = 0;
public $timeout = 3600;
/**
* Create a new job instance.
*/
public function __construct(
public string $deployment_uuid,
public string $application_uuid,
public string $commit,
) {
$this->application = Application::query()
->where('uuid', $this->application_uuid)
->firstOrFail();
$this->destination = $this->application->destination->getMorphClass()::where('id', $this->application->destination->id)->first();
$server = $this->destination->server;
$private_key_location = savePrivateKeyForServer($server);
$this->git_commit = $commit;
$remoteProcessArgs = new CoolifyTaskArgs(
server_ip: $server->ip,
private_key_location: $private_key_location,
command: 'overwritten-later',
port: $server->port,
user: $server->user,
type: ActivityTypes::DEPLOYMENT->value,
type_uuid: $this->deployment_uuid,
);
$this->activity = activity()
->performedOn($this->application)
->withProperties($remoteProcessArgs->toArray())
->event(ActivityTypes::DEPLOYMENT->value)
->log("[]");
}
protected function stopRunningContainer()
{
$this->executeNow([
"echo -n 'Removing old instance... '",
$this->execute_in_builder("docker rm -f {$this->application->uuid} >/dev/null 2>&1"),
"echo 'Done.'",
"echo -n 'Starting your application... '",
]);
}
protected function startByComposeFile()
{
$this->executeNow([
$this->execute_in_builder("docker compose --project-directory {$this->workdir} up -d >/dev/null"),
], isDebuggable: true);
$this->executeNow([
"echo 'Done. 🎉'",
], isFinished: true);
}
protected function generateComposeFile()
{
$this->docker_compose = $this->generate_docker_compose();
$docker_compose_base64 = base64_encode($this->docker_compose);
$this->executeNow([
$this->execute_in_builder("mkdir -p {$this->workdir}"),
$this->execute_in_builder("echo '{$docker_compose_base64}' | base64 -d > {$this->workdir}/docker-compose.yml")
], hideFromOutput: true);
}
/**
* Execute the job.
*/
public function handle(): void
{
try {
$this->workdir = "/artifacts/{$this->deployment_uuid}";
$this->executeNow([
"echo 'Starting rollback of {$this->application->git_repository}:{$this->application->git_branch} to {$this->git_commit}...'",
"echo -n 'Pulling latest version of the builder image (ghcr.io/coollabsio/coolify-builder)... '",
]);
$this->executeNow([
"docker run --pull=always -d --name {$this->deployment_uuid} --rm -v /var/run/docker.sock:/var/run/docker.sock ghcr.io/coollabsio/coolify-builder",
], isDebuggable: true);
$this->executeNow([
"echo 'Done.'",
"echo -n 'Checking if the image is available... '",
]);
$this->executeNow([
"docker images -q {$this->application->uuid}:{$this->git_commit} 2>/dev/null",
], 'local_image_found', hideFromOutput: true, ignoreErrors: true);
$image_found = Str::of($this->activity->properties->get('local_image_found'))->trim()->isNotEmpty();
if ($image_found) {
$this->executeNow([
"echo 'Yes, it is available.'",
]);
// Generate docker-compose.yml
$this->generateComposeFile();
// Stop running container
$this->stopRunningContainer();
// Start application
$this->startByComposeFile();
$this->application->git_commit_sha = $this->git_commit;
$this->application->settings->is_auto_deploy = false;
$this->application->settings->save();
$this->application->save();
$this->executeNow([
"echo 'Auto deployments are disabled for this application to prevent overwritten automatically.'",
"echo 'Commit SHA set to {$this->git_commit} in the Source menu.'",
]);
return;
}
throw new \Exception('Docker Image not found locally with the same Git Commit SHA.');
} catch (\Exception $e) {
$this->executeNow([
"echo '\nOops something is not okay, are you okay? 😢'",
"echo '\n\n{$e->getMessage()}'",
]);
$this->fail($e->getMessage());
} finally {
// Saving docker-compose.yml
if (isset($this->docker_compose)) {
Storage::disk('deployments')->put(Str::kebab($this->application->name) . '/docker-compose.yml', $this->docker_compose);
}
$this->executeNow(["docker rm -f {$this->deployment_uuid} >/dev/null 2>&1"], hideFromOutput: true);
dispatch(new ContainerStatusJob($this->application_uuid));
}
}
private function execute_in_builder(string $command)
{
return "docker exec {$this->deployment_uuid} bash -c '{$command}'";
}
private function generate_environment_variables($ports)
{
$environment_variables = collect();
foreach ($this->application->runtime_environment_variables as $env) {
$environment_variables->push("$env->key=$env->value");
}
// Add PORT if not exists, use the first port as default
if ($environment_variables->filter(fn ($env) => Str::of($env)->contains('PORT'))->isEmpty()) {
$environment_variables->push("PORT={$ports[0]}");
}
return $environment_variables->all();
}
private function generate_env_variables()
{
$this->env_args = collect([]);
foreach ($this->application->nixpacks_environment_variables as $env) {
$this->env_args->push("--env {$env->key}={$env->value}");
}
$this->env_args = $this->env_args->implode(' ');
}
private function generate_build_env_variables()
{
$this->build_args = collect(["--build-arg SOURCE_COMMIT={$this->git_commit}"]);
foreach ($this->application->build_environment_variables as $env) {
$this->build_args->push("--build-arg {$env->key}={$env->value}");
}
$this->build_args = $this->build_args->implode(' ');
}
private function add_build_env_variables_to_dockerfile()
{
$this->executeNow([
$this->execute_in_builder("cat {$this->workdir}/Dockerfile")
], propertyName: 'dockerfile', hideFromOutput: true);
$dockerfile = collect(Str::of($this->activity->properties->get('dockerfile'))->trim()->explode("\n"));
foreach ($this->application->build_environment_variables as $env) {
$dockerfile->splice(1, 0, "ARG {$env->key}={$env->value}");
}
$dockerfile_base64 = base64_encode($dockerfile->implode("\n"));
$this->executeNow([
$this->execute_in_builder("echo '{$dockerfile_base64}' | base64 -d > {$this->workdir}/Dockerfile")
], hideFromOutput: true);
}
private function generate_docker_compose()
{
$ports = $this->application->settings->is_static ? [80] : $this->application->ports_exposes_array;
$persistentStorages = $this->generate_local_persistent_volumes();
$volume_names = $this->generate_local_persistent_volumes_only_volume_names();
$environment_variables = $this->generate_environment_variables($ports);
$docker_compose = [
'version' => '3.8',
'services' => [
$this->application->uuid => [
'image' => "{$this->application->uuid}:$this->git_commit",
'container_name' => $this->application->uuid,
'restart' => 'always',
'environment' => $environment_variables,
'labels' => $this->set_labels_for_applications(),
'expose' => $ports,
'networks' => [
$this->destination->network,
],
'healthcheck' => [
'test' => [
'CMD-SHELL',
$this->generate_healthcheck_commands()
],
'interval' => $this->application->health_check_interval . 's',
'timeout' => $this->application->health_check_timeout . 's',
'retries' => $this->application->health_check_retries,
'start_period' => $this->application->health_check_start_period . 's'
],
'mem_limit' => $this->application->limits_memory,
'memswap_limit' => $this->application->limits_memory_swap,
'mem_swappiness' => $this->application->limits_memory_swappiness,
'mem_reservation' => $this->application->limits_memory_reservation,
'oom_kill_disable' => $this->application->limits_memory_oom_kill,
'cpus' => $this->application->limits_cpus,
'cpuset' => $this->application->limits_cpuset,
'cpu_shares' => $this->application->limits_cpu_shares,
]
],
'networks' => [
$this->destination->network => [
'external' => false,
'name' => $this->destination->network,
'attachable' => true,
]
]
];
if (count($this->application->ports_mappings_array) > 0) {
$docker_compose['services'][$this->application->uuid]['ports'] = $this->application->ports_mappings_array;
}
if (count($persistentStorages) > 0) {
$docker_compose['services'][$this->application->uuid]['volumes'] = $persistentStorages;
}
if (count($volume_names) > 0) {
$docker_compose['volumes'] = $volume_names;
}
return Yaml::dump($docker_compose, 10);
}
private function generate_local_persistent_volumes()
{
foreach ($this->application->persistentStorages as $persistentStorage) {
$volume_name = $persistentStorage->host_path ?? $persistentStorage->name;
$local_persistent_volumes[] = $volume_name . ':' . $persistentStorage->mount_path;
}
return $local_persistent_volumes ?? [];
}
private function generate_local_persistent_volumes_only_volume_names()
{
foreach ($this->application->persistentStorages as $persistentStorage) {
if ($persistentStorage->host_path) {
continue;
}
$local_persistent_volumes_names[$persistentStorage->name] = [
'name' => $persistentStorage->name,
'external' => false,
];
}
return $local_persistent_volumes_names ?? [];
}
private function generate_healthcheck_commands()
{
if (!$this->application->health_check_port) {
$this->application->health_check_port = $this->application->ports_exposes_array[0];
}
if ($this->application->health_check_path) {
$generated_healthchecks_commands = [
"curl -s -X {$this->application->health_check_method} -f {$this->application->health_check_scheme}://{$this->application->health_check_host}:{$this->application->health_check_port}{$this->application->health_check_path} > /dev/null"
];
} else {
$generated_healthchecks_commands = [
"curl -s -X {$this->application->health_check_method} -f {$this->application->health_check_scheme}://{$this->application->health_check_host}:{$this->application->health_check_port}/"
];
}
return implode(' ', $generated_healthchecks_commands);
}
private function set_labels_for_applications()
{
$labels = [];
$labels[] = 'coolify.managed=true';
$labels[] = 'coolify.version=' . config('version');
$labels[] = 'coolify.applicationId=' . $this->application->id;
$labels[] = 'coolify.type=application';
$labels[] = 'coolify.name=' . $this->application->name;
if ($this->application->fqdn) {
$domains = Str::of($this->application->fqdn)->explode(',');
$labels[] = 'traefik.enable=true';
foreach ($domains as $domain) {
$url = Url::fromString($domain);
$host = $url->getHost();
$path = $url->getPath();
$slug = Str::slug($url);
$label_id = "{$this->application->uuid}-{$slug}";
if ($path === '/') {
$labels[] = "traefik.http.routers.{$label_id}.rule=Host(`{$host}`) && PathPrefix(`{$path}`)";
} else {
$labels[] = "traefik.http.routers.{$label_id}.rule=Host(`{$host}`) && PathPrefix(`{$path}`)";
$labels[] = "traefik.http.routers.{$label_id}.middlewares={$label_id}-stripprefix";
$labels[] = "traefik.http.middlewares.{$label_id}-stripprefix.stripprefix.prefixes={$path}";
}
}
}
return $labels;
}
private function executeNow(
array|Collection $command,
string $propertyName = null,
bool $isFinished = false,
bool $hideFromOutput = false,
bool $isDebuggable = false,
bool $ignoreErrors = false
) {
static::$batch_counter++;
if ($command instanceof Collection) {
$commandText = $command->implode("\n");
} else {
$commandText = collect($command)->implode("\n");
}
$this->activity->properties = $this->activity->properties->merge([
'command' => $commandText,
]);
$this->activity->save();
if ($isDebuggable && !$this->application->settings->is_debug) {
$hideFromOutput = true;
}
$remoteProcess = resolve(RunRemoteProcess::class, [
'activity' => $this->activity,
'hideFromOutput' => $hideFromOutput,
'isFinished' => $isFinished,
'ignoreErrors' => $ignoreErrors,
]);
$result = $remoteProcess();
if ($propertyName) {
$this->activity->properties = $this->activity->properties->merge([
$propertyName => trim($result->output()),
]);
$this->activity->save();
}
if ($result->exitCode() != 0 && $result->errorOutput() && !$ignoreErrors) {
throw new \RuntimeException($result->errorOutput());
}
}
}

View File

@ -3,28 +3,32 @@
'disabled' => null, 'disabled' => null,
'confirm' => null, 'confirm' => null,
'confirmAction' => null, 'confirmAction' => null,
'tooltip' => null,
]) ])
@if ($type === 'submit') <div class="tooltip tooltip-warning" @isset($tooltip) data-tip="{{ $tooltip }}" @endisset>
<button {{ $attributes }} type="submit" @if ($disabled !== null) disabled @endif wire:target="submit" @if ($type === 'submit')
wire:loading.delay.shorter.class="loading" <button {{ $attributes }} type="submit" @if ($disabled !== null) disabled @endif wire:target="submit"
@isset($confirm) wire:loading.delay.shorter.class="loading"
@isset($confirm)
x-on:click="toggleConfirmModal('{{ $confirm }}', '{{ explode('(', $confirmAction)[0] }}')" x-on:click="toggleConfirmModal('{{ $confirm }}', '{{ explode('(', $confirmAction)[0] }}')"
@endisset @endisset
@isset($confirmAction) @isset($confirmAction)
x-on:{{ explode('(', $confirmAction)[0] }}.window="$wire.{{ explode('(', $confirmAction)[0] }}" x-on:{{ explode('(', $confirmAction)[0] }}.window="$wire.{{ explode('(', $confirmAction)[0] }}"
@endisset> @endisset>
{{ $slot }} {{ $slot }}
</button> </button>
@elseif($type === 'button') @elseif($type === 'button')
<button {{ $attributes }} @if ($disabled !== null) disabled @endif type="button" <button {{ $attributes }} @if ($disabled !== null) disabled @endif type="button"
wire:target="{{ explode('(', $attributes->whereStartsWith('wire:click')->first())[0] }}" wire:target="{{ explode('(', $attributes->whereStartsWith('wire:click')->first())[0] }}"
wire:loading.delay.shorter.class="loading" wire:loading.delay.shorter.class="loading"
@isset($confirm) @isset($confirm)
x-on:click="toggleConfirmModal('{{ $confirm }}', '{{ explode('(', $confirmAction)[0] }}')" x-on:click="toggleConfirmModal('{{ $confirm }}', '{{ explode('(', $confirmAction)[0] }}')"
@endisset @endisset
@isset($confirmAction) @isset($confirmAction)
x-on:{{ explode('(', $confirmAction)[0] }}.window="$wire.{{ explode('(', $confirmAction)[0] }}" x-on:{{ explode('(', $confirmAction)[0] }}.window="$wire.{{ explode('(', $confirmAction)[0] }}"
@endisset> @endisset>
{{ $slot }} {{ $slot }}
</button> </button>
@endif @endif
</div>

View File

@ -16,7 +16,7 @@
{{ $id }} {{ $id }}
@endif @endif
@if ($helper) @if ($helper)
<div class="-mb-1 dropdown dropdown-right"> <div class="-mb-1 dropdown dropdown-right dropdown-hover">
<label tabindex="0" class="cursor-pointer text-warning"> <label tabindex="0" class="cursor-pointer text-warning">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
class="w-4 h-4 stroke-current"> class="w-4 h-4 stroke-current">
@ -25,7 +25,7 @@ class="w-4 h-4 stroke-current">
</svg> </svg>
</label> </label>
<div tabindex="0" <div tabindex="0"
class="border-2 shadow whitespace-nowrap w-max-fit border-coolgray-500 card compact dropdown-content bg-coolgray-200"> class="border rounded shadow border-warning/30 card compact dropdown-content bg-coolgray-200 w-96">
<div class="card-body"> <div class="card-body">
{!! $helper !!} {!! $helper !!}
</div> </div>

View File

@ -22,7 +22,7 @@
<span class="text-warning">*</span> <span class="text-warning">*</span>
@endif @endif
@if ($helper) @if ($helper)
<div class="-mb-1 dropdown dropdown-right"> <div class="-mb-1 dropdown dropdown-right dropdown-hover">
<label tabindex="0" class="cursor-pointer text-warning"> <label tabindex="0" class="cursor-pointer text-warning">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
class="w-4 h-4 stroke-current"> class="w-4 h-4 stroke-current">
@ -31,7 +31,7 @@ class="w-4 h-4 stroke-current">
</svg> </svg>
</label> </label>
<div tabindex="0" <div tabindex="0"
class="border-2 shadow w-96 border-coolgray-500 card compact dropdown-content bg-coolgray-200"> class="border rounded shadow border-warning/30 card compact dropdown-content bg-coolgray-200 w-96">
<div class="card-body"> <div class="card-body">
{!! $helper !!} {!! $helper !!}
</div> </div>

View File

@ -1,8 +1,7 @@
<div> <div>
<h2>Destination</h2> <h2>Destination</h2>
<p>Server Name: {{ data_get($destination, 'server.name') }}</p> <div>
@if (data_get($destination, 'server.description')) <p>Server: {{ data_get($destination, 'server.name') }}</p>
<p>Description: {{ data_get($destination, 'server.description') }}</p> <p>Destination: {{ $destination->network }}</p>
@endif </div>
<p>Docker Network: {{ $destination->network }}</p>
</div> </div>

View File

@ -6,6 +6,7 @@
Save Save
</x-inputs.button> </x-inputs.button>
</div> </div>
<x-inputs.checkbox instantSave id="is_static" label="Static website?" />
<div class="flex flex-col gap-2 pb-4"> <div class="flex flex-col gap-2 pb-4">
<div class="flex flex-col gap-2 xl:flex-row"> <div class="flex flex-col gap-2 xl:flex-row">
<x-inputs.input class="w-full" id="application.name" label="Name" required /> <x-inputs.input class="w-full" id="application.name" label="Name" required />
@ -49,11 +50,12 @@
helper="A comma separated list of ports you would like to map to the host system. Useful when you do not want to use domains.<br><span class='inline-block font-bold text-warning'>Example</span>3000:3000,3002:3002" /> helper="A comma separated list of ports you would like to map to the host system. Useful when you do not want to use domains.<br><span class='inline-block font-bold text-warning'>Example</span>3000:3000,3002:3002" />
</div> </div>
</div> </div>
<h3>Advanced</h3>
<div class="flex flex-col"> <div class="flex flex-col">
<x-inputs.checkbox helper="More logs will be visible during a deployment." instantSave id="is_debug" <x-inputs.checkbox helper="More logs will be visible during a deployment." instantSave id="is_debug"
label="Debug" /> label="Debug" />
<x-inputs.checkbox instantSave id="is_static" label="Static website?" />
<x-inputs.checkbox helper="Git Webhooks won't deploy your application is you turn it off." instantSave <x-inputs.checkbox helper="Automatically deploy new commits based on Git webhooks." instantSave
id="is_auto_deploy" label="Auto Deploy?" /> id="is_auto_deploy" label="Auto Deploy?" />
{{-- <x-inputs.checkbox helper="Preview deployments" instantSave id="is_previews" label="Previews?" /> --}} {{-- <x-inputs.checkbox helper="Preview deployments" instantSave id="is_previews" label="Previews?" /> --}}
<x-inputs.checkbox instantSave id="is_git_submodules_allowed" label="Git Submodules Allowed?" /> <x-inputs.checkbox instantSave id="is_git_submodules_allowed" label="Git Submodules Allowed?" />

View File

@ -9,23 +9,28 @@
<div wire:loading.remove wire:target='loadImages'> <div wire:loading.remove wire:target='loadImages'>
<div class="flex flex-wrap"> <div class="flex flex-wrap">
@forelse ($images as $image) @forelse ($images as $image)
<div class="w-1/2 p-2"> <div class="w-2/4 p-2">
<div class="rounded-lg shadow-lg bg-coolgray-200"> <div class="rounded-lg shadow-lg bg-coolgray-200">
<div class="p-2"> <div class="p-2">
<div class="text-sm"> <div class="text-sm">
@if (data_get($image, 'is_current')) @if (data_get($image, 'is_current'))
<span class="font-bold text-coollabs">LIVE</span> <span class="font-bold text-warning">LIVE</span>
| |
@endif @endif
{{ data_get($image, 'tag') }} SHA: {{ data_get($image, 'tag') }}
</div> </div>
<div class="text-xs">{{ data_get($image, 'created_at') }}</div> <div class="text-xs">{{ data_get($image, 'created_at') }}</div>
</div> </div>
<div class="flex justify-end p-2"> <div class="flex justify-end p-2">
<x-inputs.button wire:click="revertImage('{{ data_get($image, 'tag') }}')"> @if (data_get($image, 'is_current'))
Revert <x-inputs.button disabled tooltip="This image is currently running.">
</x-inputs.button> Rollback
</x-inputs.button>
@else
<x-inputs.button wire:click="rollbackImage('{{ data_get($image, 'tag') }}')">
Rollback
</x-inputs.button>
@endif
</div> </div>
</div> </div>
</div> </div>