From fd546cec9d98253a49328fe33aa9cbb81ff8ca97 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Sun, 3 Nov 2024 23:08:24 +0100 Subject: [PATCH] fix: refactor destination/docker view --- app/Livewire/Destination/New/Docker.php | 108 +++++++----------- app/Livewire/Destination/Show.php | 22 ++-- app/Models/Server.php | 14 ++- .../livewire/destination/new/docker.blade.php | 2 +- .../views/livewire/destination/show.blade.php | 4 +- .../server/destination/show.blade.php | 49 -------- 6 files changed, 71 insertions(+), 128 deletions(-) delete mode 100644 resources/views/livewire/server/destination/show.blade.php diff --git a/app/Livewire/Destination/New/Docker.php b/app/Livewire/Destination/New/Docker.php index 4fc938df8..1ef8761fa 100644 --- a/app/Livewire/Destination/New/Docker.php +++ b/app/Livewire/Destination/New/Docker.php @@ -3,111 +3,89 @@ namespace App\Livewire\Destination\New; use App\Models\Server; -use App\Models\StandaloneDocker as ModelsStandaloneDocker; +use App\Models\StandaloneDocker; use App\Models\SwarmDocker; -use Illuminate\Support\Collection; +use Livewire\Attributes\Locked; +use Livewire\Attributes\Rule; use Livewire\Component; use Visus\Cuid2\Cuid2; class Docker extends Component { + #[Locked] + public $servers; + + #[Locked] + public Server $selectedServer; + + #[Rule(['required', 'string'])] public string $name; + #[Rule(['required', 'string'])] public string $network; - public ?Collection $servers = null; + #[Rule(['required', 'string'])] + public string $serverId; - public Server $server; + #[Rule(['required', 'boolean'])] + public bool $isSwarm = false; - public ?int $server_id = null; - - public bool $is_swarm = false; - - protected $rules = [ - 'name' => 'required|string', - 'network' => 'required|string', - 'server_id' => 'required|integer', - 'is_swarm' => 'boolean', - ]; - - protected $validationAttributes = [ - 'name' => 'name', - 'network' => 'network', - 'server_id' => 'server', - 'is_swarm' => 'swarm', - ]; - - public function mount() + public function mount(?string $server_id = null) { - if (is_null($this->servers)) { - $this->servers = Server::isReachable()->get(); - } - if (request()->query('server_id')) { - $this->server_id = request()->query('server_id'); + $this->network = new Cuid2; + $this->servers = Server::isUsable()->get(); + if ($server_id) { + $this->selectedServer = $this->servers->find($server_id); } else { - if ($this->servers->count() > 0) { - $this->server_id = $this->servers->first()->id; - } - } - if (request()->query('network_name')) { - $this->network = request()->query('network_name'); - } else { - $this->network = new Cuid2; - } - if ($this->servers->count() > 0) { - $this->name = str("{$this->servers->first()->name}-{$this->network}")->kebab(); + $this->selectedServer = $this->servers->first(); } + $this->generateName(); } - public function generate_name() + public function updatedServerId() { - $this->server = Server::find($this->server_id); - $this->name = str("{$this->server->name}-{$this->network}")->kebab(); + $this->selectedServer = $this->servers->find($this->serverId); + $this->generateName(); + } + + public function generateName() + { + $name = data_get($this->selectedServer, 'name', new Cuid2); + $this->name = str("{$name}-{$this->network}")->kebab(); } public function submit() { - $this->validate(); try { - $this->server = Server::find($this->server_id); - if ($this->is_swarm) { - $found = $this->server->swarmDockers()->where('network', $this->network)->first(); + $this->validate(); + if ($this->isSwarm) { + $found = $this->selectedServer->swarmDockers()->where('network', $this->network)->first(); if ($found) { - $this->dispatch('error', 'Network already added to this server.'); - - return; + throw new \Exception('Network already added to this server.'); } else { $docker = SwarmDocker::create([ 'name' => $this->name, 'network' => $this->network, - 'server_id' => $this->server_id, + 'server_id' => $this->selectedServer->id, ]); } } else { - $found = $this->server->standaloneDockers()->where('network', $this->network)->first(); + $found = $this->selectedServer->standaloneDockers()->where('network', $this->network)->first(); if ($found) { - $this->dispatch('error', 'Network already added to this server.'); - - return; + throw new \Exception('Network already added to this server.'); } else { - $docker = ModelsStandaloneDocker::create([ + $docker = StandaloneDocker::create([ 'name' => $this->name, 'network' => $this->network, - 'server_id' => $this->server_id, + 'server_id' => $this->selectedServer->id, ]); } } - $this->createNetworkAndAttachToProxy(); - - return redirect()->route('destination.show', $docker->uuid); + $connectProxyToDockerNetworks = connectProxyToNetworks($this->selectedServer); + instant_remote_process($connectProxyToDockerNetworks, $this->selectedServer, false); + $this->dispatch('reloadWindow'); } catch (\Throwable $e) { return handleError($e, $this); } } - - private function createNetworkAndAttachToProxy() - { - $connectProxyToDockerNetworks = connectProxyToNetworks($this->server); - instant_remote_process($connectProxyToDockerNetworks, $this->server, false); - } } diff --git a/app/Livewire/Destination/Show.php b/app/Livewire/Destination/Show.php index f69780479..f75749382 100644 --- a/app/Livewire/Destination/Show.php +++ b/app/Livewire/Destination/Show.php @@ -3,6 +3,8 @@ namespace App\Livewire\Destination; use App\Models\Server; +use App\Models\StandaloneDocker; +use App\Models\SwarmDocker; use Livewire\Attributes\Locked; use Livewire\Attributes\Rule; use Livewire\Component; @@ -24,17 +26,17 @@ class Show extends Component public function mount(string $destination_uuid) { try { - $destination = Server::isUsable()->whereHas('standaloneDockers', function ($query) use ($destination_uuid) { - $query->where('uuid', $destination_uuid); - })->first()->standaloneDockers()->where('uuid', $destination_uuid)->first(); + $destination = StandaloneDocker::whereUuid($destination_uuid)->first() ?? + SwarmDocker::whereUuid($destination_uuid)->firstOrFail(); - if (! $destination) { - $destination = Server::isUsable()->whereHas('swarmDockers', function ($query) use ($destination_uuid) { - $query->where('uuid', $destination_uuid); - })->first()->swarmDockers()->where('uuid', $destination_uuid)->first(); - } - if (! $destination) { - throw new \Exception('Destination not found'); + $ownedByTeam = Server::ownedByCurrentTeam()->each(function ($server) use ($destination) { + if ($server->standaloneDockers->contains($destination) || $server->swarmDockers->contains($destination)) { + $this->destination = $destination; + $this->syncData(); + } + }); + if ($ownedByTeam === false) { + return redirect()->route('destination.index'); } $this->destination = $destination; $this->syncData(); diff --git a/app/Models/Server.php b/app/Models/Server.php index 1c6651017..e9b6fd929 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -903,6 +903,18 @@ $schema://$host { return true; } + public function skipServer() + { + if ($this->ip === '1.2.3.4') { + return true; + } + if ($this->settings->force_disabled === true) { + return true; + } + + return false; + } + public function isFunctional() { $isFunctional = $this->settings->is_reachable && $this->settings->is_usable && $this->settings->force_disabled === false && $this->ip !== '1.2.3.4'; @@ -1044,7 +1056,7 @@ $schema://$host { { config()->set('constants.ssh.mux_enabled', ! $isManualCheck); - if ($this->isFunctional() === false) { + if ($this->skipServer()) { return ['uptime' => false, 'error' => 'Server skipped.']; } try { diff --git a/resources/views/livewire/destination/new/docker.blade.php b/resources/views/livewire/destination/new/docker.blade.php index a6da63c6c..1502f70af 100644 --- a/resources/views/livewire/destination/new/docker.blade.php +++ b/resources/views/livewire/destination/new/docker.blade.php @@ -5,7 +5,7 @@ - + @foreach ($servers as $server) diff --git a/resources/views/livewire/destination/show.blade.php b/resources/views/livewire/destination/show.blade.php index 014e612eb..6c3a9d9dd 100644 --- a/resources/views/livewire/destination/show.blade.php +++ b/resources/views/livewire/destination/show.blade.php @@ -14,9 +14,9 @@ @if ($destination->getMorphClass() === 'App\Models\StandaloneDocker') -
A Docker network in a non-swarm environment.
+
A simple Docker network.
@else -
Your swarm docker network. WIP
+
A swarm Docker network. WIP
@endif
diff --git a/resources/views/livewire/server/destination/show.blade.php b/resources/views/livewire/server/destination/show.blade.php deleted file mode 100644 index 88503f62d..000000000 --- a/resources/views/livewire/server/destination/show.blade.php +++ /dev/null @@ -1,49 +0,0 @@ -
- - {{ data_get_str($server, 'name')->limit(10) }} > Destinations | Coolify - - -
- -
- @if ($server->isFunctional()) -
-

Destinations

- - - - Scan for Destinations -
-
Destinations are used to segregate resources by network.
-

Available Destinations

-
- @foreach ($server->standaloneDockers as $docker) - - {{ data_get($docker, 'network') }} - - @endforeach - @foreach ($server->swarmDockers as $docker) - - {{ data_get($docker, 'network') }} - - @endforeach -
- @if ($networks->count() > 0) -
-

Found Destinations

-
- @foreach ($networks as $network) -
- Add - {{ data_get($network, 'Name') }} -
- @endforeach -
-
- @endif - @else -
Server is not validated. Validate first.
- @endif -
-
-