lasthourcloud/app/Actions/Proxy/InstallProxy.php

118 lines
3.8 KiB
PHP
Raw Normal View History

2023-05-03 08:27:44 +00:00
<?php
namespace App\Actions\Proxy;
use App\Enums\ActivityTypes;
2023-05-12 18:15:36 +00:00
use App\Enums\ProxyTypes;
2023-05-03 08:27:44 +00:00
use App\Models\Server;
2023-05-12 18:15:36 +00:00
use Spatie\Activitylog\Models\Activity;
2023-05-03 08:27:44 +00:00
use Symfony\Component\Yaml\Yaml;
class InstallProxy
{
2023-05-12 18:15:36 +00:00
public function __invoke(Server $server): Activity
2023-05-03 08:27:44 +00:00
{
$docker_compose_yml_base64 = base64_encode(
$this->getDockerComposeContents()
);
$env_file_base64 = base64_encode(
$this->getEnvContents()
);
$activity = remoteProcess([
2023-05-05 15:56:03 +00:00
"docker network ls --format '{{.Name}}' | grep '^coolify$' || docker network create coolify",
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);
2023-05-12 18:15:36 +00:00
// Persist to Database
$server->extra_attributes->proxy = ProxyTypes::TRAEFIK_V2->value;
$server->save();
2023-05-03 08:27:44 +00:00
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
: '.';
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);
}
}