2023-05-02 10:47:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Livewire\Destination\New;
|
|
|
|
|
|
|
|
use App\Models\Server;
|
|
|
|
use App\Models\StandaloneDocker as ModelsStandaloneDocker;
|
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
|
|
|
|
|
|
|
class StandaloneDocker extends Component
|
|
|
|
{
|
|
|
|
public string $name;
|
|
|
|
public string $network;
|
|
|
|
|
2023-05-04 09:14:37 +00:00
|
|
|
public Collection $servers;
|
2023-05-12 11:10:09 +00:00
|
|
|
public int|null $server_id = null;
|
2023-05-02 10:47:52 +00:00
|
|
|
|
|
|
|
protected $rules = [
|
|
|
|
'name' => 'required|string',
|
|
|
|
'network' => 'required|string',
|
|
|
|
'server_id' => 'required|integer'
|
|
|
|
];
|
|
|
|
public function mount()
|
|
|
|
{
|
2023-05-12 11:10:09 +00:00
|
|
|
if (!$this->server_id) {
|
|
|
|
if (request()->query('server_id')) {
|
|
|
|
$this->server_id = request()->query('server_id');
|
|
|
|
} else {
|
2023-05-12 13:39:07 +00:00
|
|
|
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-05-04 08:45:09 +00:00
|
|
|
$this->network = new Cuid2(7);
|
|
|
|
$this->name = generateRandomName();
|
2023-05-02 10:47:52 +00:00
|
|
|
}
|
2023-05-04 08:45:09 +00:00
|
|
|
|
2023-05-02 10:47:52 +00:00
|
|
|
public function submit()
|
|
|
|
{
|
|
|
|
$this->validate();
|
|
|
|
$found = ModelsStandaloneDocker::where('server_id', $this->server_id)->where('network', $this->network)->first();
|
|
|
|
if ($found) {
|
|
|
|
$this->addError('network', 'Network already added to this server.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$docker = ModelsStandaloneDocker::create([
|
|
|
|
'name' => $this->name,
|
|
|
|
'network' => $this->network,
|
|
|
|
'server_id' => $this->server_id,
|
|
|
|
'team_id' => session('currentTeam')->id
|
|
|
|
]);
|
|
|
|
|
|
|
|
$server = Server::find($this->server_id);
|
|
|
|
|
2023-05-03 07:38:53 +00:00
|
|
|
instantRemoteProcess(['docker network create --attachable ' . $this->network], $server, throwError: false);
|
2023-05-02 10:47:52 +00:00
|
|
|
return redirect()->route('destination.show', $docker->uuid);
|
|
|
|
}
|
|
|
|
}
|