This commit is contained in:
Joao Patricio 2023-05-03 09:27:44 +01:00
parent 1f5345dfdb
commit 7fe50a9a1f
2 changed files with 105 additions and 1 deletions

View File

@ -0,0 +1,103 @@
<?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([
'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);
}
}

View File

@ -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);
}