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

111 lines
4.1 KiB
PHP
Raw Normal View History

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
use App\Actions\Application\StopApplication;
2023-09-14 10:45:50 +00:00
use App\Jobs\ContainerStatusJob;
use App\Jobs\ServerStatusJob;
2023-06-30 08:49:00 +00:00
use App\Models\Application;
use Livewire\Component;
use Visus\Cuid2\Cuid2;
class Heading extends Component
{
public Application $application;
public array $parameters;
2023-12-04 20:37:30 +00:00
protected string $deploymentUuid;
public function getListeners()
{
$teamId = auth()->user()->currentTeam()->id;
return [
"echo-private:team.{$teamId},ApplicationStatusChanged" => 'check_status',
];
}
2023-06-30 08:49:00 +00:00
public function mount()
{
$this->parameters = get_route_parameters();
2023-06-30 08:49:00 +00:00
}
public function check_status($showNotification = false)
2023-06-30 08:49:00 +00:00
{
$all_servers = collect([]);
$all_servers = $all_servers->push($this->application->destination->server);
$all_servers = $all_servers->merge($this->application->additional_servers);
foreach ($all_servers as $server) {
if ($server->isFunctional()) {
dispatch(new ContainerStatusJob($server));
$this->application->refresh();
$this->application->previews->each(function ($preview) {
$preview->refresh();
});
} else {
dispatch(new ServerStatusJob($this->application->destination->server));
}
2023-10-09 13:08:28 +00:00
}
if ($showNotification) $this->dispatch('success', "Application status updated.");
2023-06-30 08:49:00 +00:00
}
public function force_deploy_without_cache()
{
$this->deploy(force_rebuild: true);
}
2023-06-30 08:49:00 +00:00
public function deploy(bool $force_rebuild = false)
{
2023-11-27 14:25:15 +00:00
if ($this->application->build_pack === 'dockercompose' && is_null($this->application->docker_compose_raw)) {
2023-12-07 18:06:32 +00:00
$this->dispatch('error', 'Please load a Compose file first.');
return;
}
2023-12-18 13:01:25 +00:00
if ($this->application->destination->server->isSwarm() && is_null($this->application->docker_registry_image_name)) {
$this->dispatch('error', 'To deploy to a Swarm cluster you must set a Docker image name first.');
return;
}
if (data_get($this->application, 'settings.is_build_server_enabled') && is_null($this->application->docker_registry_image_name)) {
2024-01-18 10:40:13 +00:00
$this->dispatch('error', 'To use a build server you must set a Docker image name first.<br>More information here: <a target="_blank" class="underline" href="https://coolify.io/docs/server/build-server">documentation</a>');
2023-12-18 13:01:25 +00:00
return;
}
2023-06-30 08:49:00 +00:00
$this->setDeploymentUuid();
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,
);
2024-01-07 15:23:41 +00:00
return redirect()->route('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,
'environment_name' => $this->parameters['environment_name'],
2023-12-27 15:45:01 +00:00
]);
2023-06-30 08:49:00 +00:00
}
protected function setDeploymentUuid()
2023-06-30 08:49:00 +00:00
{
$this->deploymentUuid = new Cuid2(7);
$this->parameters['deployment_uuid'] = $this->deploymentUuid;
2023-06-30 08:49:00 +00:00
}
2023-06-30 08:49:00 +00:00
public function stop()
{
StopApplication::run($this->application);
$this->application->status = 'exited';
$this->application->save();
2023-09-18 10:38:11 +00:00
$this->application->refresh();
2023-06-30 08:49:00 +00:00
}
public function restart()
{
$this->setDeploymentUuid();
queue_application_deployment(
2024-01-27 17:44:40 +00:00
application: $this->application,
deployment_uuid: $this->deploymentUuid,
restart_only: true,
);
2024-01-07 15:23:41 +00: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 15:45:01 +00:00
]);
}
2023-08-07 20:14:21 +00:00
}