coolify/app/Http/Livewire/Server/Proxy.php

92 lines
2.9 KiB
PHP
Raw Normal View History

2023-05-03 07:23:45 +02:00
<?php
namespace App\Http\Livewire\Server;
2023-05-12 20:42:39 +02:00
use App\Actions\Proxy\CheckProxySettingsInSync;
2023-05-03 10:27:44 +02:00
use App\Actions\Proxy\InstallProxy;
2023-05-15 13:45:37 +02:00
use App\Enums\ProxyTypes;
use Illuminate\Support\Str;
2023-05-03 07:23:45 +02:00
use App\Models\Server;
use Livewire\Component;
class Proxy extends Component
{
2023-05-15 21:14:45 +02:00
protected $listeners = ['serverValidated'];
2023-05-03 07:23:45 +02:00
public Server $server;
2023-05-15 13:45:37 +02:00
public ProxyTypes $selectedProxy = ProxyTypes::TRAEFIK_V2;
public $proxy_settings = null;
2023-05-03 07:23:45 +02:00
2023-05-16 11:39:18 +02:00
public function mount()
{
$this->proxyStatus();
}
2023-05-15 21:14:45 +02:00
public function serverValidated()
{
$this->server->settings->refresh();
}
2023-05-15 13:45:37 +02:00
public function installProxy()
2023-05-03 07:23:45 +02:00
{
2023-05-16 11:39:18 +02:00
if (
$this->server->extra_attributes->last_applied_proxy_settings &&
$this->server->extra_attributes->last_saved_proxy_settings !== $this->server->extra_attributes->last_applied_proxy_settings
) {
$this->saveConfiguration($this->server);
}
2023-05-03 10:27:44 +02:00
$activity = resolve(InstallProxy::class)($this->server);
2023-05-03 07:23:45 +02:00
$this->emit('newMonitorActivity', $activity->id);
}
2023-05-15 13:45:37 +02:00
public function proxyStatus()
2023-05-12 20:15:36 +02:00
{
2023-05-24 14:26:50 +02:00
$this->server->extra_attributes->proxy_status = get_container_status(server: $this->server, container_id: 'coolify-proxy');
2023-05-15 13:45:37 +02:00
$this->server->save();
2023-05-16 11:39:18 +02:00
$this->server->refresh();
2023-05-12 20:15:36 +02:00
}
2023-05-15 13:45:37 +02:00
public function setProxy()
{
$this->server->extra_attributes->proxy_type = $this->selectedProxy->value;
$this->server->extra_attributes->proxy_status = 'exited';
$this->server->save();
}
public function stopProxy()
{
instantRemoteProcess([
"docker rm -f coolify-proxy",
], $this->server);
$this->server->extra_attributes->proxy_status = 'exited';
$this->server->save();
}
public function saveConfiguration()
{
try {
$proxy_path = config('coolify.proxy_config_path');
2023-05-15 21:48:36 +02:00
$this->proxy_settings = Str::of($this->proxy_settings)->trim()->value;
2023-05-15 13:45:37 +02:00
$docker_compose_yml_base64 = base64_encode($this->proxy_settings);
$this->server->extra_attributes->last_saved_proxy_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;
$this->server->save();
instantRemoteProcess([
"echo '$docker_compose_yml_base64' | base64 -d > $proxy_path/docker-compose.yml",
], $this->server);
} catch (\Exception $e) {
2023-05-24 14:26:50 +02:00
return general_error_handler($e);
2023-05-15 13:45:37 +02:00
}
}
2023-05-15 22:17:31 +02:00
public function resetProxy()
2023-05-03 07:23:45 +02:00
{
2023-05-15 13:45:37 +02:00
try {
2023-05-15 22:06:08 +02:00
$this->proxy_settings = resolve(CheckProxySettingsInSync::class)($this->server, true);
2023-05-15 13:45:37 +02:00
} catch (\Exception $e) {
2023-05-24 14:26:50 +02:00
return general_error_handler($e);
2023-05-15 13:45:37 +02:00
}
2023-05-03 07:23:45 +02:00
}
2023-05-15 22:17:31 +02:00
public function checkProxySettingsInSync()
{
try {
$this->proxy_settings = resolve(CheckProxySettingsInSync::class)($this->server);
} catch (\Exception $e) {
2023-05-24 14:26:50 +02:00
return general_error_handler($e);
2023-05-15 22:17:31 +02:00
}
}
2023-05-03 07:23:45 +02:00
}