/** * 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 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 __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 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']); }
/** * 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; }