From 4ae2087c2e0eca2655818acebccba4c8967d0bd8 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Thu, 22 Feb 2024 11:28:45 +0100 Subject: [PATCH] fix: server validation --- app/Livewire/Server/Form.php | 2 +- app/Livewire/Server/ValidateAndInstall.php | 75 ++++++++++++------- app/Models/Server.php | 14 +++- .../server/validate-and-install.blade.php | 41 +++++----- 4 files changed, 81 insertions(+), 51 deletions(-) diff --git a/app/Livewire/Server/Form.php b/app/Livewire/Server/Form.php index 239fa86bd..75e65c319 100644 --- a/app/Livewire/Server/Form.php +++ b/app/Livewire/Server/Form.php @@ -88,7 +88,7 @@ class Form extends Component } public function validateServer($install = true) { - $this->dispatch('validateServer', $install); + $this->dispatch('init', $install); } public function submit() diff --git a/app/Livewire/Server/ValidateAndInstall.php b/app/Livewire/Server/ValidateAndInstall.php index b5c46fa32..bc2af95d2 100644 --- a/app/Livewire/Server/ValidateAndInstall.php +++ b/app/Livewire/Server/ValidateAndInstall.php @@ -2,6 +2,7 @@ namespace App\Livewire\Server; +use App\Actions\Proxy\CheckProxy; use App\Actions\Proxy\StartProxy; use App\Models\Server; use Livewire\Component; @@ -10,7 +11,7 @@ class ValidateAndInstall extends Component { public Server $server; public int $number_of_tries = 0; - public int $max_tries = 1; + public int $max_tries = 2; public bool $install = true; public $uptime = null; public $supported_os_type = null; @@ -21,7 +22,15 @@ class ValidateAndInstall extends Component public $error = null; public bool $ask = false; - protected $listeners = ['validateServer' => 'init', 'validateDockerEngine', 'validateServerNow' => 'validateServer']; + protected $listeners = [ + 'init', + 'validateConnection', + 'validateOS', + 'validateDockerEngine', + 'validateDockerVersion', + 'startProxy', + 'refresh' => '$refresh', + ]; public function init(bool $install = true) { @@ -35,31 +44,29 @@ class ValidateAndInstall extends Component $this->error = null; $this->number_of_tries = 0; if (!$this->ask) { - $this->dispatch('validateServerNow'); + $this->dispatch('validateConnection'); } } - public function startValidatingAfterAsking() { + public function startValidatingAfterAsking() + { $this->ask = false; $this->init(); } - public function validateServer() + public function startProxy() { try { - $this->validateConnection(); - $this->validateOS(); - $this->validateDockerEngine(); - - if ($this->server->isSwarm()) { - $swarmInstalled = $this->server->validateDockerSwarm(); - if ($swarmInstalled) { - $this->dispatch('success', 'Docker Swarm is initiated.'); + $shouldStart = CheckProxy::run($this->server); + if ($shouldStart) { + $proxy = StartProxy::run($this->server, false); + if ($proxy === 'OK') { + $this->proxy_started = true; + } else { + throw new \Exception("Proxy could not be started."); } } else { - $proxy = StartProxy::run($this->server); - if ($proxy) { - $this->proxy_started = true; - } + $this->proxy_started = true; } + } catch (\Throwable $e) { return handleError($e, $this); } @@ -71,6 +78,7 @@ class ValidateAndInstall extends Component $this->error = 'Server is not reachable. Please validate your configuration and connection.

Check this documentation for further help.'; return; } + $this->dispatch('validateOS'); } public function validateOS() { @@ -79,6 +87,7 @@ class ValidateAndInstall extends Component $this->error = 'Server OS type is not supported. Please install Docker manually before continuing: documentation.'; return; } + $this->dispatch('validateDockerEngine'); } public function validateDockerEngine() { @@ -90,29 +99,39 @@ class ValidateAndInstall extends Component $this->error = 'Docker Engine could not be installed. Please install Docker manually before continuing: documentation.'; return; } else { - $activity = $this->server->installDocker(); - $this->number_of_tries++; - $this->dispatch('newActivityMonitor', $activity->id, 'validateDockerEngine'); + if ($this->number_of_tries == 0 ) { + $activity = $this->server->installDocker(); + $this->number_of_tries++; + $this->dispatch('newActivityMonitor', $activity->id, 'init'); + } return; } } else { $this->error = 'Docker Engine is not installed. Please install Docker manually before continuing: documentation.'; return; } - } else { - $this->validateDockerVersion(); } + $this->dispatch('validateDockerVersion'); } public function validateDockerVersion() { - $this->docker_version = $this->server->validateDockerEngineVersion(); - if ($this->docker_version) { - $this->dispatch('serverInstalled'); - $this->dispatch('success', 'Server validated successfully.'); + if ($this->server->isSwarm()) { + $swarmInstalled = $this->server->validateDockerSwarm(); + if ($swarmInstalled) { + $this->dispatch('success', 'Docker Swarm is initiated.'); + } } else { - $this->error = 'Docker Engine version is not 22+. Please install Docker manually before continuing: documentation.'; - return; + $this->docker_version = $this->server->validateDockerEngineVersion(); + if ($this->docker_version) { + $this->dispatch('serverInstalled'); + $this->dispatch('success', 'Server validated successfully.'); + } else { + $this->error = 'Docker Engine version is not 22+. Please install Docker manually before continuing: documentation.'; + return; + } } + + $this->dispatch('startProxy'); } public function render() { diff --git a/app/Models/Server.php b/app/Models/Server.php index 76ed8c8a9..27f0432a3 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -398,10 +398,10 @@ class Server extends BaseModel } }); if ($supported->count() === 1) { - ray('supported'); + // ray('supported'); return str($supported->first()); } else { - ray('not supported'); + // ray('not supported'); return false; } } @@ -468,6 +468,16 @@ class Server extends BaseModel } return false; } + try { + $dockerRunning = instant_remote_process(["docker version"], $this); + } catch (\Throwable $e) { + $this->settings->is_usable = false; + $this->settings->save(); + if ($throwError) { + throw new \Exception('Server is not usable. Docker Engine is not running.'); + } + return false; + } $this->settings->is_usable = true; $this->settings->save(); $this->validateCoolifyNetwork(isSwarm: false, isBuildServer: $this->settings->is_build_server); diff --git a/resources/views/livewire/server/validate-and-install.blade.php b/resources/views/livewire/server/validate-and-install.blade.php index 35d5ab06a..8d50c3608 100644 --- a/resources/views/livewire/server/validate-and-install.blade.php +++ b/resources/views/livewire/server/validate-and-install.blade.php @@ -3,7 +3,7 @@ This will revalidate the server, install / update Docker Engine, Docker Compose and all related configuration. It will also restart Docker Engine, so your running containers will be unreachable for the time being. - Continue + Continue @else @if ($uptime)
Server is reachable:
@else @if ($error) -
Server is reachable: Supported OS type:
@else -
+
@endif @endif @endif @@ -85,8 +85,22 @@ d="m243.28 68.24l-24-23.56a16 16 0 0 0-22.58 0L104 136l-.11-.11l-36.64-35.27a16 16 0 0 0-22.57.06l-24 24a16 16 0 0 0 0 22.61l71.62 72a16 16 0 0 0 22.63 0l128.4-128.38a16 16 0 0 0-.05-22.67M103.62 208L32 136l24-24l.11.11l36.64 35.27a16 16 0 0 0 22.52 0L208.06 56L232 79.6Z" /> + @isset($docker_version) +
Minimum Docker version: + + + + +
+ @else +
+ @endisset @if ($proxy_started) -
Proxy Started: Proxy started:
@else @if ($error) -
Proxy Started: Proxy started:
@else -
+
@endif @endif @else @@ -120,20 +134,7 @@ @endif @endif - @isset($docker_version) -
Minimum Docker version installed: - - - - -
- @endisset - - + @isset($error)
{!! $error !!}
@endisset