lasthourcloud/bootstrap/helpers/databases.php

202 lines
7.2 KiB
PHP
Raw Permalink 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;
}
2024-07-01 14:26:50 +00:00
function create_standalone_postgresql($environmentId, $destinationUuid, ?array $otherData = null): StandalonePostgresql
{
2024-07-01 14:26:50 +00:00
$destination = StandaloneDocker::where('uuid', $destinationUuid)->first();
2024-06-10 20:43:34 +00:00
if (! $destination) {
throw new Exception('Destination not found');
}
2024-07-24 12:27:21 +00:00
$database = new StandalonePostgresql;
2024-07-01 14:26:50 +00:00
$database->name = generate_database_name('postgresql');
$database->postgres_password = \Illuminate\Support\Str::password(length: 64, symbols: false);
$database->environment_id = $environmentId;
$database->destination_id = $destination->id;
$database->destination_type = $destination->getMorphClass();
if ($otherData) {
$database->fill($otherData);
}
$database->save();
2024-06-10 20:43:34 +00:00
2024-07-01 14:26:50 +00:00
return $database;
}
2024-07-01 14:26:50 +00:00
function create_standalone_redis($environment_id, $destination_uuid, ?array $otherData = null): StandaloneRedis
2023-10-12 15:18:33 +00:00
{
$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-07-24 12:27:21 +00:00
$database = new StandaloneRedis;
2024-07-01 14:26:50 +00:00
$database->name = generate_database_name('redis');
$database->redis_password = \Illuminate\Support\Str::password(length: 64, symbols: false);
$database->environment_id = $environment_id;
$database->destination_id = $destination->id;
$database->destination_type = $destination->getMorphClass();
if ($otherData) {
$database->fill($otherData);
}
$database->save();
2024-06-10 20:43:34 +00:00
2024-07-01 14:26:50 +00:00
return $database;
2023-10-12 15:18:33 +00:00
}
2024-07-01 14:26:50 +00:00
function create_standalone_mongodb($environment_id, $destination_uuid, ?array $otherData = null): StandaloneMongodb
2023-10-19 11:32:03 +00:00
{
$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-07-24 12:27:21 +00:00
$database = new StandaloneMongodb;
2024-07-01 14:26:50 +00:00
$database->name = generate_database_name('mongodb');
$database->mongo_initdb_root_password = \Illuminate\Support\Str::password(length: 64, symbols: false);
$database->environment_id = $environment_id;
$database->destination_id = $destination->id;
$database->destination_type = $destination->getMorphClass();
if ($otherData) {
$database->fill($otherData);
}
$database->save();
2024-06-10 20:43:34 +00:00
2024-07-01 14:26:50 +00:00
return $database;
2023-10-19 11:32:03 +00:00
}
2024-07-01 14:26:50 +00:00
function create_standalone_mysql($environment_id, $destination_uuid, ?array $otherData = null): StandaloneMysql
2023-10-24 12:31:28 +00:00
{
$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-07-24 12:27:21 +00:00
$database = new StandaloneMysql;
2024-07-01 14:26:50 +00:00
$database->name = generate_database_name('mysql');
$database->mysql_root_password = \Illuminate\Support\Str::password(length: 64, symbols: false);
$database->mysql_password = \Illuminate\Support\Str::password(length: 64, symbols: false);
$database->environment_id = $environment_id;
$database->destination_id = $destination->id;
$database->destination_type = $destination->getMorphClass();
if ($otherData) {
$database->fill($otherData);
}
$database->save();
2024-06-10 20:43:34 +00:00
2024-07-01 14:26:50 +00:00
return $database;
2023-10-24 12:31:28 +00:00
}
2024-07-01 14:26:50 +00:00
function create_standalone_mariadb($environment_id, $destination_uuid, ?array $otherData = null): StandaloneMariadb
2023-10-24 12:31:28 +00:00
{
$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-07-24 12:27:21 +00:00
$database = new StandaloneMariadb;
2024-07-01 14:26:50 +00:00
$database->name = generate_database_name('mariadb');
$database->mariadb_root_password = \Illuminate\Support\Str::password(length: 64, symbols: false);
$database->mariadb_password = \Illuminate\Support\Str::password(length: 64, symbols: false);
$database->environment_id = $environment_id;
$database->destination_id = $destination->id;
$database->destination_type = $destination->getMorphClass();
if ($otherData) {
$database->fill($otherData);
}
$database->save();
2024-06-10 20:43:34 +00:00
2024-07-01 14:26:50 +00:00
return $database;
2023-10-24 12:31:28 +00:00
}
2024-07-01 14:26:50 +00:00
function create_standalone_keydb($environment_id, $destination_uuid, ?array $otherData = null): StandaloneKeydb
2024-04-10 13:00:46 +00:00
{
$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-07-24 12:27:21 +00:00
$database = new StandaloneKeydb;
2024-07-01 14:26:50 +00:00
$database->name = generate_database_name('keydb');
$database->keydb_password = \Illuminate\Support\Str::password(length: 64, symbols: false);
$database->environment_id = $environment_id;
$database->destination_id = $destination->id;
$database->destination_type = $destination->getMorphClass();
if ($otherData) {
$database->fill($otherData);
}
$database->save();
2024-06-10 20:43:34 +00:00
2024-07-01 14:26:50 +00:00
return $database;
2024-04-10 13:00:46 +00:00
}
2024-07-01 14:26:50 +00:00
function create_standalone_dragonfly($environment_id, $destination_uuid, ?array $otherData = null): StandaloneDragonfly
2024-04-10 13:00:46 +00:00
{
$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-07-24 12:27:21 +00:00
$database = new StandaloneDragonfly;
2024-07-01 14:26:50 +00:00
$database->name = generate_database_name('dragonfly');
$database->dragonfly_password = \Illuminate\Support\Str::password(length: 64, symbols: false);
$database->environment_id = $environment_id;
$database->destination_id = $destination->id;
$database->destination_type = $destination->getMorphClass();
if ($otherData) {
$database->fill($otherData);
}
$database->save();
2024-06-10 20:43:34 +00:00
2024-07-01 14:26:50 +00:00
return $database;
2024-04-10 13:00:46 +00:00
}
2024-07-01 14:26:50 +00:00
function create_standalone_clickhouse($environment_id, $destination_uuid, ?array $otherData = null): StandaloneClickhouse
2024-04-10 13:00:46 +00:00
{
$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-07-24 12:27:21 +00:00
$database = new StandaloneClickhouse;
2024-07-01 14:26:50 +00:00
$database->name = generate_database_name('clickhouse');
$database->clickhouse_admin_password = \Illuminate\Support\Str::password(length: 64, symbols: false);
$database->environment_id = $environment_id;
$database->destination_id = $destination->id;
$database->destination_type = $destination->getMorphClass();
if ($otherData) {
$database->fill($otherData);
}
$database->save();
2024-06-10 20:43:34 +00:00
2024-07-01 14:26:50 +00:00
return $database;
2024-04-10 13:00:46 +00:00
}
2023-10-19 11:32:03 +00:00
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);
}
function isPublicPortAlreadyUsed(Server $server, int $port, ?string $id = null): bool
{
if ($id) {
$foundDatabase = $server->databases()->where('public_port', $port)->where('is_public', true)->where('id', '!=', $id)->first();
} else {
$foundDatabase = $server->databases()->where('public_port', $port)->where('is_public', true)->first();
}
if ($foundDatabase) {
return true;
}
return false;
}