2023-05-16 15:53:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
2023-12-07 18:06:32 +00:00
|
|
|
namespace App\Livewire\Project\Application;
|
2023-05-16 15:53:48 +00:00
|
|
|
|
|
|
|
|
use App\Models\Application;
|
2025-08-22 14:47:59 +00:00
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
2025-12-05 10:02:07 +00:00
|
|
|
use Livewire\Attributes\Validate;
|
2023-08-08 09:51:36 +00:00
|
|
|
use Livewire\Component;
|
2023-05-22 08:34:00 +00:00
|
|
|
use Visus\Cuid2\Cuid2;
|
2023-05-16 15:53:48 +00:00
|
|
|
|
2023-05-17 10:14:18 +00:00
|
|
|
class Rollback extends Component
|
2023-05-16 15:53:48 +00:00
|
|
|
{
|
2025-08-22 14:47:59 +00:00
|
|
|
use AuthorizesRequests;
|
|
|
|
|
|
2023-05-16 15:53:48 +00:00
|
|
|
public Application $application;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-05-16 15:53:48 +00:00
|
|
|
public $images = [];
|
2024-06-10 20:43:34 +00:00
|
|
|
|
|
|
|
|
public ?string $current;
|
|
|
|
|
|
2023-05-22 08:34:00 +00:00
|
|
|
public array $parameters;
|
|
|
|
|
|
2025-12-05 10:02:07 +00:00
|
|
|
#[Validate(['integer', 'min:0', 'max:100'])]
|
|
|
|
|
public int $dockerImagesToKeep = 2;
|
|
|
|
|
|
2025-12-05 11:22:20 +00:00
|
|
|
public bool $serverRetentionDisabled = false;
|
|
|
|
|
|
2023-05-22 08:34:00 +00:00
|
|
|
public function mount()
|
|
|
|
|
{
|
2023-08-09 13:57:53 +00:00
|
|
|
$this->parameters = get_route_parameters();
|
2025-12-05 10:02:07 +00:00
|
|
|
$this->dockerImagesToKeep = $this->application->settings->docker_images_to_keep ?? 2;
|
2025-12-05 11:22:20 +00:00
|
|
|
$server = $this->application->destination->server;
|
|
|
|
|
$this->serverRetentionDisabled = $server->settings->disable_application_image_retention ?? false;
|
2025-12-05 10:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function saveSettings()
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$this->authorize('update', $this->application);
|
|
|
|
|
$this->validate();
|
|
|
|
|
$this->application->settings->docker_images_to_keep = $this->dockerImagesToKeep;
|
|
|
|
|
$this->application->settings->save();
|
|
|
|
|
$this->dispatch('success', 'Settings saved.');
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return handleError($e, $this);
|
|
|
|
|
}
|
2023-05-22 08:34:00 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-05-30 13:52:17 +00:00
|
|
|
public function rollbackImage($commit)
|
2023-05-16 16:20:24 +00:00
|
|
|
{
|
2025-08-22 14:47:59 +00:00
|
|
|
$this->authorize('deploy', $this->application);
|
|
|
|
|
|
2025-01-07 14:31:43 +00:00
|
|
|
$deployment_uuid = new Cuid2;
|
2023-05-22 08:34:00 +00:00
|
|
|
|
2025-12-04 12:52:27 +00:00
|
|
|
$result = queue_application_deployment(
|
2024-01-27 17:44:40 +00:00
|
|
|
application: $this->application,
|
2025-01-07 14:31:43 +00:00
|
|
|
deployment_uuid: $deployment_uuid,
|
2023-05-30 13:52:17 +00:00
|
|
|
commit: $commit,
|
2024-04-17 13:30:08 +00:00
|
|
|
rollback: true,
|
2023-05-30 13:52:17 +00:00
|
|
|
force_rebuild: false,
|
2023-05-24 12:26:50 +00:00
|
|
|
);
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-12-04 12:52:27 +00:00
|
|
|
if ($result['status'] === 'queue_full') {
|
|
|
|
|
$this->dispatch('error', 'Deployment queue full', $result['message']);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-26 12:29:53 +00:00
|
|
|
return redirectRoute($this, 'project.application.deployment.show', [
|
2023-05-24 12:26:50 +00:00
|
|
|
'project_uuid' => $this->parameters['project_uuid'],
|
|
|
|
|
'application_uuid' => $this->parameters['application_uuid'],
|
2025-01-07 14:31:43 +00:00
|
|
|
'deployment_uuid' => $deployment_uuid,
|
2024-11-22 15:03:20 +00:00
|
|
|
'environment_uuid' => $this->parameters['environment_uuid'],
|
2023-12-27 15:45:01 +00:00
|
|
|
]);
|
2023-05-16 16:20:24 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-11-20 12:49:10 +00:00
|
|
|
public function loadImages($showToast = false)
|
2023-05-16 15:53:48 +00:00
|
|
|
{
|
2025-08-22 14:47:59 +00:00
|
|
|
$this->authorize('view', $this->application);
|
|
|
|
|
|
2023-05-16 15:53:48 +00:00
|
|
|
try {
|
2023-11-20 12:49:10 +00:00
|
|
|
$image = $this->application->docker_registry_image_name ?? $this->application->uuid;
|
2023-11-17 13:22:05 +00:00
|
|
|
if ($this->application->destination->server->isFunctional()) {
|
|
|
|
|
$output = instant_remote_process([
|
|
|
|
|
"docker inspect --format='{{.Config.Image}}' {$this->application->uuid}",
|
|
|
|
|
], $this->application->destination->server, throwError: false);
|
2024-06-25 08:37:10 +00:00
|
|
|
$current_tag = str($output)->trim()->explode(':');
|
2023-11-17 13:22:05 +00:00
|
|
|
$this->current = data_get($current_tag, 1);
|
2023-05-16 15:53:48 +00:00
|
|
|
|
2023-11-17 13:22:05 +00:00
|
|
|
$output = instant_remote_process([
|
|
|
|
|
"docker images --format '{{.Repository}}#{{.Tag}}#{{.CreatedAt}}'",
|
|
|
|
|
], $this->application->destination->server);
|
2024-06-25 08:37:10 +00:00
|
|
|
$this->images = str($output)->trim()->explode("\n")->filter(function ($item) use ($image) {
|
|
|
|
|
return str($item)->contains($image);
|
2023-11-17 13:22:05 +00:00
|
|
|
})->map(function ($item) {
|
2024-06-25 08:37:10 +00:00
|
|
|
$item = str($item)->explode('#');
|
2025-12-05 10:02:07 +00:00
|
|
|
$is_current = $item[1] === $this->current;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-11-17 13:22:05 +00:00
|
|
|
return [
|
|
|
|
|
'tag' => $item[1],
|
|
|
|
|
'created_at' => $item[2],
|
2025-12-05 10:02:07 +00:00
|
|
|
'is_current' => $is_current,
|
2023-11-17 13:22:05 +00:00
|
|
|
];
|
|
|
|
|
})->toArray();
|
|
|
|
|
}
|
2023-12-07 18:06:32 +00:00
|
|
|
$showToast && $this->dispatch('success', 'Images loaded.');
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-11-17 13:22:05 +00:00
|
|
|
return [];
|
2025-01-07 14:31:43 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-15 13:34:25 +00:00
|
|
|
return handleError($e, $this);
|
2023-05-16 15:53:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|