2023-04-25 08:47:13 +00:00
< ? php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Server ;
2023-04-25 08:47:13 +00:00
2023-05-04 07:11:11 +00:00
use App\Actions\Server\InstallDocker ;
2023-04-25 08:47:13 +00:00
use App\Models\Server ;
use Livewire\Component ;
class Form extends Component
{
public Server $server ;
2023-10-09 09:00:18 +00:00
public bool $isValidConnection = false ;
public bool $isValidDocker = false ;
public ? string $wildcard_domain = null ;
2023-07-07 19:07:42 +00:00
public int $cleanup_after_percentage ;
2023-09-18 09:49:26 +00:00
public bool $dockerInstallationStarted = false ;
2023-10-09 09:00:18 +00:00
protected $listeners = [ 'serverRefresh' ];
2023-04-25 08:47:13 +00:00
protected $rules = [
2023-11-28 14:49:24 +00:00
'server.name' => 'required' ,
2023-04-25 08:47:13 +00:00
'server.description' => 'nullable' ,
2023-10-13 07:31:44 +00:00
'server.ip' => 'required' ,
2023-04-25 08:47:13 +00:00
'server.user' => 'required' ,
'server.port' => 'required' ,
2023-11-28 14:49:24 +00:00
'server.settings.is_cloudflare_tunnel' => 'required|boolean' ,
2023-06-15 11:51:31 +00:00
'server.settings.is_reachable' => 'required' ,
2023-11-29 09:06:52 +00:00
'server.settings.is_swarm_manager' => 'required|boolean' ,
2023-12-18 13:01:25 +00:00
'server.settings.is_swarm_worker' => 'required|boolean' ,
2023-06-23 12:20:47 +00:00
'wildcard_domain' => 'nullable|url' ,
2023-04-25 08:47:13 +00:00
];
2023-06-16 10:35:40 +00:00
protected $validationAttributes = [
2023-10-12 09:41:37 +00:00
'server.name' => 'Name' ,
'server.description' => 'Description' ,
2023-11-03 09:57:58 +00:00
'server.ip' => 'IP address/Domain' ,
2023-10-12 09:41:37 +00:00
'server.user' => 'User' ,
'server.port' => 'Port' ,
2023-09-23 11:34:40 +00:00
'server.settings.is_cloudflare_tunnel' => 'Cloudflare Tunnel' ,
2023-11-29 09:06:52 +00:00
'server.settings.is_reachable' => 'Is reachable' ,
'server.settings.is_swarm_manager' => 'Swarm Manager' ,
2023-12-18 13:01:25 +00:00
'server.settings.is_swarm_worker' => 'Swarm Worker' ,
2023-06-16 10:35:40 +00:00
];
2023-08-08 09:51:36 +00:00
2023-06-22 13:25:57 +00:00
public function mount ()
{
$this -> wildcard_domain = $this -> server -> settings -> wildcard_domain ;
2023-07-07 19:07:42 +00:00
$this -> cleanup_after_percentage = $this -> server -> settings -> cleanup_after_percentage ;
2023-06-22 13:25:57 +00:00
}
2023-11-21 10:39:19 +00:00
public function serverRefresh ( $install = true )
2023-10-13 07:31:44 +00:00
{
2023-11-21 10:39:19 +00:00
$this -> validateServer ( $install );
2023-10-09 09:00:18 +00:00
}
public function instantSave ()
{
2023-11-28 14:49:24 +00:00
try {
refresh_server_connection ( $this -> server -> privateKey );
$this -> validateServer ( false );
$this -> server -> settings -> save ();
2023-12-07 18:06:32 +00:00
$this -> dispatch ( 'success' , 'Server updated successfully.' );
2023-11-28 14:49:24 +00:00
} catch ( \Throwable $e ) {
return handleError ( $e , $this );
}
2023-09-23 11:34:40 +00:00
}
2023-11-21 11:07:06 +00:00
public function installDocker ()
2023-05-02 13:20:45 +00:00
{
2023-12-07 18:06:32 +00:00
$this -> dispatch ( 'installDocker' );
2023-09-18 09:49:26 +00:00
$this -> dockerInstallationStarted = true ;
2023-11-21 11:07:06 +00:00
$activity = InstallDocker :: run ( $this -> server );
2023-12-07 18:06:32 +00:00
$this -> dispatch ( 'newMonitorActivity' , $activity -> id );
2023-05-02 13:20:45 +00:00
}
2023-10-13 07:31:44 +00:00
public function checkLocalhostConnection ()
{
2023-10-11 11:30:36 +00:00
$uptime = $this -> server -> validateConnection ();
if ( $uptime ) {
2023-12-07 18:06:32 +00:00
$this -> dispatch ( 'success' , 'Server is reachable.' );
2023-10-11 11:30:36 +00:00
$this -> server -> settings -> is_reachable = true ;
2023-10-11 11:34:51 +00:00
$this -> server -> settings -> is_usable = true ;
2023-10-11 11:30:36 +00:00
$this -> server -> settings -> save ();
} else {
2023-12-07 18:06:32 +00:00
$this -> dispatch ( 'error' , 'Server is not reachable. Please check your connection and configuration.' );
2023-10-11 11:30:36 +00:00
return ;
}
}
2023-10-09 09:00:18 +00:00
public function validateServer ( $install = true )
2023-04-26 13:38:50 +00:00
{
2023-05-03 07:38:53 +00:00
try {
2023-10-09 09:00:18 +00:00
$uptime = $this -> server -> validateConnection ();
2023-11-21 10:39:19 +00:00
if ( ! $uptime ) {
2023-12-07 18:06:32 +00:00
$install && $this -> dispatch ( 'error' , 'Server is not reachable. Please check your connection and configuration.' );
2023-09-11 15:19:30 +00:00
return ;
2023-05-03 07:43:01 +00:00
}
2023-11-21 10:39:19 +00:00
$supported_os_type = $this -> server -> validateOS ();
if ( ! $supported_os_type ) {
2023-12-07 18:06:32 +00:00
$install && $this -> dispatch ( 'error' , 'Server OS type is not supported for automated installation. Please install Docker manually before continuing: <a target="_blank" class="underline" href="https://coolify.io/docs/servers#install-docker-engine-manually">documentation</a>.' );
2023-11-21 10:39:19 +00:00
return ;
}
2023-10-09 09:00:18 +00:00
$dockerInstalled = $this -> server -> validateDockerEngine ();
if ( $dockerInstalled ) {
2023-12-07 18:06:32 +00:00
$install && $this -> dispatch ( 'success' , 'Docker Engine is installed.<br> Checking version.' );
2023-10-09 09:00:18 +00:00
} else {
2023-11-21 11:07:06 +00:00
$install && $this -> installDocker ();
2023-10-09 09:00:18 +00:00
return ;
}
$dockerVersion = $this -> server -> validateDockerEngineVersion ();
2023-07-25 12:43:49 +00:00
if ( $dockerVersion ) {
2023-12-07 18:06:32 +00:00
$install && $this -> dispatch ( 'success' , 'Docker Engine version is 22+.' );
2023-09-09 13:30:46 +00:00
} else {
2023-11-21 11:07:06 +00:00
$install && $this -> installDocker ();
2023-10-09 09:00:18 +00:00
return ;
2023-05-03 07:43:01 +00:00
}
2023-11-28 14:49:24 +00:00
if ( $this -> server -> isSwarm ()) {
$swarmInstalled = $this -> server -> validateDockerSwarm ();
if ( $swarmInstalled ) {
2023-12-07 18:06:32 +00:00
$install && $this -> dispatch ( 'success' , 'Docker Swarm is initiated.' );
2023-11-28 14:49:24 +00:00
}
}
2023-09-11 15:36:30 +00:00
} catch ( \Throwable $e ) {
2023-10-09 09:00:18 +00:00
return handleError ( $e , $this );
2023-09-24 08:48:54 +00:00
} finally {
2023-12-07 18:06:32 +00:00
$this -> dispatch ( 'proxyStatusUpdated' );
2023-05-03 07:38:53 +00:00
}
2023-04-26 13:38:50 +00:00
}
2023-08-08 09:51:36 +00:00
2023-04-25 08:47:13 +00:00
public function submit ()
{
2023-11-06 11:31:02 +00:00
if ( isCloud () && ! isDev ()) {
2023-10-13 07:31:44 +00:00
$this -> validate ();
$this -> validate ([
2023-11-03 09:57:58 +00:00
'server.ip' => 'required' ,
2023-10-13 07:31:44 +00:00
]);
2023-10-13 07:34:11 +00:00
} else {
$this -> validate ();
2023-10-13 07:31:44 +00:00
}
2023-09-14 16:10:13 +00:00
$uniqueIPs = Server :: all () -> reject ( function ( Server $server ) {
return $server -> id === $this -> server -> id ;
}) -> pluck ( 'ip' ) -> toArray ();
if ( in_array ( $this -> server -> ip , $uniqueIPs )) {
2023-12-07 18:06:32 +00:00
$this -> dispatch ( 'error' , 'IP address is already in use by another team.' );
2023-09-14 16:10:13 +00:00
return ;
}
2023-10-11 11:34:51 +00:00
refresh_server_connection ( $this -> server -> privateKey );
2023-06-22 13:25:57 +00:00
$this -> server -> settings -> wildcard_domain = $this -> wildcard_domain ;
2023-07-07 19:07:42 +00:00
$this -> server -> settings -> cleanup_after_percentage = $this -> cleanup_after_percentage ;
2023-06-22 13:25:57 +00:00
$this -> server -> settings -> save ();
2023-04-25 08:47:13 +00:00
$this -> server -> save ();
2023-12-07 18:06:32 +00:00
$this -> dispatch ( 'success' , 'Server updated successfully.' );
2023-04-25 08:47:13 +00:00
}
2023-08-08 09:51:36 +00:00
}