lasthourcloud/app/Livewire/Server/Proxy/NewDynamicConfiguration.php

97 lines
3.2 KiB
PHP
Raw Normal View History

<?php
namespace App\Livewire\Server\Proxy;
use App\Models\Server;
use Livewire\Component;
use Symfony\Component\Yaml\Yaml;
class NewDynamicConfiguration extends Component
{
public string $fileName = '';
2024-06-10 20:43:34 +00:00
public string $value = '';
2024-06-10 20:43:34 +00:00
public bool $newFile = false;
2024-06-10 20:43:34 +00:00
public Server $server;
2024-06-10 20:43:34 +00:00
public $server_id;
2024-06-10 20:43:34 +00:00
public $parameters = [];
2024-06-10 20:43:34 +00:00
public function mount()
{
$this->parameters = get_route_parameters();
if ($this->fileName !== '') {
$this->fileName = str_replace('|', '.', $this->fileName);
}
}
2024-06-10 20:43:34 +00:00
public function addDynamicConfiguration()
{
try {
$this->validate([
'fileName' => 'required',
'value' => 'required',
]);
if (data_get($this->parameters, 'server_uuid')) {
$this->server = Server::ownedByCurrentTeam()->whereUuid(data_get($this->parameters, 'server_uuid'))->first();
}
2024-06-10 20:43:34 +00:00
if (! is_null($this->server_id)) {
$this->server = Server::ownedByCurrentTeam()->whereId($this->server_id)->first();
}
if (is_null($this->server)) {
return redirect()->route('server.index');
}
2024-03-11 16:31:28 +00:00
$proxy_type = $this->server->proxyType();
if ($proxy_type === 'TRAEFIK_V2') {
2024-06-10 20:43:34 +00:00
if (! str($this->fileName)->endsWith('.yaml') && ! str($this->fileName)->endsWith('.yml')) {
2024-03-11 16:31:28 +00:00
$this->fileName = "{$this->fileName}.yaml";
}
if ($this->fileName === 'coolify.yaml') {
$this->dispatch('error', 'File name is reserved.');
2024-06-10 20:43:34 +00:00
2024-03-11 16:31:28 +00:00
return;
}
2024-06-10 20:43:34 +00:00
} elseif ($proxy_type === 'CADDY') {
if (! str($this->fileName)->endsWith('.caddy')) {
2024-03-11 16:31:28 +00:00
$this->fileName = "{$this->fileName}.caddy";
}
}
2024-03-11 16:31:28 +00:00
$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);
if ($exists == 1) {
2024-02-22 13:53:42 +00:00
$this->dispatch('error', 'File already exists');
2024-06-10 20:43:34 +00:00
return;
}
}
2024-03-11 16:31:28 +00:00
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);
2024-03-11 16:31:28 +00:00
instant_remote_process([
"echo '{$base64_value}' | base64 -d | tee {$file} > /dev/null",
2024-03-11 16:31:28 +00:00
], $this->server);
if ($proxy_type === 'CADDY') {
$this->server->reloadCaddy();
2024-03-11 16:31:28 +00:00
}
$this->dispatch('loadDynamicConfigurations');
$this->dispatch('dynamic-configuration-added');
2024-02-22 13:53:42 +00:00
$this->dispatch('success', 'Dynamic configuration saved.');
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
2024-06-10 20:43:34 +00:00
public function render()
{
return view('livewire.server.proxy.new-dynamic-configuration');
}
}