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

105 lines
3.1 KiB
PHP
Raw Normal View History

2023-09-22 11:23:49 +02:00
<?php
2023-12-07 19:06:32 +01:00
namespace App\Livewire\Project\Service;
2023-09-22 11:23:49 +02:00
2023-11-09 14:59:38 +01:00
use App\Actions\Database\StartDatabaseProxy;
use App\Actions\Database\StopDatabaseProxy;
2023-09-22 11:23:49 +02:00
use App\Models\ServiceDatabase;
use Livewire\Component;
class Database extends Component
{
public ServiceDatabase $database;
2024-06-10 22:43:34 +02:00
2023-11-09 14:59:38 +01:00
public ?string $db_url_public = null;
2024-06-10 22:43:34 +02:00
2023-09-26 14:45:52 +02:00
public $fileStorages;
2023-11-09 14:59:38 +01:00
2024-06-10 22:43:34 +02:00
protected $listeners = ['refreshFileStorages'];
2023-09-22 11:23:49 +02:00
protected $rules = [
'database.human_name' => 'nullable',
'database.description' => 'nullable',
2023-09-26 14:45:52 +02:00
'database.image' => 'required',
'database.exclude_from_status' => 'required|boolean',
2023-11-09 14:59:38 +01:00
'database.public_port' => 'nullable|integer',
'database.is_public' => 'required|boolean',
'database.is_log_drain_enabled' => 'required|boolean',
2023-09-22 11:23:49 +02:00
];
2024-06-10 22:43:34 +02:00
2023-09-22 11:23:49 +02:00
public function render()
{
return view('livewire.project.service.database');
}
2024-06-10 22:43:34 +02:00
public function mount()
{
2023-11-09 14:59:38 +01:00
if ($this->database->is_public) {
$this->db_url_public = $this->database->getServiceDatabaseUrl();
}
2023-09-26 14:45:52 +02:00
$this->refreshFileStorages();
}
2024-06-10 22:43:34 +02:00
2024-03-21 12:44:32 +01:00
public function instantSaveExclude()
{
$this->submit();
}
2024-06-10 22:43:34 +02:00
2024-03-21 12:44:32 +01:00
public function instantSaveLogDrain()
{
2024-06-10 22:43:34 +02:00
if (! $this->database->service->destination->server->isLogDrainEnabled()) {
$this->database->is_log_drain_enabled = false;
2023-12-07 19:06:32 +01:00
$this->dispatch('error', 'Log drain is not enabled on the server. Please enable it first.');
2024-06-10 22:43:34 +02:00
return;
}
$this->submit();
2023-12-07 19:06:32 +01:00
$this->dispatch('success', 'You need to restart the service for the changes to take effect.');
}
2024-06-10 22:43:34 +02:00
public function instantSave()
{
2024-06-10 22:43:34 +02:00
if ($this->database->is_public && ! $this->database->public_port) {
2023-12-07 19:06:32 +01:00
$this->dispatch('error', 'Public port is required.');
2023-11-09 14:59:38 +01:00
$this->database->is_public = false;
2024-06-10 22:43:34 +02:00
2023-11-09 14:59:38 +01:00
return;
}
if ($this->database->is_public) {
2024-06-10 22:43:34 +02:00
if (! str($this->database->status)->startsWith('running')) {
2023-12-07 19:06:32 +01:00
$this->dispatch('error', 'Database must be started to be publicly accessible.');
2023-11-09 14:59:38 +01:00
$this->database->is_public = false;
2024-06-10 22:43:34 +02:00
2023-11-09 14:59:38 +01:00
return;
}
StartDatabaseProxy::run($this->database);
$this->db_url_public = $this->database->getServiceDatabaseUrl();
2023-12-07 19:06:32 +01:00
$this->dispatch('success', 'Database is now publicly accessible.');
2023-11-09 14:59:38 +01:00
} else {
StopDatabaseProxy::run($this->database);
$this->db_url_public = null;
2023-12-07 19:06:32 +01:00
$this->dispatch('success', 'Database is no longer publicly accessible.');
2023-11-09 14:59:38 +01:00
}
2023-09-25 15:48:43 +02:00
$this->submit();
}
2024-06-10 22:43:34 +02:00
2023-09-26 14:45:52 +02:00
public function refreshFileStorages()
{
$this->fileStorages = $this->database->fileStorages()->get();
}
2024-06-10 22:43:34 +02:00
2023-09-22 11:23:49 +02:00
public function submit()
{
try {
$this->validate();
$this->database->save();
2023-09-27 12:45:53 +02:00
updateCompose($this->database);
2024-02-22 14:53:42 +01:00
$this->dispatch('success', 'Database saved.');
2023-09-22 11:23:49 +02:00
} catch (\Throwable $e) {
ray($e);
} finally {
2023-12-07 19:06:32 +01:00
$this->dispatch('generateDockerCompose');
2023-09-22 11:23:49 +02:00
}
}
}