2023-04-19 10:42:15 +00:00
< ? php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Project\Shared ;
2023-04-19 10:42:15 +00:00
2024-02-07 13:55:06 +00:00
use App\Actions\Application\StopApplicationOneServer ;
2024-05-07 13:41:50 +00:00
use App\Actions\Docker\GetContainersStatus ;
2024-02-07 13:55:06 +00:00
use App\Events\ApplicationStatusChanged ;
2024-02-06 14:05:11 +00:00
use App\Models\Server ;
2024-02-07 13:55:06 +00:00
use App\Models\StandaloneDocker ;
2024-12-17 11:10:55 +00:00
use Illuminate\Support\Collection ;
2023-04-19 10:42:15 +00:00
use Livewire\Component ;
2024-02-07 13:55:06 +00:00
use Visus\Cuid2\Cuid2 ;
2023-04-19 10:42:15 +00:00
class Destination extends Component
{
2023-11-21 14:31:46 +00:00
public $resource ;
2024-06-10 20:43:34 +00:00
2024-12-17 11:10:55 +00:00
public Collection $networks ;
2024-02-06 14:05:11 +00:00
2024-02-07 13:55:06 +00:00
public function getListeners ()
{
$teamId = auth () -> user () -> currentTeam () -> id ;
2024-06-10 20:43:34 +00:00
2024-02-07 13:55:06 +00:00
return [
" echo-private:team. { $teamId } ,ApplicationStatusChanged " => 'loadData' ,
2025-05-26 19:44:34 +00:00
" echo-private:team. { $teamId } ,ServiceStatusChanged " => 'mount' ,
2025-05-26 19:06:00 +00:00
'refresh' => 'mount' ,
2024-02-07 13:55:06 +00:00
];
}
2024-06-10 20:43:34 +00:00
2024-02-06 14:05:11 +00:00
public function mount ()
{
2024-12-17 11:10:55 +00:00
$this -> networks = collect ([]);
2024-02-06 14:05:11 +00:00
$this -> loadData ();
}
2024-06-10 20:43:34 +00:00
2024-02-06 14:05:11 +00:00
public function loadData ()
{
$all_networks = collect ([]);
$all_networks = $all_networks -> push ( $this -> resource -> destination );
$all_networks = $all_networks -> merge ( $this -> resource -> additional_networks );
$this -> networks = Server :: isUsable () -> get () -> map ( function ( $server ) {
return $server -> standaloneDockers ;
}) -> flatten ();
$this -> networks = $this -> networks -> reject ( function ( $network ) use ( $all_networks ) {
return $all_networks -> pluck ( 'id' ) -> contains ( $network -> id );
});
2024-02-15 20:22:59 +00:00
$this -> networks = $this -> networks -> reject ( function ( $network ) {
return $this -> resource -> destination -> server -> id == $network -> server -> id ;
});
2024-02-22 09:57:05 +00:00
if ( $this -> resource ? -> additional_servers ? -> count () > 0 ) {
$this -> networks = $this -> networks -> reject ( function ( $network ) {
return $this -> resource -> additional_servers -> pluck ( 'id' ) -> contains ( $network -> server -> id );
});
}
}
2024-06-10 20:43:34 +00:00
2024-12-17 11:10:55 +00:00
public function stop ( $serverId )
2024-02-22 09:57:05 +00:00
{
2024-12-17 11:10:55 +00:00
try {
$server = Server :: ownedByCurrentTeam () -> findOrFail ( $serverId );
StopApplicationOneServer :: run ( $this -> resource , $server );
$this -> refreshServers ();
2025-01-07 14:31:43 +00:00
} catch ( \Exception $e ) {
2024-12-17 11:10:55 +00:00
return handleError ( $e , $this );
}
2024-02-07 13:55:06 +00:00
}
2024-06-10 20:43:34 +00:00
2024-02-07 13:55:06 +00:00
public function redeploy ( int $network_id , int $server_id )
{
2024-12-17 11:10:55 +00:00
try {
if ( $this -> resource -> additional_servers -> count () > 0 && str ( $this -> resource -> docker_registry_image_name ) -> isEmpty ()) {
$this -> dispatch ( 'error' , 'Failed to deploy.' , 'Before deploying to multiple servers, you must first set a Docker image in the General tab.<br>More information here: <a target="_blank" class="underline" href="https://coolify.io/docs/knowledge-base/server/multiple-servers">documentation</a>' );
2024-06-10 20:43:34 +00:00
2025-01-07 14:31:43 +00:00
return ;
2024-12-17 11:10:55 +00:00
}
2025-01-07 14:31:43 +00:00
$deployment_uuid = new Cuid2 ;
2024-12-17 11:10:55 +00:00
$server = Server :: ownedByCurrentTeam () -> findOrFail ( $server_id );
$destination = $server -> standaloneDockers -> where ( 'id' , $network_id ) -> firstOrFail ();
2025-04-11 13:27:56 +00:00
$result = queue_application_deployment (
2025-01-07 14:31:43 +00:00
deployment_uuid : $deployment_uuid ,
2024-12-17 11:10:55 +00:00
application : $this -> resource ,
server : $server ,
destination : $destination ,
only_this_server : true ,
no_questions_asked : true ,
);
2025-12-04 12:52:27 +00:00
if ( $result [ 'status' ] === 'queue_full' ) {
$this -> dispatch ( 'error' , 'Deployment queue full' , $result [ 'message' ]);
return ;
}
2025-04-11 13:27:56 +00:00
if ( $result [ 'status' ] === 'skipped' ) {
$this -> dispatch ( 'success' , 'Deployment skipped' , $result [ 'message' ]);
return ;
}
2024-12-17 11:10:55 +00:00
2025-12-26 12:29:53 +00:00
return redirectRoute ( $this , 'project.application.deployment.show' , [
2024-12-17 11:10:55 +00:00
'project_uuid' => data_get ( $this -> resource , 'environment.project.uuid' ),
'application_uuid' => data_get ( $this -> resource , 'uuid' ),
2025-01-07 14:31:43 +00:00
'deployment_uuid' => $deployment_uuid ,
2024-12-17 11:17:50 +00:00
'environment_uuid' => data_get ( $this -> resource , 'environment.uuid' ),
2024-12-17 11:10:55 +00:00
]);
2025-01-07 14:31:43 +00:00
} catch ( \Exception $e ) {
2024-12-17 11:10:55 +00:00
return handleError ( $e , $this );
2024-02-12 10:46:36 +00:00
}
2024-02-06 14:05:11 +00:00
}
2024-06-10 20:43:34 +00:00
2024-02-22 09:57:05 +00:00
public function promote ( int $network_id , int $server_id )
{
$main_destination = $this -> resource -> destination ;
$this -> resource -> update ([
'destination_id' => $network_id ,
'destination_type' => StandaloneDocker :: class ,
]);
$this -> resource -> additional_networks () -> detach ( $network_id , [ 'server_id' => $server_id ]);
$this -> resource -> additional_networks () -> attach ( $main_destination -> id , [ 'server_id' => $main_destination -> server -> id ]);
$this -> refreshServers ();
2025-05-26 19:06:00 +00:00
$this -> resource -> refresh ();
2024-02-22 09:57:05 +00:00
}
2024-06-10 20:43:34 +00:00
2024-02-22 09:57:05 +00:00
public function refreshServers ()
{
2024-05-07 13:41:50 +00:00
GetContainersStatus :: run ( $this -> resource -> destination -> server );
2024-02-22 09:57:05 +00:00
$this -> loadData ();
$this -> dispatch ( 'refresh' );
}
2024-06-10 20:43:34 +00:00
2024-02-06 14:05:11 +00:00
public function addServer ( int $network_id , int $server_id )
{
$this -> resource -> additional_networks () -> attach ( $network_id , [ 'server_id' => $server_id ]);
2025-05-26 19:06:00 +00:00
$this -> dispatch ( 'refresh' );
2024-02-06 14:05:11 +00:00
}
2024-06-10 20:43:34 +00:00
2026-03-11 14:04:45 +00:00
public function removeServer ( int $network_id , int $server_id , $password , $selectedActions = [])
2024-02-06 14:05:11 +00:00
{
2024-12-17 11:10:55 +00:00
try {
2025-12-12 13:12:02 +00:00
if ( ! verifyPasswordConfirmation ( $password , $this )) {
2026-03-11 14:04:45 +00:00
return 'The provided password is incorrect.' ;
2024-10-22 10:29:48 +00:00
}
2024-09-23 17:51:31 +00:00
2024-12-17 11:10:55 +00:00
if ( $this -> resource -> destination -> server -> id == $server_id && $this -> resource -> destination -> id == $network_id ) {
2025-05-26 19:06:00 +00:00
$this -> dispatch ( 'error' , 'You are trying to remove the main server.' );
2024-06-10 20:43:34 +00:00
2025-01-07 14:31:43 +00:00
return ;
2024-12-17 11:10:55 +00:00
}
$server = Server :: ownedByCurrentTeam () -> findOrFail ( $server_id );
StopApplicationOneServer :: run ( $this -> resource , $server );
$this -> resource -> additional_networks () -> detach ( $network_id , [ 'server_id' => $server_id ]);
$this -> loadData ();
2025-05-26 19:06:00 +00:00
$this -> dispatch ( 'refresh' );
2024-12-17 11:10:55 +00:00
ApplicationStatusChanged :: dispatch ( data_get ( $this -> resource , 'environment.project.team.id' ));
2026-03-11 14:04:45 +00:00
return true ;
2025-01-07 14:31:43 +00:00
} catch ( \Exception $e ) {
2024-12-17 11:10:55 +00:00
return handleError ( $e , $this );
2024-02-07 13:55:06 +00:00
}
2024-02-06 14:05:11 +00:00
}
2023-04-19 10:42:15 +00:00
}