coolify/app/Livewire/Destination/Show.php

103 lines
3.2 KiB
PHP
Raw Permalink Normal View History

2023-06-15 13:38:15 +00:00
<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Destination;
2023-06-15 13:38:15 +00:00
use App\Models\Server;
2024-11-03 22:08:24 +00:00
use App\Models\StandaloneDocker;
use App\Models\SwarmDocker;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Validate;
2023-06-15 13:38:15 +00:00
use Livewire\Component;
class Show extends Component
{
use AuthorizesRequests;
#[Locked]
public $destination;
2024-06-10 20:43:34 +00:00
#[Validate(['string', 'required'])]
public string $name;
#[Validate(['string', 'required'])]
public string $network;
2024-06-10 20:43:34 +00:00
#[Validate(['string', 'required'])]
public string $serverIp;
public function mount(string $destination_uuid)
2024-03-22 10:34:15 +00:00
{
try {
2024-11-03 22:08:24 +00:00
$destination = StandaloneDocker::whereUuid($destination_uuid)->first() ??
SwarmDocker::whereUuid($destination_uuid)->firstOrFail();
2024-06-10 20:43:34 +00:00
2024-11-03 22:08:24 +00:00
$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');
2024-03-22 10:34:15 +00:00
}
$this->destination = $destination;
$this->syncData();
} catch (\Throwable $e) {
return handleError($e, $this);
2024-03-22 10:34:15 +00:00
}
}
2024-06-10 20:43:34 +00:00
public function syncData(bool $toModel = false)
2023-06-15 13:38:15 +00:00
{
if ($toModel) {
$this->validate();
$this->destination->name = $this->name;
$this->destination->network = $this->network;
$this->destination->server->ip = $this->serverIp;
$this->destination->save();
2023-12-15 14:48:01 +00:00
} else {
$this->name = $this->destination->name;
$this->network = $this->destination->network;
$this->serverIp = $this->destination->server->ip;
}
}
public function submit()
{
try {
$this->authorize('update', $this->destination);
$this->syncData(true);
$this->dispatch('success', 'Destination saved.');
} catch (\Throwable $e) {
return handleError($e, $this);
2023-12-15 14:48:01 +00:00
}
}
public function delete()
{
try {
$this->authorize('delete', $this->destination);
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();
2024-06-10 20:43:34 +00:00
return redirect()->route('destination.index');
} catch (\Throwable $e) {
return handleError($e, $this);
2023-06-22 07:38:44 +00:00
}
}
public function render()
{
return view('livewire.destination.show');
2023-06-15 13:38:15 +00:00
}
}