Beispiel #1
0
 /**
  * Create a new scheduled task for a given server.
  * @param  int      $id
  * @param  array    $data
  *
  * @throws DisplayException
  * @throws DisplayValidationException
  * @return void
  */
 public function create($id, $data)
 {
     $server = Models\Server::findOrFail($id);
     $validator = Validator::make($data, ['action' => 'string|required', 'data' => 'string|required', 'year' => 'string|sometimes', 'day_of_week' => 'string|sometimes', 'month' => 'string|sometimes', 'day_of_month' => 'string|sometimes', 'hour' => 'string|sometimes', 'minute' => 'string|sometimes']);
     if ($validator->fails()) {
         throw new DisplayValidationException(json_encode($validator->errors()));
     }
     if (!in_array($data['action'], $this->actions)) {
         throw new DisplayException('The action provided is not valid.');
     }
     $cron = $this->defaults;
     foreach ($this->defaults as $setting => $value) {
         if (array_key_exists($setting, $data) && !is_null($data[$setting]) && $data[$setting] !== '') {
             $cron[$setting] = $data[$setting];
         }
     }
     // Check that is this a valid Cron Entry
     try {
         $buildCron = Cron::factory(sprintf('%s %s %s %s %s %s', $cron['minute'], $cron['hour'], $cron['day_of_month'], $cron['month'], $cron['day_of_week'], $cron['year']));
     } catch (\Exception $ex) {
         throw $ex;
     }
     $task = new Models\Task();
     $task->fill(['server' => $server->id, 'active' => 1, 'action' => $data['action'], 'data' => $data['data'], 'queued' => 0, 'year' => $cron['year'], 'day_of_week' => $cron['day_of_week'], 'month' => $cron['month'], 'day_of_month' => $cron['day_of_month'], 'hour' => $cron['hour'], 'minute' => $cron['minute'], 'last_run' => null, 'next_run' => $buildCron->getNextRunDate()]);
     return $task->save();
 }
 /**
  * Adds a new database to a given database server.
  * @param int   $server   Id of the server to add a database for.
  * @param array $options  Array of options for creating that database.
  * @return void
  */
 public function create($server, $options)
 {
     $server = Models\Server::findOrFail($server);
     $validator = Validator::make($options, ['db_server' => 'required|exists:database_servers,id', 'database' => 'required|regex:/^\\w{1,100}$/', 'remote' => 'required|regex:/^[0-9%.]{1,15}$/']);
     if ($validator->fails()) {
         throw new DisplayValidationException($validator->errors());
     }
     DB::beginTransaction();
     try {
         $db = new Models\Database();
         $db->fill(['server_id' => $server->id, 'db_server' => $options['db_server'], 'database' => $server->uuidShort . '_' . $options['database'], 'username' => $server->uuidShort . '_' . str_random(7), 'remote' => $options['remote'], 'password' => Crypt::encrypt(str_random(20))]);
         $db->save();
         // Contact Remote
         $dbr = Models\DatabaseServer::findOrFail($options['db_server']);
         $capsule = new Capsule();
         $capsule->addConnection(['driver' => 'mysql', 'host' => $dbr->host, 'port' => $dbr->port, 'database' => 'mysql', 'username' => $dbr->username, 'password' => Crypt::decrypt($dbr->password), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'options' => [\PDO::ATTR_TIMEOUT => 3]]);
         $capsule->setAsGlobal();
         Capsule::statement('CREATE DATABASE ' . $db->database);
         Capsule::statement('CREATE USER \'' . $db->username . '\'@\'' . $db->remote . '\' IDENTIFIED BY \'' . Crypt::decrypt($db->password) . '\'');
         Capsule::statement('GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX ON ' . $db->database . '.* TO \'' . $db->username . '\'@\'' . $db->remote . '\'');
         Capsule::statement('FLUSH PRIVILEGES');
         DB::commit();
         return true;
     } catch (\Exception $ex) {
         DB::rollback();
         throw $ex;
     }
 }
Beispiel #3
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $tasks = Models\Task::where('queued', 0)->where('active', 1)->where('next_run', '<=', Carbon::now()->toAtomString())->get();
     $this->info(sprintf('Preparing to queue %d tasks.', count($tasks)));
     $bar = $this->output->createProgressBar(count($tasks));
     foreach ($tasks as &$task) {
         $bar->advance();
         $this->dispatch(new SendScheduledTask(Models\Server::findOrFail($task->server), $task));
     }
     $bar->finish();
     $this->info("\nFinished queuing tasks for running.");
 }
Beispiel #4
0
 /**
  * Update Server Build Configuration
  *
  * Updates server build information on panel and on node.
  *
  * @Patch("/servers/{id}/build")
  * @Versions({"v1"})
  * @Transaction({
  *      @Request({
  *          "default": "192.168.0.1:25565",
  *          "add_additional": [
  *              "192.168.0.1:25566",
  *              "192.168.0.1:25567",
  *              "192.168.0.1:25568"
  *          ],
  *          "remove_additional": [],
  *          "memory": 1024,
  *          "swap": 0,
  *          "io": 500,
  *          "cpu": 0,
  *          "disk": 1024
  *      }, headers={"Authorization": "Bearer <token>"}),
  *      @Response(200, body={"name": "New Name"}),
  *      @Response(422)
  * })
  * @Parameters({
  *      @Parameter("id", type="integer", required=true, description="The ID of the server to modify.")
  * })
  */
 public function build(Request $request, $id)
 {
     try {
         throw new BadRequestHttpException('There was an error while attempting to add this node to the system.');
         $server = new ServerRepository();
         $server->changeBuild($id, $request->all());
         return Models\Server::findOrFail($id);
     } catch (DisplayValidationException $ex) {
         throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true));
     } catch (DisplayException $ex) {
         throw new ResourceException($ex->getMessage());
     } catch (\Exception $ex) {
         throw new ServiceUnavailableHttpException('Unable to update server on system due to an error.');
     }
 }
 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);
 }
Beispiel #6
0
 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;
 }