lasthourcloud/app/Http/Livewire/DeployApplication.php

85 lines
2.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Livewire;
2023-03-30 13:52:19 +00:00
use App\Jobs\ContainerStatusJob;
2023-03-31 11:32:07 +00:00
use App\Jobs\DeployApplicationJob;
use App\Models\Application;
use App\Models\CoolifyInstanceSettings;
use DateTimeImmutable;
use Illuminate\Support\Facades\Http;
2023-03-30 19:15:25 +00:00
use Illuminate\Support\Facades\Process;
use Livewire\Component;
2023-03-29 13:47:56 +00:00
use Symfony\Component\Yaml\Yaml;
use Visus\Cuid2\Cuid2;
use Lcobucci\JWT\Encoding\ChainedFormatter;
use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Signer\Rsa\Sha256;
use Lcobucci\JWT\Token\Builder;
class DeployApplication extends Component
{
public string $application_uuid;
public $activity;
2023-03-31 07:42:13 +00:00
public $status;
public Application $application;
public $destination;
protected string $deployment_uuid;
protected array $command = [];
protected $source;
2023-03-31 07:42:13 +00:00
public function mount($application_uuid)
{
2023-03-30 18:19:11 +00:00
$this->application_uuid = $application_uuid;
2023-03-31 07:42:13 +00:00
$this->application = Application::where('uuid', $this->application_uuid)->first();
$this->destination = $this->application->destination->getMorphClass()::where('id', $this->application->destination->id)->first();
2023-03-30 18:19:11 +00:00
}
2023-03-31 11:32:07 +00:00
2023-03-30 18:19:11 +00:00
public function render()
{
return view('livewire.deploy-application');
}
2023-03-31 11:32:07 +00:00
public function deploy()
{
// Create Deployment ID
$this->deployment_uuid = new Cuid2(7);
2023-03-29 13:47:56 +00:00
2023-03-31 11:32:07 +00:00
dispatch(new DeployApplicationJob(
deployment_uuid: $this->deployment_uuid,
application_uuid: $this->application_uuid,
));
$currentUrl = url()->previous();
$deploymentUrl = "$currentUrl/deployment/$this->deployment_uuid";
return redirect($deploymentUrl);
}
2023-03-30 18:19:11 +00:00
public function stop()
2023-03-29 13:47:56 +00:00
{
2023-03-31 09:51:04 +00:00
runRemoteCommandSync($this->destination->server, ["docker rm -f {$this->application_uuid} >/dev/null 2>&1"]);
2023-03-31 07:42:13 +00:00
$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-31 11:32:07 +00:00
2023-03-31 07:42:13 +00:00
public function checkStatus()
{
$output = runRemoteCommandSync($this->destination->server, ["docker ps -a --format '{{.State}}' --filter 'name={$this->application->uuid}'"]);
2023-03-30 19:15:25 +00:00
if ($output == '') {
2023-03-31 07:42:13 +00:00
$this->application->status = 'exited';
$this->application->save();
2023-03-30 19:15:25 +00:00
} else {
2023-03-31 07:42:13 +00:00
$this->application->status = $output;
$this->application->save();
2023-03-30 19:15:25 +00:00
}
}
}