lasthourcloud/app/Http/Livewire/Project/Application/Deploy.php

79 lines
2.5 KiB
PHP
Raw Normal View History

<?php
2023-04-25 09:01:56 +00:00
namespace App\Http\Livewire\Project\Application;
2023-06-08 08:55:50 +00:00
use App\Jobs\ApplicationContainerStatusJob;
use App\Models\Application;
use Livewire\Component;
use Visus\Cuid2\Cuid2;
2023-04-25 09:01:56 +00:00
class Deploy extends Component
{
public string $applicationId;
public $activity;
2023-03-31 07:42:13 +00:00
public $status;
public Application $application;
public $destination;
2023-04-19 10:42:15 +00:00
public array $parameters;
2023-06-15 07:15:41 +00:00
protected string $deploymentUuid;
protected array $command = [];
protected $source;
2023-05-31 10:38:36 +00:00
protected $listeners = [
2023-06-05 10:07:55 +00:00
'applicationStatusChanged',
2023-05-31 10:38:36 +00:00
];
2023-04-19 10:42:15 +00:00
public function mount()
2023-03-31 07:42:13 +00:00
{
2023-06-15 07:15:41 +00:00
$this->parameters = getRouteParameters();
2023-04-25 12:43:35 +00:00
$this->application = Application::where('id', $this->applicationId)->first();
2023-03-31 07:42:13 +00:00
$this->destination = $this->application->destination->getMorphClass()::where('id', $this->application->destination->id)->first();
2023-03-30 18:19:11 +00:00
}
2023-05-31 10:38:36 +00:00
public function applicationStatusChanged()
{
$this->application->refresh();
}
2023-06-15 07:15:41 +00:00
protected function setDeploymentUuid()
2023-03-31 11:32:07 +00:00
{
2023-06-15 07:15:41 +00:00
$this->deploymentUuid = new Cuid2(7);
2023-06-15 12:52:43 +00:00
$this->parameters['deployment_uuid'] = $this->deploymentUuid;
2023-04-19 12:28:39 +00:00
}
2023-06-05 10:07:55 +00:00
public function deploy(bool $force = false, bool|null $debug = null)
2023-04-19 12:28:39 +00:00
{
2023-06-05 10:07:55 +00:00
if ($debug && !$this->application->settings->is_debug_enabled) {
$this->application->settings->is_debug_enabled = true;
$this->application->settings->save();
}
2023-06-15 07:15:41 +00:00
$this->setDeploymentUuid();
2023-05-24 12:26:50 +00:00
queue_application_deployment(
2023-05-30 13:52:17 +00:00
application_id: $this->application->id,
2023-06-15 12:52:43 +00:00
deployment_uuid: $this->deploymentUuid,
2023-05-30 13:52:17 +00:00
force_rebuild: $force,
2023-05-24 12:26:50 +00:00
);
2023-05-31 12:42:37 +00:00
return redirect()->route('project.application.deployment', [
2023-05-24 12:26:50 +00:00
'project_uuid' => $this->parameters['project_uuid'],
'application_uuid' => $this->parameters['application_uuid'],
2023-06-15 12:52:43 +00:00
'deployment_uuid' => $this->deploymentUuid,
2023-05-24 12:26:50 +00:00
'environment_name' => $this->parameters['environment_name'],
]);
}
2023-03-30 18:19:11 +00:00
public function stop()
2023-04-14 11:18:55 +00:00
{
2023-05-25 10:00:09 +00:00
instant_remote_process(["docker rm -f {$this->application->uuid}"], $this->application->destination->server);
2023-06-15 07:15:41 +00:00
$this->application->status = 'stopped';
2023-05-25 10:00:09 +00:00
$this->application->save();
2023-06-05 10:07:55 +00:00
$this->emit('applicationStatusChanged');
2023-04-14 11:18:55 +00:00
}
2023-06-08 08:55:50 +00:00
public function pollStatus()
{
dispatch(new ApplicationContainerStatusJob(
application: $this->application,
container_name: generate_container_name($this->application->uuid),
));
}
}