2023-06-30 08:49:00 +00:00
< ? php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Project\Application ;
2023-06-30 08:49:00 +00:00
2023-10-14 12:22:07 +00:00
use App\Actions\Application\StopApplication ;
2024-05-07 13:41:50 +00:00
use App\Actions\Docker\GetContainersStatus ;
2023-06-30 08:49:00 +00:00
use App\Models\Application ;
2025-08-22 14:47:59 +00:00
use Illuminate\Foundation\Auth\Access\AuthorizesRequests ;
2023-06-30 08:49:00 +00:00
use Livewire\Component ;
use Visus\Cuid2\Cuid2 ;
class Heading extends Component
{
2025-08-22 14:47:59 +00:00
use AuthorizesRequests ;
2023-06-30 08:49:00 +00:00
public Application $application ;
2024-06-10 20:43:34 +00:00
2024-05-17 06:53:25 +00:00
public ? string $lastDeploymentInfo = null ;
2024-06-10 20:43:34 +00:00
2024-05-17 06:53:25 +00:00
public ? string $lastDeploymentLink = null ;
2024-06-10 20:43:34 +00:00
2023-06-30 08:49:00 +00:00
public array $parameters ;
2023-12-04 20:37:30 +00:00
protected string $deploymentUuid ;
2024-06-10 20:43:34 +00:00
2024-09-04 12:54:43 +00:00
public bool $docker_cleanup = true ;
2023-12-11 12:43:16 +00:00
public function getListeners ()
{
$teamId = auth () -> user () -> currentTeam () -> id ;
2024-06-10 20:43:34 +00:00
2023-12-11 12:43:16 +00:00
return [
2025-05-19 19:50:32 +00:00
" echo-private:team. { $teamId } ,ServiceStatusChanged " => 'checkStatus' ,
" echo-private:team. { $teamId } ,ServiceChecked " => '$refresh' ,
2024-06-10 20:43:34 +00:00
'compose_loaded' => '$refresh' ,
'update_links' => '$refresh' ,
2023-12-11 12:43:16 +00:00
];
}
2024-06-10 20:43:34 +00:00
2023-06-30 08:49:00 +00:00
public function mount ()
{
2024-11-23 19:44:27 +00:00
$this -> parameters = [
'project_uuid' => $this -> application -> project () -> uuid ,
2024-12-17 12:42:16 +00:00
'environment_uuid' => $this -> application -> environment -> uuid ,
2024-11-23 19:44:27 +00:00
'application_uuid' => $this -> application -> uuid ,
];
2024-05-17 06:53:25 +00:00
$lastDeployment = $this -> application -> get_last_successful_deployment ();
2024-06-10 20:43:34 +00:00
$this -> lastDeploymentInfo = data_get_str ( $lastDeployment , 'commit' ) -> limit ( 7 ) . ' ' . data_get ( $lastDeployment , 'commit_message' );
2024-05-17 06:53:25 +00:00
$this -> lastDeploymentLink = $this -> application -> gitCommitLink ( data_get ( $lastDeployment , 'commit' ));
2023-06-30 08:49:00 +00:00
}
2025-05-19 19:50:32 +00:00
public function checkStatus ()
2023-06-30 08:49:00 +00:00
{
2024-02-07 13:55:06 +00:00
if ( $this -> application -> destination -> server -> isFunctional ()) {
2024-11-22 10:16:01 +00:00
GetContainersStatus :: dispatch ( $this -> application -> destination -> server );
2025-05-20 13:22:13 +00:00
} else {
$this -> dispatch ( 'error' , 'Server is not functional.' );
2023-10-09 13:08:28 +00:00
}
2023-06-30 08:49:00 +00:00
}
2023-08-08 09:51:36 +00:00
public function force_deploy_without_cache ()
{
2025-08-22 14:47:59 +00:00
$this -> authorize ( 'deploy' , $this -> application );
2023-08-08 09:51:36 +00:00
$this -> deploy ( force_rebuild : true );
}
2023-06-30 08:49:00 +00:00
public function deploy ( bool $force_rebuild = false )
{
2025-08-22 14:47:59 +00:00
$this -> authorize ( 'deploy' , $this -> application );
2023-11-27 14:25:15 +00:00
if ( $this -> application -> build_pack === 'dockercompose' && is_null ( $this -> application -> docker_compose_raw )) {
2024-02-07 13:55:06 +00:00
$this -> dispatch ( 'error' , 'Failed to deploy' , 'Please load a Compose file first.' );
2024-06-10 20:43:34 +00:00
2025-01-07 14:31:43 +00:00
return ;
2024-02-07 13:55:06 +00:00
}
if ( $this -> application -> destination -> server -> isSwarm () && str ( $this -> application -> docker_registry_image_name ) -> isEmpty ()) {
2024-02-12 10:46:36 +00:00
$this -> dispatch ( 'error' , 'Failed to deploy.' , 'To deploy to a Swarm cluster you must set a Docker image name first.' );
2024-06-10 20:43:34 +00:00
2025-01-07 14:31:43 +00:00
return ;
2023-11-27 10:54:55 +00:00
}
2024-02-07 13:55:06 +00:00
if ( data_get ( $this -> application , 'settings.is_build_server_enabled' ) && str ( $this -> application -> docker_registry_image_name ) -> isEmpty ()) {
2024-03-27 10:07:29 +00:00
$this -> dispatch ( 'error' , 'Failed to deploy.' , 'To use a build server, you must first set a Docker image.<br>More information here: <a target="_blank" class="underline" href="https://coolify.io/docs/knowledge-base/server/build-server">documentation</a>' );
2024-06-10 20:43:34 +00:00
2025-01-07 14:31:43 +00:00
return ;
2024-01-16 14:19:14 +00:00
}
2024-02-07 13:55:06 +00:00
if ( $this -> application -> additional_servers -> count () > 0 && str ( $this -> application -> docker_registry_image_name ) -> isEmpty ()) {
2024-03-27 10:07:29 +00:00
$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 ;
2023-12-18 13:01:25 +00:00
}
2023-06-30 08:49:00 +00:00
$this -> setDeploymentUuid ();
2025-04-11 13:27:56 +00:00
$result = queue_application_deployment (
2024-01-27 17:44:40 +00:00
application : $this -> application ,
2023-06-30 08:49:00 +00:00
deployment_uuid : $this -> deploymentUuid ,
force_rebuild : $force_rebuild ,
);
2025-04-11 13:27:56 +00:00
if ( $result [ 'status' ] === 'skipped' ) {
$this -> dispatch ( 'success' , 'Deployment skipped' , $result [ 'message' ]);
return ;
}
2024-06-10 20:43:34 +00:00
2025-01-16 10:14:04 +00:00
return $this -> redirectRoute ( 'project.application.deployment.show' , [
2023-06-30 08:49:00 +00:00
'project_uuid' => $this -> parameters [ 'project_uuid' ],
'application_uuid' => $this -> parameters [ 'application_uuid' ],
'deployment_uuid' => $this -> deploymentUuid ,
2024-11-22 14:53:11 +00:00
'environment_uuid' => $this -> parameters [ 'environment_uuid' ],
2025-04-29 07:04:24 +00:00
], navigate : false );
2023-06-30 08:49:00 +00:00
}
2023-08-08 09:51:36 +00:00
protected function setDeploymentUuid ()
2023-06-30 08:49:00 +00:00
{
2024-07-25 11:31:59 +00:00
$this -> deploymentUuid = new Cuid2 ;
2023-08-08 09:51:36 +00:00
$this -> parameters [ 'deployment_uuid' ] = $this -> deploymentUuid ;
2023-06-30 08:49:00 +00:00
}
2023-08-08 09:51:36 +00:00
2023-06-30 08:49:00 +00:00
public function stop ()
{
2025-08-22 14:47:59 +00:00
$this -> authorize ( 'deploy' , $this -> application );
2025-05-30 12:04:43 +00:00
$this -> dispatch ( 'info' , 'Gracefully stopping application.<br/>It could take a while depending on the application.' );
2025-05-19 19:50:32 +00:00
StopApplication :: dispatch ( $this -> application , false , $this -> docker_cleanup );
2023-06-30 08:49:00 +00:00
}
2024-06-10 20:43:34 +00:00
2023-11-27 10:54:55 +00:00
public function restart ()
{
2025-08-22 14:47:59 +00:00
$this -> authorize ( 'deploy' , $this -> application );
2024-02-07 13:55:06 +00:00
if ( $this -> application -> additional_servers -> count () > 0 && str ( $this -> application -> docker_registry_image_name ) -> isEmpty ()) {
2024-03-27 10:07:29 +00:00
$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-02-07 13:55:06 +00:00
}
2023-11-01 11:19:08 +00:00
$this -> setDeploymentUuid ();
2025-04-11 13:27:56 +00:00
$result = queue_application_deployment (
2024-01-27 17:44:40 +00:00
application : $this -> application ,
2023-11-01 11:19:08 +00:00
deployment_uuid : $this -> deploymentUuid ,
restart_only : true ,
);
2025-04-11 13:27:56 +00:00
if ( $result [ 'status' ] === 'skipped' ) {
$this -> dispatch ( 'success' , 'Deployment skipped' , $result [ 'message' ]);
return ;
}
2024-06-10 20:43:34 +00:00
2025-01-16 10:14:04 +00:00
return $this -> redirectRoute ( 'project.application.deployment.show' , [
2023-11-01 11:19:08 +00:00
'project_uuid' => $this -> parameters [ 'project_uuid' ],
'application_uuid' => $this -> parameters [ 'application_uuid' ],
'deployment_uuid' => $this -> deploymentUuid ,
2024-11-22 14:53:11 +00:00
'environment_uuid' => $this -> parameters [ 'environment_uuid' ],
2025-04-29 07:04:24 +00:00
], navigate : false );
2023-11-01 11:19:08 +00:00
}
2024-09-04 12:54:43 +00:00
public function render ()
{
return view ( 'livewire.project.application.heading' , [
'checkboxes' => [
2024-09-19 08:27:44 +00:00
[ 'id' => 'docker_cleanup' , 'label' => __ ( 'resource.docker_cleanup' )],
],
2024-09-04 12:54:43 +00:00
]);
}
2023-08-07 20:14:21 +00:00
}