lasthourcloud/app/Actions/Database/StartDatabaseProxy.php

137 lines
5.2 KiB
PHP
Raw Normal View History

<?php
namespace App\Actions\Database;
2023-11-09 13:59:38 +00:00
use App\Models\ServiceDatabase;
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;
use App\Models\StandaloneRedis;
use Lorisleiva\Actions\Concerns\AsAction;
use Symfony\Component\Yaml\Yaml;
class StartDatabaseProxy
{
use AsAction;
2023-11-09 13:59:38 +00:00
public function handle(StandaloneRedis|StandalonePostgresql|StandaloneMongodb|StandaloneMysql|StandaloneMariadb|ServiceDatabase $database)
{
$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');
$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();
$network = data_get($database, 'service.destination.network');
$server = data_get($database, 'service.destination.server');
$proxyContainerName = "{$database->service->uuid}-proxy";
2023-11-09 13:59:38 +00:00
switch ($databaseType) {
case 'standalone-mariadb':
$type = 'App\Models\StandaloneMariadb';
$containerName = "mariadb-{$database->service->uuid}";
2023-11-09 13:59:38 +00:00
break;
case 'standalone-mongodb':
$type = 'App\Models\StandaloneMongodb';
$containerName = "mongodb-{$database->service->uuid}";
2023-11-09 13:59:38 +00:00
break;
case 'standalone-mysql':
$type = 'App\Models\StandaloneMysql';
$containerName = "mysql-{$database->service->uuid}";
2023-11-09 13:59:38 +00:00
break;
case 'standalone-postgresql':
$type = 'App\Models\StandalonePostgresql';
$containerName = "postgresql-{$database->service->uuid}";
2023-11-09 13:59:38 +00:00
break;
case 'standalone-redis':
$type = 'App\Models\StandaloneRedis';
$containerName = "redis-{$database->service->uuid}";
2023-11-09 13:59:38 +00:00
break;
}
}
if ($type === 'App\Models\StandaloneRedis') {
$internalPort = 6379;
2023-11-09 13:59:38 +00:00
} else if ($type === 'App\Models\StandalonePostgresql') {
$internalPort = 5432;
2023-11-09 13:59:38 +00:00
} else if ($type === 'App\Models\StandaloneMongodb') {
2023-10-19 11:32:03 +00:00
$internalPort = 27017;
2023-11-09 13:59:38 +00:00
} else if ($type === 'App\Models\StandaloneMysql') {
2023-10-24 12:31:28 +00:00
$internalPort = 3306;
2023-11-09 13:59:38 +00:00
} else if ($type === 'App\Models\StandaloneMariadb') {
2023-10-24 12:31:28 +00:00
$internalPort = 3306;
}
$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;
proxy_pass $containerName:$internalPort;
}
}
EOF;
$dockerfile = <<< EOF
FROM nginx:stable-alpine
COPY nginx.conf /etc/nginx/nginx.conf
EOF;
$docker_compose = [
'version' => '3.8',
'services' => [
$proxyContainerName => [
'build' => [
'context' => $configuration_dir,
'dockerfile' => 'Dockerfile',
],
'image' => "nginx:stable-alpine",
'container_name' => $proxyContainerName,
'restart' => RESTART_MODE,
'ports' => [
"$database->public_port:$database->public_port",
],
'networks' => [
2023-11-09 13:59:38 +00:00
$network,
],
'healthcheck' => [
'test' => [
'CMD-SHELL',
'stat /etc/nginx/nginx.conf || exit 1',
],
'interval' => '5s',
'timeout' => '5s',
'retries' => 3,
'start_period' => '1s'
],
]
],
'networks' => [
2023-11-09 13:59:38 +00:00
$network => [
'external' => true,
2023-11-09 13:59:38 +00:00
'name' => $network,
'attachable' => true,
]
]
];
$dockercompose_base64 = base64_encode(Yaml::dump($docker_compose, 4, 2));
$nginxconf_base64 = base64_encode($nginxconf);
$dockerfile_base64 = base64_encode($dockerfile);
instant_remote_process([
"mkdir -p $configuration_dir",
"echo '{$dockerfile_base64}' | base64 -d > $configuration_dir/Dockerfile",
"echo '{$nginxconf_base64}' | base64 -d > $configuration_dir/nginx.conf",
"echo '{$dockercompose_base64}' | base64 -d > $configuration_dir/docker-compose.yaml",
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);
}
}