feat: dynamic configuration for caddy

This commit is contained in:
Andras Bacsai 2024-03-11 17:31:28 +01:00
parent 9bdad6bb67
commit 1490828069
6 changed files with 46 additions and 19 deletions

View File

@ -15,8 +15,16 @@ public function delete(string $fileName)
{
$server = Server::ownedByCurrentTeam()->whereId($this->server_id)->first();
$proxy_path = $server->proxyPath();
$proxy_type = $server->proxyType();
$file = str_replace('|', '.', $fileName);
if ($proxy_type === 'CADDY' && $file === "Caddyfile") {
$this->dispatch('error', 'Cannot delete Caddyfile.');
return;
}
instant_remote_process(["rm -f {$proxy_path}/dynamic/{$file}"], $server);
if ($proxy_type === 'CADDY') {
$server->reloadCaddy();
}
$this->dispatch('success', 'File deleted.');
$this->dispatch('loadDynamicConfigurations');
$this->dispatch('refresh');

View File

@ -29,7 +29,6 @@ public function addDynamicConfiguration()
'fileName' => 'required',
'value' => 'required',
]);
if (data_get($this->parameters, 'server_uuid')) {
$this->server = Server::ownedByCurrentTeam()->whereUuid(data_get($this->parameters, 'server_uuid'))->first();
}
@ -39,6 +38,8 @@ public function addDynamicConfiguration()
if (is_null($this->server)) {
return redirect()->route('server.index');
}
$proxy_type = $this->server->proxyType();
if ($proxy_type === 'TRAEFIK_V2') {
if (!str($this->fileName)->endsWith('.yaml') && !str($this->fileName)->endsWith('.yml')) {
$this->fileName = "{$this->fileName}.yaml";
}
@ -46,7 +47,12 @@ public function addDynamicConfiguration()
$this->dispatch('error', 'File name is reserved.');
return;
}
$proxy_path = $this->proxyPath();
} else if ($proxy_type === 'CADDY') {
if (!str($this->fileName)->endsWith('.caddy')) {
$this->fileName = "{$this->fileName}.caddy";
}
}
$proxy_path = $this->server->proxyPath();
$file = "{$proxy_path}/dynamic/{$this->fileName}";
if ($this->newFile) {
$exists = instant_remote_process(["test -f $file && echo 1 || echo 0"], $this->server);
@ -55,11 +61,18 @@ public function addDynamicConfiguration()
return;
}
}
if ($proxy_type === 'TRAEFIK_V2') {
$yaml = Yaml::parse($this->value);
$yaml = Yaml::dump($yaml, 10, 2);
$this->value = $yaml;
}
$base64_value = base64_encode($this->value);
instant_remote_process(["echo '{$base64_value}' | base64 -d > {$file}"], $this->server);
instant_remote_process([
"echo '{$base64_value}' | base64 -d > {$file}",
], $this->server);
if ($proxy_type === 'CADDY') {
$this->server->reloadCaddy();
}
$this->dispatch('loadDynamicConfigurations');
$this->dispatch('dynamic-configuration-added');
$this->dispatch('success', 'Dynamic configuration saved.');

View File

@ -249,8 +249,9 @@ public function setupDynamicProxyConfiguration()
if (empty($settings->fqdn)) {
instant_remote_process([
"rm -f $file",
"docker exec coolify-proxy caddy reload --config /dynamic/Caddyfile",
], $this);
$this->reloadCaddy();
} else {
$url = Url::fromString($settings->fqdn);
$host = $url->getHost();
@ -262,12 +263,17 @@ public function setupDynamicProxyConfiguration()
$base64 = base64_encode($caddy_file);
instant_remote_process([
"echo '$base64' | base64 -d > $file",
], $this);
$this->reloadCaddy();
}
}
}
}
public function reloadCaddy() {
return instant_remote_process([
"docker exec coolify-proxy caddy reload --config /dynamic/Caddyfile",
], $this);
}
}
}
}
public function proxyPath()
{
$base_path = config('coolify.base_config_path');

View File

@ -5,12 +5,12 @@
<button>Configuration</button>
</a>
@if ($server->proxyType() !== 'NONE')
@if ($server->proxyType() === 'TRAEFIK_V2')
{{-- @if ($server->proxyType() === 'TRAEFIK_V2') --}}
<a class="{{ request()->routeIs('server.proxy.dynamic-confs') ? 'text-white' : '' }}"
href="{{ route('server.proxy.dynamic-confs', $parameters) }}">
<button>Dynamic Configurations</button>
</a>
@endif
{{-- @endif --}}
<a class="{{ request()->routeIs('server.proxy.logs') ? 'text-white' : '' }}"
href="{{ route('server.proxy.logs', $parameters) }}">
<button>Logs</button>

View File

@ -19,7 +19,7 @@ class="font-normal text-white normal-case border-none rounded btn btn-primary bt
Add</button>
</x-slide-over>
</div>
<div class='pb-4'>You can add dynamic Traefik configurations here.</div>
<div class='pb-4'>You can add dynamic proxy configurations here.</div>
</div>
</div>
<div wire:loading wire:target="loadDynamicConfigurations">
@ -29,7 +29,7 @@ class="font-normal text-white normal-case border-none rounded btn btn-primary bt
@if ($contents?->isNotEmpty())
@foreach ($contents as $fileName => $value)
<div class="flex flex-col gap-2 py-2">
@if (str_replace('|', '.', $fileName) === 'coolify.yaml')
@if (str_replace('|', '.', $fileName) === 'coolify.yaml' ||str_replace('|', '.', $fileName) === 'Caddyfile' )
<div>
<h3 class="text-white">File: {{ str_replace('|', '.', $fileName) }}</h3>
</div>

View File

@ -1,5 +1,5 @@
<form wire:submit.prevent="addDynamicConfiguration" class="flex flex-col gap-4">
<x-forms.input id="fileName" label="Filename (.yaml or .yml)" required />
<x-forms.input id="fileName" label="Filename" required />
<x-forms.textarea id="value" label="Configuration" required rows="20" />
<x-forms.button type="submit" @click="slideOverOpen=false">Save</x-forms.button>
</form>