lasthourcloud/app/Actions/Proxy/InstallProxy.php

113 lines
3.5 KiB
PHP
Raw Normal View History

2023-05-03 08:27:44 +00:00
<?php
namespace App\Actions\Proxy;
use App\Enums\ActivityTypes;
use App\Models\Server;
use Symfony\Component\Yaml\Yaml;
class InstallProxy
{
public function __invoke(Server $server)
{
$docker_compose_yml_base64 = base64_encode(
$this->getDockerComposeContents()
);
$env_file_base64 = base64_encode(
$this->getEnvContents()
);
$activity = remoteProcess([
2023-05-03 12:05:31 +00:00
'mkdir -p projects',
'mkdir -p projects/proxy',
'mkdir -p projects/proxy/letsencrypt',
'cd projects/proxy',
2023-05-03 08:27:44 +00:00
"echo '$docker_compose_yml_base64' | base64 -d > docker-compose.yml",
"echo '$env_file_base64' | base64 -d > .env",
2023-05-03 12:05:31 +00:00
'docker compose up -d --remove-orphans',
'docker ps',
2023-05-03 08:27:44 +00:00
], $server, ActivityTypes::INLINE->value);
return $activity;
}
protected function getDockerComposeContents()
{
return Yaml::dump($this->getComposeData());
}
/**
* @return array
*/
protected function getComposeData(): array
{
2023-05-03 12:05:31 +00:00
$cwd = config('app.env') === 'local'
2023-05-04 07:09:41 +00:00
? config('proxy.project_path_on_host') . '/_testing_hosts/host_2_proxy'
2023-05-03 12:05:31 +00:00
: '.';
ray($cwd);
2023-05-03 08:27:44 +00:00
return [
"version" => "3.7",
"networks" => [
"coolify" => [
"external" => true,
],
],
"services" => [
"traefik" => [
2023-05-04 06:33:06 +00:00
"container_name" => "coolify-proxy",
"image" => "traefik:v2.10",
2023-05-03 08:27:44 +00:00
"restart" => "always",
"extra_hosts" => [
"host.docker.internal:host-gateway",
],
"networks" => [
"coolify",
],
"ports" => [
"80:80",
"443:443",
"8080:8080",
],
"volumes" => [
"/var/run/docker.sock:/var/run/docker.sock:ro",
2023-05-03 12:05:31 +00:00
"{$cwd}/letsencrypt:/letsencrypt",
"{$cwd}/traefik.auth:/auth/traefik.auth",
2023-05-03 08:27:44 +00:00
],
"command" => [
"--api.dashboard=true",
"--api.insecure=true",
"--entrypoints.http.address=:80",
"--entrypoints.https.address=:443",
"--providers.docker=true",
"--providers.docker.exposedbydefault=false",
],
"labels" => [
"traefik.enable=true",
"traefik.http.routers.traefik.entrypoints=http",
'traefik.http.routers.traefik.rule=Host(`${TRAEFIK_DASHBOARD_HOST}`)',
"traefik.http.routers.traefik.service=api@internal",
"traefik.http.services.traefik.loadbalancer.server.port=8080",
"traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https",
],
],
],
];
}
protected function getEnvContents()
{
$data = [
'TRAEFIK_DASHBOARD_HOST' => '',
'LETS_ENCRYPT_EMAIL' => '',
];
return collect($data)
2023-05-03 19:35:34 +00:00
->map(fn ($v, $k) => "{$k}={$v}")
2023-05-03 08:27:44 +00:00
->push(PHP_EOL)
->implode(PHP_EOL);
}
}