From 5e36c37838ae78a82773a04934c1674f0cf5406c Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 9 Apr 2024 11:09:13 +0200 Subject: [PATCH] Add coolify database and handle exceptions --- app/Livewire/Settings/Backup.php | 62 +++++++++++++++++--------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/app/Livewire/Settings/Backup.php b/app/Livewire/Settings/Backup.php index 67a457774..121b73af8 100644 --- a/app/Livewire/Settings/Backup.php +++ b/app/Livewire/Settings/Backup.php @@ -41,35 +41,39 @@ public function mount() } public function add_coolify_database() { - $server = Server::find(0); - $out = instant_remote_process(['docker inspect coolify-db'], $server); - $envs = format_docker_envs_to_json($out); - $postgres_password = $envs['POSTGRES_PASSWORD']; - $postgres_user = $envs['POSTGRES_USER']; - $postgres_db = $envs['POSTGRES_DB']; - $this->database = StandalonePostgresql::create([ - 'id' => 0, - 'name' => 'coolify-db', - 'description' => 'Coolify database', - 'postgres_user' => $postgres_user, - 'postgres_password' => $postgres_password, - 'postgres_db' => $postgres_db, - 'status' => 'running', - 'destination_type' => 'App\Models\StandaloneDocker', - 'destination_id' => 0, - ]); - $this->backup = ScheduledDatabaseBackup::create([ - 'id' => 0, - 'enabled' => true, - 'save_s3' => false, - 'frequency' => '0 0 * * *', - 'database_id' => $this->database->id, - 'database_type' => 'App\Models\StandalonePostgresql', - 'team_id' => currentTeam()->id, - ]); - $this->database->refresh(); - $this->backup->refresh(); - $this->s3s = S3Storage::whereTeamId(0)->get(); + try { + $server = Server::findOrFail(0); + $out = instant_remote_process(['docker inspect coolify-db'], $server); + $envs = format_docker_envs_to_json($out); + $postgres_password = $envs['POSTGRES_PASSWORD']; + $postgres_user = $envs['POSTGRES_USER']; + $postgres_db = $envs['POSTGRES_DB']; + $this->database = StandalonePostgresql::create([ + 'id' => 0, + 'name' => 'coolify-db', + 'description' => 'Coolify database', + 'postgres_user' => $postgres_user, + 'postgres_password' => $postgres_password, + 'postgres_db' => $postgres_db, + 'status' => 'running', + 'destination_type' => 'App\Models\StandaloneDocker', + 'destination_id' => 0, + ]); + $this->backup = ScheduledDatabaseBackup::create([ + 'id' => 0, + 'enabled' => true, + 'save_s3' => false, + 'frequency' => '0 0 * * *', + 'database_id' => $this->database->id, + 'database_type' => 'App\Models\StandalonePostgresql', + 'team_id' => currentTeam()->id, + ]); + $this->database->refresh(); + $this->backup->refresh(); + $this->s3s = S3Storage::whereTeamId(0)->get(); + } catch (\Exception $e) { + return handleError($e, $this); + } } public function backup_now()