2023-03-28 20:13:08 +00:00
|
|
|
<?php
|
|
|
|
|
2023-04-25 09:01:56 +00:00
|
|
|
namespace App\Http\Livewire\Project\Application;
|
2023-03-28 20:13:08 +00:00
|
|
|
|
2023-03-31 11:32:07 +00:00
|
|
|
use App\Jobs\DeployApplicationJob;
|
2023-03-28 20:13:08 +00:00
|
|
|
use App\Models\Application;
|
2023-04-19 10:42:15 +00:00
|
|
|
use Illuminate\Support\Facades\Route;
|
2023-03-28 20:13:08 +00:00
|
|
|
use Livewire\Component;
|
|
|
|
use Visus\Cuid2\Cuid2;
|
|
|
|
|
2023-04-25 09:01:56 +00:00
|
|
|
class Deploy extends Component
|
2023-03-28 20:13:08 +00:00
|
|
|
{
|
2023-04-14 19:09:38 +00:00
|
|
|
public string $applicationId;
|
2023-03-28 20:13:08 +00:00
|
|
|
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-03-30 09:10:31 +00:00
|
|
|
|
2023-03-28 20:13:08 +00:00
|
|
|
protected string $deployment_uuid;
|
|
|
|
protected array $command = [];
|
2023-03-30 09:10:31 +00:00
|
|
|
protected $source;
|
2023-03-28 20:13:08 +00:00
|
|
|
|
2023-04-19 10:42:15 +00:00
|
|
|
public function mount()
|
2023-03-31 07:42:13 +00:00
|
|
|
{
|
2023-04-19 10:42:15 +00:00
|
|
|
$this->parameters = Route::current()->parameters();
|
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-04-19 12:28:39 +00:00
|
|
|
protected function setDeploymentUuid()
|
2023-03-31 11:32:07 +00:00
|
|
|
{
|
2023-03-28 20:13:08 +00:00
|
|
|
// Create Deployment ID
|
2023-03-29 10:52:22 +00:00
|
|
|
$this->deployment_uuid = new Cuid2(7);
|
2023-04-19 10:42:15 +00:00
|
|
|
$this->parameters['deployment_uuid'] = $this->deployment_uuid;
|
2023-04-19 12:28:39 +00:00
|
|
|
}
|
|
|
|
protected function redirectToDeployment()
|
|
|
|
{
|
2023-04-25 09:01:56 +00:00
|
|
|
return redirect()->route('project.application.deployment', $this->parameters);
|
2023-04-19 12:28:39 +00:00
|
|
|
}
|
|
|
|
public function start()
|
|
|
|
{
|
|
|
|
$this->setDeploymentUuid();
|
2023-03-29 13:47:56 +00:00
|
|
|
|
2023-03-31 11:32:07 +00:00
|
|
|
dispatch(new DeployApplicationJob(
|
|
|
|
deployment_uuid: $this->deployment_uuid,
|
2023-04-14 19:09:38 +00:00
|
|
|
application_uuid: $this->application->uuid,
|
2023-04-19 12:28:39 +00:00
|
|
|
force_rebuild: false,
|
2023-03-31 11:32:07 +00:00
|
|
|
));
|
2023-03-28 20:13:08 +00:00
|
|
|
|
2023-04-19 12:28:39 +00: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-28 20:13:08 +00:00
|
|
|
}
|
2023-03-30 18:19:11 +00:00
|
|
|
|
|
|
|
public function stop()
|
2023-03-29 13:47:56 +00:00
|
|
|
{
|
2023-04-14 19:09:38 +00:00
|
|
|
runRemoteCommandSync($this->destination->server, ["docker stop -t 0 {$this->application->uuid} >/dev/null 2>&1"]);
|
2023-04-14 09:24:58 +00:00
|
|
|
$this->application->status = 'stopped';
|
2023-03-31 07:42:13 +00:00
|
|
|
$this->application->save();
|
|
|
|
}
|
2023-04-14 11:18:55 +00:00
|
|
|
public function kill()
|
|
|
|
{
|
2023-04-14 19:09:38 +00:00
|
|
|
runRemoteCommandSync($this->destination->server, ["docker rm -f {$this->application->uuid}"]);
|
2023-04-14 11:18:55 +00:00
|
|
|
if ($this->application->status != 'exited') {
|
|
|
|
$this->application->status = 'exited';
|
|
|
|
$this->application->save();
|
|
|
|
}
|
|
|
|
}
|
2023-03-31 11:32:07 +00:00
|
|
|
|
2023-03-31 07:42:13 +00:00
|
|
|
public function pollingStatus()
|
|
|
|
{
|
|
|
|
$this->application->refresh();
|
2023-03-29 13:47:56 +00:00
|
|
|
}
|
2023-03-28 20:13:08 +00:00
|
|
|
}
|