wip: swarm

This commit is contained in:
Andras Bacsai
2023-12-15 15:48:01 +01:00
parent 967fca9eca
commit c6b8eabe10
10 changed files with 112 additions and 60 deletions

View File

@@ -4,29 +4,32 @@ namespace App\Livewire\Destination\New;
use App\Models\Server;
use App\Models\StandaloneDocker as ModelsStandaloneDocker;
use App\Models\SwarmDocker;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Str;
use Livewire\Component;
use Visus\Cuid2\Cuid2;
class StandaloneDocker extends Component
class Docker extends Component
{
public string $name;
public string $network;
public Collection $servers;
public Server $server;
public int|null $server_id = null;
public ?int $server_id = null;
public bool $is_swarm = false;
protected $rules = [
'name' => 'required|string',
'network' => 'required|string',
'server_id' => 'required|integer'
'server_id' => 'required|integer',
'is_swarm' => 'boolean'
];
protected $validationAttributes = [
'name' => 'name',
'network' => 'network',
'server_id' => 'server'
'server_id' => 'server',
'is_swarm' => 'swarm'
];
public function mount()
@@ -43,13 +46,13 @@ class StandaloneDocker extends Component
} else {
$this->network = new Cuid2(7);
}
$this->name = Str::kebab("{$this->servers->first()->name}-{$this->network}");
$this->name = str("{$this->servers->first()->name}-{$this->network}")->kebab();
}
public function generate_name()
{
$this->server = Server::find($this->server_id);
$this->name = Str::kebab("{$this->server->name}-{$this->network}");
$this->name = str("{$this->server->name}-{$this->network}")->kebab();
}
public function submit()
@@ -57,17 +60,30 @@ class StandaloneDocker extends Component
$this->validate();
try {
$this->server = Server::find($this->server_id);
$found = $this->server->standaloneDockers()->where('network', $this->network)->first();
if ($found) {
$this->createNetworkAndAttachToProxy();
$this->dispatch('error', 'Network already added to this server.');
return;
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,
]);
}
} else {
$docker = ModelsStandaloneDocker::create([
'name' => $this->name,
'network' => $this->network,
'server_id' => $this->server_id,
]);
$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,
]);
}
}
$this->createNetworkAndAttachToProxy();
return $this->redirectRoute('destination.show', $docker->uuid, navigate: true);

View File

@@ -13,7 +13,11 @@ class Show extends Component
public function scan()
{
$alreadyAddedNetworks = $this->server->standaloneDockers;
if ($this->server->isSwarm()) {
$alreadyAddedNetworks = $this->server->swarmDockers;
} else {
$alreadyAddedNetworks = $this->server->standaloneDockers;
}
$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';

View File

@@ -6,13 +6,12 @@ use App\Models\Project;
use App\Models\Server;
use Countable;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Livewire\Component;
class Select extends Component
{
public $current_step = 'type';
public ?int $server = null;
public ?Server $server = null;
public string $type;
public string $server_id;
public string $destination_uuid;
@@ -31,7 +30,7 @@ class Select extends Component
public ?string $search = null;
protected $queryString = [
'server',
'server_id',
'search'
];
@@ -111,7 +110,7 @@ class Select extends Component
$this->setServer($server);
}
if (!is_null($this->server)) {
$foundServer = $this->servers->where('id', $this->server)->first();
$foundServer = $this->servers->where('id', $this->server->id)->first();
if ($foundServer) {
return $this->setServer($foundServer);
}
@@ -122,6 +121,7 @@ class Select extends Component
public function setServer(Server $server)
{
$this->server_id = $server->id;
$this->server = $server;
$this->standaloneDockers = $server->standaloneDockers;
$this->swarmDockers = $server->swarmDockers;
$this->current_step = 'destinations';