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;
|
|
|
|
use Illuminate\Support\Str;
|
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
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
public function mount()
|
|
|
|
{
|
2023-08-09 13:57:53 +00:00
|
|
|
$this->parameters = get_route_parameters();
|
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
|
|
|
{
|
2023-05-22 08:34:00 +00:00
|
|
|
$deployment_uuid = new Cuid2(7);
|
|
|
|
|
2023-05-24 12:26:50 +00:00
|
|
|
queue_application_deployment(
|
2024-01-27 17:44:40 +00:00
|
|
|
application: $this->application,
|
2023-05-30 13:52:17 +00:00
|
|
|
deployment_uuid: $deployment_uuid,
|
|
|
|
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
|
|
|
|
2024-01-07 15:23:41 +00:00
|
|
|
return redirect()->route('project.application.deployment.show', [
|
2023-05-24 12:26:50 +00:00
|
|
|
'project_uuid' => $this->parameters['project_uuid'],
|
|
|
|
'application_uuid' => $this->parameters['application_uuid'],
|
2023-05-31 12:42:37 +00:00
|
|
|
'deployment_uuid' => $deployment_uuid,
|
2023-05-24 12:26:50 +00:00
|
|
|
'environment_name' => $this->parameters['environment_name'],
|
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
|
|
|
{
|
|
|
|
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-10 20:43:34 +00:00
|
|
|
$current_tag = Str::of($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);
|
|
|
|
$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;
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-11-17 13:22:05 +00:00
|
|
|
return [
|
|
|
|
'tag' => $item[1],
|
|
|
|
'created_at' => $item[2],
|
|
|
|
'is_current' => $is_current ?? null,
|
|
|
|
];
|
|
|
|
})->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 [];
|
2023-05-16 15:53:48 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|