lasthourcloud/app/Livewire/Project/Shared/Destination.php

132 lines
5.0 KiB
PHP
Raw Normal View History

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;
use App\Jobs\ContainerStatusJob;
use App\Models\Server;
2024-02-07 13:55:06 +00:00
use App\Models\StandaloneDocker;
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
{
public $resource;
2024-06-10 20:43:34 +00:00
public $networks = [];
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',
];
}
2024-06-10 20:43:34 +00:00
public function mount()
{
$this->loadData();
}
2024-06-10 20:43:34 +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;
});
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
public function stop(int $server_id)
{
$server = Server::find($server_id);
StopApplicationOneServer::run($this->resource, $server);
$this->refreshServers();
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)
{
if ($this->resource->additional_servers->count() > 0 && str($this->resource->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
return;
}
2024-02-07 13:55:06 +00:00
$deployment_uuid = new Cuid2(7);
$server = Server::find($server_id);
$destination = StandaloneDocker::find($network_id);
queue_application_deployment(
deployment_uuid: $deployment_uuid,
application: $this->resource,
server: $server,
destination: $destination,
only_this_server: true,
2024-02-07 13:55:06 +00:00
no_questions_asked: true,
);
2024-06-10 20:43:34 +00:00
2024-02-07 13:55:06 +00:00
return redirect()->route('project.application.deployment.show', [
'project_uuid' => data_get($this->resource, 'environment.project.uuid'),
'application_uuid' => data_get($this->resource, 'uuid'),
'deployment_uuid' => $deployment_uuid,
'environment_name' => data_get($this->resource, 'environment.name'),
]);
}
2024-06-10 20:43:34 +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();
}
2024-06-10 20:43:34 +00:00
public function refreshServers()
{
2024-05-07 13:41:50 +00:00
GetContainersStatus::run($this->resource->destination->server);
// ContainerStatusJob::dispatchSync($this->resource->destination->server);
$this->loadData();
$this->dispatch('refresh');
ApplicationStatusChanged::dispatch(data_get($this->resource, 'environment.project.team.id'));
}
2024-06-10 20:43:34 +00:00
public function addServer(int $network_id, int $server_id)
{
$this->resource->additional_networks()->attach($network_id, ['server_id' => $server_id]);
$this->loadData();
2024-02-07 13:55:06 +00:00
ApplicationStatusChanged::dispatch(data_get($this->resource, 'environment.project.team.id'));
}
2024-06-10 20:43:34 +00:00
public function removeServer(int $network_id, int $server_id)
{
2024-02-15 20:25:43 +00:00
if ($this->resource->destination->server->id == $server_id && $this->resource->destination->id == $network_id) {
2024-02-07 13:55:06 +00:00
$this->dispatch('error', 'You cannot remove this destination server.', 'You are trying to remove the main server.');
2024-06-10 20:43:34 +00:00
2024-02-07 13:55:06 +00:00
return;
}
$server = Server::find($server_id);
StopApplicationOneServer::run($this->resource, $server);
$this->resource->additional_networks()->detach($network_id, ['server_id' => $server_id]);
$this->loadData();
2024-02-07 13:55:06 +00:00
ApplicationStatusChanged::dispatch(data_get($this->resource, 'environment.project.team.id'));
}
2023-04-19 10:42:15 +00:00
}