Skip to content

Commit

Permalink
fix: proxy configuration saving
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasbacsai committed Sep 18, 2023
1 parent 026f3fd commit 61f58fa
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
27 changes: 27 additions & 0 deletions app/Actions/Proxy/SaveConfiguration.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
29 changes: 0 additions & 29 deletions app/Actions/Proxy/SaveConfigurationSync.php

This file was deleted.

6 changes: 4 additions & 2 deletions app/Actions/Proxy/StartProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
13 changes: 5 additions & 8 deletions app/Http/Livewire/Server/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();

Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
25 changes: 15 additions & 10 deletions app/Http/Livewire/Server/Proxy/Deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
{
if (
$this->server->proxy->last_applied_settings &&
$this->server->proxy->last_saved_settings !== $this->server->proxy->last_applied_settings
) {
resolve(SaveConfigurationSync::class)($this->server);
}
try {
if (
$this->server->proxy->last_applied_settings &&
$this->server->proxy->last_saved_settings !== $this->server->proxy->last_applied_settings
) {
SaveConfiguration::run($this->server);
}

$activity = resolve(StartProxy::class)($this->server);
$this->emit('newMonitorActivity', $activity->id);
$activity = resolve(StartProxy::class)($this->server);
$this->emit('newMonitorActivity', $activity->id);
} catch (\Throwable $e) {
return handleError($e);
}
}

public function stop()
Expand Down
4 changes: 2 additions & 2 deletions bootstrap/helpers/proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
151 changes: 149 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 61f58fa

Please sign in to comment.