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;
|
|
|
|
use App\Models\StandaloneDocker as ModelsStandaloneDocker;
|
2023-12-15 14:48:01 +00:00
|
|
|
use App\Models\SwarmDocker;
|
2023-05-04 09:14:37 +00:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
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
|
|
|
{
|
|
|
|
public string $name;
|
|
|
|
public string $network;
|
|
|
|
|
2023-05-04 09:14:37 +00:00
|
|
|
public Collection $servers;
|
2023-06-15 12:41:39 +00:00
|
|
|
public Server $server;
|
2023-12-15 14:48:01 +00:00
|
|
|
public ?int $server_id = null;
|
|
|
|
public bool $is_swarm = false;
|
2023-05-02 10:47:52 +00:00
|
|
|
|
|
|
|
protected $rules = [
|
|
|
|
'name' => 'required|string',
|
|
|
|
'network' => 'required|string',
|
2023-12-15 14:48:01 +00:00
|
|
|
'server_id' => 'required|integer',
|
|
|
|
'is_swarm' => 'boolean'
|
2023-05-02 10:47:52 +00:00
|
|
|
];
|
2023-06-16 10:35:40 +00:00
|
|
|
protected $validationAttributes = [
|
|
|
|
'name' => 'name',
|
|
|
|
'network' => 'network',
|
2023-12-15 14:48:01 +00:00
|
|
|
'server_id' => 'server',
|
|
|
|
'is_swarm' => 'swarm'
|
2023-06-16 10:35:40 +00:00
|
|
|
];
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-05-02 10:47:52 +00:00
|
|
|
public function mount()
|
|
|
|
{
|
2023-06-15 13:38:15 +00:00
|
|
|
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;
|
2023-05-12 11:10:09 +00:00
|
|
|
}
|
2023-05-04 09:14:37 +00:00
|
|
|
}
|
2023-06-15 13:38:15 +00:00
|
|
|
if (request()->query('network_name')) {
|
|
|
|
$this->network = request()->query('network_name');
|
|
|
|
} else {
|
|
|
|
$this->network = new Cuid2(7);
|
|
|
|
}
|
2023-12-15 14:48:01 +00:00
|
|
|
$this->name = str("{$this->servers->first()->name}-{$this->network}")->kebab();
|
2023-07-28 11:31:47 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
|
|
|
public function generate_name()
|
|
|
|
{
|
2023-07-28 11:31:47 +00:00
|
|
|
$this->server = Server::find($this->server_id);
|
2023-12-15 14:48:01 +00:00
|
|
|
$this->name = str("{$this->server->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()
|
|
|
|
{
|
|
|
|
$this->validate();
|
2023-06-13 08:02:58 +00:00
|
|
|
try {
|
2023-06-15 12:41:39 +00:00
|
|
|
$this->server = Server::find($this->server_id);
|
2023-12-15 14:48:01 +00:00
|
|
|
if ($this->is_swarm) {
|
|
|
|
$found = $this->server->swarmDockers()->where('network', $this->network)->first();
|
|
|
|
if ($found) {
|
|
|
|
$this->dispatch('error', 'Network already added to this server.');
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
$docker = SwarmDocker::create([
|
|
|
|
'name' => $this->name,
|
|
|
|
'network' => $this->network,
|
|
|
|
'server_id' => $this->server_id,
|
|
|
|
]);
|
|
|
|
}
|
2023-06-15 12:41:39 +00:00
|
|
|
} else {
|
2023-12-15 14:48:01 +00:00
|
|
|
$found = $this->server->standaloneDockers()->where('network', $this->network)->first();
|
|
|
|
if ($found) {
|
|
|
|
$this->dispatch('error', 'Network already added to this server.');
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
$docker = ModelsStandaloneDocker::create([
|
|
|
|
'name' => $this->name,
|
|
|
|
'network' => $this->network,
|
|
|
|
'server_id' => $this->server_id,
|
|
|
|
]);
|
|
|
|
}
|
2023-06-13 08:02:58 +00:00
|
|
|
}
|
2023-06-15 12:41:39 +00:00
|
|
|
$this->createNetworkAndAttachToProxy();
|
2023-12-07 21:56:55 +00:00
|
|
|
return $this->redirectRoute('destination.show', $docker->uuid, navigate: true);
|
2023-09-11 15:36:30 +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
|
|
|
|
|
|
|
private function createNetworkAndAttachToProxy()
|
|
|
|
{
|
2023-09-22 06:52:07 +00:00
|
|
|
$connectProxyToDockerNetworks = connectProxyToNetworks($this->server);
|
|
|
|
instant_remote_process($connectProxyToDockerNetworks, $this->server, false);
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|
|
|
|
}
|