From e8c175f385ac9b84f7e8865d4fff250bf12fd7cd Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Wed, 31 Aug 2016 16:03:37 -0400 Subject: [PATCH] Add IP Aliasing (#72) * complete support for IP Alias's throughout panel Includes a database change and probably better allocation handling anyways closes #37 --- .../Controllers/Admin/ServersController.php | 25 ++++-- .../Controllers/Server/AjaxController.php | 4 +- .../Controllers/Server/ServerController.php | 11 +-- app/Models/Server.php | 14 +++- app/Repositories/ServerRepository.php | 28 ++++--- ...5_234600_fix_column_name_for_databases.php | 31 ------- .../2016_08_30_212718_add_ip_alias.php | 42 ++++++++++ ..._08_30_213301_modify_ip_storage_method.php | 84 +++++++++++++++++++ resources/views/admin/servers/index.blade.php | 2 +- resources/views/admin/servers/view.blade.php | 26 ++++-- .../admin/services/options/view.blade.php | 2 - resources/views/admin/users/view.blade.php | 4 +- resources/views/base/index.blade.php | 2 +- resources/views/server/index.blade.php | 7 +- 14 files changed, 206 insertions(+), 76 deletions(-) delete mode 100644 database/migrations/2016_08_15_234600_fix_column_name_for_databases.php create mode 100644 database/migrations/2016_08_30_212718_add_ip_alias.php create mode 100644 database/migrations/2016_08_30_213301_modify_ip_storage_method.php diff --git a/app/Http/Controllers/Admin/ServersController.php b/app/Http/Controllers/Admin/ServersController.php index 21c7be754..88975c572 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -52,10 +52,17 @@ class ServersController extends Controller public function getIndex(Request $request) { return view('admin.servers.index', [ - 'servers' => Models\Server::select('servers.*', 'nodes.name as a_nodeName', 'users.email as a_ownerEmail') - ->join('nodes', 'servers.node', '=', 'nodes.id') - ->join('users', 'servers.owner', '=', 'users.id') - ->paginate(20), + 'servers' => Models\Server::select( + 'servers.*', + 'nodes.name as a_nodeName', + 'users.email as a_ownerEmail', + 'allocations.ip', + 'allocations.port', + 'allocations.ip_alias' + )->join('nodes', 'servers.node', '=', 'nodes.id') + ->join('users', 'servers.owner', '=', 'users.id') + ->join('allocations', 'servers.allocation', '=', 'allocations.id') + ->paginate(20), ]); } @@ -76,12 +83,16 @@ class ServersController extends Controller 'locations.long as a_locationName', 'services.name as a_serviceName', 'services.executable as a_serviceExecutable', - 'service_options.name as a_servceOptionName' + 'service_options.name as a_servceOptionName', + 'allocations.ip', + 'allocations.port', + 'allocations.ip_alias' )->join('nodes', 'servers.node', '=', 'nodes.id') ->join('users', 'servers.owner', '=', 'users.id') ->join('locations', 'nodes.location', '=', 'locations.id') ->join('services', 'servers.service', '=', 'services.id') ->join('service_options', 'servers.option', '=', 'service_options.id') + ->join('allocations', 'servers.allocation', '=', 'allocations.id') ->where('servers.id', $id) ->first(); @@ -91,8 +102,8 @@ class ServersController extends Controller return view('admin.servers.view', [ 'server' => $server, - 'assigned' => Models\Allocation::select('id', 'ip', 'port')->where('assigned_to', $id)->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(), - 'unassigned' => Models\Allocation::select('id', 'ip', 'port')->where('node', $server->node)->whereNull('assigned_to')->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(), + 'assigned' => Models\Allocation::where('assigned_to', $id)->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(), + 'unassigned' => Models\Allocation::where('node', $server->node)->whereNull('assigned_to')->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(), 'startup' => Models\ServiceVariables::select('service_variables.*', 'server_variables.variable_value as a_serverValue') ->join('server_variables', 'server_variables.variable_id', '=', 'service_variables.id') ->where('service_variables.option_id', $server->option) diff --git a/app/Http/Controllers/Server/AjaxController.php b/app/Http/Controllers/Server/AjaxController.php index d1bf443a6..83f62bb71 100644 --- a/app/Http/Controllers/Server/AjaxController.php +++ b/app/Http/Controllers/Server/AjaxController.php @@ -194,9 +194,11 @@ class AjaxController extends Controller { $server = Server::getByUUID($uuid); + $allocation = Models\Allocation::findOrFail($server->allocation); + $this->authorize('set-connection', $server); - if ($request->input('connection') === $server->ip . ':' . $server->port) { + if ($request->input('connection') === $allocation->ip . ':' . $allocation->port) { return response()->json([ 'error' => 'You are already using this as your default connection.' ], 409); diff --git a/app/Http/Controllers/Server/ServerController.php b/app/Http/Controllers/Server/ServerController.php index c60196948..0f8bc8b7f 100644 --- a/app/Http/Controllers/Server/ServerController.php +++ b/app/Http/Controllers/Server/ServerController.php @@ -195,11 +195,8 @@ class ServerController extends Controller public function getSettings(Request $request, $uuid) { $server = Models\Server::getByUUID($uuid); - // $variables = Models\ServiceVariables::select('service_variables.*', 'server_variables.variable_value as a_serverValue') - // ->join('server_variables', 'server_variables.variable_id', '=', 'service_variables.id') - // ->where('service_variables.option_id', $server->option) - // ->where('server_variables.server_id', $server->id) - // ->get(); + $allocation = Models\Allocation::findOrFail($server->allocation); + $variables = Models\ServiceVariables::select( 'service_variables.*', DB::raw('COALESCE(server_variables.variable_value, service_variables.default_value) as a_serverValue') @@ -217,8 +214,8 @@ class ServerController extends Controller $serverVariables = [ '{{SERVER_MEMORY}}' => $server->memory, - '{{SERVER_IP}}' => $server->ip, - '{{SERVER_PORT}}' => $server->port, + '{{SERVER_IP}}' => $allocation->ip, + '{{SERVER_PORT}}' => $allocation->port, ]; $processed = str_replace(array_keys($serverVariables), array_values($serverVariables), $server->startup); diff --git a/app/Models/Server.php b/app/Models/Server.php index 4fa860f15..02857590b 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -124,10 +124,16 @@ class Server extends Model public static function getUserServers($paginate = null) { - $query = self::select('servers.*', 'nodes.name as nodeName', 'locations.short as a_locationShort') - ->join('nodes', 'servers.node', '=', 'nodes.id') - ->join('locations', 'nodes.location', '=', 'locations.id') - ->where('active', 1); + $query = self::select( + 'servers.*', + 'nodes.name as nodeName', + 'locations.short as a_locationShort', + 'allocations.ip_alias', + 'allocations.port' + )->join('nodes', 'servers.node', '=', 'nodes.id') + ->join('locations', 'nodes.location', '=', 'locations.id') + ->join('allocations', 'servers.allocation', '=', 'allocations.id') + ->where('active', 1); if (self::$user->root_admin !== 1) { $query->whereIn('servers.id', Subuser::accessServers()); diff --git a/app/Repositories/ServerRepository.php b/app/Repositories/ServerRepository.php index 9291d254c..8a815958c 100644 --- a/app/Repositories/ServerRepository.php +++ b/app/Repositories/ServerRepository.php @@ -213,8 +213,7 @@ class ServerRepository 'io' => $data['io'], 'cpu' => $data['cpu'], 'oom_disabled' => (isset($data['oom_disabled'])) ? true : false, - 'ip' => $data['ip'], - 'port' => $data['port'], + 'allocation' => $allocation->id, 'service' => $data['service'], 'option' => $data['option'], 'startup' => $data['startup'], @@ -253,11 +252,11 @@ class ServerRepository 'user' => $server->username, 'build' => [ 'default' => [ - 'ip' => $server->ip, - 'port' => (int) $server->port + 'ip' => $allocation->ip, + 'port' => (int) $allocation->port ], 'ports' => [ - (string) $server->ip => [ (int) $server->port ] + (string) $allocation->ip => [ (int) $allocation->port ] ], 'env' => $environmentVariables, 'memory' => (int) $server->memory, @@ -413,17 +412,20 @@ class ServerRepository try { $server = Models\Server::findOrFail($id); + $allocation = Models\Allocation::findOrFail($server->allocation); if (isset($data['default'])) { list($ip, $port) = explode(':', $data['default']); - if ($ip !== $server->ip || $port !== $server->port) { - $allocation = Models\Allocation::where('ip', $ip)->where('port', $port)->where('assigned_to', $server->id)->first(); - if (!$allocation) { + if ($ip !== $allocation->ip || $port !== $allocation->port) { + $selection = Models\Allocation::where('ip', $ip)->where('port', $port)->where('assigned_to', $server->id)->first(); + if (!$selection) { throw new DisplayException('The requested default connection (' . $ip . ':' . $port . ') is not allocated to this server.'); } - $server->ip = $ip; - $server->port = $port; + $server->allocation = $selection->id; + + // Re-Run to keep updated for rest of function + $allocation = Models\Allocation::findOrFail($server->allocation); } } @@ -437,7 +439,7 @@ class ServerRepository } // Can't remove the assigned IP/Port combo - if ($ip === $server->ip && $port === $server->port) { + if ($ip === $allocation->ip && $port === $allocation->port) { continue; } @@ -513,8 +515,8 @@ class ServerRepository 'json' => [ 'build' => [ 'default' => [ - 'ip' => $server->ip, - 'port' => (int) $server->port + 'ip' => $allocation->ip, + 'port' => (int) $allocation->port ], 'ports|overwrite' => $additionalAssignments, 'memory' => (int) $server->memory, diff --git a/database/migrations/2016_08_15_234600_fix_column_name_for_databases.php b/database/migrations/2016_08_15_234600_fix_column_name_for_databases.php deleted file mode 100644 index e6cd70392..000000000 --- a/database/migrations/2016_08_15_234600_fix_column_name_for_databases.php +++ /dev/null @@ -1,31 +0,0 @@ -renameColumn('server', 'server_id'); - }); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - Schema::table('databases', function (Blueprint $table) { - $table->renameColumn('server_id', 'server'); - }); - } -} diff --git a/database/migrations/2016_08_30_212718_add_ip_alias.php b/database/migrations/2016_08_30_212718_add_ip_alias.php new file mode 100644 index 000000000..4726e5068 --- /dev/null +++ b/database/migrations/2016_08_30_212718_add_ip_alias.php @@ -0,0 +1,42 @@ +text('ip_alias')->nullable()->after('ip'); + }); + + $allocations = DB::select('SELECT id, ip FROM allocations'); + foreach($allocations as $allocation) { + DB::update( + 'UPDATE allocations SET ip_alias = :ip WHERE id = :id', + [ + 'ip' => $allocation->ip, + 'id' => $allocation->id + ] + ); + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('allocations', function (Blueprint $table) { + $table->dropColumn('ip_alias'); + }); + } +} diff --git a/database/migrations/2016_08_30_213301_modify_ip_storage_method.php b/database/migrations/2016_08_30_213301_modify_ip_storage_method.php new file mode 100644 index 000000000..e49d335d5 --- /dev/null +++ b/database/migrations/2016_08_30_213301_modify_ip_storage_method.php @@ -0,0 +1,84 @@ +mediumInteger('allocation')->unsigned()->after('oom_disabled'); + }); + + // Parse All Servers + $servers = DB::select('SELECT id, ip, port, node FROM servers'); + foreach($servers as $server) { + $allocation = DB::select( + 'SELECT id FROM allocations WHERE ip = :ip AND port = :port AND node = :node', + [ + 'ip' => $server->ip, + 'port' => $server->port, + 'node' => $server->node + ] + ); + + if (isset($allocation[0])) { + DB::update( + 'UPDATE servers SET allocation = :alocid WHERE id = :id', + [ + 'alocid' => $allocation[0]->id, + 'id' => $server->id + ] + ); + } + } + + // Updated the server allocations, remove old fields + Schema::table('servers', function (Blueprint $table) { + $table->dropColumn('ip'); + $table->dropColumn('port'); + }); + + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + + Schema::table('servers', function (Blueprint $table) { + $table->text('ip')->after('allocation'); + $table->integer('port')->unsigned()->after('ip'); + }); + + // Find the allocations and reset the servers... + $servers = DB::select('SELECT id, allocation FROM servers'); + foreach($servers as $server) { + $allocation = DB::select('SELECT * FROM allocations WHERE id = :alocid', [ 'alocid' => $server->allocation ]); + + if (isset($allocation[0])) { + DB::update( + 'UPDATE servers SET ip = :ip, port = :port WHERE id = :id', + [ + 'ip' => $allocation[0]->ip, + 'port' => $allocation[0]->port, + 'id' => $server->id + ] + ); + } + } + + Schema::table('servers', function (Blueprint $table) { + $table->dropColumn('allocation'); + }); + } +} diff --git a/resources/views/admin/servers/index.blade.php b/resources/views/admin/servers/index.blade.php index 927c23a5a..23dd5dcb6 100644 --- a/resources/views/admin/servers/index.blade.php +++ b/resources/views/admin/servers/index.blade.php @@ -46,7 +46,7 @@ {{ $server->name }} {{ $server->a_ownerEmail }} {{ $server->a_nodeName }} - {{ $server->ip }}:{{ $server->port }} + {{ $server->ip_alias }}:{{ $server->port }} @if($server->ip !== $server->ip_alias)alias@endif {{ $server->username }} @endforeach diff --git a/resources/views/admin/servers/view.blade.php b/resources/views/admin/servers/view.blade.php index 7498d899c..dd5cb3f46 100644 --- a/resources/views/admin/servers/view.blade.php +++ b/resources/views/admin/servers/view.blade.php @@ -85,11 +85,15 @@ Memory - {{ $server->memory }}MB (Swap: {{ $server->swap }}MB) (OOM Killer: {{ ($server->oom_disabled === 0) ? 'enabled' : 'disabled' }}) + {{ $server->memory }}MB / {{ $server->swap }}MB + + + OOM Killer + {!! ($server->oom_disabled === 0) ? 'enabled' : 'disabled' !!} Disk Space - {{ $server->disk }}MB (Enforced: no) + {{ $server->disk }}MB Block IO Weight @@ -103,9 +107,19 @@ Default Connection {{ $server->ip }}:{{ $server->port }} + + Connection Alias + + @if($server->ip !== $server->ip_alias) + {{ $server->ip_alias }}:{{ $server->port }} + @else + No Alias Assigned + @endif + + Installed - {{ ($server->installed === 1) ? 'Yes' : 'No' }} + {!! ($server->installed === 1) ? 'Yes' : 'No' !!} @@ -213,7 +227,7 @@ ip == $server->ip && $assignment->port == $server->port) checked="checked" @endif name="default" value="{{ $assignment->ip }}:{{ $assignment->port }}"/> - + @endforeach @@ -224,7 +238,7 @@
@@ -237,7 +251,7 @@
diff --git a/resources/views/admin/services/options/view.blade.php b/resources/views/admin/services/options/view.blade.php index b405d751d..d5de1a910 100644 --- a/resources/views/admin/services/options/view.blade.php +++ b/resources/views/admin/services/options/view.blade.php @@ -171,7 +171,6 @@ Name Owner - Connection Updated @@ -180,7 +179,6 @@ {{ $server->name }} {{ $server->a_ownerEmail }} - {{ $server->ip }}:{{ $server->port }} {{ $server->updated_at }} @endforeach diff --git a/resources/views/admin/users/view.blade.php b/resources/views/admin/users/view.blade.php index 64b02a831..6610bd442 100644 --- a/resources/views/admin/users/view.blade.php +++ b/resources/views/admin/users/view.blade.php @@ -95,9 +95,9 @@ + Identifier Server Name Node - Connection @@ -105,9 +105,9 @@ @foreach($servers as $server) + {{ $server->uuidShort }} {{ $server->name }} {{ $server->nodeName }} - {{ $server->ip }}:{{ $server->port }} @if($server->active)Enabled@elseDisabled@endif @endforeach diff --git a/resources/views/base/index.blade.php b/resources/views/base/index.blade.php index 5487d35d9..1027a1604 100644 --- a/resources/views/base/index.blade.php +++ b/resources/views/base/index.blade.php @@ -59,7 +59,7 @@ @endif {{ $server->name }} {{ $server->nodeName }} ({{ $server->a_locationShort }}) - {{ $server->ip }}:{{ $server->port }} + {{ $server->ip_alias }}:{{ $server->port }} -- -- / {{ $server->memory === 0 ? '∞' : $server->memory }} MB -- % diff --git a/resources/views/server/index.blade.php b/resources/views/server/index.blade.php index 61ddf8366..085bb2ed1 100644 --- a/resources/views/server/index.blade.php +++ b/resources/views/server/index.blade.php @@ -94,7 +94,12 @@
Below is a listing of all avaliable IPs and Ports for your service. To change the default connection address for your server, simply click on the one you would like to make default below.