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->ip }}:{{ $server->port }}
{{ $server->ip_alias }}:{{ $server->port }}
@if($server->ip !== $server->ip_alias)alias@endif{{ $server->username }}
{{ $server->memory }}MB
(Swap: {{ $server->swap }}MB) (OOM Killer: {{ ($server->oom_disabled === 0) ? 'enabled' : 'disabled' }}){{ $server->memory }}MB
/ {{ $server->swap }}MB
{{ $server->disk }}MB
(Enforced: no){{ $server->disk }}MB
{{ $server->ip }}:{{ $server->port }}
{{ $server->ip_alias }}:{{ $server->port }}
+ @else
+ No Alias Assigned
+ @endif
+ {{ $server->ip }}:{{ $server->port }}
{{ $server->uuidShort }}
{{ $server->ip }}:{{ $server->port }}
{{ $server->ip }}:{{ $server->port }}
{{ $server->ip_alias }}:{{ $server->port }}