/** * Constructor * * @param string $server The server Short UUID */ public function __construct($uuid) { $this->server = Server::getByUUID($uuid); $this->node = Node::getByID($this->server->node); $this->client = Node::guzzleRequest($this->server->node); $this->headers = Server::getGuzzleHeaders($uuid); }
/** * Returns an instance of the database object for the requested node ID. * * @param int $id * @return \Illuminate\Database\Eloquent\Model */ public static function getByID($id) { // The Node is already cached. if (array_key_exists($id, self::$nodes)) { return self::$nodes[$id]; } self::$nodes[$id] = Node::where('id', $id)->first(); return self::$nodes[$id]; }
public function postInstall(Request $request) { $server = Models\Server::where('uuid', $request->input('server'))->first(); if (!$server) { return response()->json(['error' => 'No server by that ID was found on the system.'], 422); } $node = Models\Node::findOrFail($server->node); $hmac = $request->input('signed'); $status = $request->input('installed'); if (base64_decode($hmac) !== hash_hmac('sha256', $server->uuid, $node->daemonSecret, true)) { return response()->json(['error' => 'Signed HMAC was invalid.'], 403); } $server->installed = $status === 'installed' ? 1 : 2; $server->save(); return response()->json(['message' => 'Recieved!'], 200); }
/** * Returns true or false depending on the power status of the requested server. * * @param \Illuminate\Http\Request $request * @param string $uuid * @return \Illuminate\Contracts\View\View */ public function getStatus(Request $request, $uuid) { $server = Models\Server::getByUUID($uuid); if (!$server) { return response()->json([], 404); } $client = Models\Node::guzzleRequest($server->node); try { $res = $client->request('GET', '/server', ['headers' => Models\Server::getGuzzleHeaders($uuid)]); if ($res->getStatusCode() === 200) { return response()->json(json_decode($res->getBody())); } } catch (RequestException $e) { // } return response()->json([]); }
public function getNew(Request $request) { return view('admin.databases.new', ['nodes' => Models\Node::select('nodes.id', 'nodes.name', 'locations.long as a_location')->join('locations', 'locations.id', '=', 'nodes.location')->get()]); }
public function addAllocations($id, array $allocations) { $node = Models\Node::findOrFail($id); DB::beginTransaction(); try { foreach ($allocations as $rawIP => $ports) { $parsedIP = Network::parse($rawIP); foreach ($parsedIP as $ip) { foreach ($ports as $port) { if (!is_int($port) && !preg_match('/^(\\d{1,5})-(\\d{1,5})$/', $port)) { throw new DisplayException('The mapping for ' . $port . ' is invalid and cannot be processed.'); } if (preg_match('/^(\\d{1,5})-(\\d{1,5})$/', $port, $matches)) { foreach (range($matches[1], $matches[2]) as $assignPort) { $alloc = Models\Allocation::firstOrNew(['node' => $node->id, 'ip' => $ip, 'port' => $assignPort]); if (!$alloc->exists) { $alloc->fill(['node' => $node->id, 'ip' => $ip, 'port' => $assignPort, 'assigned_to' => null]); $alloc->save(); } } } else { $alloc = Models\Allocation::firstOrNew(['node' => $node->id, 'ip' => $ip, 'port' => $port]); if (!$alloc->exists) { $alloc->fill(['node' => $node->id, 'ip' => $ip, 'port' => $port, 'assigned_to' => null]); $alloc->save(); } } } } } DB::commit(); return true; } catch (\Exception $ex) { DB::rollBack(); throw $ex; } }
/** * List Specific Node * * Lists specific fields about a server or all fields pertaining to that node. * * @Get("/nodes/{id}/{?fields}") * @Versions({"v1"}) * @Parameters({ * @Parameter("id", type="integer", required=true, description="The ID of the node to get information on."), * @Parameter("fields", type="string", required=false, description="A comma delimidated list of fields to include.") * }) * @Response(200) */ public function view(Request $request, $id, $fields = null) { $query = Models\Node::where('id', $id); if (!is_null($request->input('fields'))) { foreach (explode(',', $request->input('fields')) as $field) { if (!empty($field)) { $query->addSelect($field); } } } try { if (!$query->first()) { throw new NotFoundHttpException('No node by that ID was found.'); } return ['node' => $query->first(), 'allocations' => ['assigned' => Models\Allocation::where('node', $id)->whereNotNull('assigned_to')->get(), 'unassigned' => Models\Allocation::where('node', $id)->whereNull('assigned_to')->get()]]; } catch (NotFoundHttpException $ex) { throw $ex; } catch (\Exception $ex) { throw new BadRequestHttpException('There was an issue with the fields passed in the request.'); } }
/** * 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 getView(Request $request, $uuid, $id) { $server = Models\Server::getByUUID($uuid); $this->authorize('view-task', $server); return view('server.tasks.view', ['server' => $server, 'node' => Models\Node::findOrFail($server->node), 'task' => Models\Task::where('id', $id)->where('server', $server->id)->firstOrFail()]); }
public function __construct($server) { $this->server = $server instanceof Models\Server ? $server : Models\Server::findOrFail($server); $this->node = Models\Node::getByID($this->server->node); $this->client = Models\Node::guzzleRequest($this->server->node); }
public function updateSFTPPassword($id, $password) { $server = Models\Server::findOrFail($id); $node = Models\Node::findOrFail($server->node); $validator = Validator::make(['password' => $password], ['password' => 'required|regex:/^((?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,})$/']); if ($validator->fails()) { throw new DisplayValidationException(json_encode($validator->errors())); } DB::beginTransaction(); $server->sftp_password = Crypt::encrypt($password); try { $server->save(); $client = Models\Node::guzzleRequest($server->node); $client->request('POST', '/server/password', ['headers' => ['X-Access-Token' => $node->daemonSecret, 'X-Access-Server' => $server->uuid], 'json' => ['password' => $password]]); DB::commit(); return true; } catch (\GuzzleHttp\Exception\TransferException $ex) { DB::rollBack(); throw new DisplayException('There was an error while attmping to contact the remote service to change the password.', $ex); } catch (\Exception $ex) { DB::rollBack(); throw $ex; } }
public function getNew(Request $request, $uuid) { $server = Models\Server::getByUUID($uuid); $this->authorize('create-subuser', $server); return view('server.users.new', ['server' => $server, 'node' => Models\Node::find($server->node)]); }
public function postUpdateServerToggleBuild(Request $request, $id) { $server = Models\Server::findOrFail($id); $node = Models\Node::findOrFail($server->node); $client = Models\Node::guzzleRequest($server->node); try { $res = $client->request('POST', '/server/rebuild', ['headers' => ['X-Access-Server' => $server->uuid, 'X-Access-Token' => $node->daemonSecret]]); Alert::success('A rebuild has been queued successfully. It will run the next time this server is booted.')->flash(); } catch (\GuzzleHttp\Exception\TransferException $ex) { Log::warning($ex); Alert::danger('An error occured while attempting to toggle a rebuild.')->flash(); } return redirect()->route('admin.servers.view', ['id' => $id, 'tab' => 'tab_manage']); }
public function deleteNode(Request $request, $id) { $node = Models\Node::findOrFail($id); $servers = Models\Server::where('node', $id)->count(); if ($servers > 0) { Alert::danger('You cannot delete a node with servers currently attached to it.')->flash(); return redirect()->route('admin.nodes.view', ['id' => $id, 'tab' => 'tab_delete']); } $node->delete(); Alert::success('Node successfully deleted.')->flash(); return redirect()->route('admin.nodes'); }
/** * Updates permissions for a given subuser. * @param integer $id The ID of the subuser row in MySQL. (Not the user ID) * @param array $data * @throws DisplayValidationException * @throws DisplayException * @return void */ public function update($id, array $data) { $validator = Validator::make($data, ['permissions' => 'required|array', 'user' => 'required|exists:users,id', 'server' => 'required|exists:servers,id']); if ($validator->fails()) { throw new DisplayValidationException(json_encode($validator->all())); } $subuser = Models\Subuser::findOrFail($id); $server = Models\Server::findOrFail($data['server']); DB::beginTransaction(); try { Models\Permission::where('user_id', $subuser->user_id)->where('server_id', $subuser->server_id)->delete(); $daemonPermissions = $this->coreDaemonPermissions; foreach ($data['permissions'] as $permission) { if (array_key_exists($permission, $this->permissions)) { // Build the daemon permissions array for sending. if (!is_null($this->permissions[$permission])) { array_push($daemonPermissions, $this->permissions[$permission]); } $model = new Models\Permission(); $model->fill(['user_id' => $data['user'], 'server_id' => $data['server'], 'permission' => $permission]); $model->save(); } } // Contact Daemon // We contact even if they don't have any daemon permissions to overwrite // if they did have them previously. $node = Models\Node::getByID($server->node); $client = Models\Node::guzzleRequest($server->node); $res = $client->request('PATCH', '/server', ['headers' => ['X-Access-Server' => $server->uuid, 'X-Access-Token' => $node->daemonSecret], 'json' => ['keys' => [$subuser->daemonSecret => $daemonPermissions]]]); DB::commit(); return true; } catch (\GuzzleHttp\Exception\TransferException $ex) { DB::rollBack(); throw new DisplayException('There was an error attempting to connect to the daemon to update permissions.', $ex); } catch (\Exception $ex) { DB::rollBack(); throw $ex; } return false; }