lasthourcloud/app/Actions/Server/ConfigureCloudflared.php

52 lines
1.7 KiB
PHP
Raw Normal View History

2024-03-27 17:24:24 +00:00
<?php
namespace App\Actions\Server;
use App\Models\Server;
use Lorisleiva\Actions\Concerns\AsAction;
use Symfony\Component\Yaml\Yaml;
class ConfigureCloudflared
{
use AsAction;
2024-06-10 20:43:34 +00:00
2024-03-27 17:24:24 +00:00
public function handle(Server $server, string $cloudflare_token)
{
try {
$config = [
2024-06-10 20:43:34 +00:00
'services' => [
'coolify-cloudflared' => [
'container_name' => 'coolify-cloudflared',
'image' => 'cloudflare/cloudflared:latest',
'restart' => RESTART_MODE,
'network_mode' => 'host',
'command' => 'tunnel run',
'environment' => [
2024-03-27 17:24:24 +00:00
"TUNNEL_TOKEN={$cloudflare_token}",
],
],
],
];
$config = Yaml::dump($config, 12, 2);
$docker_compose_yml_base64 = base64_encode($config);
$commands = collect([
2024-06-10 20:43:34 +00:00
'mkdir -p /tmp/cloudflared',
'cd /tmp/cloudflared',
"echo '$docker_compose_yml_base64' | base64 -d | tee docker-compose.yml > /dev/null",
2024-06-10 20:43:34 +00:00
'docker compose pull',
'docker compose down -v --remove-orphans > /dev/null 2>&1',
'docker compose up -d --remove-orphans',
2024-03-27 17:24:24 +00:00
]);
instant_remote_process($commands, $server);
} catch (\Throwable $e) {
ray($e);
throw $e;
} finally {
$commands = collect([
2024-06-10 20:43:34 +00:00
'rm -fr /tmp/cloudflared',
]);
instant_remote_process($commands, $server);
2024-03-27 17:24:24 +00:00
}
}
}