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 @@ protected static function booted()
'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 @@
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 @@ public function applications()
{
return $this->morphMany(Application::class, 'destination');
}
public function server()
{
return $this->belongsTo(Server::class);
}
}

View File

@ -119,7 +119,7 @@ function formatDockerLabelsToJson($rawOutput): Collection
}
}
if (!function_exists('runRemoteCommandSync')) {
function runRemoteCommandSync($server, array $command)
function runRemoteCommandSync(Server $server, array $command, $throwError = true)
{
$command_string = implode("\n", $command);
$private_key_location = savePrivateKeyForServer($server);
@ -128,6 +128,9 @@ function runRemoteCommandSync($server, array $command)
$output = trim($process->output());
$exitCode = $process->exitCode();
if ($exitCode !== 0) {
if (!$throwError) {
return false;
}
Log::error($output);
throw new \RuntimeException('There was an error running the command.');
}

View File

@ -13,10 +13,12 @@ public function up(): void
{
Schema::create('standalone_dockers', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('uuid')->unique();
$table->string('network');
$table->foreignId('server_id');
$table->unique(['server_id', 'network']);
$table->timestamps();
});
}

View File

@ -13,6 +13,7 @@ public function up(): void
{
Schema::create('swarm_dockers', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('uuid')->unique();
$table->foreignId('server_id');

View File

@ -17,7 +17,7 @@ public function run(): void
{
$server_1 = Server::find(1);
StandaloneDocker::create([
'id' => 1,
'name' => 'Standalone Docker 1',
'network' => 'coolify',
'server_id' => $server_1->id,
]);

View File

@ -16,10 +16,10 @@ class SwarmDockerSeeder extends Seeder
*/
public function run(): void
{
$server_1 = Server::find(1);
$server_2 = Server::find(2);
SwarmDocker::create([
'id' => 1,
'server_id' => $server_1->id,
'name' => 'Swarm Docker 1',
'server_id' => $server_2->id,
]);
}
}

View File

@ -11,4 +11,10 @@
@empty
<p>No servers found.</p>
@endforelse
<h1>Destinations <a href="{{ route('destination.new') }}"><button>New</button></a></h1>
@forelse ($destinations as $destination)
<a href="{{ route('destination.show', [$destination->uuid]) }}">{{ data_get($destination, 'name') }}</a>
@empty
<p>No servers found.</p>
@endforelse
</x-layout>

View File

@ -0,0 +1,4 @@
<x-layout>
<h1>New Destination</h1>
<livewire:destination.new.standalone-docker />
</x-layout>

View File

@ -0,0 +1,11 @@
<x-layout>
<h1>Destination</h1>
<p>Name: {{ data_get($destination, 'name') }}</p>
<p>Server:{{ data_get($destination, 'server.ip') }}:{{ data_get($destination, 'server.port') }} </p>
@if ($destination->getMorphClass() === 'App\Models\StandaloneDocker')
<p>Network: {{ data_get($destination, 'network') }}</p>
@endif
@if ($destination->getMorphClass() === 'App\Models\SwarmDocker')
<p>Uuid: {{ data_get($destination, 'uuid') }}</p>
@endif
</x-layout>

View File

@ -0,0 +1,15 @@
<div>
<form class="flex flex-col" wire:submit.prevent='submit'>
<x-form-input id="name" label="Name" required />
<x-form-input id="network" label="Network" required />
<x-form-input id="server_id" label="Server ID" required />
@foreach ($servers as $key)
<button @if ($server_id == $key->id) class="bg-green-500" @endif
wire:click.prevent="setServerId('{{ $key->id }}')">{{ $key->name }}</button>
@endforeach
<button class="mt-4" type="submit">
Submit
</button>
</form>
</div>

View File

@ -1,4 +1,10 @@
<x-layout>
<h1>Server</h1>
<livewire:server.form :server_id="$server_id" />
<livewire:server.form :server_id="$server->id" />
<h2>Destinations</h2>
@if ($server->standaloneDockers)
@foreach ($server->standaloneDockers as $docker)
<p>Network: {{ data_get($docker, 'network') }}</p>
@endforeach
@endif
</x-layout>

View File

@ -4,6 +4,8 @@
use App\Http\Controllers\HomeController;
use App\Http\Controllers\ProjectController;
use App\Models\InstanceSettings;
use App\Models\StandaloneDocker;
use App\Models\SwarmDocker;
use Illuminate\Support\Facades\Route;
/*
@ -23,9 +25,13 @@
Route::get('/', function () {
$projects = session('currentTeam')->load(['projects'])->projects;
$servers = session('currentTeam')->load(['servers'])->servers;
$destinations = $servers->map(function ($server) {
return $server->standaloneDockers->merge($server->swarmDockers);
})->flatten();
return view('dashboard', [
'servers' => $servers->sortBy('name'),
'projects' => $projects->sortBy('name')
'projects' => $projects->sortBy('name'),
'destinations' => $destinations->sortBy('name'),
]);
})->name('dashboard');
@ -62,11 +68,26 @@
abort(404);
}
return view('server.show', [
'server_id' => $server->id,
'server' => $server,
]);
})->name('server.show');
});
Route::middleware(['auth'])->group(function () {
Route::get('/destination/new', fn () => view('destination.new'))->name('destination.new');
Route::get('/destination/{destination_uuid}', function () {
$standalone_dockers = StandaloneDocker::where('uuid', request()->destination_uuid)->first();
$swarm_dockers = SwarmDocker::where('uuid', request()->destination_uuid)->first();
if (!$standalone_dockers && !$swarm_dockers) {
abort(404);
}
$destination = $standalone_dockers ? $standalone_dockers : $swarm_dockers;
return view('destination.show', [
'destination' => $destination,
]);
})->name('destination.show');
});
Route::middleware(['auth'])->group(function () {
Route::get('/project/new', fn () => view('project.new', ['type' => 'project']))->name('project.new');
Route::get(