2024-02-05 13:40:54 +00:00
< ? php
namespace App\Livewire\Server ;
2024-02-22 10:28:45 +00:00
use App\Actions\Proxy\CheckProxy ;
2024-02-12 10:46:36 +00:00
use App\Actions\Proxy\StartProxy ;
2024-02-05 13:40:54 +00:00
use App\Models\Server ;
use Livewire\Component ;
class ValidateAndInstall extends Component
{
public Server $server ;
public int $number_of_tries = 0 ;
2024-02-22 10:28:45 +00:00
public int $max_tries = 2 ;
2024-02-05 13:40:54 +00:00
public bool $install = true ;
public $uptime = null ;
public $supported_os_type = null ;
public $docker_installed = null ;
2024-02-11 14:32:58 +00:00
public $docker_compose_installed = null ;
2024-02-05 13:40:54 +00:00
public $docker_version = null ;
2024-02-12 10:46:36 +00:00
public $proxy_started = false ;
2024-02-05 13:40:54 +00:00
public $error = null ;
2024-02-15 13:14:11 +00:00
public bool $ask = false ;
2024-02-05 13:40:54 +00:00
2024-02-22 10:28:45 +00:00
protected $listeners = [
'init' ,
'validateConnection' ,
'validateOS' ,
'validateDockerEngine' ,
'validateDockerVersion' ,
'startProxy' ,
'refresh' => '$refresh' ,
];
2024-02-05 13:40:54 +00:00
2024-02-05 14:13:39 +00:00
public function init ( bool $install = true )
2024-02-05 13:40:54 +00:00
{
$this -> install = $install ;
$this -> uptime = null ;
$this -> supported_os_type = null ;
$this -> docker_installed = null ;
$this -> docker_version = null ;
2024-02-12 10:46:36 +00:00
$this -> docker_compose_installed = null ;
$this -> proxy_started = null ;
2024-02-05 14:13:39 +00:00
$this -> error = null ;
$this -> number_of_tries = 0 ;
2024-02-15 13:14:11 +00:00
if ( ! $this -> ask ) {
2024-02-22 10:28:45 +00:00
$this -> dispatch ( 'validateConnection' );
2024-02-15 13:14:11 +00:00
}
}
2024-02-22 10:28:45 +00:00
public function startValidatingAfterAsking ()
{
2024-02-15 13:14:11 +00:00
$this -> ask = false ;
$this -> init ();
2024-02-05 14:13:39 +00:00
}
2024-02-22 10:28:45 +00:00
public function startProxy ()
2024-02-05 14:13:39 +00:00
{
2024-02-05 13:40:54 +00:00
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' ) {
2024-02-12 10:46:36 +00:00
$this -> proxy_started = true ;
2024-02-22 10:28:45 +00:00
} else {
throw new \Exception ( " Proxy could not be started. " );
2024-02-12 10:46:36 +00:00
}
2024-02-22 10:28:45 +00:00
} else {
$this -> proxy_started = true ;
2024-02-05 13:40:54 +00:00
}
2024-02-22 10:28:45 +00:00
2024-02-05 13:40:54 +00:00
} catch ( \Throwable $e ) {
return handleError ( $e , $this );
}
}
public function validateConnection ()
{
$this -> uptime = $this -> server -> validateConnection ();
if ( ! $this -> uptime ) {
2024-02-05 14:13:39 +00:00
$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/server/openssh">documentation</a> for further help.' ;
2024-02-05 13:40:54 +00:00
return ;
}
2024-02-22 10:28:45 +00:00
$this -> dispatch ( 'validateOS' );
2024-02-05 13:40:54 +00:00
}
public function validateOS ()
{
$this -> supported_os_type = $this -> server -> validateOS ();
if ( ! $this -> supported_os_type ) {
2024-02-05 14:13:39 +00:00
$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-02-05 13:40:54 +00:00
return ;
}
2024-02-22 10:28:45 +00:00
$this -> dispatch ( 'validateDockerEngine' );
2024-02-05 13:40:54 +00:00
}
public function validateDockerEngine ()
{
$this -> docker_installed = $this -> server -> validateDockerEngine ();
2024-02-11 14:32:58 +00:00
$this -> docker_compose_installed = $this -> server -> validateDockerCompose ();
if ( ! $this -> docker_installed || ! $this -> docker_compose_installed ) {
2024-02-05 13:40:54 +00:00
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-02-05 14:13:39 +00:00
return ;
2024-02-05 13:40:54 +00:00
} else {
2024-02-22 10:28:45 +00:00
if ( $this -> number_of_tries == 0 ) {
$activity = $this -> server -> installDocker ();
$this -> number_of_tries ++ ;
$this -> dispatch ( 'newActivityMonitor' , $activity -> id , 'init' );
}
2024-02-05 13:40:54 +00:00
return ;
}
} else {
2024-02-05 14:13:39 +00:00
$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-02-05 13:40:54 +00:00
return ;
}
}
2024-02-22 10:28:45 +00:00
$this -> dispatch ( 'validateDockerVersion' );
2024-02-05 13:40:54 +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.' );
}
2024-02-05 13:40:54 +00:00
} else {
2024-02-22 10:28:45 +00:00
$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: <a target="_blank" class="underline" href="https://docs.docker.com/engine/install/#server">documentation</a>.' ;
return ;
}
2024-02-05 13:40:54 +00:00
}
2024-02-22 10:28:45 +00:00
$this -> dispatch ( 'startProxy' );
2024-02-05 13:40:54 +00:00
}
public function render ()
{
return view ( 'livewire.server.validate-and-install' );
}
}