2023-10-14 12:22:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Actions\Database;
|
|
|
|
|
2023-11-09 13:59:38 +00:00
|
|
|
use App\Models\ServiceDatabase;
|
2024-04-10 13:00:46 +00:00
|
|
|
use App\Models\StandaloneClickhouse;
|
|
|
|
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;
|
2023-10-14 12:22:07 +00:00
|
|
|
use App\Models\StandalonePostgresql;
|
|
|
|
use App\Models\StandaloneRedis;
|
|
|
|
use Lorisleiva\Actions\Concerns\AsAction;
|
|
|
|
use Symfony\Component\Yaml\Yaml;
|
|
|
|
|
|
|
|
class StartDatabaseProxy
|
|
|
|
{
|
|
|
|
use AsAction;
|
|
|
|
|
2024-04-10 13:00:46 +00:00
|
|
|
public function handle(StandaloneRedis|StandalonePostgresql|StandaloneMongodb|StandaloneMysql|StandaloneMariadb|StandaloneKeydb|StandaloneDragonfly|StandaloneClickhouse|ServiceDatabase $database)
|
2023-10-14 12:22:07 +00:00
|
|
|
{
|
|
|
|
$internalPort = null;
|
2023-11-09 13:59:38 +00:00
|
|
|
$type = $database->getMorphClass();
|
|
|
|
$network = data_get($database, 'destination.network');
|
|
|
|
$server = data_get($database, 'destination.server');
|
2023-11-10 08:41:42 +00:00
|
|
|
$containerName = data_get($database, 'uuid');
|
|
|
|
$proxyContainerName = "{$database->uuid}-proxy";
|
2023-11-09 13:59:38 +00:00
|
|
|
if ($database->getMorphClass() === 'App\Models\ServiceDatabase') {
|
|
|
|
$databaseType = $database->databaseType();
|
2024-03-25 11:13:43 +00:00
|
|
|
// $connectPredefined = data_get($database, 'service.connect_to_docker_network');
|
|
|
|
$network = $database->service->uuid;
|
2023-11-09 13:59:38 +00:00
|
|
|
$server = data_get($database, 'service.destination.server');
|
2023-11-10 08:41:42 +00:00
|
|
|
$proxyContainerName = "{$database->service->uuid}-proxy";
|
2023-11-09 13:59:38 +00:00
|
|
|
switch ($databaseType) {
|
|
|
|
case 'standalone-mariadb':
|
|
|
|
$type = 'App\Models\StandaloneMariadb';
|
2023-11-10 08:41:42 +00:00
|
|
|
$containerName = "mariadb-{$database->service->uuid}";
|
2023-11-09 13:59:38 +00:00
|
|
|
break;
|
|
|
|
case 'standalone-mongodb':
|
|
|
|
$type = 'App\Models\StandaloneMongodb';
|
2023-11-10 08:41:42 +00:00
|
|
|
$containerName = "mongodb-{$database->service->uuid}";
|
2023-11-09 13:59:38 +00:00
|
|
|
break;
|
|
|
|
case 'standalone-mysql':
|
|
|
|
$type = 'App\Models\StandaloneMysql';
|
2023-11-10 08:41:42 +00:00
|
|
|
$containerName = "mysql-{$database->service->uuid}";
|
2023-11-09 13:59:38 +00:00
|
|
|
break;
|
|
|
|
case 'standalone-postgresql':
|
|
|
|
$type = 'App\Models\StandalonePostgresql';
|
2023-11-10 08:41:42 +00:00
|
|
|
$containerName = "postgresql-{$database->service->uuid}";
|
2023-11-09 13:59:38 +00:00
|
|
|
break;
|
|
|
|
case 'standalone-redis':
|
|
|
|
$type = 'App\Models\StandaloneRedis';
|
2023-11-10 08:41:42 +00:00
|
|
|
$containerName = "redis-{$database->service->uuid}";
|
2023-11-09 13:59:38 +00:00
|
|
|
break;
|
2024-04-10 13:00:46 +00:00
|
|
|
case 'standalone-keydb':
|
|
|
|
$type = 'App\Models\StandaloneKeydb';
|
|
|
|
$containerName = "keydb-{$database->service->uuid}";
|
|
|
|
break;
|
|
|
|
case 'standalone-dragonfly':
|
|
|
|
$type = 'App\Models\StandaloneDragonfly';
|
|
|
|
$containerName = "dragonfly-{$database->service->uuid}";
|
|
|
|
break;
|
|
|
|
case 'standalone-clickhouse':
|
|
|
|
$type = 'App\Models\StandaloneClickhouse';
|
|
|
|
$containerName = "clickhouse-{$database->service->uuid}";
|
|
|
|
break;
|
2023-11-09 13:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($type === 'App\Models\StandaloneRedis') {
|
2023-10-14 12:22:07 +00:00
|
|
|
$internalPort = 6379;
|
2024-06-10 20:43:34 +00:00
|
|
|
} elseif ($type === 'App\Models\StandalonePostgresql') {
|
2023-10-14 12:22:07 +00:00
|
|
|
$internalPort = 5432;
|
2024-06-10 20:43:34 +00:00
|
|
|
} elseif ($type === 'App\Models\StandaloneMongodb') {
|
2023-10-19 11:32:03 +00:00
|
|
|
$internalPort = 27017;
|
2024-06-10 20:43:34 +00:00
|
|
|
} elseif ($type === 'App\Models\StandaloneMysql') {
|
2023-10-24 12:31:28 +00:00
|
|
|
$internalPort = 3306;
|
2024-06-10 20:43:34 +00:00
|
|
|
} elseif ($type === 'App\Models\StandaloneMariadb') {
|
2023-10-24 12:31:28 +00:00
|
|
|
$internalPort = 3306;
|
2024-06-10 20:43:34 +00:00
|
|
|
} elseif ($type === 'App\Models\StandaloneKeydb') {
|
2024-04-10 13:00:46 +00:00
|
|
|
$internalPort = 6379;
|
2024-06-10 20:43:34 +00:00
|
|
|
} elseif ($type === 'App\Models\StandaloneDragonfly') {
|
2024-04-10 13:00:46 +00:00
|
|
|
$internalPort = 6379;
|
2024-06-10 20:43:34 +00:00
|
|
|
} elseif ($type === 'App\Models\StandaloneClickhouse') {
|
2024-04-10 13:00:46 +00:00
|
|
|
$internalPort = 9000;
|
2023-10-14 12:22:07 +00:00
|
|
|
}
|
|
|
|
$configuration_dir = database_proxy_dir($database->uuid);
|
|
|
|
$nginxconf = <<<EOF
|
|
|
|
user nginx;
|
|
|
|
worker_processes auto;
|
|
|
|
|
|
|
|
error_log /var/log/nginx/error.log;
|
|
|
|
|
|
|
|
events {
|
|
|
|
worker_connections 1024;
|
|
|
|
}
|
|
|
|
stream {
|
|
|
|
server {
|
|
|
|
listen $database->public_port;
|
2023-11-10 08:41:42 +00:00
|
|
|
proxy_pass $containerName:$internalPort;
|
2023-10-14 12:22:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
EOF;
|
2024-06-10 20:43:34 +00:00
|
|
|
$dockerfile = <<< 'EOF'
|
2023-10-14 12:22:07 +00:00
|
|
|
FROM nginx:stable-alpine
|
|
|
|
|
|
|
|
COPY nginx.conf /etc/nginx/nginx.conf
|
|
|
|
EOF;
|
|
|
|
$docker_compose = [
|
|
|
|
'services' => [
|
2023-11-10 08:41:42 +00:00
|
|
|
$proxyContainerName => [
|
2023-10-14 12:22:07 +00:00
|
|
|
'build' => [
|
|
|
|
'context' => $configuration_dir,
|
|
|
|
'dockerfile' => 'Dockerfile',
|
|
|
|
],
|
2024-06-10 20:43:34 +00:00
|
|
|
'image' => 'nginx:stable-alpine',
|
2023-11-10 08:41:42 +00:00
|
|
|
'container_name' => $proxyContainerName,
|
2023-10-14 12:22:07 +00:00
|
|
|
'restart' => RESTART_MODE,
|
|
|
|
'ports' => [
|
|
|
|
"$database->public_port:$database->public_port",
|
|
|
|
],
|
|
|
|
'networks' => [
|
2023-11-09 13:59:38 +00:00
|
|
|
$network,
|
2023-10-14 12:22:07 +00:00
|
|
|
],
|
|
|
|
'healthcheck' => [
|
|
|
|
'test' => [
|
|
|
|
'CMD-SHELL',
|
|
|
|
'stat /etc/nginx/nginx.conf || exit 1',
|
|
|
|
],
|
|
|
|
'interval' => '5s',
|
|
|
|
'timeout' => '5s',
|
|
|
|
'retries' => 3,
|
2024-06-10 20:43:34 +00:00
|
|
|
'start_period' => '1s',
|
2023-10-14 12:22:07 +00:00
|
|
|
],
|
2024-06-10 20:43:34 +00:00
|
|
|
],
|
2023-10-14 12:22:07 +00:00
|
|
|
],
|
|
|
|
'networks' => [
|
2023-11-09 13:59:38 +00:00
|
|
|
$network => [
|
2023-10-14 12:22:07 +00:00
|
|
|
'external' => true,
|
2023-11-09 13:59:38 +00:00
|
|
|
'name' => $network,
|
2023-10-14 12:22:07 +00:00
|
|
|
'attachable' => true,
|
2024-06-10 20:43:34 +00:00
|
|
|
],
|
|
|
|
],
|
2023-10-14 12:22:07 +00:00
|
|
|
];
|
|
|
|
$dockercompose_base64 = base64_encode(Yaml::dump($docker_compose, 4, 2));
|
|
|
|
$nginxconf_base64 = base64_encode($nginxconf);
|
|
|
|
$dockerfile_base64 = base64_encode($dockerfile);
|
2024-03-25 11:17:52 +00:00
|
|
|
instant_remote_process(["docker rm -f $proxyContainerName"], $server, false);
|
2023-10-14 12:22:07 +00:00
|
|
|
instant_remote_process([
|
|
|
|
"mkdir -p $configuration_dir",
|
2024-04-17 08:49:34 +00:00
|
|
|
"echo '{$dockerfile_base64}' | base64 -d | tee $configuration_dir/Dockerfile > /dev/null",
|
|
|
|
"echo '{$nginxconf_base64}' | base64 -d | tee $configuration_dir/nginx.conf > /dev/null",
|
|
|
|
"echo '{$dockercompose_base64}' | base64 -d | tee $configuration_dir/docker-compose.yaml > /dev/null",
|
2023-11-13 14:27:33 +00:00
|
|
|
"docker compose --project-directory {$configuration_dir} pull",
|
2023-10-17 12:04:21 +00:00
|
|
|
"docker compose --project-directory {$configuration_dir} up --build -d",
|
2023-11-09 13:59:38 +00:00
|
|
|
], $server);
|
2023-10-14 12:22:07 +00:00
|
|
|
}
|
|
|
|
}
|