2023-10-19 11:32:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
2023-12-07 18:06:32 +00:00
|
|
|
namespace App\Livewire\Project\Database\Mongodb;
|
2023-10-19 11:32:03 +00:00
|
|
|
|
|
|
|
|
use App\Actions\Database\StartDatabaseProxy;
|
|
|
|
|
use App\Actions\Database\StopDatabaseProxy;
|
2024-06-09 19:33:17 +00:00
|
|
|
use App\Models\Server;
|
2023-10-19 11:32:03 +00:00
|
|
|
use App\Models\StandaloneMongodb;
|
2025-08-19 12:15:31 +00:00
|
|
|
use App\Support\ValidationPatterns;
|
2023-10-19 11:32:03 +00:00
|
|
|
use Exception;
|
2025-08-23 16:50:35 +00:00
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
2023-10-19 11:32:03 +00:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
|
|
class General extends Component
|
|
|
|
|
{
|
2025-08-23 16:50:35 +00:00
|
|
|
use AuthorizesRequests;
|
|
|
|
|
|
2025-10-14 15:04:48 +00:00
|
|
|
public ?Server $server = null;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-10-19 11:32:03 +00:00
|
|
|
public StandaloneMongodb $database;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-10-13 08:01:17 +00:00
|
|
|
public string $name;
|
|
|
|
|
|
|
|
|
|
public ?string $description = null;
|
|
|
|
|
|
|
|
|
|
public ?string $mongoConf = null;
|
|
|
|
|
|
|
|
|
|
public string $mongoInitdbRootUsername;
|
|
|
|
|
|
|
|
|
|
public string $mongoInitdbRootPassword;
|
|
|
|
|
|
|
|
|
|
public string $mongoInitdbDatabase;
|
|
|
|
|
|
|
|
|
|
public string $image;
|
|
|
|
|
|
|
|
|
|
public ?string $portsMappings = null;
|
|
|
|
|
|
|
|
|
|
public ?bool $isPublic = null;
|
|
|
|
|
|
2026-03-29 13:41:28 +00:00
|
|
|
public mixed $publicPort = null;
|
2025-10-13 08:01:17 +00:00
|
|
|
|
2026-03-29 13:41:28 +00:00
|
|
|
public mixed $publicPortTimeout = 3600;
|
2026-02-27 05:07:09 +00:00
|
|
|
|
2025-10-13 08:01:17 +00:00
|
|
|
public bool $isLogDrainEnabled = false;
|
|
|
|
|
|
|
|
|
|
public ?string $customDockerRunOptions = null;
|
|
|
|
|
|
2025-08-19 12:15:31 +00:00
|
|
|
protected function rules(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
2025-10-13 08:01:17 +00:00
|
|
|
'name' => ValidationPatterns::nameRules(),
|
|
|
|
|
'description' => ValidationPatterns::descriptionRules(),
|
|
|
|
|
'mongoConf' => 'nullable',
|
2026-04-20 11:45:57 +00:00
|
|
|
'mongoInitdbRootUsername' => ValidationPatterns::databaseIdentifierRules(
|
|
|
|
|
enforcePattern: $this->mongoInitdbRootUsername !== $this->database->mongo_initdb_root_username,
|
|
|
|
|
),
|
|
|
|
|
'mongoInitdbRootPassword' => ValidationPatterns::databasePasswordRules(
|
|
|
|
|
enforcePattern: $this->mongoInitdbRootPassword !== $this->database->mongo_initdb_root_password,
|
|
|
|
|
),
|
|
|
|
|
'mongoInitdbDatabase' => ValidationPatterns::databaseIdentifierRules(
|
|
|
|
|
enforcePattern: $this->mongoInitdbDatabase !== $this->database->mongo_initdb_database,
|
|
|
|
|
),
|
2025-10-13 08:01:17 +00:00
|
|
|
'image' => 'required',
|
2026-03-28 17:53:25 +00:00
|
|
|
'portsMappings' => ValidationPatterns::portMappingRules(),
|
2025-10-13 08:01:17 +00:00
|
|
|
'isPublic' => 'nullable|boolean',
|
2026-03-29 13:41:28 +00:00
|
|
|
'publicPort' => 'nullable|integer|min:1|max:65535',
|
2026-02-27 22:24:04 +00:00
|
|
|
'publicPortTimeout' => 'nullable|integer|min:1',
|
2025-10-13 08:01:17 +00:00
|
|
|
'isLogDrainEnabled' => 'nullable|boolean',
|
|
|
|
|
'customDockerRunOptions' => 'nullable',
|
2025-08-19 12:15:31 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function messages(): array
|
|
|
|
|
{
|
|
|
|
|
return array_merge(
|
|
|
|
|
ValidationPatterns::combinedMessages(),
|
2026-03-28 17:53:25 +00:00
|
|
|
ValidationPatterns::portMappingMessages(),
|
2025-08-19 12:15:31 +00:00
|
|
|
[
|
2025-10-13 08:01:17 +00:00
|
|
|
'name.required' => 'The Name field is required.',
|
2026-04-20 11:58:36 +00:00
|
|
|
...ValidationPatterns::databaseIdentifierMessages('mongoInitdbRootUsername', 'Root Username'),
|
|
|
|
|
...ValidationPatterns::databasePasswordMessages('mongoInitdbRootPassword', 'Root Password'),
|
|
|
|
|
...ValidationPatterns::databaseIdentifierMessages('mongoInitdbDatabase', 'MongoDB Database'),
|
2025-10-13 08:01:17 +00:00
|
|
|
'image.required' => 'The Docker Image field is required.',
|
|
|
|
|
'publicPort.integer' => 'The Public Port must be an integer.',
|
2026-03-29 13:41:28 +00:00
|
|
|
'publicPort.min' => 'The Public Port must be at least 1.',
|
|
|
|
|
'publicPort.max' => 'The Public Port must not exceed 65535.',
|
2026-02-27 05:07:09 +00:00
|
|
|
'publicPortTimeout.integer' => 'The Public Port Timeout must be an integer.',
|
2026-02-27 22:24:04 +00:00
|
|
|
'publicPortTimeout.min' => 'The Public Port Timeout must be at least 1.',
|
2025-08-19 12:15:31 +00:00
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-10-19 11:32:03 +00:00
|
|
|
protected $validationAttributes = [
|
2025-10-13 08:01:17 +00:00
|
|
|
'name' => 'Name',
|
|
|
|
|
'description' => 'Description',
|
|
|
|
|
'mongoConf' => 'Mongo Configuration',
|
|
|
|
|
'mongoInitdbRootUsername' => 'Root Username',
|
|
|
|
|
'mongoInitdbRootPassword' => 'Root Password',
|
|
|
|
|
'mongoInitdbDatabase' => 'Database',
|
|
|
|
|
'image' => 'Image',
|
|
|
|
|
'portsMappings' => 'Port Mapping',
|
|
|
|
|
'isPublic' => 'Is Public',
|
|
|
|
|
'publicPort' => 'Public Port',
|
2026-02-27 05:07:09 +00:00
|
|
|
'publicPortTimeout' => 'Public Port Timeout',
|
2025-10-13 08:01:17 +00:00
|
|
|
'customDockerRunOptions' => 'Custom Docker Run Options',
|
2023-10-19 11:32:03 +00:00
|
|
|
];
|
2023-11-08 11:26:57 +00:00
|
|
|
|
|
|
|
|
public function mount()
|
|
|
|
|
{
|
2025-10-13 08:01:17 +00:00
|
|
|
try {
|
2025-10-14 15:33:42 +00:00
|
|
|
$this->authorize('view', $this->database);
|
2025-10-13 08:01:17 +00:00
|
|
|
$this->syncData();
|
|
|
|
|
$this->server = data_get($this->database, 'destination.server');
|
2025-10-14 15:04:48 +00:00
|
|
|
if (! $this->server) {
|
|
|
|
|
$this->dispatch('error', 'Database destination server is not configured.');
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-10-13 08:01:17 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
|
return handleError($e, $this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function syncData(bool $toModel = false)
|
|
|
|
|
{
|
|
|
|
|
if ($toModel) {
|
|
|
|
|
$this->validate();
|
|
|
|
|
$this->database->name = $this->name;
|
|
|
|
|
$this->database->description = $this->description;
|
|
|
|
|
$this->database->mongo_conf = $this->mongoConf;
|
|
|
|
|
$this->database->mongo_initdb_root_username = $this->mongoInitdbRootUsername;
|
|
|
|
|
$this->database->mongo_initdb_root_password = $this->mongoInitdbRootPassword;
|
|
|
|
|
$this->database->mongo_initdb_database = $this->mongoInitdbDatabase;
|
|
|
|
|
$this->database->image = $this->image;
|
|
|
|
|
$this->database->ports_mappings = $this->portsMappings;
|
|
|
|
|
$this->database->is_public = $this->isPublic;
|
2026-03-29 13:41:28 +00:00
|
|
|
$this->database->public_port = $this->publicPort ?: null;
|
|
|
|
|
$this->database->public_port_timeout = $this->publicPortTimeout ?: null;
|
2025-10-13 08:01:17 +00:00
|
|
|
$this->database->is_log_drain_enabled = $this->isLogDrainEnabled;
|
|
|
|
|
$this->database->custom_docker_run_options = $this->customDockerRunOptions;
|
|
|
|
|
$this->database->save();
|
|
|
|
|
} else {
|
|
|
|
|
$this->name = $this->database->name;
|
|
|
|
|
$this->description = $this->database->description;
|
|
|
|
|
$this->mongoConf = $this->database->mongo_conf;
|
|
|
|
|
$this->mongoInitdbRootUsername = $this->database->mongo_initdb_root_username;
|
|
|
|
|
$this->mongoInitdbRootPassword = $this->database->mongo_initdb_root_password;
|
|
|
|
|
$this->mongoInitdbDatabase = $this->database->mongo_initdb_database;
|
|
|
|
|
$this->image = $this->database->image;
|
|
|
|
|
$this->portsMappings = $this->database->ports_mappings;
|
|
|
|
|
$this->isPublic = $this->database->is_public;
|
|
|
|
|
$this->publicPort = $this->database->public_port;
|
2026-02-27 05:07:09 +00:00
|
|
|
$this->publicPortTimeout = $this->database->public_port_timeout;
|
2025-10-13 08:01:17 +00:00
|
|
|
$this->isLogDrainEnabled = $this->database->is_log_drain_enabled;
|
|
|
|
|
$this->customDockerRunOptions = $this->database->custom_docker_run_options;
|
2025-02-05 21:56:29 +00:00
|
|
|
}
|
2023-11-08 11:26:57 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-11-17 19:08:21 +00:00
|
|
|
public function instantSaveAdvanced()
|
|
|
|
|
{
|
|
|
|
|
try {
|
2025-08-23 16:50:35 +00:00
|
|
|
$this->authorize('update', $this->database);
|
|
|
|
|
|
2024-09-23 17:51:31 +00:00
|
|
|
if (! $this->server->isLogDrainEnabled()) {
|
2025-10-13 08:01:17 +00:00
|
|
|
$this->isLogDrainEnabled = false;
|
2023-12-07 18:06:32 +00: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 14:31:43 +00:00
|
|
|
return;
|
2023-11-17 19:08:21 +00:00
|
|
|
}
|
2025-10-13 08:01:17 +00:00
|
|
|
$this->syncData(true);
|
2024-02-22 13:53:42 +00:00
|
|
|
$this->dispatch('success', 'Database updated.');
|
2023-12-07 18:06:32 +00:00
|
|
|
$this->dispatch('success', 'You need to restart the service for the changes to take effect.');
|
2023-11-17 19:08:21 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
|
return handleError($e, $this);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-10-24 12:31:28 +00:00
|
|
|
public function submit()
|
|
|
|
|
{
|
2023-10-19 11:32:03 +00:00
|
|
|
try {
|
2025-08-23 16:50:35 +00:00
|
|
|
$this->authorize('update', $this->database);
|
|
|
|
|
|
2026-03-28 17:53:25 +00:00
|
|
|
if ($this->portsMappings) {
|
|
|
|
|
$this->portsMappings = str($this->portsMappings)->replace(' ', '')->trim()->toString();
|
|
|
|
|
}
|
2025-10-13 08:01:17 +00:00
|
|
|
if (str($this->publicPort)->isEmpty()) {
|
|
|
|
|
$this->publicPort = null;
|
2023-11-08 08:30:38 +00:00
|
|
|
}
|
2025-10-13 08:01:17 +00:00
|
|
|
if (str($this->mongoConf)->isEmpty()) {
|
|
|
|
|
$this->mongoConf = null;
|
2023-10-19 11:32:03 +00:00
|
|
|
}
|
2025-10-13 08:01:17 +00:00
|
|
|
$this->syncData(true);
|
2024-02-22 13:53:42 +00:00
|
|
|
$this->dispatch('success', 'Database updated.');
|
2026-05-21 08:31:08 +00:00
|
|
|
$this->dispatch('databaseUpdated');
|
2023-10-19 11:32:03 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
|
return handleError($e, $this);
|
2024-04-12 10:44:49 +00:00
|
|
|
} finally {
|
|
|
|
|
if (is_null($this->database->config_hash)) {
|
|
|
|
|
$this->database->isConfigurationChanged(true);
|
|
|
|
|
} else {
|
|
|
|
|
$this->dispatch('configurationChanged');
|
|
|
|
|
}
|
2023-10-19 11:32:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-10-19 11:32:03 +00:00
|
|
|
public function instantSave()
|
|
|
|
|
{
|
|
|
|
|
try {
|
2025-08-23 16:50:35 +00:00
|
|
|
$this->authorize('update', $this->database);
|
|
|
|
|
|
2025-10-13 08:01:17 +00:00
|
|
|
if ($this->isPublic && ! $this->publicPort) {
|
2023-12-07 18:06:32 +00:00
|
|
|
$this->dispatch('error', 'Public port is required.');
|
2025-10-13 08:01:17 +00:00
|
|
|
$this->isPublic = false;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-01-07 14:31:43 +00:00
|
|
|
return;
|
2023-10-19 11:32:03 +00:00
|
|
|
}
|
2026-01-06 12:44: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
|
|
|
|
2026-01-06 12:44:46 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$this->syncData(true);
|
|
|
|
|
if ($this->isPublic) {
|
2023-10-19 11:32:03 +00:00
|
|
|
StartDatabaseProxy::run($this->database);
|
2023-12-07 18:06:32 +00:00
|
|
|
$this->dispatch('success', 'Database is now publicly accessible.');
|
2023-10-19 11:32:03 +00:00
|
|
|
} else {
|
|
|
|
|
StopDatabaseProxy::run($this->database);
|
2023-12-07 18:06:32 +00:00
|
|
|
$this->dispatch('success', 'Database is no longer publicly accessible.');
|
2023-10-19 11:32:03 +00:00
|
|
|
}
|
2026-05-21 08:31:08 +00:00
|
|
|
$this->dispatch('databaseUpdated');
|
2025-01-07 14:31:43 +00:00
|
|
|
} catch (\Throwable $e) {
|
2025-10-13 08:01:17 +00:00
|
|
|
$this->isPublic = ! $this->isPublic;
|
2026-01-06 12:44:46 +00:00
|
|
|
$this->syncData(true);
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-10-19 11:32:03 +00:00
|
|
|
return handleError($e, $this);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-10-19 11:32:03 +00:00
|
|
|
public function refresh(): void
|
|
|
|
|
{
|
|
|
|
|
$this->database->refresh();
|
2025-10-13 08:01:17 +00:00
|
|
|
$this->syncData();
|
2023-10-19 11:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function render()
|
|
|
|
|
{
|
|
|
|
|
return view('livewire.project.database.mongodb.general');
|
|
|
|
|
}
|
|
|
|
|
}
|