add new server

add new private key
check server connection
This commit is contained in:
Andras Bacsai 2023-04-26 15:38:50 +02:00
parent 2c68eed072
commit 2487dde69e
17 changed files with 145 additions and 20 deletions

View File

@ -10,6 +10,7 @@ class Form extends Component
{
public $server_id;
public Server $server;
public $uptime;
protected $rules = [
'server.name' => 'required|min:6',
@ -22,6 +23,10 @@ public function mount()
{
$this->server = Server::find($this->server_id);
}
public function checkConnection()
{
$this->uptime = runRemoteCommandSync($this->server, ['uptime']);
}
public function submit()
{
$this->validate();

View File

@ -0,0 +1,55 @@
<?php
namespace App\Http\Livewire\Server\New;
use App\Models\PrivateKey;
use App\Models\Server;
use Livewire\Component;
class ByIp extends Component
{
public $private_keys;
public int $private_key_id;
public $new_private_key_name;
public $new_private_key_description;
public $new_private_key_value;
public string $ip;
public string $user = 'root';
public int $port = 22;
public function mount()
{
$this->private_keys = PrivateKey::where('team_id', session('currentTeam')->id)->get();
}
public function setPrivateKey($private_key_id)
{
$this->private_key_id = $private_key_id;
}
public function addPrivateKey()
{
$this->new_private_key_value = trim($this->new_private_key_value);
if (!str_ends_with($this->new_private_key_value, "\n")) {
$this->new_private_key_value .= "\n";
}
PrivateKey::create([
'name' => $this->new_private_key_name,
'description' => $this->new_private_key_description,
'private_key' => $this->new_private_key_value,
'team_id' => session('currentTeam')->id
]);
session('currentTeam')->privateKeys = $this->private_keys = PrivateKey::where('team_id', session('currentTeam')->id)->get();
}
public function submit()
{
$server = Server::create([
'name' => fake()->company,
'ip' => $this->ip,
'user' => $this->user,
'port' => $this->port,
'team_id' => session('currentTeam')->id,
'private_key_id' => $this->private_key_id
]);
return redirect()->route('server.show', $server->uuid);
}
}

View File

@ -4,6 +4,12 @@
class PrivateKey extends BaseModel
{
protected $fillable = [
'name',
'description',
'private_key',
'team_id',
];
public function servers()
{
return $this->hasMany(Server::class);

View File

@ -12,6 +12,14 @@ protected static function booted()
]);
});
}
protected $fillable = [
'name',
'ip',
'user',
'port',
'team_id',
'private_key_id',
];
public function destinations()
{
return $this->hasMany(PrivateKey::class);

View File

@ -4,6 +4,9 @@
class ServerSetting extends BaseModel
{
protected $fillable = [
'server_id'
];
public function server()
{
return $this->belongsTo(Server::class);

View File

@ -12,13 +12,16 @@ class Team extends BaseModel
'name',
'personal_team'
];
public function projects() {
public function projects()
{
return $this->hasMany(Project::class);
}
public function servers() {
public function servers()
{
return $this->hasMany(Server::class);
}
public function applications() {
public function applications()
{
return $this->hasManyThrough(Application::class, Project::class);
}
}

View File

@ -52,7 +52,8 @@ protected static function boot()
$model->uuid = (string) new Cuid2(7);
});
}
public function isRoot() {
public function isRoot()
{
return $this->id == 0;
}
public function teams()

View File

@ -17,6 +17,7 @@ public function up(): void
$table->string('name');
$table->string('description')->nullable();
$table->longText('private_key');
$table->foreignId('team_id');
$table->timestamps();
});
}

View File

@ -4,6 +4,7 @@
use App\Models\PrivateKey;
use App\Models\Server;
use App\Models\Team;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
@ -14,8 +15,9 @@ class PrivateKeySeeder extends Seeder
*/
public function run(): void
{
$team_1 = Team::find(0);
PrivateKey::create([
"id" => 1,
"team_id" => $team_1->id,
"name" => "Testing-host",
"description" => "This is a test docker container",
"private_key" => "-----BEGIN OPENSSH PRIVATE KEY-----
@ -26,9 +28,10 @@ public function run(): void
uZx9iFkCELtxrh31QJ68AAAAEXNhaWxANzZmZjY2ZDJlMmRkAQIDBA==
-----END OPENSSH PRIVATE KEY-----
"
]);
PrivateKey::create([
"id" => 2,
"team_id" => $team_1->id,
"name" => "development-github-app",
"description" => "This is the key for using the development GitHub app",
"private_key" => "-----BEGIN RSA PRIVATE KEY-----
@ -60,7 +63,7 @@ public function run(): void
-----END RSA PRIVATE KEY-----"
]);
PrivateKey::create([
"id" => 3,
"team_id" => $team_1->id,
"name" => "development-gitlab-app",
"description" => "This is the key for using the development Gitlab app",
"private_key" => "asdf"

View File

@ -17,7 +17,6 @@ public function run(): void
$root_team = Team::find(0);
$private_key_1 = PrivateKey::find(1);
Server::create([
'id' => 1,
'name' => "testing-local-docker-container",
'description' => "This is a test docker container",
'ip' => "coolify-testing-host",
@ -25,13 +24,11 @@ public function run(): void
'private_key_id' => $private_key_1->id,
]);
Server::create([
'id' => 2,
'name' => "testing-local-docker-container-2",
'description' => "This is a test docker container",
'ip' => "coolify-testing-host-2",
'team_id' => $root_team->id,
'private_key_id' => $private_key_1->id,
]);
}
}

View File

@ -25,9 +25,16 @@
*
@endif
</label>
<input type={{ $type }} id={{ $id }} wire:model.defer={{ $id }}
@if ($required) required @endif @if ($disabled) disabled @endif
@if ($readonly) readOnly disabled @endif />
@if ($type === 'textarea')
<textarea type={{ $type }} id={{ $id }} wire:model.defer={{ $id }}
@if ($required) required @endif @if ($disabled) disabled @endif
@if ($readonly) readOnly disabled @endif></textarea>
@else
<input type={{ $type }} id={{ $id }} wire:model.defer={{ $id }}
@if ($required) required @endif @if ($disabled) disabled @endif
@if ($readonly) readOnly disabled @endif />
@endif
@error($id)
<div class="text-red-500">{{ $message }}</div>
@enderror

View File

@ -5,9 +5,9 @@
@empty
<p>No projects found.</p>
@endforelse
<h1>Servers</h1>
<h1>Servers <a href="{{ route('server.new') }}"><button>New</button></a></h1>
@forelse ($servers as $server)
<a href="{{ route('server.dashboard', [$server->uuid]) }}">{{ data_get($server, 'name') }}</a>
<a href="{{ route('server.show', [$server->uuid]) }}">{{ data_get($server, 'name') }}</a>
@empty
<p>No servers found.</p>
@endforelse

View File

@ -11,8 +11,15 @@
<x-form-input type="number" id="server.port" label="Port" required />
</div>
</div>
<button class="w-16 mt-4" type="submit">
Submit
</button>
<div>
<button class="w-16 mt-4" type="submit">
Submit
</button>
<button wire:click.prevent='checkConnection'>Check Connection</button>
</div>
</form>
@isset($uptime)
<p>Connection OK</p>
<p>Uptime: {{ $uptime }}</p>
@endisset
</div>

View File

@ -0,0 +1,24 @@
<div>
<form wire:submit.prevent='submit'>
<x-form-input id="ip" label="IP Address" required />
<x-form-input id="user" label="User" />
<x-form-input type="number" id="port" label="Port" />
<button type="submit">
Submit
</button>
</form>
<div>Select a private key:</div>
@foreach ($private_keys as $key)
<button @if ($private_key_id == $key->id) class="bg-green-500" @endif
wire:click="setPrivateKey('{{ $key->id }}')">{{ $key->name }}</button>
@endforeach
<div> Add a new One:</div>
<form wire:submit.prevent='addPrivateKey'>
<x-form-input id="new_private_key_name" label="Name" required />
<x-form-input id="new_private_key_description" label="Longer Description" />
<x-form-input type="textarea" id="new_private_key_value" label="Private Key" required />
<button type="submit">
Submit
</button>
</form>
</div>

View File

@ -0,0 +1,4 @@
<x-layout>
<h1>New Server</h1>
<livewire:server.new.by-ip />
</x-layout>

View File

@ -55,15 +55,16 @@
});
Route::middleware(['auth'])->group(function () {
Route::get('/server/new', fn () => view('server.new'))->name('server.new');
Route::get('/server/{server_uuid}', function () {
$server = session('currentTeam')->load(['servers'])->servers->firstWhere('uuid', request()->server_uuid);
if (!$server) {
abort(404);
}
return view('server.dashboard', [
return view('server.show', [
'server_id' => $server->id,
]);
})->name('server.dashboard');
})->name('server.show');
});
Route::middleware(['auth'])->group(function () {