diff --git a/app/Http/Livewire/Destination/New/StandaloneDocker.php b/app/Http/Livewire/Destination/New/StandaloneDocker.php new file mode 100644 index 000000000..1d22e65fb --- /dev/null +++ b/app/Http/Livewire/Destination/New/StandaloneDocker.php @@ -0,0 +1,51 @@ + '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); + } +} diff --git a/app/Http/Livewire/Server/New/ByIp.php b/app/Http/Livewire/Server/New/ByIp.php index a279ea5ef..f003beb5f 100644 --- a/app/Http/Livewire/Server/New/ByIp.php +++ b/app/Http/Livewire/Server/New/ByIp.php @@ -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) diff --git a/app/Models/Server.php b/app/Models/Server.php index 42739ce23..d72bf0b5d 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -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() { diff --git a/app/Models/StandaloneDocker.php b/app/Models/StandaloneDocker.php index 05724fc54..adb251094 100644 --- a/app/Models/StandaloneDocker.php +++ b/app/Models/StandaloneDocker.php @@ -4,6 +4,11 @@ class StandaloneDocker extends BaseModel { + protected $fillable = [ + 'name', + 'network', + 'server_id', + ]; public function applications() { return $this->morphMany(Application::class, 'destination'); diff --git a/app/Models/SwarmDocker.php b/app/Models/SwarmDocker.php index 6d9a93c40..b0f341b9b 100644 --- a/app/Models/SwarmDocker.php +++ b/app/Models/SwarmDocker.php @@ -8,4 +8,8 @@ public function applications() { return $this->morphMany(Application::class, 'destination'); } + public function server() + { + return $this->belongsTo(Server::class); + } } diff --git a/bootstrap/helpers.php b/bootstrap/helpers.php index d0d8902b8..5f844dadf 100644 --- a/bootstrap/helpers.php +++ b/bootstrap/helpers.php @@ -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.'); } diff --git a/database/migrations/2023_03_27_085020_create_standalone_dockers_table.php b/database/migrations/2023_03_27_085020_create_standalone_dockers_table.php index 814d3eee3..f7f430099 100644 --- a/database/migrations/2023_03_27_085020_create_standalone_dockers_table.php +++ b/database/migrations/2023_03_27_085020_create_standalone_dockers_table.php @@ -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(); }); } diff --git a/database/migrations/2023_03_27_085022_create_swarm_dockers_table.php b/database/migrations/2023_03_27_085022_create_swarm_dockers_table.php index 44e38e3c5..ea85640ba 100644 --- a/database/migrations/2023_03_27_085022_create_swarm_dockers_table.php +++ b/database/migrations/2023_03_27_085022_create_swarm_dockers_table.php @@ -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'); diff --git a/database/seeders/StandaloneDockerSeeder.php b/database/seeders/StandaloneDockerSeeder.php index 77583700f..340f102c9 100644 --- a/database/seeders/StandaloneDockerSeeder.php +++ b/database/seeders/StandaloneDockerSeeder.php @@ -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, ]); diff --git a/database/seeders/SwarmDockerSeeder.php b/database/seeders/SwarmDockerSeeder.php index 2d1a87b53..f1a79d3ba 100644 --- a/database/seeders/SwarmDockerSeeder.php +++ b/database/seeders/SwarmDockerSeeder.php @@ -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, ]); } } diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index 1dfe9afa3..22a1b8310 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -11,4 +11,10 @@ @empty

No servers found.

@endforelse +

Destinations

+ @forelse ($destinations as $destination) + {{ data_get($destination, 'name') }} + @empty +

No servers found.

+ @endforelse diff --git a/resources/views/destination/new.blade.php b/resources/views/destination/new.blade.php new file mode 100644 index 000000000..a3be8a53b --- /dev/null +++ b/resources/views/destination/new.blade.php @@ -0,0 +1,4 @@ + +

New Destination

+ +
diff --git a/resources/views/destination/show.blade.php b/resources/views/destination/show.blade.php new file mode 100644 index 000000000..51379c451 --- /dev/null +++ b/resources/views/destination/show.blade.php @@ -0,0 +1,11 @@ + +

Destination

+

Name: {{ data_get($destination, 'name') }}

+

Server:{{ data_get($destination, 'server.ip') }}:{{ data_get($destination, 'server.port') }}

+ @if ($destination->getMorphClass() === 'App\Models\StandaloneDocker') +

Network: {{ data_get($destination, 'network') }}

+ @endif + @if ($destination->getMorphClass() === 'App\Models\SwarmDocker') +

Uuid: {{ data_get($destination, 'uuid') }}

+ @endif +
diff --git a/resources/views/livewire/destination/new/standalone-docker.blade.php b/resources/views/livewire/destination/new/standalone-docker.blade.php new file mode 100644 index 000000000..28b0b5f45 --- /dev/null +++ b/resources/views/livewire/destination/new/standalone-docker.blade.php @@ -0,0 +1,15 @@ +
+
+ + + + @foreach ($servers as $key) + + @endforeach + + + +
diff --git a/resources/views/server/show.blade.php b/resources/views/server/show.blade.php index 43bfb21c4..1fb710c42 100644 --- a/resources/views/server/show.blade.php +++ b/resources/views/server/show.blade.php @@ -1,4 +1,10 @@

Server

- + +

Destinations

+ @if ($server->standaloneDockers) + @foreach ($server->standaloneDockers as $docker) +

Network: {{ data_get($docker, 'network') }}

+ @endforeach + @endif
diff --git a/routes/web.php b/routes/web.php index f469a0394..deb7be6f0 100644 --- a/routes/web.php +++ b/routes/web.php @@ -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(