coolify/app/Livewire/Project/Application/Heading.php

145 lines
5.8 KiB
PHP
Raw Normal View History

2023-06-30 10:49:00 +02:00
<?php
2023-12-07 19:06:32 +01:00
namespace App\Livewire\Project\Application;
2023-06-30 10:49:00 +02:00
use App\Actions\Application\StopApplication;
2024-05-07 15:41:50 +02:00
use App\Actions\Docker\GetContainersStatus;
2024-02-07 14:55:06 +01:00
use App\Events\ApplicationStatusChanged;
use App\Jobs\ContainerStatusJob;
use App\Jobs\ServerStatusJob;
2023-06-30 10:49:00 +02:00
use App\Models\Application;
use Livewire\Component;
use Visus\Cuid2\Cuid2;
class Heading extends Component
{
public Application $application;
2024-06-10 22:43:34 +02:00
public ?string $lastDeploymentInfo = null;
2024-06-10 22:43:34 +02:00
public ?string $lastDeploymentLink = null;
2024-06-10 22:43:34 +02:00
2023-06-30 10:49:00 +02:00
public array $parameters;
2023-12-04 21:37:30 +01:00
protected string $deploymentUuid;
2024-06-10 22:43:34 +02:00
public function getListeners()
{
$teamId = auth()->user()->currentTeam()->id;
2024-06-10 22:43:34 +02:00
return [
"echo-private:team.{$teamId},ApplicationStatusChanged" => 'check_status',
2024-06-10 22:43:34 +02:00
'compose_loaded' => '$refresh',
'update_links' => '$refresh',
];
}
2024-06-10 22:43:34 +02:00
2023-06-30 10:49:00 +02:00
public function mount()
{
$this->parameters = get_route_parameters();
$lastDeployment = $this->application->get_last_successful_deployment();
2024-06-10 22:43:34 +02:00
$this->lastDeploymentInfo = data_get_str($lastDeployment, 'commit')->limit(7).' '.data_get($lastDeployment, 'commit_message');
$this->lastDeploymentLink = $this->application->gitCommitLink(data_get($lastDeployment, 'commit'));
2023-06-30 10:49:00 +02:00
}
public function check_status($showNotification = false)
2023-06-30 10:49:00 +02:00
{
2024-02-07 14:55:06 +01:00
if ($this->application->destination->server->isFunctional()) {
2024-05-07 15:41:50 +02:00
GetContainersStatus::dispatch($this->application->destination->server);
// dispatch(new ContainerStatusJob($this->application->destination->server));
2024-02-07 14:55:06 +01:00
} else {
dispatch(new ServerStatusJob($this->application->destination->server));
2023-10-09 15:08:28 +02:00
}
2024-02-07 14:55:06 +01:00
2024-06-10 22:43:34 +02:00
if ($showNotification) {
$this->dispatch('success', 'Success', 'Application status updated.');
}
// Removed because it caused flickering
// $this->dispatch('configurationChanged');
2023-06-30 10:49:00 +02:00
}
public function force_deploy_without_cache()
{
$this->deploy(force_rebuild: true);
}
2023-06-30 10:49:00 +02:00
public function deploy(bool $force_rebuild = false)
{
2023-11-27 15:25:15 +01:00
if ($this->application->build_pack === 'dockercompose' && is_null($this->application->docker_compose_raw)) {
2024-02-07 14:55:06 +01:00
$this->dispatch('error', 'Failed to deploy', 'Please load a Compose file first.');
2024-06-10 22:43:34 +02:00
2024-02-07 14:55:06 +01:00
return;
}
if ($this->application->destination->server->isSwarm() && str($this->application->docker_registry_image_name)->isEmpty()) {
$this->dispatch('error', 'Failed to deploy.', 'To deploy to a Swarm cluster you must set a Docker image name first.');
2024-06-10 22:43:34 +02:00
return;
}
2024-02-07 14:55:06 +01:00
if (data_get($this->application, 'settings.is_build_server_enabled') && str($this->application->docker_registry_image_name)->isEmpty()) {
2024-03-27 11:07:29 +01: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 22:43:34 +02:00
return;
}
2024-02-07 14:55:06 +01:00
if ($this->application->additional_servers->count() > 0 && str($this->application->docker_registry_image_name)->isEmpty()) {
2024-03-27 11:07:29 +01: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 22:43:34 +02:00
2023-12-18 14:01:25 +01:00
return;
}
2023-06-30 10:49:00 +02:00
$this->setDeploymentUuid();
queue_application_deployment(
2024-01-27 18:44:40 +01:00
application: $this->application,
2023-06-30 10:49:00 +02:00
deployment_uuid: $this->deploymentUuid,
force_rebuild: $force_rebuild,
);
2024-06-10 22:43:34 +02:00
2024-01-07 16:23:41 +01:00
return redirect()->route('project.application.deployment.show', [
2023-06-30 10:49:00 +02:00
'project_uuid' => $this->parameters['project_uuid'],
'application_uuid' => $this->parameters['application_uuid'],
'deployment_uuid' => $this->deploymentUuid,
'environment_name' => $this->parameters['environment_name'],
2023-12-27 16:45:01 +01:00
]);
2023-06-30 10:49:00 +02:00
}
protected function setDeploymentUuid()
2023-06-30 10:49:00 +02:00
{
$this->deploymentUuid = new Cuid2(7);
$this->parameters['deployment_uuid'] = $this->deploymentUuid;
2023-06-30 10:49:00 +02:00
}
2023-06-30 10:49:00 +02:00
public function stop()
{
StopApplication::run($this->application);
$this->application->status = 'exited';
$this->application->save();
2024-02-07 14:55:06 +01:00
if ($this->application->additional_servers->count() > 0) {
$this->application->additional_servers->each(function ($server) {
2024-06-10 22:43:34 +02:00
$server->pivot->status = 'exited:unhealthy';
2024-02-07 14:55:06 +01:00
$server->pivot->save();
});
}
ApplicationStatusChanged::dispatch(data_get($this->application, 'environment.project.team.id'));
2023-06-30 10:49:00 +02:00
}
2024-06-10 22:43:34 +02:00
public function restart()
{
2024-02-07 14:55:06 +01:00
if ($this->application->additional_servers->count() > 0 && str($this->application->docker_registry_image_name)->isEmpty()) {
2024-03-27 11:07:29 +01: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 22:43:34 +02:00
2024-02-07 14:55:06 +01:00
return;
}
$this->setDeploymentUuid();
queue_application_deployment(
2024-01-27 18:44:40 +01:00
application: $this->application,
deployment_uuid: $this->deploymentUuid,
restart_only: true,
);
2024-06-10 22:43:34 +02:00
2024-01-07 16:23:41 +01:00
return redirect()->route('project.application.deployment.show', [
'project_uuid' => $this->parameters['project_uuid'],
'application_uuid' => $this->parameters['application_uuid'],
'deployment_uuid' => $this->deploymentUuid,
'environment_name' => $this->parameters['environment_name'],
2023-12-27 16:45:01 +01:00
]);
}
2023-08-07 22:14:21 +02:00
}