Files
coolify/app/Livewire/Project/Database/Postgresql/General.php
T

325 lines
11 KiB
PHP
Raw Normal View History

2023-08-07 18:46:40 +02:00
<?php
2023-12-07 19:06:32 +01:00
namespace App\Livewire\Project\Database\Postgresql;
2023-08-07 18:46:40 +02:00
2023-10-14 14:22:07 +02:00
use App\Actions\Database\StartDatabaseProxy;
use App\Actions\Database\StopDatabaseProxy;
use App\Helpers\SslHelper;
2024-06-09 21:33:17 +02:00
use App\Models\Server;
use App\Models\SslCertificate;
2023-08-08 14:35:01 +02:00
use App\Models\StandalonePostgresql;
use Carbon\Carbon;
2023-08-08 14:35:01 +02:00
use Exception;
use Illuminate\Support\Facades\Auth;
2023-08-07 18:46:40 +02:00
use Livewire\Component;
2023-09-15 15:34:25 +02:00
2023-08-07 18:46:40 +02:00
class General extends Component
{
2023-08-08 14:35:01 +02:00
public StandalonePostgresql $database;
2024-06-10 20:43:34 +00:00
2024-06-09 21:33:17 +02:00
public Server $server;
2024-06-10 20:43:34 +00:00
2023-08-08 14:35:01 +02:00
public string $new_filename;
2024-06-10 20:43:34 +00:00
2023-08-08 14:35:01 +02:00
public string $new_content;
2024-06-10 20:43:34 +00:00
2023-11-08 12:26:57 +01:00
public ?string $db_url = null;
2024-06-10 20:43:34 +00:00
2023-11-08 12:26:57 +01:00
public ?string $db_url_public = null;
2023-08-08 14:35:01 +02:00
public ?Carbon $certificateValidUntil = null;
2025-02-03 23:21:09 +01:00
2024-06-14 14:09:56 +02:00
public function getListeners()
{
$userId = Auth::id();
2024-06-14 14:09:56 +02:00
return [
"echo-private:user.{$userId},DatabaseStatusChanged" => '$refresh',
'refresh' => '$refresh',
2024-06-14 12:10:40 +00:00
'save_init_script',
'delete_init_script',
2024-06-14 14:09:56 +02:00
];
}
2023-08-07 22:14:21 +02:00
2023-08-07 18:46:40 +02:00
protected $rules = [
'database.name' => 'required',
'database.description' => 'nullable',
'database.postgres_user' => 'required',
'database.postgres_password' => 'required',
'database.postgres_db' => 'required',
'database.postgres_initdb_args' => 'nullable',
'database.postgres_host_auth_method' => 'nullable',
2023-11-08 12:40:05 +01:00
'database.postgres_conf' => 'nullable',
2023-08-07 18:46:40 +02:00
'database.init_scripts' => 'nullable',
2023-08-07 22:14:21 +02:00
'database.image' => 'required',
2023-08-08 14:35:01 +02:00
'database.ports_mappings' => 'nullable',
2023-09-07 13:23:34 +02:00
'database.is_public' => 'nullable|boolean',
'database.public_port' => 'nullable|integer',
2023-11-17 20:08:21 +01:00
'database.is_log_drain_enabled' => 'nullable|boolean',
'database.custom_docker_run_options' => 'nullable',
2025-01-29 13:25:05 +01:00
'database.enable_ssl' => 'boolean',
'database.ssl_mode' => 'nullable|string|in:allow,prefer,require,verify-ca,verify-full',
2023-08-07 18:46:40 +02:00
];
2024-06-10 20:43:34 +00:00
2023-08-07 18:46:40 +02:00
protected $validationAttributes = [
'database.name' => 'Name',
'database.description' => 'Description',
'database.postgres_user' => 'Postgres User',
'database.postgres_password' => 'Postgres Password',
'database.postgres_db' => 'Postgres DB',
'database.postgres_initdb_args' => 'Postgres Initdb Args',
'database.postgres_host_auth_method' => 'Postgres Host Auth Method',
2023-11-08 12:40:05 +01:00
'database.postgres_conf' => 'Postgres Configuration',
2023-08-07 18:46:40 +02:00
'database.init_scripts' => 'Init Scripts',
2023-08-07 22:14:21 +02:00
'database.image' => 'Image',
2023-08-08 14:35:01 +02:00
'database.ports_mappings' => 'Port Mapping',
2023-09-07 13:23:34 +02:00
'database.is_public' => 'Is Public',
'database.public_port' => 'Public Port',
'database.custom_docker_run_options' => 'Custom Docker Run Options',
2025-01-29 13:25:05 +01:00
'database.enable_ssl' => 'Enable SSL',
'database.ssl_mode' => 'SSL Mode',
2023-08-07 18:46:40 +02:00
];
2024-06-10 20:43:34 +00:00
2023-09-07 13:23:34 +02:00
public function mount()
{
2024-07-02 12:15:58 +02:00
$this->db_url = $this->database->internal_db_url;
$this->db_url_public = $this->database->external_db_url;
2024-06-10 20:43:34 +00:00
$this->server = data_get($this->database, 'destination.server');
2025-02-03 23:21:09 +01:00
$existingCert = $this->database->sslCertificates()->first();
2025-02-03 23:21:09 +01:00
if ($existingCert) {
$this->certificateValidUntil = $existingCert->valid_until;
}
2023-09-07 13:23:34 +02:00
}
2024-06-14 12:10:40 +00:00
public function instantSaveAdvanced()
{
2023-11-17 20:08:21 +01:00
try {
2024-06-14 12:10:40 +00:00
if (! $this->server->isLogDrainEnabled()) {
2023-11-17 20:08:21 +01:00
$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 20:43:34 +00:00
2025-01-07 15:31:43 +01:00
return;
2023-11-17 20:08:21 +01:00
}
$this->database->save();
2024-02-22 14:53:42 +01:00
$this->dispatch('success', 'Database updated.');
2023-12-07 19:06:32 +01:00
$this->dispatch('success', 'You need to restart the service for the changes to take effect.');
2023-11-17 20:08:21 +01:00
} catch (Exception $e) {
return handleError($e, $this);
}
}
2024-06-10 20:43:34 +00:00
public function updatedDatabaseSslMode()
{
$this->instantSaveSSL();
}
2025-01-29 13:25:05 +01:00
public function instantSaveSSL()
{
try {
$this->database->save();
$this->dispatch('success', 'SSL configuration updated.');
2025-05-03 09:59:42 +02:00
$this->db_url = $this->database->internal_db_url;
$this->db_url_public = $this->database->external_db_url;
2025-01-29 13:25:05 +01:00
} catch (Exception $e) {
return handleError($e, $this);
}
}
public function regenerateSslCertificate()
{
try {
$existingCert = $this->database->sslCertificates()->first();
if (! $existingCert) {
$this->dispatch('error', 'No existing SSL certificate found for this database.');
return;
}
$caCert = SslCertificate::where('server_id', $existingCert->server_id)->where('is_ca_certificate', true)->first();
SslHelper::generateSslCertificate(
commonName: $existingCert->common_name,
subjectAlternativeNames: $existingCert->subject_alternative_names ?? [],
resourceType: $existingCert->resource_type,
resourceId: $existingCert->resource_id,
serverId: $existingCert->server_id,
caCert: $caCert->ssl_certificate,
caKey: $caCert->ssl_private_key,
2025-02-04 16:34:24 +01:00
configurationDir: $existingCert->configuration_dir,
mountPath: $existingCert->mount_path,
isPemKeyFileRequired: true,
);
$this->dispatch('success', 'SSL certificates have been regenerated. Please restart the database for changes to take effect.');
} catch (Exception $e) {
return handleError($e, $this);
}
}
2023-09-07 13:23:34 +02:00
public function instantSave()
{
try {
2024-06-14 12:10:40 +00: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-09-07 13:23:34 +02:00
$this->database->is_public = false;
2024-06-10 20:43:34 +00:00
2025-01-07 15:31:43 +01:00
return;
2023-09-07 13:23:34 +02:00
}
if ($this->database->is_public) {
2024-06-14 12:10:40 +00: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-10-24 14:31:28 +02:00
$this->database->is_public = false;
2024-06-10 20:43:34 +00:00
2025-01-07 15:31:43 +01:00
return;
2023-10-24 14:31:28 +02:00
}
2023-10-14 14:22:07 +02:00
StartDatabaseProxy::run($this->database);
2023-12-07 19:06:32 +01:00
$this->dispatch('success', 'Database is now publicly accessible.');
2023-09-07 13:23:34 +02:00
} else {
2023-10-14 14:22:07 +02:00
StopDatabaseProxy::run($this->database);
2023-12-07 19:06:32 +01:00
$this->dispatch('success', 'Database is no longer publicly accessible.');
2023-09-07 13:23:34 +02:00
}
2024-07-02 12:15:58 +02:00
$this->db_url_public = $this->database->external_db_url;
2023-09-07 13:23:34 +02:00
$this->database->save();
2025-01-07 15:31:43 +01:00
} catch (\Throwable $e) {
2024-06-14 12:10:40 +00:00
$this->database->is_public = ! $this->database->is_public;
2024-06-10 20:43:34 +00:00
2023-09-15 15:34:25 +02:00
return handleError($e, $this);
2023-09-07 13:23:34 +02:00
}
}
2024-06-10 20:43:34 +00:00
2023-08-08 14:35:01 +02:00
public function save_init_script($script)
{
2024-10-18 16:09:21 +02:00
$initScripts = collect($this->database->init_scripts ?? []);
$existingScript = $initScripts->firstWhere('filename', $script['filename']);
$oldScript = $initScripts->firstWhere('index', $script['index']);
2024-10-18 16:16:06 +02:00
2024-10-18 16:09:21 +02:00
if ($existingScript && $existingScript['index'] !== $script['index']) {
$this->dispatch('error', 'A script with this filename already exists.');
return;
}
$container_name = $this->database->uuid;
$configuration_dir = database_configuration_dir().'/'.$container_name;
if ($oldScript && $oldScript['filename'] !== $script['filename']) {
$old_file_path = "$configuration_dir/docker-entrypoint-initdb.d/{$oldScript['filename']}";
$delete_command = "rm -f $old_file_path";
try {
instant_remote_process([$delete_command], $this->server);
} catch (Exception $e) {
$this->dispatch('error', 'Failed to remove old init script from server: '.$e->getMessage());
return;
}
}
2024-10-18 16:09:21 +02:00
$index = $initScripts->search(function ($item) use ($script) {
return $item['index'] === $script['index'];
});
if ($index !== false) {
$initScripts[$index] = $script;
} else {
$initScripts->push($script);
}
$this->database->init_scripts = $initScripts->values()
->map(function ($item, $index) {
$item['index'] = $index;
2024-10-18 16:09:21 +02:00
return $item;
})
->all();
2024-10-18 16:09:21 +02:00
2023-08-08 14:35:01 +02:00
$this->database->save();
$this->dispatch('success', 'Init script saved and updated.');
2023-08-08 14:35:01 +02:00
}
public function delete_init_script($script)
{
$collection = collect($this->database->init_scripts);
$found = $collection->firstWhere('filename', $script['filename']);
if ($found) {
$container_name = $this->database->uuid;
$configuration_dir = database_configuration_dir().'/'.$container_name;
$file_path = "$configuration_dir/docker-entrypoint-initdb.d/{$script['filename']}";
$command = "rm -f $file_path";
try {
instant_remote_process([$command], $this->server);
} catch (Exception $e) {
$this->dispatch('error', 'Failed to remove init script from server: '.$e->getMessage());
return;
}
$updatedScripts = $collection->filter(fn ($s) => $s['filename'] !== $script['filename'])
->values()
->map(function ($item, $index) {
$item['index'] = $index;
return $item;
})
->all();
$this->database->init_scripts = $updatedScripts;
2023-08-08 14:35:01 +02:00
$this->database->save();
$this->dispatch('refresh')->self();
$this->dispatch('success', 'Init script deleted from the database and the server.');
2023-08-08 14:35:01 +02:00
}
}
public function save_new_init_script()
{
$this->validate([
'new_filename' => 'required|string',
'new_content' => 'required|string',
]);
$found = collect($this->database->init_scripts)->firstWhere('filename', $this->new_filename);
if ($found) {
2023-12-07 19:06:32 +01:00
$this->dispatch('error', 'Filename already exists.');
2024-06-10 20:43:34 +00:00
2023-08-08 14:35:01 +02:00
return;
}
2025-01-07 15:31:43 +01:00
if (! isset($this->database->init_scripts)) {
2023-08-09 14:44:36 +02:00
$this->database->init_scripts = [];
}
2023-08-08 14:35:01 +02:00
$this->database->init_scripts = array_merge($this->database->init_scripts, [
[
'index' => count($this->database->init_scripts),
'filename' => $this->new_filename,
'content' => $this->new_content,
2024-06-10 20:43:34 +00:00
],
2023-08-08 14:35:01 +02:00
]);
$this->database->save();
2024-02-22 14:53:42 +01:00
$this->dispatch('success', 'Init script added.');
2023-08-08 14:35:01 +02:00
$this->new_content = '';
$this->new_filename = '';
}
2023-08-08 11:51:36 +02:00
public function submit()
{
2023-08-07 18:46:40 +02:00
try {
if (str($this->database->public_port)->isEmpty()) {
$this->database->public_port = null;
}
2023-08-07 18:46:40 +02:00
$this->validate();
$this->database->save();
2024-02-22 14:53:42 +01:00
$this->dispatch('success', 'Database updated.');
2023-08-08 14:35:01 +02:00
} catch (Exception $e) {
2023-09-15 15:34:25 +02:00
return handleError($e, $this);
} finally {
if (is_null($this->database->config_hash)) {
$this->database->isConfigurationChanged(true);
} else {
$this->dispatch('configurationChanged');
}
2023-08-07 18:46:40 +02:00
}
}
2023-08-07 22:14:21 +02:00
}