2023-04-25 10:47:13 +02:00
< ? php
2023-12-07 19:06:32 +01:00
namespace App\Livewire\Server ;
2023-04-25 10:47:13 +02:00
2024-06-18 16:42:42 +02:00
use App\Actions\Server\StartSentinel ;
use App\Actions\Server\StopSentinel ;
use App\Jobs\PullSentinelImageJob ;
2023-04-25 10:47:13 +02:00
use App\Models\Server ;
use Livewire\Component ;
class Form extends Component
{
public Server $server ;
2024-06-10 20:43:34 +00:00
2023-10-09 11:00:18 +02:00
public bool $isValidConnection = false ;
2024-06-10 20:43:34 +00:00
2023-10-09 11:00:18 +02:00
public bool $isValidDocker = false ;
2024-06-10 20:43:34 +00:00
2023-10-09 11:00:18 +02:00
public ? string $wildcard_domain = null ;
2024-06-10 20:43:34 +00:00
2023-07-07 21:07:42 +02:00
public int $cleanup_after_percentage ;
2024-06-10 20:43:34 +00:00
2023-09-18 11:49:26 +02:00
public bool $dockerInstallationStarted = false ;
2024-06-10 20:43:34 +00:00
2024-02-15 14:14:11 +01:00
public bool $revalidate = false ;
2024-02-05 14:40:54 +01:00
2024-02-15 14:14:11 +01:00
protected $listeners = [ 'serverInstalled' , 'revalidate' => '$refresh' ];
2023-04-25 10:47:13 +02:00
protected $rules = [
2023-11-28 15:49:24 +01:00
'server.name' => 'required' ,
2023-04-25 10:47:13 +02:00
'server.description' => 'nullable' ,
2023-10-13 09:31:44 +02:00
'server.ip' => 'required' ,
2023-04-25 10:47:13 +02:00
'server.user' => 'required' ,
'server.port' => 'required' ,
2023-11-28 15:49:24 +01:00
'server.settings.is_cloudflare_tunnel' => 'required|boolean' ,
2023-06-15 13:51:31 +02:00
'server.settings.is_reachable' => 'required' ,
2023-11-29 10:06:52 +01:00
'server.settings.is_swarm_manager' => 'required|boolean' ,
2023-12-18 14:01:25 +01:00
'server.settings.is_swarm_worker' => 'required|boolean' ,
2024-01-16 15:19:14 +01:00
'server.settings.is_build_server' => 'required|boolean' ,
2024-01-25 11:57:47 +01:00
'server.settings.concurrent_builds' => 'required|integer|min:1' ,
2024-02-08 12:34:01 +01:00
'server.settings.dynamic_timeout' => 'required|integer|min:1' ,
2024-06-18 16:42:42 +02:00
'server.settings.is_metrics_enabled' => 'required|boolean' ,
'server.settings.metrics_token' => 'required' ,
'server.settings.metrics_refresh_rate_seconds' => 'required|integer|min:1' ,
'server.settings.metrics_history_days' => 'required|integer|min:1' ,
2023-06-23 14:20:47 +02:00
'wildcard_domain' => 'nullable|url' ,
2024-06-20 13:17:06 +02:00
'server.settings.is_server_api_enabled' => 'required|boolean' ,
2023-04-25 10:47:13 +02:00
];
2024-06-10 20:43:34 +00:00
2023-06-16 12:35:40 +02:00
protected $validationAttributes = [
2023-10-12 11:41:37 +02:00
'server.name' => 'Name' ,
'server.description' => 'Description' ,
2023-11-03 10:57:58 +01:00
'server.ip' => 'IP address/Domain' ,
2023-10-12 11:41:37 +02:00
'server.user' => 'User' ,
'server.port' => 'Port' ,
2023-09-23 13:34:40 +02:00
'server.settings.is_cloudflare_tunnel' => 'Cloudflare Tunnel' ,
2023-11-29 10:06:52 +01:00
'server.settings.is_reachable' => 'Is reachable' ,
'server.settings.is_swarm_manager' => 'Swarm Manager' ,
2023-12-18 14:01:25 +01:00
'server.settings.is_swarm_worker' => 'Swarm Worker' ,
2024-01-16 15:19:14 +01:00
'server.settings.is_build_server' => 'Build Server' ,
2024-01-24 11:12:23 +01:00
'server.settings.concurrent_builds' => 'Concurrent Builds' ,
2024-02-08 12:34:01 +01:00
'server.settings.dynamic_timeout' => 'Dynamic Timeout' ,
2024-06-18 16:42:42 +02:00
'server.settings.is_metrics_enabled' => 'Metrics' ,
'server.settings.metrics_token' => 'Metrics Token' ,
'server.settings.metrics_refresh_rate_seconds' => 'Metrics Interval' ,
'server.settings.metrics_history_days' => 'Metrics History' ,
2024-06-20 13:17:06 +02:00
'server.settings.is_server_api_enabled' => 'Server API' ,
2023-06-16 12:35:40 +02:00
];
2023-08-08 11:51:36 +02:00
2023-06-22 15:25:57 +02:00
public function mount ()
{
$this -> wildcard_domain = $this -> server -> settings -> wildcard_domain ;
2023-07-07 21:07:42 +02:00
$this -> cleanup_after_percentage = $this -> server -> settings -> cleanup_after_percentage ;
2023-06-22 15:25:57 +02:00
}
2024-06-10 20:43:34 +00:00
2024-02-05 14:40:54 +01:00
public function serverInstalled ()
2023-10-13 09:31:44 +02:00
{
2024-02-05 14:40:54 +01:00
$this -> server -> refresh ();
$this -> server -> settings -> refresh ();
2023-10-09 11:00:18 +02:00
}
2024-06-10 20:43:34 +00:00
2024-05-24 12:01:04 +02:00
public function updatedServerSettingsIsBuildServer ()
{
2024-06-18 16:42:42 +02:00
$this -> dispatch ( 'refreshServerShow' );
2024-05-24 12:01:04 +02:00
$this -> dispatch ( 'serverRefresh' );
$this -> dispatch ( 'proxyStatusUpdated' );
}
2024-06-10 20:43:34 +00:00
2024-06-20 13:17:06 +02:00
public function checkPortForServerApi ()
{
try {
if ( $this -> server -> settings -> is_server_api_enabled === true ) {
$this -> server -> checkServerApi ();
$this -> dispatch ( 'success' , 'Server API is reachable.' );
}
} catch ( \Throwable $e ) {
return handleError ( $e , $this );
}
}
2023-10-09 11:00:18 +02:00
public function instantSave ()
{
2023-11-28 15:49:24 +01:00
try {
refresh_server_connection ( $this -> server -> privateKey );
$this -> validateServer ( false );
$this -> server -> settings -> save ();
2024-06-18 16:42:42 +02:00
$this -> server -> save ();
2024-02-22 14:53:42 +01:00
$this -> dispatch ( 'success' , 'Server updated.' );
2024-06-18 16:42:42 +02:00
$this -> dispatch ( 'refreshServerShow' );
2024-06-20 13:17:06 +02:00
if ( $this -> server -> isSentinelEnabled ()) {
2024-06-18 16:42:42 +02:00
PullSentinelImageJob :: dispatchSync ( $this -> server );
2024-06-20 13:17:06 +02:00
ray ( 'Sentinel is enabled' );
if ( $this -> server -> settings -> isDirty ( 'is_metrics_enabled' )) {
$this -> dispatch ( 'reloadWindow' );
}
if ( $this -> server -> settings -> isDirty ( 'is_server_api_enabled' ) && $this -> server -> settings -> is_server_api_enabled === true ) {
ray ( 'Starting sentinel' );
}
2024-06-18 16:42:42 +02:00
} else {
2024-06-20 13:17:06 +02:00
ray ( 'Sentinel is not enabled' );
2024-06-18 16:42:42 +02:00
StopSentinel :: dispatch ( $this -> server );
}
2024-06-20 13:17:06 +02:00
// $this->checkPortForServerApi();
2024-06-18 16:42:42 +02:00
} catch ( \Throwable $e ) {
return handleError ( $e , $this );
}
}
2024-06-18 14:43:18 +00:00
public function restartSentinel ()
{
2024-06-18 16:42:42 +02:00
try {
$version = get_latest_sentinel_version ();
StartSentinel :: run ( $this -> server , $version , true );
$this -> dispatch ( 'success' , 'Sentinel restarted.' );
2023-11-28 15:49:24 +01:00
} catch ( \Throwable $e ) {
return handleError ( $e , $this );
}
2023-09-23 13:34:40 +02:00
}
2024-06-10 20:43:34 +00:00
2024-02-15 14:14:11 +01:00
public function revalidate ()
{
$this -> revalidate = true ;
2024-02-15 13:52:54 +01:00
}
2024-06-10 20:43:34 +00:00
2023-10-13 09:31:44 +02:00
public function checkLocalhostConnection ()
{
2024-04-10 15:00:46 +02:00
$this -> submit ();
2024-04-16 15:42:38 +02:00
[ 'uptime' => $uptime , 'error' => $error ] = $this -> server -> validateConnection ();
2023-10-11 13:30:36 +02:00
if ( $uptime ) {
2023-12-07 19:06:32 +01:00
$this -> dispatch ( 'success' , 'Server is reachable.' );
2023-10-11 13:30:36 +02:00
$this -> server -> settings -> is_reachable = true ;
2023-10-11 13:34:51 +02:00
$this -> server -> settings -> is_usable = true ;
2023-10-11 13:30:36 +02:00
$this -> server -> settings -> save ();
2024-05-03 10:54:46 +02:00
$this -> dispatch ( 'proxyStatusUpdated' );
2023-10-11 13:30:36 +02:00
} else {
2024-06-10 20:43:34 +00:00
$this -> dispatch ( '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 );
2023-10-11 13:30:36 +02:00
return ;
}
}
2024-06-10 20:43:34 +00:00
2023-10-09 11:00:18 +02:00
public function validateServer ( $install = true )
2023-04-26 15:38:50 +02:00
{
2024-02-22 11:28:45 +01:00
$this -> dispatch ( 'init' , $install );
2023-04-26 15:38:50 +02:00
}
2023-08-08 11:51:36 +02:00
2023-04-25 10:47:13 +02:00
public function submit ()
{
2024-06-10 20:43:34 +00:00
if ( isCloud () && ! isDev ()) {
2023-10-13 09:31:44 +02:00
$this -> validate ();
$this -> validate ([
2023-11-03 10:57:58 +01:00
'server.ip' => 'required' ,
2023-10-13 09:31:44 +02:00
]);
2023-10-13 09:34:11 +02:00
} else {
$this -> validate ();
2023-10-13 09:31:44 +02:00
}
2023-09-14 18:10:13 +02: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 19:06:32 +01:00
$this -> dispatch ( 'error' , 'IP address is already in use by another team.' );
2024-06-10 20:43:34 +00:00
2023-09-14 18:10:13 +02:00
return ;
}
2023-10-11 13:34:51 +02:00
refresh_server_connection ( $this -> server -> privateKey );
2023-06-22 15:25:57 +02:00
$this -> server -> settings -> wildcard_domain = $this -> wildcard_domain ;
2023-07-07 21:07:42 +02:00
$this -> server -> settings -> cleanup_after_percentage = $this -> cleanup_after_percentage ;
2023-06-22 15:25:57 +02:00
$this -> server -> settings -> save ();
2023-04-25 10:47:13 +02:00
$this -> server -> save ();
2024-02-22 14:53:42 +01:00
$this -> dispatch ( 'success' , 'Server updated.' );
2023-04-25 10:47:13 +02:00
}
2023-08-08 11:51:36 +02:00
}