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 +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 @@ +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 +Network: {{ data_get($docker, 'network') }}
+ @endforeach + @endif