public function getView(Request $request, $id) { $server = Models\Server::select('servers.*', 'nodes.name as a_nodeName', 'users.email as a_ownerEmail', 'locations.long as a_locationName', 'services.name as a_serviceName', DB::raw('IFNULL(service_options.executable, services.executable) as a_serviceExecutable'), 'service_options.docker_image', '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(); if (!$server) { return abort(404); } return view('admin.servers.view', ['server' => $server, '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)->where('server_variables.server_id', $server->id)->get(), '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(), 'db_servers' => Models\DatabaseServer::all()]); }
/** * 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]); }
public function updateStartup($id, array $data, $admin = false) { $server = Models\Server::findOrFail($id); DB::beginTransaction(); try { // Check the startup if (isset($data['startup'])) { $server->startup = $data['startup']; $server->save(); } // Check those Variables $variables = Models\ServiceVariables::select('service_variables.*', DB::raw('COALESCE(server_variables.variable_value, service_variables.default_value) as a_currentValue'))->leftJoin('server_variables', 'server_variables.variable_id', '=', 'service_variables.id')->where('option_id', $server->option)->get(); $variableList = []; if ($variables) { foreach ($variables as &$variable) { // Move on if the new data wasn't even sent if (!isset($data[$variable->env_variable])) { $variableList = array_merge($variableList, [['id' => $variable->id, 'env' => $variable->env_variable, 'val' => $variable->a_currentValue]]); continue; } // Update Empty but skip validation if (empty($data[$variable->env_variable])) { $variableList = array_merge($variableList, [['id' => $variable->id, 'env' => $variable->env_variable, 'val' => null]]); continue; } // Is the variable required? // @TODO: is this even logical to perform this check? if (isset($data[$variable->env_variable]) && empty($data[$variable->env_variable])) { if ($variable->required === 1) { throw new DisplayException('A required service option variable field (' . $variable->env_variable . ') was included in this request but was left blank.'); } } // Variable hidden and/or not user editable if (($variable->user_viewable === 0 || $variable->user_editable === 0) && !$admin) { throw new DisplayException('A service option variable field (' . $variable->env_variable . ') does not exist or you do not have permission to edit it.'); } // Check aganist Regex Pattern if (!is_null($variable->regex) && !preg_match($variable->regex, $data[$variable->env_variable])) { throw new DisplayException('Failed to validate service option variable field (' . $variable->env_variable . ') aganist regex (' . $variable->regex . ').'); } $variableList = array_merge($variableList, [['id' => $variable->id, 'env' => $variable->env_variable, 'val' => $data[$variable->env_variable]]]); } } // Add Variables $environmentVariables = []; $environmentVariables = array_merge($environmentVariables, ['STARTUP' => $server->startup]); foreach ($variableList as $item) { $environmentVariables = array_merge($environmentVariables, [$item['env'] => $item['val']]); // Update model or make a new record if it doesn't exist. $model = Models\ServerVariables::firstOrNew(['variable_id' => $item['id'], 'server_id' => $server->id]); $model->variable_value = $item['val']; $model->save(); } $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' => ['env|overwrite' => $environmentVariables]]]); DB::commit(); return true; } catch (\GuzzleHttp\Exception\TransferException $ex) { DB::rollBack(); throw new DisplayException('An error occured while attempting to update the server configuration.', $ex); } catch (\Exception $ex) { DB::rollBack(); throw $ex; } }