2023-09-22 09:23:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
2023-12-07 18:06:32 +00:00
|
|
|
namespace App\Livewire\Project\Service;
|
2023-09-22 09:23:49 +00:00
|
|
|
|
2023-11-09 13:59:38 +00:00
|
|
|
use App\Actions\Database\StartDatabaseProxy;
|
|
|
|
|
use App\Actions\Database\StopDatabaseProxy;
|
2025-03-12 15:33:35 +00:00
|
|
|
use App\Models\InstanceSettings;
|
2023-09-22 09:23:49 +00:00
|
|
|
use App\Models\ServiceDatabase;
|
2025-08-26 08:27:31 +00:00
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
2025-03-12 15:33:35 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2025-04-30 15:39:41 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2025-03-12 15:33:35 +00:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
2023-09-22 09:23:49 +00:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
|
|
class Database extends Component
|
|
|
|
|
{
|
2025-08-26 08:27:31 +00:00
|
|
|
use AuthorizesRequests;
|
|
|
|
|
|
2023-09-22 09:23:49 +00:00
|
|
|
public ServiceDatabase $database;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-11-09 13:59:38 +00:00
|
|
|
public ?string $db_url_public = null;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-26 12:45:52 +00:00
|
|
|
public $fileStorages;
|
2023-11-09 13:59:38 +00:00
|
|
|
|
2025-03-12 15:33:35 +00:00
|
|
|
public $parameters;
|
|
|
|
|
|
2025-10-13 13:38:59 +00:00
|
|
|
public ?string $humanName = null;
|
|
|
|
|
|
|
|
|
|
public ?string $description = null;
|
|
|
|
|
|
|
|
|
|
public ?string $image = null;
|
|
|
|
|
|
|
|
|
|
public bool $excludeFromStatus = false;
|
|
|
|
|
|
|
|
|
|
public ?int $publicPort = null;
|
|
|
|
|
|
|
|
|
|
public bool $isPublic = false;
|
|
|
|
|
|
|
|
|
|
public bool $isLogDrainEnabled = false;
|
|
|
|
|
|
2024-06-10 20:43:34 +00:00
|
|
|
protected $listeners = ['refreshFileStorages'];
|
|
|
|
|
|
2023-09-22 09:23:49 +00:00
|
|
|
protected $rules = [
|
2025-10-13 13:38:59 +00:00
|
|
|
'humanName' => 'nullable',
|
|
|
|
|
'description' => 'nullable',
|
|
|
|
|
'image' => 'required',
|
|
|
|
|
'excludeFromStatus' => 'required|boolean',
|
|
|
|
|
'publicPort' => 'nullable|integer',
|
|
|
|
|
'isPublic' => 'required|boolean',
|
|
|
|
|
'isLogDrainEnabled' => 'required|boolean',
|
2023-09-22 09:23:49 +00:00
|
|
|
];
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-22 09:23:49 +00:00
|
|
|
public function render()
|
|
|
|
|
{
|
|
|
|
|
return view('livewire.project.service.database');
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-11-17 19:08:21 +00:00
|
|
|
public function mount()
|
|
|
|
|
{
|
2025-08-26 08:27:31 +00:00
|
|
|
try {
|
|
|
|
|
$this->parameters = get_route_parameters();
|
|
|
|
|
$this->authorize('view', $this->database);
|
|
|
|
|
if ($this->database->is_public) {
|
|
|
|
|
$this->db_url_public = $this->database->getServiceDatabaseUrl();
|
|
|
|
|
}
|
|
|
|
|
$this->refreshFileStorages();
|
2025-10-13 13:38:59 +00:00
|
|
|
$this->syncData(false);
|
2025-08-26 08:27:31 +00:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return handleError($e, $this);
|
2023-11-09 13:59:38 +00:00
|
|
|
}
|
2023-09-26 12:45:52 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-10-13 13:38:59 +00:00
|
|
|
private function syncData(bool $toModel = false): void
|
|
|
|
|
{
|
|
|
|
|
if ($toModel) {
|
|
|
|
|
$this->database->human_name = $this->humanName;
|
|
|
|
|
$this->database->description = $this->description;
|
|
|
|
|
$this->database->image = $this->image;
|
|
|
|
|
$this->database->exclude_from_status = $this->excludeFromStatus;
|
|
|
|
|
$this->database->public_port = $this->publicPort;
|
|
|
|
|
$this->database->is_public = $this->isPublic;
|
|
|
|
|
$this->database->is_log_drain_enabled = $this->isLogDrainEnabled;
|
|
|
|
|
} else {
|
|
|
|
|
$this->humanName = $this->database->human_name;
|
|
|
|
|
$this->description = $this->database->description;
|
|
|
|
|
$this->image = $this->database->image;
|
|
|
|
|
$this->excludeFromStatus = $this->database->exclude_from_status ?? false;
|
|
|
|
|
$this->publicPort = $this->database->public_port;
|
|
|
|
|
$this->isPublic = $this->database->is_public ?? false;
|
|
|
|
|
$this->isLogDrainEnabled = $this->database->is_log_drain_enabled ?? false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-12 15:33:35 +00:00
|
|
|
public function delete($password)
|
|
|
|
|
{
|
2025-08-26 08:27:31 +00:00
|
|
|
try {
|
|
|
|
|
$this->authorize('delete', $this->database);
|
2025-03-12 15:33:35 +00:00
|
|
|
|
2025-08-26 08:27:31 +00:00
|
|
|
if (! data_get(InstanceSettings::get(), 'disable_two_step_confirmation')) {
|
|
|
|
|
if (! Hash::check($password, Auth::user()->password)) {
|
|
|
|
|
$this->addError('password', 'The provided password is incorrect.');
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-03-12 15:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->database->delete();
|
|
|
|
|
$this->dispatch('success', 'Database deleted.');
|
|
|
|
|
|
|
|
|
|
return redirect()->route('project.service.configuration', $this->parameters);
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return handleError($e, $this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-21 11:44:32 +00:00
|
|
|
public function instantSaveExclude()
|
|
|
|
|
{
|
2025-08-26 08:27:31 +00:00
|
|
|
try {
|
|
|
|
|
$this->authorize('update', $this->database);
|
|
|
|
|
$this->submit();
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return handleError($e, $this);
|
|
|
|
|
}
|
2024-03-21 11:44:32 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-03-21 11:44:32 +00:00
|
|
|
public function instantSaveLogDrain()
|
2023-11-17 19:08:21 +00:00
|
|
|
{
|
2025-08-26 08:27:31 +00:00
|
|
|
try {
|
|
|
|
|
$this->authorize('update', $this->database);
|
|
|
|
|
if (! $this->database->service->destination->server->isLogDrainEnabled()) {
|
2025-10-13 13:38:59 +00:00
|
|
|
$this->isLogDrainEnabled = false;
|
2025-08-26 08:27:31 +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-08-26 08:27:31 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$this->submit();
|
|
|
|
|
$this->dispatch('success', 'You need to restart the service for the changes to take effect.');
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return handleError($e, $this);
|
2023-11-17 19:08:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-04-29 12:27:17 +00:00
|
|
|
public function convertToApplication()
|
|
|
|
|
{
|
|
|
|
|
try {
|
2025-08-26 08:27:31 +00:00
|
|
|
$this->authorize('update', $this->database);
|
2025-04-29 12:27:17 +00:00
|
|
|
$service = $this->database->service;
|
|
|
|
|
$serviceDatabase = $this->database;
|
2025-04-30 16:30:33 +00:00
|
|
|
|
2025-04-30 16:26:43 +00:00
|
|
|
// Check if application with same name already exists
|
|
|
|
|
if ($service->applications()->where('name', $serviceDatabase->name)->exists()) {
|
|
|
|
|
throw new \Exception('An application with this name already exists.');
|
|
|
|
|
}
|
2025-04-30 16:30:33 +00:00
|
|
|
|
2025-04-30 16:26:43 +00:00
|
|
|
// Create new parameters removing database_uuid
|
|
|
|
|
$redirectParams = collect($this->parameters)
|
|
|
|
|
->except('database_uuid')
|
|
|
|
|
->all();
|
2025-04-30 16:30:33 +00:00
|
|
|
|
2025-04-30 16:26:43 +00:00
|
|
|
DB::transaction(function () use ($service, $serviceDatabase) {
|
|
|
|
|
$service->applications()->create([
|
|
|
|
|
'name' => $serviceDatabase->name,
|
|
|
|
|
'human_name' => $serviceDatabase->human_name,
|
|
|
|
|
'description' => $serviceDatabase->description,
|
|
|
|
|
'exclude_from_status' => $serviceDatabase->exclude_from_status,
|
|
|
|
|
'is_log_drain_enabled' => $serviceDatabase->is_log_drain_enabled,
|
|
|
|
|
'image' => $serviceDatabase->image,
|
|
|
|
|
'service_id' => $service->id,
|
|
|
|
|
'is_migrated' => true,
|
|
|
|
|
]);
|
|
|
|
|
$serviceDatabase->delete();
|
|
|
|
|
});
|
2025-04-30 16:30:33 +00:00
|
|
|
|
2025-04-30 16:26:43 +00:00
|
|
|
return redirect()->route('project.service.configuration', $redirectParams);
|
2025-04-29 12:27:17 +00:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return handleError($e, $this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-17 19:08:21 +00:00
|
|
|
public function instantSave()
|
|
|
|
|
{
|
2025-08-26 08:27:31 +00:00
|
|
|
try {
|
|
|
|
|
$this->authorize('update', $this->database);
|
2025-10-13 13:38:59 +00:00
|
|
|
if ($this->isPublic && ! $this->publicPort) {
|
2025-08-26 08:27:31 +00:00
|
|
|
$this->dispatch('error', 'Public port is required.');
|
2025-10-13 13:38:59 +00:00
|
|
|
$this->isPublic = false;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-11-09 13:59:38 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2025-10-13 13:38:59 +00:00
|
|
|
$this->syncData(true);
|
2025-08-26 08:27:31 +00:00
|
|
|
if ($this->database->is_public) {
|
|
|
|
|
if (! str($this->database->status)->startsWith('running')) {
|
|
|
|
|
$this->dispatch('error', 'Database must be started to be publicly accessible.');
|
2025-10-13 13:38:59 +00:00
|
|
|
$this->isPublic = false;
|
2025-08-26 08:27:31 +00:00
|
|
|
$this->database->is_public = false;
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
StartDatabaseProxy::run($this->database);
|
|
|
|
|
$this->db_url_public = $this->database->getServiceDatabaseUrl();
|
|
|
|
|
$this->dispatch('success', 'Database is now publicly accessible.');
|
|
|
|
|
} else {
|
|
|
|
|
StopDatabaseProxy::run($this->database);
|
|
|
|
|
$this->db_url_public = null;
|
|
|
|
|
$this->dispatch('success', 'Database is no longer publicly accessible.');
|
|
|
|
|
}
|
|
|
|
|
$this->submit();
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return handleError($e, $this);
|
2023-11-09 13:59:38 +00:00
|
|
|
}
|
2023-09-25 13:48:43 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-26 12:45:52 +00:00
|
|
|
public function refreshFileStorages()
|
|
|
|
|
{
|
|
|
|
|
$this->fileStorages = $this->database->fileStorages()->get();
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-22 09:23:49 +00:00
|
|
|
public function submit()
|
|
|
|
|
{
|
|
|
|
|
try {
|
2025-08-26 08:27:31 +00:00
|
|
|
$this->authorize('update', $this->database);
|
2023-09-22 09:23:49 +00:00
|
|
|
$this->validate();
|
2025-10-13 13:38:59 +00:00
|
|
|
$this->syncData(true);
|
2023-09-22 09:23:49 +00:00
|
|
|
$this->database->save();
|
2025-10-13 13:38:59 +00:00
|
|
|
$this->database->refresh();
|
|
|
|
|
$this->syncData(false);
|
2023-09-27 10:45:53 +00:00
|
|
|
updateCompose($this->database);
|
2024-02-22 13:53:42 +00:00
|
|
|
$this->dispatch('success', 'Database saved.');
|
2025-08-26 08:27:31 +00:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return handleError($e, $this);
|
2023-09-22 09:23:49 +00:00
|
|
|
} finally {
|
2023-12-07 18:06:32 +00:00
|
|
|
$this->dispatch('generateDockerCompose');
|
2023-09-22 09:23:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|