2023-05-03 08:27:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Actions\Proxy;
|
|
|
|
|
|
|
|
use App\Enums\ActivityTypes;
|
2023-05-16 13:10:29 +00:00
|
|
|
use App\Models\InstanceSettings;
|
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-15 11:45:37 +00:00
|
|
|
use Illuminate\Support\Str;
|
2023-05-16 13:10:29 +00:00
|
|
|
use Spatie\Url\Url;
|
2023-05-03 08:27:44 +00:00
|
|
|
|
|
|
|
class InstallProxy
|
|
|
|
{
|
2023-05-12 18:15:36 +00:00
|
|
|
public function __invoke(Server $server): Activity
|
2023-05-03 08:27:44 +00:00
|
|
|
{
|
2023-05-15 11:45:37 +00:00
|
|
|
$proxy_path = config('coolify.proxy_config_path');
|
|
|
|
|
|
|
|
$networks = collect($server->standaloneDockers)->map(function ($docker) {
|
|
|
|
return $docker['network'];
|
|
|
|
})->unique();
|
|
|
|
if ($networks->count() === 0) {
|
2023-05-15 13:13:34 +00:00
|
|
|
$networks = collect(['coolify']);
|
2023-05-15 11:45:37 +00:00
|
|
|
}
|
2023-05-15 13:13:34 +00:00
|
|
|
$create_networks_command = $networks->map(function ($network) {
|
2023-05-15 11:45:37 +00:00
|
|
|
return "docker network ls --format '{{.Name}}' | grep '^$network$' >/dev/null 2>&1 || docker network create --attachable $network > /dev/null 2>&1";
|
|
|
|
});
|
|
|
|
|
|
|
|
$configuration = instantRemoteProcess([
|
|
|
|
"cat $proxy_path/docker-compose.yml",
|
|
|
|
], $server, false);
|
|
|
|
if (is_null($configuration)) {
|
2023-05-15 19:48:36 +00:00
|
|
|
$configuration = Str::of(getProxyConfiguration($server))->trim()->value;
|
2023-05-15 11:45:37 +00:00
|
|
|
} else {
|
2023-05-15 19:48:36 +00:00
|
|
|
$configuration = Str::of($configuration)->trim()->value;
|
2023-05-15 11:45:37 +00:00
|
|
|
}
|
|
|
|
$docker_compose_yml_base64 = base64_encode($configuration);
|
|
|
|
$server->extra_attributes->last_applied_proxy_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;
|
|
|
|
$server->save();
|
2023-05-03 08:27:44 +00:00
|
|
|
|
|
|
|
$env_file_base64 = base64_encode(
|
|
|
|
$this->getEnvContents()
|
|
|
|
);
|
|
|
|
$activity = remoteProcess([
|
2023-05-15 11:45:37 +00:00
|
|
|
...$create_networks_command,
|
|
|
|
"echo 'Docker networks created...'",
|
|
|
|
"mkdir -p $proxy_path",
|
|
|
|
"cd $proxy_path",
|
|
|
|
"echo '$docker_compose_yml_base64' | base64 -d > $proxy_path/docker-compose.yml",
|
|
|
|
"echo '$env_file_base64' | base64 -d > $proxy_path/.env",
|
|
|
|
"echo 'Docker compose file created...'",
|
|
|
|
"echo 'Pulling docker image...'",
|
|
|
|
'docker compose pull -q',
|
|
|
|
"echo 'Stopping proxy...'",
|
|
|
|
'docker compose down -v --remove-orphans',
|
|
|
|
"echo 'Starting proxy...'",
|
2023-05-03 12:05:31 +00:00
|
|
|
'docker compose up -d --remove-orphans',
|
2023-05-15 11:45:37 +00:00
|
|
|
"echo 'Proxy installed successfully...'"
|
2023-05-03 08:27:44 +00:00
|
|
|
], $server, ActivityTypes::INLINE->value);
|
|
|
|
|
|
|
|
return $activity;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getEnvContents()
|
|
|
|
{
|
2023-05-16 15:09:50 +00:00
|
|
|
$instance_fqdn = InstanceSettings::get()->fqdn ?? config('app.url');
|
2023-05-16 13:10:29 +00:00
|
|
|
$url = Url::fromString($instance_fqdn);
|
2023-05-03 08:27:44 +00:00
|
|
|
$data = [
|
2023-05-16 13:10:29 +00:00
|
|
|
'TRAEFIK_DASHBOARD_HOST' => $url->getHost(),
|
2023-05-03 08:27:44 +00:00
|
|
|
'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);
|
|
|
|
}
|
|
|
|
}
|