coolify/app/Livewire/Project/Service/Configuration.php

110 lines
3 KiB
PHP
Raw Normal View History

2024-01-07 15:23:41 +00:00
<?php
namespace App\Livewire\Project\Service;
2024-05-07 13:41:50 +00:00
use App\Actions\Docker\GetContainersStatus;
2024-01-07 15:23:41 +00:00
use App\Models\Service;
use Illuminate\Support\Facades\Auth;
2024-01-07 15:23:41 +00:00
use Livewire\Component;
class Configuration extends Component
{
2024-12-17 10:13:13 +00:00
public $currentRoute;
public $project;
public $environment;
2024-01-31 12:46:40 +00:00
public ?Service $service = null;
2024-06-10 20:43:34 +00:00
2024-01-07 15:23:41 +00:00
public $applications;
2024-06-10 20:43:34 +00:00
2024-01-07 15:23:41 +00:00
public $databases;
2024-06-10 20:43:34 +00:00
2024-01-07 15:23:41 +00:00
public array $query;
2024-06-10 20:43:34 +00:00
2024-12-17 10:13:13 +00:00
public array $parameters;
2024-01-07 15:23:41 +00:00
public function getListeners()
{
$userId = Auth::id();
2024-06-10 20:43:34 +00:00
2024-01-07 15:23:41 +00:00
return [
2024-03-01 09:36:32 +00:00
"echo-private:user.{$userId},ServiceStatusChanged" => 'check_status',
2024-06-10 20:43:34 +00:00
'check_status',
'refreshStatus' => '$refresh',
2024-01-07 15:23:41 +00:00
];
}
2024-06-10 20:43:34 +00:00
2024-01-07 15:23:41 +00:00
public function render()
{
return view('livewire.project.service.configuration');
}
2024-06-10 20:43:34 +00:00
2024-01-07 15:23:41 +00:00
public function mount()
{
$this->parameters = get_route_parameters();
2024-12-17 10:13:13 +00:00
$this->currentRoute = request()->route()->getName();
2024-01-07 15:23:41 +00:00
$this->query = request()->query();
2024-12-17 10:13:13 +00:00
$project = currentTeam()
->projects()
->select('id', 'uuid', 'team_id')
->where('uuid', request()->route('project_uuid'))
->firstOrFail();
$environment = $project->environments()
->select('id', 'uuid', 'name', 'project_id')
->where('uuid', request()->route('environment_uuid'))
2024-12-17 10:13:13 +00:00
->firstOrFail();
$this->service = $environment->services()->whereUuid(request()->route('service_uuid'))->firstOrFail();
$this->project = $project;
$this->environment = $environment;
2024-01-07 15:23:41 +00:00
$this->applications = $this->service->applications->sort();
$this->databases = $this->service->databases->sort();
}
2024-06-10 20:43:34 +00:00
2024-03-25 10:33:38 +00:00
public function restartApplication($id)
{
try {
$application = $this->service->applications->find($id);
if ($application) {
$application->restart();
2024-09-03 14:59:51 +00:00
$this->dispatch('success', 'Service application restarted successfully.');
2024-03-25 10:33:38 +00:00
}
} catch (\Exception $e) {
2024-03-25 10:33:38 +00:00
return handleError($e, $this);
}
}
2024-06-10 20:43:34 +00:00
2024-03-25 10:33:38 +00:00
public function restartDatabase($id)
{
try {
$database = $this->service->databases->find($id);
if ($database) {
$database->restart();
2024-09-03 14:59:51 +00:00
$this->dispatch('success', 'Service database restarted successfully.');
2024-03-25 10:33:38 +00:00
}
} catch (\Exception $e) {
2024-03-25 10:33:38 +00:00
return handleError($e, $this);
}
}
2024-06-10 20:43:34 +00:00
2024-03-01 09:36:32 +00:00
public function check_status()
2024-01-07 15:23:41 +00:00
{
2024-03-14 08:21:48 +00:00
try {
if ($this->service->server->isFunctional()) {
GetContainersStatus::dispatch($this->service->server);
}
2024-08-29 11:00:43 +00:00
$this->service->applications->each(function ($application) {
$application->refresh();
});
$this->service->databases->each(function ($database) {
$database->refresh();
});
$this->dispatch('refreshStatus');
} catch (\Exception $e) {
2024-03-14 08:21:48 +00:00
return handleError($e, $this);
}
2024-01-07 15:23:41 +00:00
}
}