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-03-22 10:34:15 +00:00
|
|
|
use App\Models\StandaloneDocker;
|
|
|
|
use App\Models\SwarmDocker;
|
2023-06-15 13:38:15 +00:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
class Show extends Component
|
|
|
|
{
|
|
|
|
public Server $server;
|
|
|
|
public Collection|array $networks = [];
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2024-03-22 10:34:15 +00:00
|
|
|
private function createNetworkAndAttachToProxy()
|
|
|
|
{
|
|
|
|
$connectProxyToDockerNetworks = connectProxyToNetworks($this->server);
|
|
|
|
instant_remote_process($connectProxyToDockerNetworks, $this->server, false);
|
|
|
|
}
|
|
|
|
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 {
|
2024-03-22 10:34:33 +00:00
|
|
|
SwarmDocker::create([
|
2024-03-22 10:34:15 +00:00
|
|
|
'name' => $this->server->name . "-" . $name,
|
|
|
|
'network' => $this->name,
|
|
|
|
'server_id' => $this->server->id,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$found = $this->server->standaloneDockers()->where('network', $name)->first();
|
|
|
|
if ($found) {
|
|
|
|
$this->dispatch('error', 'Network already added to this server.');
|
|
|
|
return;
|
|
|
|
} else {
|
2024-03-22 10:34:33 +00:00
|
|
|
StandaloneDocker::create([
|
2024-03-22 10:34:15 +00:00
|
|
|
'name' => $this->server->name . "-" . $name,
|
|
|
|
'network' => $name,
|
|
|
|
'server_id' => $this->server->id,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
$this->createNetworkAndAttachToProxy();
|
|
|
|
}
|
|
|
|
}
|
2023-06-15 13:38:15 +00:00
|
|
|
public function scan()
|
|
|
|
{
|
2023-12-15 14:48:01 +00:00
|
|
|
if ($this->server->isSwarm()) {
|
|
|
|
$alreadyAddedNetworks = $this->server->swarmDockers;
|
|
|
|
} else {
|
|
|
|
$alreadyAddedNetworks = $this->server->standaloneDockers;
|
|
|
|
}
|
2023-06-15 13:38:15 +00:00
|
|
|
$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']);
|
|
|
|
});
|
2023-06-22 07:38:44 +00:00
|
|
|
if ($this->networks->count() === 0) {
|
2023-12-07 18:06:32 +00:00
|
|
|
$this->dispatch('success', 'No new networks found.');
|
2024-03-22 10:34:15 +00:00
|
|
|
return;
|
2023-06-22 07:38:44 +00:00
|
|
|
}
|
2024-03-22 10:34:15 +00:00
|
|
|
$this->dispatch('success', 'Scan done.');
|
2023-06-15 13:38:15 +00:00
|
|
|
}
|
|
|
|
}
|