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

88 lines
2.5 KiB
PHP
Raw Normal View History

<?php
2023-04-25 11:01:56 +02:00
namespace App\Http\Livewire\Project\Application;
2023-03-31 13:32:07 +02:00
use App\Jobs\DeployApplicationJob;
use App\Models\Application;
2023-04-19 12:42:15 +02:00
use Illuminate\Support\Facades\Route;
use Livewire\Component;
use Visus\Cuid2\Cuid2;
2023-04-25 11:01:56 +02:00
class Deploy extends Component
{
public string $applicationId;
public $activity;
2023-03-31 09:42:13 +02:00
public $status;
public Application $application;
public $destination;
2023-04-19 12:42:15 +02:00
public array $parameters;
protected string $deployment_uuid;
protected array $command = [];
protected $source;
2023-04-19 12:42:15 +02:00
public function mount()
2023-03-31 09:42:13 +02:00
{
2023-05-08 11:51:03 +02:00
$this->parameters = saveParameters();
2023-04-25 14:43:35 +02:00
$this->application = Application::where('id', $this->applicationId)->first();
2023-03-31 09:42:13 +02:00
$this->destination = $this->application->destination->getMorphClass()::where('id', $this->application->destination->id)->first();
2023-03-30 20:19:11 +02:00
}
2023-04-19 14:28:39 +02:00
protected function setDeploymentUuid()
2023-03-31 13:32:07 +02:00
{
// Create Deployment ID
$this->deployment_uuid = new Cuid2(7);
2023-04-19 12:42:15 +02:00
$this->parameters['deployment_uuid'] = $this->deployment_uuid;
2023-04-19 14:28:39 +02:00
}
protected function redirectToDeployment()
{
2023-04-25 11:01:56 +02:00
return redirect()->route('project.application.deployment', $this->parameters);
2023-04-19 14:28:39 +02:00
}
public function start()
{
$this->setDeploymentUuid();
2023-03-29 15:47:56 +02:00
2023-03-31 13:32:07 +02:00
dispatch(new DeployApplicationJob(
deployment_uuid: $this->deployment_uuid,
application_uuid: $this->application->uuid,
2023-04-19 14:28:39 +02:00
force_rebuild: false,
2023-03-31 13:32:07 +02:00
));
2023-04-19 14:28:39 +02:00
return $this->redirectToDeployment();
}
public function forceRebuild()
{
$this->setDeploymentUuid();
dispatch(new DeployApplicationJob(
deployment_uuid: $this->deployment_uuid,
application_uuid: $this->application->uuid,
force_rebuild: true,
));
return $this->redirectToDeployment();
}
2023-03-30 20:19:11 +02:00
public function delete()
{
$this->stop();
Application::find($this->applicationId)->delete();
return redirect()->route('project.resources', [
'project_uuid' => $this->parameters['project_uuid'],
'environment_name' => $this->parameters['environment_name']
]);
}
2023-03-30 20:19:11 +02:00
public function stop()
2023-04-14 13:18:55 +02:00
{
instantRemoteProcess(["docker rm -f {$this->application->uuid}"], $this->destination->server);
2023-04-14 13:18:55 +02:00
if ($this->application->status != 'exited') {
$this->application->status = 'exited';
$this->application->save();
}
}
2023-03-31 13:32:07 +02:00
2023-03-31 09:42:13 +02:00
public function pollingStatus()
{
$this->application->refresh();
2023-03-29 15:47:56 +02:00
}
}