/** * [postSetConnection description] * @param Request $request * @param string $uuid * @return \Illuminate\Http\Response */ public function postSetConnection(Request $request, $uuid) { $server = Models\Server::getByUUID($uuid); $allocation = Models\Allocation::findOrFail($server->allocation); $this->authorize('set-connection', $server); if ($request->input('connection') === $allocation->ip . ':' . $allocation->port) { return response()->json(['error' => 'You are already using this as your default connection.'], 409); } try { $repo = new Repositories\ServerRepository(); $repo->changeBuild($server->id, ['default' => $request->input('connection')]); return response('The default connection for this server has been updated. Please be aware that you will need to restart your server for this change to go into effect.'); } catch (DisplayValidationException $ex) { return response()->json(['error' => json_decode($ex->getMessage(), true)], 503); } catch (DisplayException $ex) { return response()->json(['error' => $ex->getMessage()], 503); } catch (\Exception $ex) { Log::error($ex); return response()->json(['error' => 'An unhandled exception occured while attemping to modify the default connection for this server.'], 503); } }
/** * Renders server settings page. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Contracts\View\View */ public function getSettings(Request $request, $uuid) { $server = Models\Server::getByUUID($uuid); $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'))->leftJoin('server_variables', 'server_variables.variable_id', '=', 'service_variables.id')->where('service_variables.option_id', $server->option)->where('server_variables.server_id', $server->id)->get(); $service = Models\Service::select(DB::raw('IFNULL(service_options.executable, services.executable) as executable'))->leftJoin('service_options', 'service_options.parent_service', '=', 'services.id')->where('service_options.id', $server->option)->where('services.id', $server->service)->first(); $serverVariables = ['{{SERVER_MEMORY}}' => $server->memory, '{{SERVER_IP}}' => $allocation->ip, '{{SERVER_PORT}}' => $allocation->port]; $processed = str_replace(array_keys($serverVariables), array_values($serverVariables), $server->startup); foreach ($variables as &$variable) { $replace = $variable->user_viewable === 1 ? $variable->a_serverValue : '**'; $processed = str_replace('{{' . $variable->env_variable . '}}', $replace, $processed); } return view('server.settings', ['server' => $server, 'databases' => Models\Database::select('databases.*', 'database_servers.host as a_host', 'database_servers.port as a_port')->where('server_id', $server->id)->join('database_servers', 'database_servers.id', '=', 'databases.db_server')->get(), 'node' => Models\Node::find($server->node), 'variables' => $variables->where('user_viewable', 1), 'service' => $service, 'processedStartup' => $processed]); }
/** * [changeBuild description] * @param integer $id * @param array $data * @return boolean */ public function changeBuild($id, array $data) { $validator = Validator::make($data, ['default' => ['string', 'regex:/^(\\d|[1-9]\\d|1\\d\\d|2([0-4]\\d|5[0-5]))\\.(\\d|[1-9]\\d|1\\d\\d|2([0-4]\\d|5[0-5]))\\.(\\d|[1-9]\\d|1\\d\\d|2([0-4]\\d|5[0-5]))\\.(\\d|[1-9]\\d|1\\d\\d|2([0-4]\\d|5[0-5])):(\\d{1,5})$/'], 'add_additional' => 'nullable|array', 'remove_additional' => 'nullable|array', 'memory' => 'integer|min:0', 'swap' => 'integer|min:-1', 'io' => 'integer|min:10|max:1000', 'cpu' => 'integer|min:0', 'disk' => 'integer|min:0']); // Run validator, throw catchable and displayable exception if it fails. // Exception includes a JSON result of failed validation rules. if ($validator->fails()) { throw new DisplayValidationException($validator->errors()); } DB::beginTransaction(); try { $server = Models\Server::findOrFail($id); $allocation = Models\Allocation::findOrFail($server->allocation); $newBuild = []; if (isset($data['default'])) { list($ip, $port) = explode(':', $data['default']); if ($ip !== $allocation->ip || (int) $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->allocation = $selection->id; $newBuild['default'] = ['ip' => $ip, 'port' => (int) $port]; // Re-Run to keep updated for rest of function $allocation = Models\Allocation::findOrFail($server->allocation); } } $newPorts = false; // Remove Assignments if (isset($data['remove_additional'])) { $newPorts = true; foreach ($data['remove_additional'] as $id => $combo) { list($ip, $port) = explode(':', $combo); // Invalid, not worth killing the whole thing, we'll just skip over it. if (!filter_var($ip, FILTER_VALIDATE_IP) || !preg_match('/^(\\d{1,5})$/', $port)) { continue; } // Can't remove the assigned IP/Port combo if ($ip === $allocation->ip && $port === $allocation->port) { continue; } Models\Allocation::where('ip', $ip)->where('port', $port)->where('assigned_to', $server->id)->update(['assigned_to' => null]); } } // Add Assignments if (isset($data['add_additional'])) { $newPorts = true; foreach ($data['add_additional'] as $id => $combo) { list($ip, $port) = explode(':', $combo); // Invalid, not worth killing the whole thing, we'll just skip over it. if (!filter_var($ip, FILTER_VALIDATE_IP) || !preg_match('/^(\\d{1,5})$/', $port)) { continue; } // Don't allow double port assignments if (Models\Allocation::where('port', $port)->where('assigned_to', $server->id)->count() !== 0) { continue; } Models\Allocation::where('ip', $ip)->where('port', $port)->whereNull('assigned_to')->update(['assigned_to' => $server->id]); } } // Loop All Assignments $additionalAssignments = []; $assignments = Models\Allocation::where('assigned_to', $server->id)->get(); foreach ($assignments as &$assignment) { if (array_key_exists((string) $assignment->ip, $additionalAssignments)) { array_push($additionalAssignments[(string) $assignment->ip], (int) $assignment->port); } else { $additionalAssignments[(string) $assignment->ip] = [(int) $assignment->port]; } } if ($newPorts === true) { $newBuild['ports|overwrite'] = $additionalAssignments; } // @TODO: verify that server can be set to this much memory without // going over node limits. if (isset($data['memory'])) { $server->memory = $data['memory']; } if (isset($data['swap'])) { $server->swap = $data['swap']; } // @TODO: verify that server can be set to this much disk without // going over node limits. if (isset($data['disk'])) { $server->disk = $data['disk']; } if (isset($data['cpu'])) { $server->cpu = $data['cpu']; } if (isset($data['io'])) { $server->io = $data['io']; } // Try save() here so if it fails we haven't contacted the daemon // This won't be committed unless the HTTP request succeedes anyways $server->save(); $newBuild['memory'] = (int) $server->memory; $newBuild['swap'] = (int) $server->swap; $newBuild['io'] = (int) $server->io; $newBuild['cpu'] = (int) $server->cpu; $newBuild['disk'] = (int) $server->disk; $node = Models\Node::getByID($server->node); $client = Models\Node::guzzleRequest($server->node); $client->request('PATCH', '/server', ['headers' => ['X-Access-Server' => $server->uuid, 'X-Access-Token' => $node->daemonSecret], 'json' => ['build' => $newBuild]]); DB::commit(); return true; } catch (\GuzzleHttp\Exception\TransferException $ex) { DB::rollBack(); throw new DisplayException('An error occured while attempting to update the configuration.', $ex); } catch (\Exception $ex) { DB::rollBack(); throw $ex; } }