lasthourcloud/bootstrap/helpers/databases.php

159 lines
5.8 KiB
PHP
Raw Normal View History

<?php
use App\Models\Server;
2024-04-10 13:00:46 +00:00
use App\Models\StandaloneClickhouse;
use App\Models\StandaloneDocker;
2024-04-10 13:00:46 +00:00
use App\Models\StandaloneDragonfly;
use App\Models\StandaloneKeydb;
2023-10-24 12:31:28 +00:00
use App\Models\StandaloneMariadb;
2023-10-19 11:32:03 +00:00
use App\Models\StandaloneMongodb;
2023-10-24 12:31:28 +00:00
use App\Models\StandaloneMysql;
use App\Models\StandalonePostgresql;
2023-10-12 15:18:33 +00:00
use App\Models\StandaloneRedis;
use Visus\Cuid2\Cuid2;
function generate_database_name(string $type): string
{
$cuid = new Cuid2(7);
2024-06-10 20:43:34 +00:00
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();
2024-06-10 20:43:34 +00:00
if (! $destination) {
throw new Exception('Destination not found');
}
2024-06-10 20:43:34 +00:00
return StandalonePostgresql::create([
'name' => generate_database_name('postgresql'),
2023-11-28 11:04:21 +00:00
'postgres_password' => \Illuminate\Support\Str::password(length: 64, symbols: false),
'environment_id' => $environment_id,
'destination_id' => $destination->id,
'destination_type' => $destination->getMorphClass(),
]);
}
2023-10-12 15:18:33 +00:00
function create_standalone_redis($environment_id, $destination_uuid): StandaloneRedis
{
$destination = StandaloneDocker::where('uuid', $destination_uuid)->first();
2024-06-10 20:43:34 +00:00
if (! $destination) {
2023-10-12 15:18:33 +00:00
throw new Exception('Destination not found');
}
2024-06-10 20:43:34 +00:00
2023-10-12 15:18:33 +00:00
return StandaloneRedis::create([
'name' => generate_database_name('redis'),
2023-11-28 11:04:21 +00:00
'redis_password' => \Illuminate\Support\Str::password(length: 64, symbols: false),
2023-10-12 15:18:33 +00:00
'environment_id' => $environment_id,
'destination_id' => $destination->id,
'destination_type' => $destination->getMorphClass(),
]);
}
2023-10-19 11:32:03 +00:00
function create_standalone_mongodb($environment_id, $destination_uuid): StandaloneMongodb
{
$destination = StandaloneDocker::where('uuid', $destination_uuid)->first();
2024-06-10 20:43:34 +00:00
if (! $destination) {
2023-10-19 11:32:03 +00:00
throw new Exception('Destination not found');
}
2024-06-10 20:43:34 +00:00
2023-10-19 11:32:03 +00:00
return StandaloneMongodb::create([
'name' => generate_database_name('mongodb'),
2023-11-28 11:04:21 +00:00
'mongo_initdb_root_password' => \Illuminate\Support\Str::password(length: 64, symbols: false),
2023-10-19 11:32:03 +00:00
'environment_id' => $environment_id,
'destination_id' => $destination->id,
'destination_type' => $destination->getMorphClass(),
]);
}
2023-10-24 12:31:28 +00:00
function create_standalone_mysql($environment_id, $destination_uuid): StandaloneMysql
{
$destination = StandaloneDocker::where('uuid', $destination_uuid)->first();
2024-06-10 20:43:34 +00:00
if (! $destination) {
2023-10-24 12:31:28 +00:00
throw new Exception('Destination not found');
}
2024-06-10 20:43:34 +00:00
2023-10-24 12:31:28 +00:00
return StandaloneMysql::create([
'name' => generate_database_name('mysql'),
2023-11-28 11:04:21 +00:00
'mysql_root_password' => \Illuminate\Support\Str::password(length: 64, symbols: false),
'mysql_password' => \Illuminate\Support\Str::password(length: 64, symbols: false),
2023-10-24 12:31:28 +00:00
'environment_id' => $environment_id,
'destination_id' => $destination->id,
'destination_type' => $destination->getMorphClass(),
]);
}
function create_standalone_mariadb($environment_id, $destination_uuid): StandaloneMariadb
{
$destination = StandaloneDocker::where('uuid', $destination_uuid)->first();
2024-06-10 20:43:34 +00:00
if (! $destination) {
2023-10-24 12:31:28 +00:00
throw new Exception('Destination not found');
}
2024-06-10 20:43:34 +00:00
2023-10-24 12:31:28 +00:00
return StandaloneMariadb::create([
'name' => generate_database_name('mariadb'),
2023-11-28 11:04:21 +00:00
'mariadb_root_password' => \Illuminate\Support\Str::password(length: 64, symbols: false),
'mariadb_password' => \Illuminate\Support\Str::password(length: 64, symbols: false),
2023-10-24 12:31:28 +00:00
'environment_id' => $environment_id,
'destination_id' => $destination->id,
'destination_type' => $destination->getMorphClass(),
]);
}
2024-04-10 13:00:46 +00:00
function create_standalone_keydb($environment_id, $destination_uuid): StandaloneKeydb
{
$destination = StandaloneDocker::where('uuid', $destination_uuid)->first();
2024-06-10 20:43:34 +00:00
if (! $destination) {
2024-04-10 13:00:46 +00:00
throw new Exception('Destination not found');
}
2024-06-10 20:43:34 +00:00
2024-04-10 13:00:46 +00:00
return StandaloneKeydb::create([
'name' => generate_database_name('keydb'),
'keydb_password' => \Illuminate\Support\Str::password(length: 64, symbols: false),
'environment_id' => $environment_id,
'destination_id' => $destination->id,
'destination_type' => $destination->getMorphClass(),
]);
}
function create_standalone_dragonfly($environment_id, $destination_uuid): StandaloneDragonfly
{
$destination = StandaloneDocker::where('uuid', $destination_uuid)->first();
2024-06-10 20:43:34 +00:00
if (! $destination) {
2024-04-10 13:00:46 +00:00
throw new Exception('Destination not found');
}
2024-06-10 20:43:34 +00:00
2024-04-10 13:00:46 +00:00
return StandaloneDragonfly::create([
'name' => generate_database_name('dragonfly'),
'dragonfly_password' => \Illuminate\Support\Str::password(length: 64, symbols: false),
'environment_id' => $environment_id,
'destination_id' => $destination->id,
'destination_type' => $destination->getMorphClass(),
]);
}
function create_standalone_clickhouse($environment_id, $destination_uuid): StandaloneClickhouse
{
$destination = StandaloneDocker::where('uuid', $destination_uuid)->first();
2024-06-10 20:43:34 +00:00
if (! $destination) {
2024-04-10 13:00:46 +00:00
throw new Exception('Destination not found');
}
2024-06-10 20:43:34 +00:00
2024-04-10 13:00:46 +00:00
return StandaloneClickhouse::create([
'name' => generate_database_name('clickhouse'),
'clickhouse_admin_password' => \Illuminate\Support\Str::password(length: 64, symbols: false),
2024-04-10 13:00:46 +00:00
'environment_id' => $environment_id,
'destination_id' => $destination->id,
'destination_type' => $destination->getMorphClass(),
]);
}
2023-10-19 11:32:03 +00:00
/**
* Delete file locally on the filesystem.
*/
2024-06-10 20:43:34 +00:00
function delete_backup_locally(?string $filename, Server $server): void
{
if (empty($filename)) {
return;
}
instant_remote_process(["rm -f \"{$filename}\""], $server, throwError: false);
}