2023-08-09 13:57:53 +00:00
|
|
|
<?php
|
|
|
|
|
2023-08-10 13:52:54 +00:00
|
|
|
use App\Models\Server;
|
2023-08-09 13:57:53 +00:00
|
|
|
use App\Models\StandaloneDocker;
|
|
|
|
use App\Models\StandalonePostgresql;
|
2023-10-12 15:18:33 +00:00
|
|
|
use App\Models\StandaloneRedis;
|
2023-08-09 13:57:53 +00:00
|
|
|
use Visus\Cuid2\Cuid2;
|
|
|
|
|
|
|
|
function generate_database_name(string $type): string
|
|
|
|
{
|
|
|
|
$cuid = new Cuid2(7);
|
|
|
|
return $type . '-database-' . $cuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
function create_standalone_postgresql($environment_id, $destination_uuid): StandalonePostgresql
|
|
|
|
{
|
|
|
|
// TODO: If another type of destination is added, this will need to be updated.
|
|
|
|
$destination = StandaloneDocker::where('uuid', $destination_uuid)->first();
|
|
|
|
if (!$destination) {
|
|
|
|
throw new Exception('Destination not found');
|
|
|
|
}
|
|
|
|
return StandalonePostgresql::create([
|
|
|
|
'name' => generate_database_name('postgresql'),
|
2023-09-07 11:23:34 +00:00
|
|
|
'postgres_password' => \Illuminate\Support\Str::password(symbols: false),
|
2023-08-09 13:57:53 +00:00
|
|
|
'environment_id' => $environment_id,
|
|
|
|
'destination_id' => $destination->id,
|
|
|
|
'destination_type' => $destination->getMorphClass(),
|
|
|
|
]);
|
2023-08-10 13:52:54 +00:00
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
|
2023-10-12 15:18:33 +00:00
|
|
|
function create_standalone_redis($environment_id, $destination_uuid): StandaloneRedis
|
|
|
|
{
|
|
|
|
$destination = StandaloneDocker::where('uuid', $destination_uuid)->first();
|
|
|
|
if (!$destination) {
|
|
|
|
throw new Exception('Destination not found');
|
|
|
|
}
|
|
|
|
return StandaloneRedis::create([
|
|
|
|
'name' => generate_database_name('redis'),
|
|
|
|
'redis_password' => \Illuminate\Support\Str::password(symbols: false),
|
|
|
|
'environment_id' => $environment_id,
|
|
|
|
'destination_id' => $destination->id,
|
|
|
|
'destination_type' => $destination->getMorphClass(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2023-08-10 13:52:54 +00:00
|
|
|
/**
|
|
|
|
* Delete file locally on the filesystem.
|
|
|
|
* @param string $filename
|
|
|
|
* @param Server $server
|
|
|
|
* @return void
|
|
|
|
*/
|
2023-08-11 18:19:42 +00:00
|
|
|
function delete_backup_locally(string | null $filename, Server $server): void
|
2023-08-10 13:52:54 +00:00
|
|
|
{
|
|
|
|
if (empty($filename)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
instant_remote_process(["rm -f \"{$filename}\""], $server, throwError: false);
|
2023-08-09 13:57:53 +00:00
|
|
|
}
|