coolify/app/Livewire/Project/Application/Rollback.php

74 lines
2.6 KiB
PHP
Raw Normal View History

2023-05-16 17:53:48 +02:00
<?php
2023-12-07 19:06:32 +01:00
namespace App\Livewire\Project\Application;
2023-05-16 17:53:48 +02:00
use App\Models\Application;
use Illuminate\Support\Str;
use Livewire\Component;
2023-05-22 10:34:00 +02:00
use Visus\Cuid2\Cuid2;
2023-05-16 17:53:48 +02:00
2023-05-17 12:14:18 +02:00
class Rollback extends Component
2023-05-16 17:53:48 +02:00
{
public Application $application;
public $images = [];
2023-05-16 21:49:29 +02:00
public string|null $current;
2023-05-22 10:34:00 +02:00
public array $parameters;
public function mount()
{
$this->parameters = get_route_parameters();
2023-05-22 10:34:00 +02:00
}
2023-05-30 15:52:17 +02:00
public function rollbackImage($commit)
2023-05-16 18:20:24 +02:00
{
2023-05-22 10:34:00 +02:00
$deployment_uuid = new Cuid2(7);
2023-05-24 14:26:50 +02:00
queue_application_deployment(
2024-01-27 18:44:40 +01:00
application: $this->application,
2023-05-30 15:52:17 +02:00
deployment_uuid: $deployment_uuid,
commit: $commit,
force_rebuild: false,
2023-05-24 14:26:50 +02:00
);
2024-01-07 16:23:41 +01:00
return redirect()->route('project.application.deployment.show', [
2023-05-24 14:26:50 +02:00
'project_uuid' => $this->parameters['project_uuid'],
'application_uuid' => $this->parameters['application_uuid'],
2023-05-31 14:42:37 +02:00
'deployment_uuid' => $deployment_uuid,
2023-05-24 14:26:50 +02:00
'environment_name' => $this->parameters['environment_name'],
2023-12-27 16:45:01 +01:00
]);
2023-05-16 18:20:24 +02:00
}
public function loadImages($showToast = false)
2023-05-16 17:53:48 +02:00
{
try {
$image = $this->application->docker_registry_image_name ?? $this->application->uuid;
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);
2023-05-16 17:53:48 +02:00
$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();
}
2023-12-07 19:06:32 +01:00
$showToast && $this->dispatch('success', 'Images loaded.');
return [];
2023-05-16 17:53:48 +02:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-05-16 17:53:48 +02:00
}
}
}