coolify/app/Livewire/Project/Database/Keydb/General.php

291 lines
9.7 KiB
PHP
Raw Normal View History

2024-04-10 13:00:46 +00:00
<?php
namespace App\Livewire\Project\Database\Keydb;
use App\Actions\Database\StartDatabaseProxy;
use App\Actions\Database\StopDatabaseProxy;
2025-02-10 20:29:45 +00:00
use App\Helpers\SslHelper;
2024-06-09 19:33:17 +00:00
use App\Models\Server;
2024-04-10 13:00:46 +00:00
use App\Models\StandaloneKeydb;
use App\Support\ValidationPatterns;
use Carbon\Carbon;
2024-04-10 13:00:46 +00:00
use Exception;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
2024-11-04 13:33:44 +00:00
use Illuminate\Support\Facades\Auth;
2024-04-10 13:00:46 +00:00
use Livewire\Component;
class General extends Component
{
use AuthorizesRequests;
public ?Server $server = null;
2024-06-10 20:43:34 +00:00
2024-04-10 13:00:46 +00:00
public StandaloneKeydb $database;
2024-06-10 20:43:34 +00:00
2024-11-04 13:33:44 +00:00
public string $name;
2024-06-10 20:43:34 +00:00
2024-11-04 13:33:44 +00:00
public ?string $description = null;
public ?string $keydbConf = null;
public string $keydbPassword;
public string $image;
public ?string $portsMappings = null;
public ?bool $isPublic = null;
public ?int $publicPort = null;
public ?int $publicPortTimeout = 3600;
2024-11-04 13:33:44 +00:00
public ?string $customDockerRunOptions = null;
public ?string $dbUrl = null;
public ?string $dbUrlPublic = null;
public bool $isLogDrainEnabled = false;
public ?Carbon $certificateValidUntil = null;
2025-02-10 20:29:45 +00:00
public bool $enable_ssl = false;
2024-11-04 13:33:44 +00:00
public function getListeners()
2024-04-10 13:00:46 +00:00
{
$userId = Auth::id();
2024-11-04 13:33:44 +00:00
$teamId = Auth::user()->currentTeam()->id;
return [
"echo-private:team.{$teamId},DatabaseProxyStopped" => 'databaseProxyStopped',
"echo-private:user.{$userId},DatabaseStatusChanged" => '$refresh',
2024-11-04 13:33:44 +00:00
];
2024-04-10 13:00:46 +00:00
}
2024-06-10 20:43:34 +00:00
2024-11-04 13:33:44 +00:00
public function mount()
2024-06-10 20:43:34 +00:00
{
2024-04-10 13:00:46 +00:00
try {
$this->authorize('view', $this->database);
2024-11-04 13:33:44 +00:00
$this->syncData();
$this->server = data_get($this->database, 'destination.server');
if (! $this->server) {
$this->dispatch('error', 'Database destination server is not configured.');
return;
}
2025-02-10 20:29:45 +00:00
$existingCert = $this->database->sslCertificates()->first();
2025-02-10 20:29:45 +00:00
if ($existingCert) {
$this->certificateValidUntil = $existingCert->valid_until;
}
} catch (\Throwable $e) {
2024-11-04 13:33:44 +00:00
return handleError($e, $this);
}
}
2024-06-10 20:43:34 +00:00
protected function rules(): array
{
$baseRules = [
'name' => ValidationPatterns::nameRules(),
'description' => ValidationPatterns::descriptionRules(),
'keydbConf' => 'nullable|string',
'keydbPassword' => 'required|string',
'image' => 'required|string',
'portsMappings' => 'nullable|string',
'isPublic' => 'nullable|boolean',
'publicPort' => 'nullable|integer',
'publicPortTimeout' => 'nullable|integer|min:1',
'customDockerRunOptions' => 'nullable|string',
'dbUrl' => 'nullable|string',
'dbUrlPublic' => 'nullable|string',
'isLogDrainEnabled' => 'nullable|boolean',
'enable_ssl' => 'boolean',
];
return $baseRules;
}
protected function messages(): array
{
return array_merge(
ValidationPatterns::combinedMessages(),
[
'keydbPassword.required' => 'The KeyDB Password field is required.',
'keydbPassword.string' => 'The KeyDB Password must be a string.',
'image.required' => 'The Docker Image field is required.',
'image.string' => 'The Docker Image must be a string.',
'publicPort.integer' => 'The Public Port must be an integer.',
'publicPortTimeout.integer' => 'The Public Port Timeout must be an integer.',
'publicPortTimeout.min' => 'The Public Port Timeout must be at least 1.',
]
);
}
2024-11-04 13:33:44 +00:00
public function syncData(bool $toModel = false)
{
if ($toModel) {
$this->validate();
$this->database->name = $this->name;
$this->database->description = $this->description;
$this->database->keydb_conf = $this->keydbConf;
$this->database->keydb_password = $this->keydbPassword;
$this->database->image = $this->image;
$this->database->ports_mappings = $this->portsMappings;
$this->database->is_public = $this->isPublic;
$this->database->public_port = $this->publicPort;
$this->database->public_port_timeout = $this->publicPortTimeout;
2024-11-04 13:33:44 +00:00
$this->database->custom_docker_run_options = $this->customDockerRunOptions;
$this->database->is_log_drain_enabled = $this->isLogDrainEnabled;
2025-02-10 20:29:45 +00:00
$this->database->enable_ssl = $this->enable_ssl;
2024-04-10 13:00:46 +00:00
$this->database->save();
2024-11-04 13:33:44 +00:00
$this->dbUrl = $this->database->internal_db_url;
$this->dbUrlPublic = $this->database->external_db_url;
} else {
$this->name = $this->database->name;
$this->description = $this->database->description;
$this->keydbConf = $this->database->keydb_conf;
$this->keydbPassword = $this->database->keydb_password;
$this->image = $this->database->image;
$this->portsMappings = $this->database->ports_mappings;
$this->isPublic = $this->database->is_public;
$this->publicPort = $this->database->public_port;
$this->publicPortTimeout = $this->database->public_port_timeout;
2024-11-04 13:33:44 +00:00
$this->customDockerRunOptions = $this->database->custom_docker_run_options;
$this->isLogDrainEnabled = $this->database->is_log_drain_enabled;
2025-02-10 20:29:45 +00:00
$this->enable_ssl = $this->database->enable_ssl;
2024-11-04 13:33:44 +00:00
$this->dbUrl = $this->database->internal_db_url;
$this->dbUrlPublic = $this->database->external_db_url;
2024-04-10 13:00:46 +00:00
}
}
2024-06-10 20:43:34 +00:00
2024-11-04 13:33:44 +00:00
public function instantSaveAdvanced()
2024-04-10 13:00:46 +00:00
{
try {
$this->authorize('update', $this->database);
2024-11-04 13:33:44 +00:00
if (! $this->server->isLogDrainEnabled()) {
$this->isLogDrainEnabled = false;
$this->dispatch('error', 'Log drain is not enabled on the server. Please enable it first.');
return;
2024-04-10 13:00:46 +00:00
}
2024-11-04 13:33:44 +00:00
$this->syncData(true);
2024-04-10 13:00:46 +00:00
$this->dispatch('success', 'Database updated.');
2024-11-04 13:33:44 +00:00
$this->dispatch('success', 'You need to restart the service for the changes to take effect.');
2024-04-10 13:00:46 +00:00
} catch (Exception $e) {
return handleError($e, $this);
}
}
2024-06-10 20:43:34 +00:00
2024-04-10 13:00:46 +00:00
public function instantSave()
{
try {
$this->authorize('update', $this->database);
2024-11-04 13:33:44 +00:00
if ($this->isPublic && ! $this->publicPort) {
2024-04-10 13:00:46 +00:00
$this->dispatch('error', 'Public port is required.');
2024-11-04 13:33:44 +00:00
$this->isPublic = false;
2024-06-10 20:43:34 +00:00
return;
2024-04-10 13:00:46 +00:00
}
if ($this->isPublic && ! str($this->database->status)->startsWith('running')) {
$this->dispatch('error', 'Database must be started to be publicly accessible.');
$this->isPublic = false;
2024-06-10 20:43:34 +00:00
return;
}
$this->syncData(true);
if ($this->isPublic) {
2024-04-10 13:00:46 +00:00
StartDatabaseProxy::run($this->database);
$this->dispatch('success', 'Database is now publicly accessible.');
} else {
StopDatabaseProxy::run($this->database);
$this->dispatch('success', 'Database is no longer publicly accessible.');
}
} catch (\Throwable $e) {
2024-11-04 13:33:44 +00:00
$this->isPublic = ! $this->isPublic;
$this->syncData(true);
2024-06-10 20:43:34 +00:00
2024-04-10 13:00:46 +00:00
return handleError($e, $this);
}
}
2024-06-10 20:43:34 +00:00
2024-11-04 13:33:44 +00:00
public function databaseProxyStopped()
2024-04-10 13:00:46 +00:00
{
2024-11-04 13:33:44 +00:00
$this->syncData();
2024-04-10 13:00:46 +00:00
}
2024-11-04 13:33:44 +00:00
public function submit()
2024-04-10 13:00:46 +00:00
{
2024-11-04 13:33:44 +00:00
try {
$this->authorize('manageEnvironment', $this->database);
2024-11-04 13:33:44 +00:00
if (str($this->publicPort)->isEmpty()) {
$this->publicPort = null;
}
$this->syncData(true);
$this->dispatch('success', 'Database updated.');
} catch (Exception $e) {
return handleError($e, $this);
} finally {
if (is_null($this->database->config_hash)) {
$this->database->isConfigurationChanged(true);
} else {
$this->dispatch('configurationChanged');
}
}
2024-04-10 13:00:46 +00:00
}
2025-02-10 20:29:45 +00:00
public function instantSaveSSL()
{
try {
$this->authorize('update', $this->database);
2025-02-10 20:29:45 +00:00
$this->syncData(true);
$this->dispatch('success', 'SSL configuration updated.');
} catch (Exception $e) {
return handleError($e, $this);
}
}
public function regenerateSslCertificate()
{
try {
$this->authorize('update', $this->database);
$existingCert = $this->database->sslCertificates()->first();
2025-02-10 20:29:45 +00:00
if (! $existingCert) {
$this->dispatch('error', 'No existing SSL certificate found for this database.');
return;
}
$caCert = $this->server->sslCertificates()
2025-02-10 20:29:45 +00:00
->where('is_ca_certificate', true)
->first();
SslHelper::generateSslCertificate(
commonName: $existingCert->commonName,
subjectAlternativeNames: $existingCert->subjectAlternativeNames ?? [],
resourceType: $existingCert->resource_type,
resourceId: $existingCert->resource_id,
serverId: $existingCert->server_id,
caCert: $caCert->ssl_certificate,
caKey: $caCert->ssl_private_key,
configurationDir: $existingCert->configuration_dir,
mountPath: $existingCert->mount_path,
isPemKeyFileRequired: true,
2025-02-10 20:29:45 +00:00
);
$this->dispatch('success', 'SSL certificates regenerated. Restart database to apply changes.');
} catch (Exception $e) {
handleError($e, $this);
}
}
2024-04-10 13:00:46 +00:00
}