coolify/app/Livewire/Destination/New/Docker.php

104 lines
3.2 KiB
PHP
Raw Permalink Normal View History

2023-05-02 10:47:52 +00:00
<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Destination\New;
2023-05-02 10:47:52 +00:00
use App\Models\Server;
2024-11-03 22:08:24 +00:00
use App\Models\StandaloneDocker;
2023-12-15 14:48:01 +00:00
use App\Models\SwarmDocker;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
2024-11-03 22:08:24 +00:00
use Livewire\Attributes\Locked;
use Livewire\Attributes\Validate;
2023-05-02 10:47:52 +00:00
use Livewire\Component;
2023-05-04 08:45:09 +00:00
use Visus\Cuid2\Cuid2;
2023-05-02 10:47:52 +00:00
2023-12-15 14:48:01 +00:00
class Docker extends Component
2023-05-02 10:47:52 +00:00
{
use AuthorizesRequests;
2024-11-03 22:08:24 +00:00
#[Locked]
public $servers;
2023-05-02 10:47:52 +00:00
2024-11-03 22:08:24 +00:00
#[Locked]
public Server $selectedServer;
2024-06-10 20:43:34 +00:00
#[Validate(['required', 'string'])]
2024-11-03 22:08:24 +00:00
public string $name;
2024-06-10 20:43:34 +00:00
#[Validate(['required', 'string'])]
2024-11-03 22:08:24 +00:00
public string $network;
2023-05-02 10:47:52 +00:00
#[Validate(['required', 'string'])]
2024-11-03 22:08:24 +00:00
public string $serverId;
2024-06-10 20:43:34 +00:00
#[Validate(['required', 'boolean'])]
2024-11-03 22:08:24 +00:00
public bool $isSwarm = false;
2024-11-03 22:08:24 +00:00
public function mount(?string $server_id = null)
2023-05-02 10:47:52 +00:00
{
2024-11-03 22:08:24 +00:00
$this->network = new Cuid2;
$this->servers = Server::isUsable()->get();
if ($server_id) {
$foundServer = $this->servers->find($server_id) ?: $this->servers->first();
if (! $foundServer) {
throw new \Exception('Server not found.');
}
$this->selectedServer = $foundServer;
2024-11-07 09:00:18 +00:00
$this->serverId = $this->selectedServer->id;
2023-06-15 13:38:15 +00:00
} else {
$foundServer = $this->servers->first();
if (! $foundServer) {
throw new \Exception('Server not found.');
}
$this->selectedServer = $foundServer;
2024-11-07 09:00:18 +00:00
$this->serverId = $this->selectedServer->id;
}
2024-11-03 22:08:24 +00:00
$this->generateName();
2023-07-28 11:31:47 +00:00
}
2024-11-03 22:08:24 +00:00
public function updatedServerId()
{
2024-11-03 22:08:24 +00:00
$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();
2023-05-02 10:47:52 +00:00
}
2023-05-02 10:47:52 +00:00
public function submit()
{
2023-06-13 08:02:58 +00:00
try {
$this->authorize('create', StandaloneDocker::class);
2024-11-03 22:08:24 +00:00
$this->validate();
if ($this->isSwarm) {
$found = $this->selectedServer->swarmDockers()->where('network', $this->network)->first();
2023-12-15 14:48:01 +00:00
if ($found) {
throw new \Exception('Network already added to this server.');
} else {
$docker = SwarmDocker::create([
'name' => $this->name,
'network' => $this->network,
'server_id' => $this->selectedServer->id,
]);
2023-12-15 14:48:01 +00:00
}
2023-06-15 12:41:39 +00:00
} else {
2024-11-03 22:08:24 +00:00
$found = $this->selectedServer->standaloneDockers()->where('network', $this->network)->first();
2023-12-15 14:48:01 +00:00
if ($found) {
throw new \Exception('Network already added to this server.');
} else {
$docker = StandaloneDocker::create([
'name' => $this->name,
'network' => $this->network,
'server_id' => $this->selectedServer->id,
]);
2023-12-15 14:48:01 +00:00
}
2023-06-13 08:02:58 +00:00
}
redirectRoute($this, 'destination.show', [$docker->uuid]);
} catch (\Throwable $e) {
return handleError($e, $this);
2023-06-13 08:02:58 +00:00
}
2023-05-02 10:47:52 +00:00
}
}