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

85 lines
2.6 KiB
PHP
Raw Normal View History

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