lasthourcloud/app/Livewire/Server/ValidateAndInstall.php

167 lines
5.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Livewire\Server;
2024-02-22 10:28:45 +00:00
use App\Actions\Proxy\CheckProxy;
use App\Actions\Proxy\StartProxy;
use App\Models\Server;
use Livewire\Component;
class ValidateAndInstall extends Component
{
public Server $server;
2024-06-10 20:43:34 +00:00
public int $number_of_tries = 0;
2024-06-10 20:43:34 +00:00
2024-04-16 13:42:38 +00:00
public int $max_tries = 3;
2024-06-10 20:43:34 +00:00
public bool $install = true;
2024-06-10 20:43:34 +00:00
public $uptime = null;
2024-06-10 20:43:34 +00:00
public $supported_os_type = null;
2024-06-10 20:43:34 +00:00
public $docker_installed = null;
2024-06-10 20:43:34 +00:00
public $docker_compose_installed = null;
2024-06-10 20:43:34 +00:00
public $docker_version = null;
2024-06-10 20:43:34 +00:00
public $proxy_started = false;
2024-06-10 20:43:34 +00:00
public $error = null;
2024-06-10 20:43:34 +00:00
public bool $ask = false;
2024-02-22 10:28:45 +00:00
protected $listeners = [
'init',
'validateConnection',
'validateOS',
'validateDockerEngine',
'validateDockerVersion',
'startProxy',
'refresh' => '$refresh',
];
2024-04-16 13:42:38 +00:00
public function init(int $data = 0)
{
$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;
2024-04-16 13:42:38 +00:00
$this->number_of_tries = $data;
2024-06-10 20:43:34 +00:00
if (! $this->ask) {
2024-02-22 10:28:45 +00:00
$this->dispatch('validateConnection');
}
}
2024-06-10 20:43:34 +00:00
2024-02-22 10:28:45 +00:00
public function startValidatingAfterAsking()
{
$this->ask = false;
$this->init();
}
2024-06-10 20:43:34 +00:00
2024-02-22 10:28:45 +00:00
public function startProxy()
{
try {
2024-02-22 10:28:45 +00:00
$shouldStart = CheckProxy::run($this->server);
if ($shouldStart) {
$proxy = StartProxy::run($this->server, false);
if ($proxy === 'OK') {
$this->proxy_started = true;
2024-02-22 10:28:45 +00:00
} else {
2024-06-10 20:43:34 +00:00
throw new \Exception('Proxy could not be started.');
}
2024-02-22 10:28:45 +00:00
} else {
$this->proxy_started = true;
}
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
2024-06-10 20:43:34 +00:00
public function validateConnection()
{
2024-04-16 13:42:38 +00:00
['uptime' => $this->uptime, 'error' => $error] = $this->server->validateConnection();
2024-06-10 20:43:34 +00:00
if (! $this->uptime) {
$this->error = 'Server is not reachable. Please validate your configuration and connection.<br><br>Check this <a target="_blank" class="underline" href="https://coolify.io/docs/knowledge-base/server/openssh">documentation</a> for further help. <br><br>Error: '.$error;
return;
}
2024-02-22 10:28:45 +00:00
$this->dispatch('validateOS');
}
2024-06-10 20:43:34 +00:00
public function validateOS()
{
$this->supported_os_type = $this->server->validateOS();
2024-06-10 20:43:34 +00:00
if (! $this->supported_os_type) {
$this->error = 'Server OS type is not supported. Please install Docker manually before continuing: <a target="_blank" class="underline" href="https://docs.docker.com/engine/install/#server">documentation</a>.';
2024-06-10 20:43:34 +00:00
return;
}
2024-02-22 10:28:45 +00:00
$this->dispatch('validateDockerEngine');
}
2024-06-10 20:43:34 +00:00
public function validateDockerEngine()
{
$this->docker_installed = $this->server->validateDockerEngine();
$this->docker_compose_installed = $this->server->validateDockerCompose();
2024-06-10 20:43:34 +00:00
if (! $this->docker_installed || ! $this->docker_compose_installed) {
if ($this->install) {
if ($this->number_of_tries == $this->max_tries) {
$this->error = 'Docker Engine could not be installed. Please install Docker manually before continuing: <a target="_blank" class="underline" href="https://docs.docker.com/engine/install/#server">documentation</a>.';
2024-06-10 20:43:34 +00:00
return;
} else {
2024-04-16 13:42:38 +00:00
if ($this->number_of_tries <= $this->max_tries) {
2024-02-22 10:28:45 +00:00
$activity = $this->server->installDocker();
$this->number_of_tries++;
2024-04-16 13:42:38 +00:00
$this->dispatch('newActivityMonitor', $activity->id, 'init', $this->number_of_tries);
2024-02-22 10:28:45 +00:00
}
2024-06-10 20:43:34 +00:00
return;
}
} else {
$this->error = 'Docker Engine is not installed. Please install Docker manually before continuing: <a target="_blank" class="underline" href="https://docs.docker.com/engine/install/#server">documentation</a>.';
2024-06-10 20:43:34 +00:00
return;
}
}
2024-02-22 10:28:45 +00:00
$this->dispatch('validateDockerVersion');
}
2024-06-10 20:43:34 +00:00
public function validateDockerVersion()
{
2024-02-22 10:28:45 +00:00
if ($this->server->isSwarm()) {
$swarmInstalled = $this->server->validateDockerSwarm();
if ($swarmInstalled) {
$this->dispatch('success', 'Docker Swarm is initiated.');
}
} else {
2024-02-22 10:28:45 +00:00
$this->docker_version = $this->server->validateDockerEngineVersion();
if ($this->docker_version) {
2024-06-18 14:42:42 +00:00
$this->dispatch('refreshServerShow');
$this->dispatch('refreshBoardingIndex');
2024-02-22 13:53:42 +00:00
$this->dispatch('success', 'Server validated.');
2024-02-22 10:28:45 +00:00
} else {
$this->error = 'Docker Engine version is not 22+. Please install Docker manually before continuing: <a target="_blank" class="underline" href="https://docs.docker.com/engine/install/#server">documentation</a>.';
2024-06-10 20:43:34 +00:00
2024-02-22 10:28:45 +00:00
return;
}
}
2024-02-22 10:28:45 +00:00
if ($this->server->isBuildServer()) {
return;
}
2024-02-22 10:28:45 +00:00
$this->dispatch('startProxy');
}
2024-06-10 20:43:34 +00:00
public function render()
{
return view('livewire.server.validate-and-install');
}
}