fix: destinations livewire refactor

removed unnecessary livewire components, renamed them ,etc etc
This commit is contained in:
Andras Bacsai
2024-11-03 22:19:41 +01:00
parent ec81b4ce5c
commit 4ed76f88f8
12 changed files with 219 additions and 264 deletions

View File

@@ -1,46 +0,0 @@
<?php
namespace App\Livewire\Destination;
use Livewire\Component;
class Form extends Component
{
public mixed $destination;
protected $rules = [
'destination.name' => 'required',
'destination.network' => 'required',
'destination.server.ip' => 'required',
];
protected $validationAttributes = [
'destination.name' => 'name',
'destination.network' => 'network',
'destination.server.ip' => 'IP Address/Domain',
];
public function submit()
{
$this->validate();
$this->destination->save();
}
public function delete()
{
try {
if ($this->destination->getMorphClass() === \App\Models\StandaloneDocker::class) {
if ($this->destination->attachedTo()) {
return $this->dispatch('error', 'You must delete all resources before deleting this destination.');
}
instant_remote_process(["docker network disconnect {$this->destination->network} coolify-proxy"], $this->destination->server, throwError: false);
instant_remote_process(['docker network rm -f '.$this->destination->network], $this->destination->server);
}
$this->destination->delete();
return redirect()->route('destination.all');
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Livewire\Destination;
use App\Models\Server;
use Livewire\Attributes\Locked;
use Livewire\Component;
class Index extends Component
{
#[Locked]
public $servers;
public function mount()
{
$this->servers = Server::isUsable()->get();
}
public function render()
{
return view('livewire.destination.index');
}
}

View File

@@ -3,73 +3,91 @@
namespace App\Livewire\Destination;
use App\Models\Server;
use App\Models\StandaloneDocker;
use App\Models\SwarmDocker;
use Illuminate\Support\Collection;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Rule;
use Livewire\Component;
class Show extends Component
{
public Server $server;
#[Locked]
public $destination;
public Collection|array $networks = [];
#[Rule(['string', 'required'])]
public string $name;
private function createNetworkAndAttachToProxy()
#[Rule(['string', 'required'])]
public string $network;
#[Rule(['string', 'required'])]
public string $serverIp;
public function mount(string $destination_uuid)
{
$connectProxyToDockerNetworks = connectProxyToNetworks($this->server);
instant_remote_process($connectProxyToDockerNetworks, $this->server, false);
}
try {
$destination = Server::isUsable()->whereHas('standaloneDockers', function ($query) use ($destination_uuid) {
$query->where('uuid', $destination_uuid);
})->first()->standaloneDockers()->where('uuid', $destination_uuid)->first();
public function add($name)
{
if ($this->server->isSwarm()) {
$found = $this->server->swarmDockers()->where('network', $name)->first();
if ($found) {
$this->dispatch('error', 'Network already added to this server.');
return;
} else {
SwarmDocker::create([
'name' => $this->server->name.'-'.$name,
'network' => $this->name,
'server_id' => $this->server->id,
]);
if (! $destination) {
$destination = Server::isUsable()->whereHas('swarmDockers', function ($query) use ($destination_uuid) {
$query->where('uuid', $destination_uuid);
})->first()->swarmDockers()->where('uuid', $destination_uuid)->first();
}
} else {
$found = $this->server->standaloneDockers()->where('network', $name)->first();
if ($found) {
$this->dispatch('error', 'Network already added to this server.');
return;
} else {
StandaloneDocker::create([
'name' => $this->server->name.'-'.$name,
'network' => $name,
'server_id' => $this->server->id,
]);
if (! $destination) {
throw new \Exception('Destination not found');
}
$this->createNetworkAndAttachToProxy();
$this->destination = $destination;
$this->syncData();
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
public function scan()
public function syncData(bool $toModel = false)
{
if ($this->server->isSwarm()) {
$alreadyAddedNetworks = $this->server->swarmDockers;
if ($toModel) {
$this->validate();
$this->destination->name = $this->name;
$this->destination->network = $this->network;
$this->destination->server->ip = $this->serverIp;
$this->destination->save();
} else {
$alreadyAddedNetworks = $this->server->standaloneDockers;
$this->name = $this->destination->name;
$this->network = $this->destination->network;
$this->serverIp = $this->destination->server->ip;
}
$networks = instant_remote_process(['docker network ls --format "{{json .}}"'], $this->server, false);
$this->networks = format_docker_command_output_to_json($networks)->filter(function ($network) {
return $network['Name'] !== 'bridge' && $network['Name'] !== 'host' && $network['Name'] !== 'none';
})->filter(function ($network) use ($alreadyAddedNetworks) {
return ! $alreadyAddedNetworks->contains('network', $network['Name']);
});
if ($this->networks->count() === 0) {
$this->dispatch('success', 'No new destinations found on this server.');
}
return;
public function submit()
{
try {
$this->syncData(true);
$this->dispatch('success', 'Destination saved.');
} catch (\Throwable $e) {
return handleError($e, $this);
}
$this->dispatch('success', 'Scan done.');
}
public function delete()
{
try {
if ($this->destination->getMorphClass() === \App\Models\StandaloneDocker::class) {
if ($this->destination->attachedTo()) {
return $this->dispatch('error', 'You must delete all resources before deleting this destination.');
}
instant_remote_process(["docker network disconnect {$this->destination->network} coolify-proxy"], $this->destination->server, throwError: false);
instant_remote_process(['docker network rm -f '.$this->destination->network], $this->destination->server);
}
$this->destination->delete();
return redirect()->route('destination.index');
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
public function render()
{
return view('livewire.destination.show');
}
}

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Livewire\Server\Destination;
namespace App\Livewire\Server;
use App\Models\Server;
use App\Models\StandaloneDocker;
@@ -8,7 +8,7 @@ use App\Models\SwarmDocker;
use Illuminate\Support\Collection;
use Livewire\Component;
class Show extends Component
class Destinations extends Component
{
public Server $server;
@@ -86,6 +86,6 @@ class Show extends Component
public function render()
{
return view('livewire.server.destination.show');
return view('livewire.server.destinations');
}
}