fix: refactor destination/docker view

This commit is contained in:
Andras Bacsai
2024-11-03 23:08:24 +01:00
parent d4c3a27da7
commit fd546cec9d
6 changed files with 71 additions and 128 deletions

View File

@@ -3,111 +3,89 @@
namespace App\Livewire\Destination\New; namespace App\Livewire\Destination\New;
use App\Models\Server; use App\Models\Server;
use App\Models\StandaloneDocker as ModelsStandaloneDocker; use App\Models\StandaloneDocker;
use App\Models\SwarmDocker; use App\Models\SwarmDocker;
use Illuminate\Support\Collection; use Livewire\Attributes\Locked;
use Livewire\Attributes\Rule;
use Livewire\Component; use Livewire\Component;
use Visus\Cuid2\Cuid2; use Visus\Cuid2\Cuid2;
class Docker extends Component class Docker extends Component
{ {
#[Locked]
public $servers;
#[Locked]
public Server $selectedServer;
#[Rule(['required', 'string'])]
public string $name; public string $name;
#[Rule(['required', 'string'])]
public string $network; 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 function mount(?string $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()
{ {
if (is_null($this->servers)) {
$this->servers = Server::isReachable()->get();
}
if (request()->query('server_id')) {
$this->server_id = request()->query('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; $this->network = new Cuid2;
$this->servers = Server::isUsable()->get();
if ($server_id) {
$this->selectedServer = $this->servers->find($server_id);
} else {
$this->selectedServer = $this->servers->first();
} }
if ($this->servers->count() > 0) { $this->generateName();
$this->name = str("{$this->servers->first()->name}-{$this->network}")->kebab();
}
} }
public function generate_name() public function updatedServerId()
{ {
$this->server = Server::find($this->server_id); $this->selectedServer = $this->servers->find($this->serverId);
$this->name = str("{$this->server->name}-{$this->network}")->kebab(); $this->generateName();
}
public function generateName()
{
$name = data_get($this->selectedServer, 'name', new Cuid2);
$this->name = str("{$name}-{$this->network}")->kebab();
} }
public function submit() public function submit()
{ {
$this->validate();
try { try {
$this->server = Server::find($this->server_id); $this->validate();
if ($this->is_swarm) { if ($this->isSwarm) {
$found = $this->server->swarmDockers()->where('network', $this->network)->first(); $found = $this->selectedServer->swarmDockers()->where('network', $this->network)->first();
if ($found) { if ($found) {
$this->dispatch('error', 'Network already added to this server.'); throw new \Exception('Network already added to this server.');
return;
} else { } else {
$docker = SwarmDocker::create([ $docker = SwarmDocker::create([
'name' => $this->name, 'name' => $this->name,
'network' => $this->network, 'network' => $this->network,
'server_id' => $this->server_id, 'server_id' => $this->selectedServer->id,
]); ]);
} }
} else { } else {
$found = $this->server->standaloneDockers()->where('network', $this->network)->first(); $found = $this->selectedServer->standaloneDockers()->where('network', $this->network)->first();
if ($found) { if ($found) {
$this->dispatch('error', 'Network already added to this server.'); throw new \Exception('Network already added to this server.');
return;
} else { } else {
$docker = ModelsStandaloneDocker::create([ $docker = StandaloneDocker::create([
'name' => $this->name, 'name' => $this->name,
'network' => $this->network, 'network' => $this->network,
'server_id' => $this->server_id, 'server_id' => $this->selectedServer->id,
]); ]);
} }
} }
$this->createNetworkAndAttachToProxy(); $connectProxyToDockerNetworks = connectProxyToNetworks($this->selectedServer);
instant_remote_process($connectProxyToDockerNetworks, $this->selectedServer, false);
return redirect()->route('destination.show', $docker->uuid); $this->dispatch('reloadWindow');
} catch (\Throwable $e) { } catch (\Throwable $e) {
return handleError($e, $this); return handleError($e, $this);
} }
} }
private function createNetworkAndAttachToProxy()
{
$connectProxyToDockerNetworks = connectProxyToNetworks($this->server);
instant_remote_process($connectProxyToDockerNetworks, $this->server, false);
}
} }

View File

@@ -3,6 +3,8 @@
namespace App\Livewire\Destination; namespace App\Livewire\Destination;
use App\Models\Server; use App\Models\Server;
use App\Models\StandaloneDocker;
use App\Models\SwarmDocker;
use Livewire\Attributes\Locked; use Livewire\Attributes\Locked;
use Livewire\Attributes\Rule; use Livewire\Attributes\Rule;
use Livewire\Component; use Livewire\Component;
@@ -24,17 +26,17 @@ class Show extends Component
public function mount(string $destination_uuid) public function mount(string $destination_uuid)
{ {
try { try {
$destination = Server::isUsable()->whereHas('standaloneDockers', function ($query) use ($destination_uuid) { $destination = StandaloneDocker::whereUuid($destination_uuid)->first() ??
$query->where('uuid', $destination_uuid); SwarmDocker::whereUuid($destination_uuid)->firstOrFail();
})->first()->standaloneDockers()->where('uuid', $destination_uuid)->first();
if (! $destination) { $ownedByTeam = Server::ownedByCurrentTeam()->each(function ($server) use ($destination) {
$destination = Server::isUsable()->whereHas('swarmDockers', function ($query) use ($destination_uuid) { if ($server->standaloneDockers->contains($destination) || $server->swarmDockers->contains($destination)) {
$query->where('uuid', $destination_uuid); $this->destination = $destination;
})->first()->swarmDockers()->where('uuid', $destination_uuid)->first(); $this->syncData();
} }
if (! $destination) { });
throw new \Exception('Destination not found'); if ($ownedByTeam === false) {
return redirect()->route('destination.index');
} }
$this->destination = $destination; $this->destination = $destination;
$this->syncData(); $this->syncData();

View File

@@ -903,6 +903,18 @@ $schema://$host {
return true; 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() public function isFunctional()
{ {
$isFunctional = $this->settings->is_reachable && $this->settings->is_usable && $this->settings->force_disabled === false && $this->ip !== '1.2.3.4'; $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); config()->set('constants.ssh.mux_enabled', ! $isManualCheck);
if ($this->isFunctional() === false) { if ($this->skipServer()) {
return ['uptime' => false, 'error' => 'Server skipped.']; return ['uptime' => false, 'error' => 'Server skipped.'];
} }
try { try {

View File

@@ -5,7 +5,7 @@
<x-forms.input id="name" label="Name" required /> <x-forms.input id="name" label="Name" required />
<x-forms.input id="network" label="Network" required /> <x-forms.input id="network" label="Network" required />
</div> </div>
<x-forms.select id="server_id" label="Select a server" required wire:change="generate_name"> <x-forms.select id="serverId" label="Select a server" required wire:change="generateName">
<option disabled>Select a server</option> <option disabled>Select a server</option>
@foreach ($servers as $server) @foreach ($servers as $server)
<option value="{{ $server->id }}">{{ $server->name }}</option> <option value="{{ $server->id }}">{{ $server->name }}</option>

View File

@@ -14,9 +14,9 @@
</div> </div>
@if ($destination->getMorphClass() === 'App\Models\StandaloneDocker') @if ($destination->getMorphClass() === 'App\Models\StandaloneDocker')
<div class="subtitle ">A Docker network in a non-swarm environment.</div> <div class="subtitle ">A simple Docker network.</div>
@else @else
<div class="subtitle ">Your swarm docker network. WIP</div> <div class="subtitle ">A swarm Docker network. WIP</div>
@endif @endif
<div class="flex gap-2"> <div class="flex gap-2">
<x-forms.input id="name" label="Name" /> <x-forms.input id="name" label="Name" />

View File

@@ -1,49 +0,0 @@
<div>
<x-slot:title>
{{ data_get_str($server, 'name')->limit(10) }} > Destinations | Coolify
</x-slot>
<x-server.navbar :server="$server" />
<div class="flex flex-col h-full gap-8 sm:flex-row">
<x-server.sidebar :server="$server" activeMenu="destinations" />
<div class="w-full">
@if ($server->isFunctional())
<div class="flex items-end gap-2">
<h2>Destinations</h2>
<x-modal-input buttonTitle="+ Add" title="New Destination">
<livewire:destination.new.docker :server_id="$server->id" />
</x-modal-input>
<x-forms.button isHighlighted wire:click='scan'>Scan for Destinations</x-forms.button>
</div>
<div>Destinations are used to segregate resources by network.</div>
<h4 class="pt-4 pb-2">Available Destinations</h4>
<div class="flex gap-2">
@foreach ($server->standaloneDockers as $docker)
<a href="{{ route('destination.show', ['destination_uuid' => data_get($docker, 'uuid')]) }}">
<x-forms.button>{{ data_get($docker, 'network') }} </x-forms.button>
</a>
@endforeach
@foreach ($server->swarmDockers as $docker)
<a href="{{ route('destination.show', ['destination_uuid' => data_get($docker, 'uuid')]) }}">
<x-forms.button>{{ data_get($docker, 'network') }} </x-forms.button>
</a>
@endforeach
</div>
@if ($networks->count() > 0)
<div class="pt-2">
<h3 class="pb-4">Found Destinations</h3>
<div class="flex flex-wrap gap-2 ">
@foreach ($networks as $network)
<div class="min-w-fit">
<x-forms.button wire:click="add('{{ data_get($network, 'Name') }}')">Add
{{ data_get($network, 'Name') }}</x-forms.button>
</div>
@endforeach
</div>
</div>
@endif
@else
<div>Server is not validated. Validate first.</div>
@endif
</div>
</div>
</div>