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 @@ class DynamicConfigurationNavbar extends Component
{ {
$server = Server::ownedByCurrentTeam()->whereId($this->server_id)->first(); $server = Server::ownedByCurrentTeam()->whereId($this->server_id)->first();
$proxy_path = $server->proxyPath(); $proxy_path = $server->proxyPath();
$proxy_type = $server->proxyType();
$file = str_replace('|', '.', $fileName); $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); instant_remote_process(["rm -f {$proxy_path}/dynamic/{$file}"], $server);
if ($proxy_type === 'CADDY') {
$server->reloadCaddy();
}
$this->dispatch('success', 'File deleted.'); $this->dispatch('success', 'File deleted.');
$this->dispatch('loadDynamicConfigurations'); $this->dispatch('loadDynamicConfigurations');
$this->dispatch('refresh'); $this->dispatch('refresh');

View File

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

View File

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

View File

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

View File

@@ -19,7 +19,7 @@
Add</button> Add</button>
</x-slide-over> </x-slide-over>
</div> </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> </div>
<div wire:loading wire:target="loadDynamicConfigurations"> <div wire:loading wire:target="loadDynamicConfigurations">
@@ -29,7 +29,7 @@
@if ($contents?->isNotEmpty()) @if ($contents?->isNotEmpty())
@foreach ($contents as $fileName => $value) @foreach ($contents as $fileName => $value)
<div class="flex flex-col gap-2 py-2"> <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> <div>
<h3 class="text-white">File: {{ str_replace('|', '.', $fileName) }}</h3> <h3 class="text-white">File: {{ str_replace('|', '.', $fileName) }}</h3>
</div> </div>

View File

@@ -1,5 +1,5 @@
<form wire:submit.prevent="addDynamicConfiguration" class="flex flex-col gap-4"> <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.textarea id="value" label="Configuration" required rows="20" />
<x-forms.button type="submit" @click="slideOverOpen=false">Save</x-forms.button> <x-forms.button type="submit" @click="slideOverOpen=false">Save</x-forms.button>
</form> </form>