fix: keydb view

This commit is contained in:
Andras Bacsai
2024-11-04 14:33:44 +01:00
parent 431c67e9b1
commit bf29dd4789

View File

@@ -7,62 +7,116 @@ use App\Actions\Database\StopDatabaseProxy;
use App\Models\Server; use App\Models\Server;
use App\Models\StandaloneKeydb; use App\Models\StandaloneKeydb;
use Exception; use Exception;
use Illuminate\Support\Facades\Auth;
use Livewire\Attributes\Rule;
use Livewire\Component; use Livewire\Component;
class General extends Component class General extends Component
{ {
protected $listeners = ['refresh'];
public Server $server; public Server $server;
public StandaloneKeydb $database; public StandaloneKeydb $database;
public ?string $db_url = null; #[Rule(['required', 'string'])]
public string $name;
public ?string $db_url_public = null; #[Rule(['nullable', 'string'])]
public ?string $description = null;
protected $rules = [ #[Rule(['nullable', 'string'])]
'database.name' => 'required', public ?string $keydbConf = null;
'database.description' => 'nullable',
'database.keydb_conf' => 'nullable',
'database.keydb_password' => 'required',
'database.image' => 'required',
'database.ports_mappings' => 'nullable',
'database.is_public' => 'nullable|boolean',
'database.public_port' => 'nullable|integer',
'database.is_log_drain_enabled' => 'nullable|boolean',
'database.custom_docker_run_options' => 'nullable',
];
protected $validationAttributes = [ #[Rule(['required', 'string'])]
'database.name' => 'Name', public string $keydbPassword;
'database.description' => 'Description',
'database.keydb_conf' => 'Redis Configuration', #[Rule(['required', 'string'])]
'database.keydb_password' => 'Redis Password', public string $image;
'database.image' => 'Image',
'database.ports_mappings' => 'Port Mapping', #[Rule(['nullable', 'string'])]
'database.is_public' => 'Is Public', public ?string $portsMappings = null;
'database.public_port' => 'Public Port',
'database.custom_docker_run_options' => 'Custom Docker Run Options', #[Rule(['nullable', 'boolean'])]
]; public ?bool $isPublic = null;
#[Rule(['nullable', 'integer'])]
public ?int $publicPort = null;
#[Rule(['nullable', 'string'])]
public ?string $customDockerRunOptions = null;
#[Rule(['nullable', 'string'])]
public ?string $dbUrl = null;
#[Rule(['nullable', 'string'])]
public ?string $dbUrlPublic = null;
#[Rule(['nullable', 'boolean'])]
public bool $isLogDrainEnabled = false;
public function getListeners()
{
$teamId = Auth::user()->currentTeam()->id;
return [
"echo-private:team.{$teamId},DatabaseProxyStopped" => 'databaseProxyStopped',
];
}
public function mount() public function mount()
{ {
$this->db_url = $this->database->internal_db_url; try {
$this->db_url_public = $this->database->external_db_url; $this->syncData();
$this->server = data_get($this->database, 'destination.server'); $this->server = data_get($this->database, 'destination.server');
} catch (\Throwable $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->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->custom_docker_run_options = $this->customDockerRunOptions;
$this->database->is_log_drain_enabled = $this->isLogDrainEnabled;
$this->database->save();
$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->customDockerRunOptions = $this->database->custom_docker_run_options;
$this->isLogDrainEnabled = $this->database->is_log_drain_enabled;
$this->dbUrl = $this->database->internal_db_url;
$this->dbUrlPublic = $this->database->external_db_url;
}
} }
public function instantSaveAdvanced() public function instantSaveAdvanced()
{ {
try { try {
if (! $this->server->isLogDrainEnabled()) { if (! $this->server->isLogDrainEnabled()) {
$this->database->is_log_drain_enabled = false; $this->isLogDrainEnabled = false;
$this->dispatch('error', 'Log drain is not enabled on the server. Please enable it first.'); $this->dispatch('error', 'Log drain is not enabled on the server. Please enable it first.');
return; return;
} }
$this->database->save(); $this->syncData(true);
$this->dispatch('success', 'Database updated.'); $this->dispatch('success', 'Database updated.');
$this->dispatch('success', 'You need to restart the service for the changes to take effect.'); $this->dispatch('success', 'You need to restart the service for the changes to take effect.');
} catch (Exception $e) { } catch (Exception $e) {
@@ -70,14 +124,50 @@ class General extends Component
} }
} }
public function instantSave()
{
try {
if ($this->isPublic && ! $this->publicPort) {
$this->dispatch('error', 'Public port is required.');
$this->isPublic = false;
return;
}
if ($this->isPublic) {
if (! str($this->database->status)->startsWith('running')) {
$this->dispatch('error', 'Database must be started to be publicly accessible.');
$this->isPublic = false;
return;
}
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.');
}
$this->dbUrlPublic = $this->database->external_db_url;
$this->syncData(true);
} catch (\Throwable $e) {
$this->isPublic = ! $this->isPublic;
$this->syncData(true);
return handleError($e, $this);
}
}
public function databaseProxyStopped()
{
$this->syncData();
}
public function submit() public function submit()
{ {
try { try {
$this->validate(); if (str($this->publicPort)->isEmpty()) {
if ($this->database->keydb_conf === '') { $this->publicPort = null;
$this->database->keydb_conf = null;
} }
$this->database->save(); $this->syncData(true);
$this->dispatch('success', 'Database updated.'); $this->dispatch('success', 'Database updated.');
} catch (Exception $e) { } catch (Exception $e) {
return handleError($e, $this); return handleError($e, $this);
@@ -89,45 +179,4 @@ class General extends Component
} }
} }
} }
public function instantSave()
{
try {
if ($this->database->is_public && ! $this->database->public_port) {
$this->dispatch('error', 'Public port is required.');
$this->database->is_public = false;
return;
}
if ($this->database->is_public) {
if (! str($this->database->status)->startsWith('running')) {
$this->dispatch('error', 'Database must be started to be publicly accessible.');
$this->database->is_public = false;
return;
}
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.');
}
$this->db_url_public = $this->database->external_db_url;
$this->database->save();
} catch (\Throwable $e) {
$this->database->is_public = ! $this->database->is_public;
return handleError($e, $this);
}
}
public function refresh(): void
{
$this->database->refresh();
}
public function render()
{
return view('livewire.project.database.keydb.general');
}
} }