From 7fe50a9a1fb43c5ac53ac18b9974129e5c04a1e1 Mon Sep 17 00:00:00 2001 From: Joao Patricio Date: Wed, 3 May 2023 09:27:44 +0100 Subject: [PATCH] wip --- app/Actions/Proxy/InstallProxy.php | 103 +++++++++++++++++++++++++++++ app/Http/Livewire/Server/Proxy.php | 3 +- 2 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 app/Actions/Proxy/InstallProxy.php diff --git a/app/Actions/Proxy/InstallProxy.php b/app/Actions/Proxy/InstallProxy.php new file mode 100644 index 000000000..6dba178d0 --- /dev/null +++ b/app/Actions/Proxy/InstallProxy.php @@ -0,0 +1,103 @@ +getDockerComposeContents() + ); + + $env_file_base64 = base64_encode( + $this->getEnvContents() + ); + + $activity = remoteProcess([ + 'mkdir -p proxy', + 'mkdir -p proxy/letsencrypt', + 'cd proxy', + "echo '$docker_compose_yml_base64' | base64 -d > docker-compose.yml", + "echo '$env_file_base64' | base64 -d > .env", + 'cat .env', + ], $server, ActivityTypes::INLINE->value); + + return $activity; + } + + protected function getDockerComposeContents() + { + return Yaml::dump($this->getComposeData()); + } + + /** + * @return array + */ + protected function getComposeData(): array + { + return [ + "version" => "3.7", + "networks" => [ + "coolify" => [ + "external" => true, + ], + ], + "services" => [ + "traefik" => [ + "image" => "traefik:v2.9", + "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", + "./letsencrypt:/letsencrypt", + "./traefik.auth:/auth/traefik.auth", + ], + "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) + ->map(fn($v, $k) => "{$k}={$v}") + ->push(PHP_EOL) + ->implode(PHP_EOL); + } +} diff --git a/app/Http/Livewire/Server/Proxy.php b/app/Http/Livewire/Server/Proxy.php index fe3ed74f3..bc4298287 100644 --- a/app/Http/Livewire/Server/Proxy.php +++ b/app/Http/Livewire/Server/Proxy.php @@ -2,6 +2,7 @@ namespace App\Http\Livewire\Server; +use App\Actions\Proxy\InstallProxy; use App\Enums\ActivityTypes; use App\Models\Server; use Livewire\Component; @@ -19,7 +20,7 @@ public function mount(Server $server) public function runInstallProxy() { - $activity = remoteProcess(['ls -alh'], $this->server, ActivityTypes::INLINE->value); + $activity = resolve(InstallProxy::class)($this->server); $this->emit('newMonitorActivity', $activity->id); }