add destinations

This commit is contained in:
Andras Bacsai
2023-05-02 12:47:52 +02:00
parent 77c86400c0
commit 1a9f360132
16 changed files with 144 additions and 11 deletions

View File

@@ -0,0 +1,51 @@
<?php
namespace App\Http\Livewire\Destination\New;
use App\Models\Server;
use App\Models\StandaloneDocker as ModelsStandaloneDocker;
use Livewire\Component;
class StandaloneDocker extends Component
{
public string $name;
public string $network;
public $servers;
public int|null $server_id = null;
protected $rules = [
'name' => 'required|string',
'network' => 'required|string',
'server_id' => 'required|integer'
];
public function mount()
{
$this->name = generateRandomName();
$this->servers = Server::where('team_id', session('currentTeam')->id)->get();
}
public function setServerId($server_id)
{
$this->server_id = $server_id;
}
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);
runRemoteCommandSync($server, ['docker network create --attachable ' . $this->network], throwError: false);
return redirect()->route('destination.show', $docker->uuid);
}
}

View File

@@ -22,7 +22,7 @@ class ByIp extends Component
public function mount()
{
$this->name = generateRandomName();
$this->name = generateRandomName();
$this->private_keys = PrivateKey::where('team_id', session('currentTeam')->id)->get();
}
public function setPrivateKey($private_key_id)

View File

@@ -20,9 +20,13 @@ class Server extends BaseModel
'team_id',
'private_key_id',
];
public function destinations()
public function standaloneDockers()
{
return $this->hasMany(PrivateKey::class);
return $this->hasMany(StandaloneDocker::class);
}
public function swarmDockers()
{
return $this->hasMany(SwarmDocker::class);
}
public function privateKey()
{

View File

@@ -4,6 +4,11 @@ namespace App\Models;
class StandaloneDocker extends BaseModel
{
protected $fillable = [
'name',
'network',
'server_id',
];
public function applications()
{
return $this->morphMany(Application::class, 'destination');

View File

@@ -8,4 +8,8 @@ class SwarmDocker extends BaseModel
{
return $this->morphMany(Application::class, 'destination');
}
public function server()
{
return $this->belongsTo(Server::class);
}
}