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); }
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; } }
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 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 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'); }