2023-08-29 12:36:17 +00:00
< ? php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Server ;
2023-08-29 12:36:17 +00:00
2024-11-01 22:48:46 +00:00
use App\Actions\Server\StartSentinel ;
use App\Actions\Server\StopSentinel ;
2024-12-16 13:06:16 +00:00
use App\Events\ServerReachabilityChanged ;
2023-08-29 12:36:17 +00:00
use App\Models\Server ;
2024-12-03 21:24:36 +00:00
use Livewire\Attributes\Computed ;
2025-02-18 15:44:16 +00:00
use Livewire\Attributes\Locked ;
2024-11-05 08:36:40 +00:00
use Livewire\Attributes\Validate ;
2024-10-17 20:00:27 +00:00
use Livewire\Component ;
2023-08-29 12:36:17 +00:00
2024-10-17 20:00:27 +00:00
class Show extends Component
2023-08-29 12:36:17 +00:00
{
2024-10-17 19:48:43 +00:00
public Server $server ;
2024-06-10 20:43:34 +00:00
2024-11-05 08:36:40 +00:00
#[Validate(['required'])]
2024-10-30 13:54:27 +00:00
public string $name ;
2024-11-05 08:36:40 +00:00
#[Validate(['nullable'])]
2024-11-08 10:45:56 +00:00
public ? string $description = null ;
2024-10-30 13:54:27 +00:00
2024-11-05 08:36:40 +00:00
#[Validate(['required'])]
2024-10-30 13:54:27 +00:00
public string $ip ;
2024-11-05 08:36:40 +00:00
#[Validate(['required'])]
2024-10-30 13:54:27 +00:00
public string $user ;
2024-11-05 08:36:40 +00:00
#[Validate(['required'])]
2024-10-30 13:54:27 +00:00
public string $port ;
2024-11-05 08:36:40 +00:00
#[Validate(['nullable'])]
2024-10-30 13:54:27 +00:00
public ? string $validationLogs = null ;
2024-11-05 08:36:40 +00:00
#[Validate(['nullable', 'url'])]
2024-11-08 10:45:56 +00:00
public ? string $wildcardDomain = null ;
2024-10-30 13:54:27 +00:00
2024-11-05 08:36:40 +00:00
#[Validate(['required'])]
2024-10-30 13:54:27 +00:00
public bool $isReachable ;
2024-11-05 08:36:40 +00:00
#[Validate(['required'])]
2024-10-30 13:54:27 +00:00
public bool $isUsable ;
2024-11-05 08:36:40 +00:00
#[Validate(['required'])]
2024-10-30 13:54:27 +00:00
public bool $isSwarmManager ;
2024-11-05 08:36:40 +00:00
#[Validate(['required'])]
2024-10-30 13:54:27 +00:00
public bool $isSwarmWorker ;
2024-11-05 08:36:40 +00:00
#[Validate(['required'])]
2024-10-30 13:54:27 +00:00
public bool $isBuildServer ;
2025-02-18 15:44:16 +00:00
#[Locked]
public bool $isBuildServerLocked = false ;
2024-11-05 08:36:40 +00:00
#[Validate(['required'])]
2024-10-30 13:54:27 +00:00
public bool $isMetricsEnabled ;
2024-11-05 08:36:40 +00:00
#[Validate(['required'])]
2024-10-30 13:54:27 +00:00
public string $sentinelToken ;
2024-11-05 08:36:40 +00:00
#[Validate(['nullable'])]
2024-11-08 10:45:56 +00:00
public ? string $sentinelUpdatedAt = null ;
2024-10-30 13:54:27 +00:00
2024-11-05 08:36:40 +00:00
#[Validate(['required', 'integer', 'min:1'])]
2024-10-30 13:54:27 +00:00
public int $sentinelMetricsRefreshRateSeconds ;
2024-11-05 08:36:40 +00:00
#[Validate(['required', 'integer', 'min:1'])]
2024-10-30 13:54:27 +00:00
public int $sentinelMetricsHistoryDays ;
2024-11-05 08:36:40 +00:00
#[Validate(['required', 'integer', 'min:10'])]
2024-10-30 13:54:27 +00:00
public int $sentinelPushIntervalSeconds ;
2024-11-05 08:36:40 +00:00
#[Validate(['nullable', 'url'])]
2024-11-08 10:45:56 +00:00
public ? string $sentinelCustomUrl = null ;
2024-10-30 13:54:27 +00:00
2024-11-05 08:36:40 +00:00
#[Validate(['required'])]
2024-10-30 13:54:27 +00:00
public bool $isSentinelEnabled ;
2024-11-05 08:36:40 +00:00
#[Validate(['required'])]
2024-10-30 13:54:27 +00:00
public bool $isSentinelDebugEnabled ;
2024-11-05 08:36:40 +00:00
#[Validate(['required'])]
2024-10-30 13:54:27 +00:00
public string $serverTimezone ;
public function getListeners ()
{
$teamId = auth () -> user () -> currentTeam () -> id ;
return [
2024-11-05 10:52:14 +00:00
" echo-private:team. { $teamId } ,CloudflareTunnelConfigured " => 'refresh' ,
'refreshServerShow' => 'refresh' ,
2024-10-30 13:54:27 +00:00
];
}
public function mount ( string $server_uuid )
{
try {
$this -> server = Server :: ownedByCurrentTeam () -> whereUuid ( $server_uuid ) -> firstOrFail ();
$this -> syncData ();
2025-02-18 15:44:16 +00:00
if ( ! $this -> server -> isEmpty ()) {
$this -> isBuildServerLocked = true ;
}
2025-01-07 14:31:43 +00:00
} catch ( \Throwable $e ) {
2024-10-30 13:54:27 +00:00
return handleError ( $e , $this );
}
}
2024-12-03 21:24:36 +00:00
#[Computed]
public function timezones () : array
{
return collect ( timezone_identifiers_list ())
-> sort ()
-> values ()
-> toArray ();
}
2024-10-30 13:54:27 +00:00
public function syncData ( bool $toModel = false )
{
if ( $toModel ) {
$this -> validate ();
2024-11-14 16:48:35 +00:00
2025-01-07 14:31:43 +00:00
if ( Server :: where ( 'team_id' , currentTeam () -> id )
2024-11-14 16:48:35 +00:00
-> where ( 'ip' , $this -> ip )
-> where ( 'id' , '!=' , $this -> server -> id )
-> exists ()) {
$this -> ip = $this -> server -> ip ;
2025-01-07 14:31:43 +00:00
throw new \Exception ( 'This IP/Domain is already in use by another server in your team.' );
2024-11-14 16:48:35 +00:00
}
2024-10-30 13:54:27 +00:00
$this -> server -> name = $this -> name ;
$this -> server -> description = $this -> description ;
$this -> server -> ip = $this -> ip ;
$this -> server -> user = $this -> user ;
$this -> server -> port = $this -> port ;
$this -> server -> validation_logs = $this -> validationLogs ;
$this -> server -> save ();
2024-10-17 20:00:27 +00:00
2024-10-30 13:54:27 +00:00
$this -> server -> settings -> is_swarm_manager = $this -> isSwarmManager ;
2024-11-08 12:58:40 +00:00
$this -> server -> settings -> wildcard_domain = $this -> wildcardDomain ;
2024-10-30 13:54:27 +00:00
$this -> server -> settings -> is_swarm_worker = $this -> isSwarmWorker ;
$this -> server -> settings -> is_build_server = $this -> isBuildServer ;
$this -> server -> settings -> is_metrics_enabled = $this -> isMetricsEnabled ;
$this -> server -> settings -> sentinel_token = $this -> sentinelToken ;
$this -> server -> settings -> sentinel_metrics_refresh_rate_seconds = $this -> sentinelMetricsRefreshRateSeconds ;
$this -> server -> settings -> sentinel_metrics_history_days = $this -> sentinelMetricsHistoryDays ;
$this -> server -> settings -> sentinel_push_interval_seconds = $this -> sentinelPushIntervalSeconds ;
$this -> server -> settings -> sentinel_custom_url = $this -> sentinelCustomUrl ;
$this -> server -> settings -> is_sentinel_enabled = $this -> isSentinelEnabled ;
$this -> server -> settings -> is_sentinel_debug_enabled = $this -> isSentinelDebugEnabled ;
2024-11-14 09:02:37 +00:00
if ( ! validate_timezone ( $this -> serverTimezone )) {
$this -> serverTimezone = config ( 'app.timezone' );
2025-01-07 14:31:43 +00:00
throw new \Exception ( 'Invalid timezone.' );
} else {
$this -> server -> settings -> server_timezone = $this -> serverTimezone ;
2024-11-14 09:02:37 +00:00
}
2024-10-30 13:54:27 +00:00
$this -> server -> settings -> save ();
} else {
$this -> name = $this -> server -> name ;
$this -> description = $this -> server -> description ;
$this -> ip = $this -> server -> ip ;
$this -> user = $this -> server -> user ;
$this -> port = $this -> server -> port ;
2024-11-08 12:58:40 +00:00
2024-10-30 13:54:27 +00:00
$this -> wildcardDomain = $this -> server -> settings -> wildcard_domain ;
$this -> isReachable = $this -> server -> settings -> is_reachable ;
$this -> isUsable = $this -> server -> settings -> is_usable ;
$this -> isSwarmManager = $this -> server -> settings -> is_swarm_manager ;
$this -> isSwarmWorker = $this -> server -> settings -> is_swarm_worker ;
$this -> isBuildServer = $this -> server -> settings -> is_build_server ;
$this -> isMetricsEnabled = $this -> server -> settings -> is_metrics_enabled ;
$this -> sentinelToken = $this -> server -> settings -> sentinel_token ;
$this -> sentinelMetricsRefreshRateSeconds = $this -> server -> settings -> sentinel_metrics_refresh_rate_seconds ;
$this -> sentinelMetricsHistoryDays = $this -> server -> settings -> sentinel_metrics_history_days ;
$this -> sentinelPushIntervalSeconds = $this -> server -> settings -> sentinel_push_interval_seconds ;
$this -> sentinelCustomUrl = $this -> server -> settings -> sentinel_custom_url ;
$this -> isSentinelEnabled = $this -> server -> settings -> is_sentinel_enabled ;
$this -> isSentinelDebugEnabled = $this -> server -> settings -> is_sentinel_debug_enabled ;
$this -> sentinelUpdatedAt = $this -> server -> settings -> updated_at ;
$this -> serverTimezone = $this -> server -> settings -> server_timezone ;
}
}
2024-06-10 20:43:34 +00:00
2024-11-05 10:52:14 +00:00
public function refresh ()
{
$this -> syncData ();
$this -> dispatch ( '$refresh' );
}
2024-10-30 13:54:27 +00:00
public function validateServer ( $install = true )
2023-08-29 12:36:17 +00:00
{
2023-08-29 13:51:30 +00:00
try {
2024-10-30 13:54:27 +00:00
$this -> validationLogs = $this -> server -> validation_logs = null ;
$this -> server -> save ();
$this -> dispatch ( 'init' , $install );
2025-01-07 14:31:43 +00:00
} catch ( \Throwable $e ) {
2023-09-15 13:34:25 +00:00
return handleError ( $e , $this );
2023-08-29 13:51:30 +00:00
}
2023-08-29 12:36:17 +00:00
}
2024-06-10 20:43:34 +00:00
2024-10-30 13:54:27 +00:00
public function checkLocalhostConnection ()
2024-09-12 10:34:09 +00:00
{
2024-10-30 13:54:27 +00:00
$this -> syncData ( true );
[ 'uptime' => $uptime , 'error' => $error ] = $this -> server -> validateConnection ();
if ( $uptime ) {
$this -> dispatch ( 'success' , 'Server is reachable.' );
$this -> server -> settings -> is_reachable = $this -> isReachable = true ;
$this -> server -> settings -> is_usable = $this -> isUsable = true ;
$this -> server -> settings -> save ();
2024-12-16 13:06:16 +00:00
ServerReachabilityChanged :: dispatch ( $this -> server );
2024-10-30 13:54:27 +00:00
$this -> dispatch ( 'proxyStatusUpdated' );
} else {
$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 );
return ;
}
}
2024-11-01 22:48:46 +00:00
public function restartSentinel ()
{
$this -> server -> restartSentinel ();
$this -> dispatch ( 'success' , 'Sentinel restarted.' );
}
public function updatedIsSentinelDebugEnabled ( $value )
{
2024-11-01 22:51:23 +00:00
$this -> submit ();
2024-11-01 22:48:46 +00:00
$this -> restartSentinel ();
}
public function updatedIsMetricsEnabled ( $value )
{
2024-11-01 22:51:23 +00:00
$this -> submit ();
2024-11-01 22:48:46 +00:00
$this -> restartSentinel ();
}
public function updatedIsSentinelEnabled ( $value )
{
if ( $value === true ) {
StartSentinel :: run ( $this -> server , true );
} else {
2024-11-01 22:51:23 +00:00
$this -> isMetricsEnabled = false ;
$this -> isSentinelDebugEnabled = false ;
2024-11-01 22:48:46 +00:00
StopSentinel :: dispatch ( $this -> server );
}
2024-11-01 22:51:23 +00:00
$this -> submit ();
2024-11-01 22:48:46 +00:00
}
2024-10-30 13:54:27 +00:00
public function regenerateSentinelToken ()
{
try {
$this -> server -> settings -> generateSentinelToken ();
$this -> dispatch ( 'success' , 'Token regenerated & Sentinel restarted.' );
2025-01-07 14:31:43 +00:00
} catch ( \Throwable $e ) {
2024-10-30 13:54:27 +00:00
return handleError ( $e , $this );
}
}
public function instantSave ()
{
$this -> submit ();
2024-09-12 10:34:09 +00:00
}
2023-10-09 09:00:18 +00:00
public function submit ()
{
2024-10-30 13:54:27 +00:00
try {
$this -> syncData ( true );
2024-11-01 22:48:46 +00:00
$this -> dispatch ( 'success' , 'Server updated.' );
2025-01-07 14:31:43 +00:00
} catch ( \Throwable $e ) {
2024-10-30 13:54:27 +00:00
return handleError ( $e , $this );
}
2023-10-09 09:00:18 +00:00
}
2024-06-10 20:43:34 +00:00
2023-08-29 12:36:17 +00:00
public function render ()
{
return view ( 'livewire.server.show' );
}
}