fix: proxy configuration saving

This commit is contained in:
Andras Bacsai 2023-09-18 12:18:45 +02:00
parent 026f3fd72d
commit 61f58fa30f
9 changed files with 207 additions and 55 deletions

View File

@ -2,12 +2,14 @@
namespace App\Actions\Proxy;
use Lorisleiva\Actions\Concerns\AsAction;
use App\Models\Server;
use Illuminate\Support\Str;
class CheckConfigurationSync
class CheckConfiguration
{
public function __invoke(Server $server, bool $reset = false)
use AsAction;
public function handle(Server $server, bool $reset = false)
{
$proxy_path = get_proxy_path();
$proxy_configuration = instant_remote_process([

View File

@ -0,0 +1,27 @@
<?php
namespace App\Actions\Proxy;
use App\Models\Server;
use Illuminate\Support\Str;
use Lorisleiva\Actions\Concerns\AsAction;
class SaveConfiguration
{
use AsAction;
public function handle(Server $server)
{
$proxy_settings = CheckConfiguration::run($server, true);
$proxy_path = get_proxy_path();
$docker_compose_yml_base64 = base64_encode($proxy_settings);
$server->proxy->last_saved_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;
$server->save();
return instant_remote_process([
"mkdir -p $proxy_path",
"echo '$docker_compose_yml_base64' | base64 -d > $proxy_path/docker-compose.yml",
], $server);
}
}

View File

@ -1,29 +0,0 @@
<?php
namespace App\Actions\Proxy;
use App\Models\Server;
use Illuminate\Support\Str;
class SaveConfigurationSync
{
public function __invoke(Server $server)
{
try {
$proxy_settings = resolve(CheckConfigurationSync::class)($server, true);
$proxy_path = get_proxy_path();
$docker_compose_yml_base64 = base64_encode($proxy_settings);
$server->proxy->last_saved_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;
$server->save();
instant_remote_process([
"mkdir -p $proxy_path",
"echo '$docker_compose_yml_base64' | base64 -d > $proxy_path/docker-compose.yml",
], $server);
} catch (\Throwable $e) {
ray($e);
}
}
}

View File

@ -32,8 +32,10 @@ public function __invoke(Server $server, bool $async = true): Activity|string
return "docker network ls --format '{{.Name}}' | grep '^$network$' >/dev/null 2>&1 || docker network create --attachable $network > /dev/null 2>&1";
});
$configuration = resolve(CheckConfigurationSync::class)($server);
$configuration = CheckConfiguration::run($server);
if (!$configuration) {
throw new \Exception("Configuration is not synced");
}
$docker_compose_yml_base64 = base64_encode($configuration);
$server->proxy->last_applied_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;
$server->save();

View File

@ -2,9 +2,8 @@
namespace App\Http\Livewire\Server;
use App\Actions\Proxy\CheckConfigurationSync;
use App\Actions\Proxy\SaveConfigurationSync;
use App\Enums\ProxyTypes;
use App\Actions\Proxy\CheckConfiguration;
use App\Actions\Proxy\SaveConfiguration;
use App\Models\Server;
use Livewire\Component;
@ -48,8 +47,7 @@ public function select_proxy($proxy_type)
public function submit()
{
try {
resolve(SaveConfigurationSync::class)($this->server);
SaveConfiguration::run($this->server);
$this->server->proxy->redirect_url = $this->redirect_url;
$this->server->save();
@ -63,7 +61,7 @@ public function submit()
public function reset_proxy_configuration()
{
try {
$this->proxy_settings = resolve(CheckConfigurationSync::class)($this->server, true);
$this->proxy_settings = CheckConfiguration::run($this->server, true);
} catch (\Throwable $e) {
return handleError($e);
}
@ -72,8 +70,7 @@ public function reset_proxy_configuration()
public function loadProxyConfiguration()
{
try {
ray('loadProxyConfiguration');
$this->proxy_settings = resolve(CheckConfigurationSync::class)($this->server);
$this->proxy_settings = CheckConfiguration::run($this->server);
} catch (\Throwable $e) {
return handleError($e);
}

View File

@ -2,7 +2,7 @@
namespace App\Http\Livewire\Server\Proxy;
use App\Actions\Proxy\SaveConfigurationSync;
use App\Actions\Proxy\SaveConfiguration;
use App\Actions\Proxy\StartProxy;
use App\Models\Server;
use Livewire\Component;
@ -13,20 +13,25 @@ class Deploy extends Component
public $proxy_settings = null;
protected $listeners = ['proxyStatusUpdated'];
public function proxyStatusUpdated() {
public function proxyStatusUpdated()
{
$this->server->refresh();
}
public function startProxy()
{
try {
if (
$this->server->proxy->last_applied_settings &&
$this->server->proxy->last_saved_settings !== $this->server->proxy->last_applied_settings
) {
resolve(SaveConfigurationSync::class)($this->server);
SaveConfiguration::run($this->server);
}
$activity = resolve(StartProxy::class)($this->server);
$this->emit('newMonitorActivity', $activity->id);
} catch (\Throwable $e) {
return handleError($e);
}
}
public function stop()

View File

@ -96,7 +96,7 @@ function setup_default_redirect_404(string|null $redirect_url, Server $server)
$traefik_default_redirect_file = "$traefik_dynamic_conf_path/default_redirect_404.yaml";
ray($redirect_url);
if (empty($redirect_url)) {
remote_process([
instant_remote_process([
"rm -f $traefik_default_redirect_file",
], $server);
} else {
@ -157,7 +157,7 @@ function setup_default_redirect_404(string|null $redirect_url, Server $server)
$base64 = base64_encode($yaml);
ray("mkdir -p $traefik_dynamic_conf_path");
remote_process([
instant_remote_process([
"mkdir -p $traefik_dynamic_conf_path",
"echo '$base64' | base64 -d > $traefik_default_redirect_file",
], $server);

View File

@ -22,6 +22,7 @@
"lcobucci/jwt": "^5.0.0",
"league/flysystem-aws-s3-v3": "^3.0",
"livewire/livewire": "^v2.12.3",
"lorisleiva/laravel-actions": "^2.7",
"masmerise/livewire-toaster": "^1.2",
"nubs/random-name-generator": "^2.2",
"phpseclib/phpseclib": "~3.0",

151
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "cf138424c896f30b035bc8cdff63e8d1",
"content-hash": "de2c45be3f03d43430549d963778dc4a",
"packages": [
{
"name": "aws/aws-crt-php",
@ -3059,6 +3059,153 @@
],
"time": "2023-08-11T04:02:34+00:00"
},
{
"name": "lorisleiva/laravel-actions",
"version": "v2.7.1",
"source": {
"type": "git",
"url": "https://github.com/lorisleiva/laravel-actions.git",
"reference": "5250614fd6b77e8e2780be0206174e069e94661d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/lorisleiva/laravel-actions/zipball/5250614fd6b77e8e2780be0206174e069e94661d",
"reference": "5250614fd6b77e8e2780be0206174e069e94661d",
"shasum": ""
},
"require": {
"illuminate/contracts": "9.0 - 9.34 || ^9.36 || ^10.0",
"lorisleiva/lody": "^0.4",
"php": "^8.0"
},
"require-dev": {
"orchestra/testbench": "^8.5",
"pestphp/pest": "^1.23",
"phpunit/phpunit": "^9.6"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Lorisleiva\\Actions\\ActionServiceProvider"
],
"aliases": {
"Action": "Lorisleiva\\Actions\\Facades\\Actions"
}
}
},
"autoload": {
"psr-4": {
"Lorisleiva\\Actions\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Loris Leiva",
"email": "loris.leiva@gmail.com",
"homepage": "https://lorisleiva.com",
"role": "Developer"
}
],
"description": "Laravel components that take care of one specific task",
"homepage": "https://github.com/lorisleiva/laravel-actions",
"keywords": [
"action",
"command",
"component",
"controller",
"job",
"laravel",
"object"
],
"support": {
"issues": "https://github.com/lorisleiva/laravel-actions/issues",
"source": "https://github.com/lorisleiva/laravel-actions/tree/v2.7.1"
},
"funding": [
{
"url": "https://github.com/sponsors/lorisleiva",
"type": "github"
}
],
"time": "2023-08-24T10:20:57+00:00"
},
{
"name": "lorisleiva/lody",
"version": "v0.4.0",
"source": {
"type": "git",
"url": "https://github.com/lorisleiva/lody.git",
"reference": "1a43e8e423f3b2b64119542bc44a2071208fae16"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/lorisleiva/lody/zipball/1a43e8e423f3b2b64119542bc44a2071208fae16",
"reference": "1a43e8e423f3b2b64119542bc44a2071208fae16",
"shasum": ""
},
"require": {
"illuminate/contracts": "^8.0|^9.0|^10.0",
"php": "^8.0"
},
"require-dev": {
"orchestra/testbench": "^8.0",
"pestphp/pest": "^1.20.0",
"phpunit/phpunit": "^9.5.10"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Lorisleiva\\Lody\\LodyServiceProvider"
],
"aliases": {
"Lody": "Lorisleiva\\Lody\\Lody"
}
}
},
"autoload": {
"psr-4": {
"Lorisleiva\\Lody\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Loris Leiva",
"email": "loris.leiva@gmail.com",
"homepage": "https://lorisleiva.com",
"role": "Developer"
}
],
"description": "Load files and classes as lazy collections in Laravel.",
"homepage": "https://github.com/lorisleiva/lody",
"keywords": [
"classes",
"collection",
"files",
"laravel",
"load"
],
"support": {
"issues": "https://github.com/lorisleiva/lody/issues",
"source": "https://github.com/lorisleiva/lody/tree/v0.4.0"
},
"funding": [
{
"url": "https://github.com/sponsors/lorisleiva",
"type": "github"
}
],
"time": "2023-02-05T15:03:45+00:00"
},
{
"name": "masmerise/livewire-toaster",
"version": "1.3.0",
@ -13089,5 +13236,5 @@
"php": "^8.2"
},
"platform-dev": [],
"plugin-api-version": "2.3.0"
"plugin-api-version": "2.6.0"
}