2023-09-22 09:23:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
2023-12-07 18:06:32 +00:00
|
|
|
namespace App\Livewire\Project\Service;
|
2023-09-22 09:23:49 +00:00
|
|
|
|
|
|
|
|
use App\Actions\Service\StartService;
|
|
|
|
|
use App\Actions\Service\StopService;
|
2025-01-13 11:03:03 +00:00
|
|
|
use App\Enums\ProcessStatus;
|
2023-12-08 12:07:42 +00:00
|
|
|
use App\Events\ServiceStatusChanged;
|
2023-09-22 09:23:49 +00:00
|
|
|
use App\Models\Service;
|
2024-11-04 13:18:16 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2023-09-22 09:23:49 +00:00
|
|
|
use Livewire\Component;
|
2023-12-08 11:12:44 +00:00
|
|
|
use Spatie\Activitylog\Models\Activity;
|
2023-09-22 09:23:49 +00:00
|
|
|
|
|
|
|
|
class Navbar extends Component
|
|
|
|
|
{
|
|
|
|
|
public Service $service;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-22 09:23:49 +00:00
|
|
|
public array $parameters;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-22 09:23:49 +00:00
|
|
|
public array $query;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-12-08 11:12:44 +00:00
|
|
|
public $isDeploymentProgress = false;
|
2024-03-21 11:44:32 +00:00
|
|
|
|
2024-09-04 12:34:46 +00:00
|
|
|
public $docker_cleanup = true;
|
2024-09-19 08:27:44 +00:00
|
|
|
|
2024-09-17 15:15:34 +00:00
|
|
|
public $title = 'Configuration';
|
2024-09-04 12:34:46 +00:00
|
|
|
|
2024-04-12 10:44:49 +00:00
|
|
|
public function mount()
|
|
|
|
|
{
|
2024-12-13 11:03:05 +00:00
|
|
|
if (str($this->service->status)->contains('running') && is_null($this->service->config_hash)) {
|
2024-04-12 10:44:49 +00:00
|
|
|
$this->service->isConfigurationChanged(true);
|
|
|
|
|
$this->dispatch('configurationChanged');
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-12-08 11:12:44 +00:00
|
|
|
public function getListeners()
|
|
|
|
|
{
|
2024-11-04 13:18:16 +00:00
|
|
|
$userId = Auth::id();
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-12-08 11:12:44 +00:00
|
|
|
return [
|
2024-03-21 11:44:32 +00:00
|
|
|
"echo-private:user.{$userId},ServiceStatusChanged" => 'serviceStarted',
|
2024-10-17 20:08:23 +00:00
|
|
|
'envsUpdated' => '$refresh',
|
2025-02-17 08:52:18 +00:00
|
|
|
'refreshStatus' => '$refresh',
|
2023-12-08 11:12:44 +00:00
|
|
|
];
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-04-12 10:44:49 +00:00
|
|
|
public function serviceStarted()
|
|
|
|
|
{
|
2024-09-19 08:57:34 +00:00
|
|
|
// $this->dispatch('success', 'Service status changed.');
|
2024-04-12 11:15:24 +00:00
|
|
|
if (is_null($this->service->config_hash) || $this->service->isConfigurationChanged()) {
|
|
|
|
|
$this->service->isConfigurationChanged(true);
|
|
|
|
|
$this->dispatch('configurationChanged');
|
|
|
|
|
} else {
|
|
|
|
|
$this->dispatch('configurationChanged');
|
|
|
|
|
}
|
2024-03-21 11:44:32 +00:00
|
|
|
}
|
2024-04-12 11:15:24 +00:00
|
|
|
|
2024-08-06 12:02:24 +00:00
|
|
|
public function check_status_without_notification()
|
|
|
|
|
{
|
|
|
|
|
$this->dispatch('check_status');
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-21 11:44:32 +00:00
|
|
|
public function check_status()
|
|
|
|
|
{
|
2024-03-04 07:57:18 +00:00
|
|
|
$this->dispatch('check_status');
|
|
|
|
|
$this->dispatch('success', 'Service status updated.');
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-03-01 09:36:32 +00:00
|
|
|
public function checkDeployments()
|
2023-12-08 11:12:44 +00:00
|
|
|
{
|
2024-07-25 19:51:53 +00:00
|
|
|
try {
|
2025-01-07 14:31:43 +00:00
|
|
|
$activity = Activity::where('properties->type_uuid', $this->service->uuid)->latest()->first();
|
2024-07-25 19:51:53 +00:00
|
|
|
$status = data_get($activity, 'properties.status');
|
2025-01-13 11:03:03 +00:00
|
|
|
if ($status === ProcessStatus::QUEUED->value || $status === ProcessStatus::IN_PROGRESS->value) {
|
2025-01-07 14:31:43 +00:00
|
|
|
$this->isDeploymentProgress = true;
|
|
|
|
|
} else {
|
|
|
|
|
$this->isDeploymentProgress = false;
|
|
|
|
|
}
|
|
|
|
|
} catch (\Throwable) {
|
2024-03-01 09:36:32 +00:00
|
|
|
$this->isDeploymentProgress = false;
|
|
|
|
|
}
|
2025-01-13 11:03:03 +00:00
|
|
|
|
|
|
|
|
return $this->isDeploymentProgress;
|
2023-11-05 08:49:23 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-03-21 11:44:32 +00:00
|
|
|
public function start()
|
2023-09-22 09:23:49 +00:00
|
|
|
{
|
2025-02-04 13:34:34 +00:00
|
|
|
$activity = StartService::run($this->service, pullLatestImages: true);
|
2024-02-05 13:40:54 +00:00
|
|
|
$this->dispatch('activityMonitor', $activity->id);
|
2023-09-22 09:23:49 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-01-13 11:03:03 +00:00
|
|
|
public function forceDeploy()
|
2023-09-22 09:23:49 +00:00
|
|
|
{
|
2025-01-13 11:03:03 +00:00
|
|
|
try {
|
|
|
|
|
$activities = Activity::where('properties->type_uuid', $this->service->uuid)->where('properties->status', ProcessStatus::IN_PROGRESS->value)->orWhere('properties->status', ProcessStatus::QUEUED->value)->get();
|
|
|
|
|
foreach ($activities as $activity) {
|
|
|
|
|
$activity->properties->status = ProcessStatus::ERROR->value;
|
|
|
|
|
$activity->save();
|
|
|
|
|
}
|
2025-02-04 13:34:34 +00:00
|
|
|
$activity = StartService::run($this->service, pullLatestImages: true, stopBeforeStart: true);
|
2025-01-13 11:03:03 +00:00
|
|
|
$this->dispatch('activityMonitor', $activity->id);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$this->dispatch('error', $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function stop($cleanupContainers = false)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
StopService::run($this->service, false, $this->docker_cleanup);
|
|
|
|
|
ServiceStatusChanged::dispatch();
|
|
|
|
|
if ($cleanupContainers) {
|
|
|
|
|
$this->dispatch('success', 'Containers cleaned up.');
|
|
|
|
|
} else {
|
|
|
|
|
$this->dispatch('success', 'Service stopped.');
|
|
|
|
|
}
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$this->dispatch('error', $e->getMessage());
|
|
|
|
|
}
|
2023-09-22 09:23:49 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-12-14 13:50:38 +00:00
|
|
|
public function restart()
|
|
|
|
|
{
|
|
|
|
|
$this->checkDeployments();
|
|
|
|
|
if ($this->isDeploymentProgress) {
|
|
|
|
|
$this->dispatch('error', 'There is a deployment in progress.');
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-12-14 13:50:38 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2025-02-04 13:34:34 +00:00
|
|
|
$activity = StartService::run($this->service, stopBeforeStart: true);
|
2024-10-03 11:17:35 +00:00
|
|
|
$this->dispatch('activityMonitor', $activity->id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function pullAndRestartEvent()
|
|
|
|
|
{
|
|
|
|
|
$this->checkDeployments();
|
|
|
|
|
if ($this->isDeploymentProgress) {
|
|
|
|
|
$this->dispatch('error', 'There is a deployment in progress.');
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-02-04 13:34:34 +00:00
|
|
|
$activity = StartService::run($this->service, pullLatestImages: true, stopBeforeStart: true);
|
2024-02-05 13:40:54 +00:00
|
|
|
$this->dispatch('activityMonitor', $activity->id);
|
2023-12-14 13:50:38 +00:00
|
|
|
}
|
2024-09-04 12:34:46 +00:00
|
|
|
|
|
|
|
|
public function render()
|
|
|
|
|
{
|
|
|
|
|
return view('livewire.project.service.navbar', [
|
|
|
|
|
'checkboxes' => [
|
2024-09-19 08:27:44 +00:00
|
|
|
['id' => 'docker_cleanup', 'label' => __('resource.docker_cleanup')],
|
|
|
|
|
],
|
2024-09-04 12:34:46 +00:00
|
|
|
]);
|
|
|
|
|
}
|
2023-09-22 09:23:49 +00:00
|
|
|
}
|