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;
|
2024-11-03 22:08:24 +00:00
|
|
|
use Livewire\Attributes\Locked;
|
2024-11-05 08:36:40 +00:00
|
|
|
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
|
|
|
{
|
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
|
|
|
|
2024-11-05 08:36:40 +00:00
|
|
|
#[Validate(['required', 'string'])]
|
2024-11-03 22:08:24 +00:00
|
|
|
public string $name;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-11-05 08:36:40 +00:00
|
|
|
#[Validate(['required', 'string'])]
|
2024-11-03 22:08:24 +00:00
|
|
|
public string $network;
|
2023-05-02 10:47:52 +00:00
|
|
|
|
2024-11-05 08:36:40 +00:00
|
|
|
#[Validate(['required', 'string'])]
|
2024-11-03 22:08:24 +00:00
|
|
|
public string $serverId;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-11-05 08:36:40 +00:00
|
|
|
#[Validate(['required', 'boolean'])]
|
2024-11-03 22:08:24 +00:00
|
|
|
public bool $isSwarm = false;
|
2023-08-08 09:51:36 +00:00
|
|
|
|
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) {
|
2025-02-18 15:44:05 +00:00
|
|
|
$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 {
|
2025-02-18 15:44:05 +00:00
|
|
|
$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-03-25 12:10:05 +00:00
|
|
|
}
|
2024-11-03 22:08:24 +00:00
|
|
|
$this->generateName();
|
2023-07-28 11:31:47 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2024-11-03 22:08:24 +00:00
|
|
|
public function updatedServerId()
|
2023-08-08 09:51:36 +00:00
|
|
|
{
|
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-08-08 09:51:36 +00:00
|
|
|
|
2023-05-02 10:47:52 +00:00
|
|
|
public function submit()
|
|
|
|
|
{
|
2023-06-13 08:02:58 +00:00
|
|
|
try {
|
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) {
|
2025-01-07 14:31:43 +00:00
|
|
|
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) {
|
2025-01-07 14:31:43 +00:00
|
|
|
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
|
|
|
}
|
2025-01-12 16:26:28 +00:00
|
|
|
$this->redirect(route('destination.show', $docker->uuid));
|
2025-01-07 14:31:43 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-15 13:34:25 +00:00
|
|
|
return handleError($e, $this);
|
2023-06-13 08:02:58 +00:00
|
|
|
}
|
2023-05-02 10:47:52 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|