From 9eda2eeae754919458c41192867a5447752642f4 Mon Sep 17 00:00:00 2001
From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com>
Date: Wed, 27 Nov 2024 17:48:54 +0100
Subject: [PATCH] use the new functions in validate and install
---
app/Livewire/Server/ValidateAndInstall.php | 302 ++++++++++++---------
1 file changed, 170 insertions(+), 132 deletions(-)
diff --git a/app/Livewire/Server/ValidateAndInstall.php b/app/Livewire/Server/ValidateAndInstall.php
index 791ef9350..9aa3842a4 100644
--- a/app/Livewire/Server/ValidateAndInstall.php
+++ b/app/Livewire/Server/ValidateAndInstall.php
@@ -4,6 +4,8 @@ namespace App\Livewire\Server;
use App\Actions\Proxy\CheckProxy;
use App\Actions\Proxy\StartProxy;
+use App\Actions\Server\InstallCoolifyDependencies;
+use App\Actions\Server\ValidateServer;
use App\Models\Server;
use Livewire\Component;
@@ -11,57 +13,183 @@ class ValidateAndInstall extends Component
{
public Server $server;
- public int $number_of_tries = 0;
+ public ?string $error = null;
- public int $max_tries = 3;
+ public ?string $supported_os_type = null;
+
+ public bool $docker_installed = false;
+
+ public bool $docker_compose_installed = false;
+
+ public bool $docker_version_valid = false;
+
+ public bool $proxy_started = false;
public bool $install = true;
- public $uptime = null;
+ // Step tracking
+ public string $currentStep = 'connection';
- public $supported_os_type = null;
-
- public $docker_installed = null;
-
- public $docker_compose_installed = null;
-
- public $docker_version = null;
-
- public $proxy_started = false;
-
- public $error = null;
-
- public bool $ask = false;
-
- protected $listeners = [
- 'init',
- 'validateConnection',
- 'validateOS',
- 'validateDockerEngine',
- 'validateDockerVersion',
- 'startProxy',
- 'refresh' => '$refresh',
+ public array $steps = [
+ 'connection' => [
+ 'label' => 'Connection Check',
+ 'completed' => false,
+ ],
+ 'os' => [
+ 'label' => 'OS Validation',
+ 'completed' => false,
+ ],
+ 'docker' => [
+ 'label' => 'Docker Engine',
+ 'completed' => false,
+ ],
+ 'compose' => [
+ 'label' => 'Docker Compose',
+ 'completed' => false,
+ ],
+ 'dependencies' => [
+ 'label' => 'Dependencies',
+ 'completed' => false,
+ ],
+ 'proxy' => [
+ 'label' => 'Proxy',
+ 'completed' => false,
+ ],
];
- public function init(int $data = 0)
+ public function mount()
{
- $this->uptime = null;
- $this->supported_os_type = null;
- $this->docker_installed = null;
- $this->docker_version = null;
- $this->docker_compose_installed = null;
- $this->proxy_started = null;
- $this->error = null;
- $this->number_of_tries = $data;
- if (! $this->ask) {
- $this->dispatch('validateConnection');
+ $this->currentStep = 'connection';
+ }
+
+ public function validateConnection()
+ {
+ ['uptime' => $uptime, 'error' => $error] = (new ValidateServer)->validateConnection($this->server);
+
+ if (! $uptime) {
+ $this->error = $error;
+
+ return;
+ }
+
+ $this->steps['connection']['completed'] = true;
+ $this->currentStep = 'os';
+ $this->validateOS();
+ }
+
+ public function validateOS()
+ {
+ $result = (new ValidateServer)->validateOS($this->server);
+
+ if (! $result['supported']) {
+ $this->error = $result['error'];
+
+ return;
+ }
+
+ $this->supported_os_type = $result['os_type'];
+ $this->steps['os']['completed'] = true;
+ $this->currentStep = 'docker';
+ $this->validateDockerEngine();
+ }
+
+ public function validateDockerEngine()
+ {
+ $result = (new ValidateServer)->validateDockerEngine($this->server);
+ $this->docker_installed = $result['installed'];
+
+ if (! $this->docker_installed) {
+ if ($this->install) {
+ $this->currentStep = 'dependencies';
+ $this->installDependencies();
+
+ return;
+ }
+ $this->error = $result['error'];
+
+ return;
+ }
+
+ $this->steps['docker']['completed'] = true;
+ $this->currentStep = 'compose';
+ $this->validateDockerCompose();
+ }
+
+ public function validateDockerCompose()
+ {
+ $result = (new ValidateServer)->validateDockerCompose($this->server);
+ $this->docker_compose_installed = $result['installed'];
+
+ if (! $this->docker_compose_installed) {
+ $this->error = $result['error'];
+
+ return;
+ }
+
+ $this->steps['compose']['completed'] = true;
+ $this->validateDockerVersion();
+ }
+
+ protected function installDependencies()
+ {
+ try {
+ InstallCoolifyDependencies::run($this->server, $this->supported_os_type);
+ $result = (new ValidateServer)->validateDockerEngine($this->server);
+ if (! $result['installed']) {
+ $this->error = __('server.docker_install_failed');
+ $this->server->update(['validation_logs' => $this->error]);
+
+ return;
+ }
+ $this->docker_installed = true;
+ $this->steps['dependencies']['completed'] = true;
+ $this->currentStep = 'compose';
+ $this->validateDockerCompose();
+ } catch (\Exception $e) {
+ $this->error = __('server.dependency_install_failed', ['error' => $e->getMessage()]);
+ $this->server->update(['validation_logs' => $this->error]);
}
}
- public function startValidatingAfterAsking()
+ public function validateDockerVersion()
{
- $this->ask = false;
- $this->init();
+ if ($this->server->isSwarm()) {
+ $this->validateSwarmSetup();
+ } else {
+ $this->validateStandardSetup();
+ }
+ }
+
+ protected function validateSwarmSetup()
+ {
+ try {
+ $swarmInstalled = (new ValidateServer)->validateDockerSwarm($this->server);
+ if ($swarmInstalled) {
+ $this->currentStep = 'proxy';
+ $this->startProxy();
+ }
+ } catch (\Exception $e) {
+ $this->error = $e->getMessage();
+ }
+ }
+
+ protected function validateStandardSetup()
+ {
+ $result = (new ValidateServer)->validateDockerEngineVersion($this->server);
+ $this->docker_version_valid = $result['valid'];
+
+ if (! $this->docker_version_valid) {
+ $requiredDockerVersion = str(config('constants.docker.minimum_required_version'))->before('.');
+ $this->error = __('server.docker_version_not_supported', ['version' => $requiredDockerVersion]);
+ $this->server->update(['validation_logs' => $this->error]);
+
+ return;
+ }
+
+ if (! $this->server->isBuildServer()) {
+ $this->currentStep = 'proxy';
+ $this->startProxy();
+ }
}
public function startProxy()
@@ -72,109 +200,19 @@ class ValidateAndInstall extends Component
$proxy = StartProxy::run($this->server, false);
if ($proxy === 'OK') {
$this->proxy_started = true;
+ $this->steps['proxy']['completed'] = true;
} else {
- throw new \Exception('Proxy could not be started.');
+ throw new \Exception(__('server.proxy_start_failed'));
}
} else {
$this->proxy_started = true;
+ $this->steps['proxy']['completed'] = true;
}
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
- public function validateConnection()
- {
- ['uptime' => $this->uptime, 'error' => $error] = $this->server->validateConnection();
- if (! $this->uptime) {
- $this->error = 'Server is not reachable. Please validate your configuration and connection.
Check this documentation for further help.