From 38eae88bd07d02216c21e17ef666209a83e925e2 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 1 Sep 2016 21:16:38 -0400 Subject: [PATCH 1/2] Add support for suspension --- .../Controllers/Admin/ServersController.php | 38 +++++++++++++ app/Http/Middleware/CheckServer.php | 8 ++- app/Http/Routes/AdminRoutes.php | 34 +++++++---- app/Repositories/ServerRepository.php | 56 +++++++++++++++++-- ...9_01_193520_add_suspension_for_servers.php | 31 ++++++++++ resources/views/admin/servers/index.blade.php | 4 +- resources/views/admin/servers/view.blade.php | 37 +++++++++++- resources/views/base/index.blade.php | 4 +- resources/views/errors/suspended.blade.php | 36 ++++++++++++ 9 files changed, 223 insertions(+), 25 deletions(-) create mode 100644 database/migrations/2016_09_01_193520_add_suspension_for_servers.php create mode 100644 resources/views/errors/suspended.blade.php diff --git a/app/Http/Controllers/Admin/ServersController.php b/app/Http/Controllers/Admin/ServersController.php index 88975c572..fb2c568ea 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -424,4 +424,42 @@ class ServersController extends Controller ])->withInput(); } + public function postSuspendServer(Request $request, $id) + { + try { + $repo = new ServerRepository; + $repo->suspend($id); + Alert::success('Server has been suspended on the system. All running processes have been stopped and will not be startable until it is un-suspended.'); + } catch (\Pterodactyl\Exceptions\DisplayException $e) { + Alert::danger($e->getMessage())->flash(); + } catch(\Exception $e) { + Log::error($e); + Alert::danger('An unhandled exception occured while attemping to suspend this server. Please try again.')->flash(); + } finally { + return redirect()->route('admin.servers.view', [ + 'id' => $id, + 'tab' => 'tab_manage' + ]); + } + } + + public function postUnsuspendServer(Request $request, $id) + { + try { + $repo = new ServerRepository; + $repo->unsuspend($id); + Alert::success('Server has been unsuspended on the system. Access has been re-enabled.'); + } catch (\Pterodactyl\Exceptions\DisplayException $e) { + Alert::danger($e->getMessage())->flash(); + } catch(\Exception $e) { + Log::error($e); + Alert::danger('An unhandled exception occured while attemping to unsuspend this server. Please try again.')->flash(); + } finally { + return redirect()->route('admin.servers.view', [ + 'id' => $id, + 'tab' => 'tab_manage' + ]); + } + } + } diff --git a/app/Http/Middleware/CheckServer.php b/app/Http/Middleware/CheckServer.php index c4b06da48..7ae0c115f 100644 --- a/app/Http/Middleware/CheckServer.php +++ b/app/Http/Middleware/CheckServer.php @@ -46,11 +46,15 @@ class CheckServer $server = Server::getByUUID($request->route()->server); if (!$server) { - return response()->view('errors.403', [], 403); + return response()->view('errors.404', [], 404); + } + + if ($server->suspended === 1) { + return response()->view('errors.suspended', [], 403); } if ($server->installed !== 1) { - return response()->view('errors.installing', [], 503); + return response()->view('errors.installing', [], 403); } return $next($request); diff --git a/app/Http/Routes/AdminRoutes.php b/app/Http/Routes/AdminRoutes.php index 53f1a23a4..e4c2a09f4 100644 --- a/app/Http/Routes/AdminRoutes.php +++ b/app/Http/Routes/AdminRoutes.php @@ -129,21 +129,21 @@ class AdminRoutes { ]); // Assorted Page Helpers - $router->post('/new/get-nodes', [ - 'uses' => 'Admin\ServersController@postNewServerGetNodes' - ]); + $router->post('/new/get-nodes', [ + 'uses' => 'Admin\ServersController@postNewServerGetNodes' + ]); - $router->post('/new/get-ips', [ - 'uses' => 'Admin\ServersController@postNewServerGetIps' - ]); + $router->post('/new/get-ips', [ + 'uses' => 'Admin\ServersController@postNewServerGetIps' + ]); - $router->post('/new/service-options', [ - 'uses' => 'Admin\ServersController@postNewServerServiceOptions' - ]); + $router->post('/new/service-options', [ + 'uses' => 'Admin\ServersController@postNewServerServiceOptions' + ]); - $router->post('/new/service-variables', [ - 'uses' => 'Admin\ServersController@postNewServerServiceVariables' - ]); + $router->post('/new/service-variables', [ + 'uses' => 'Admin\ServersController@postNewServerServiceVariables' + ]); // End Assorted Page Helpers // View Specific Server @@ -179,6 +179,16 @@ class AdminRoutes { 'uses' => 'Admin\ServersController@postUpdateServerUpdateBuild' ]); + // Suspend Server + $router->post('/view/{id}/suspend', [ + 'uses' => 'Admin\ServersController@postSuspendServer' + ]); + + // Unsuspend Server + $router->post('/view/{id}/unsuspend', [ + 'uses' => 'Admin\ServersController@postUnsuspendServer' + ]); + // Change Install Status $router->post('/view/{id}/installed', [ 'uses' => 'Admin\ServersController@postToggleInstall' diff --git a/app/Repositories/ServerRepository.php b/app/Repositories/ServerRepository.php index 21be6597a..c5d0c6bcf 100644 --- a/app/Repositories/ServerRepository.php +++ b/app/Repositories/ServerRepository.php @@ -728,9 +728,31 @@ class ServerRepository */ public function suspend($id) { - // @TODO: Implement logic; not doing it now since that is outside of the - // scope of this API brance. - return true; + $server = Models\Server::findOrFail($id); + $node = Models\Node::findOrFail($server->node); + + DB::beginTransaction(); + + try { + $server->suspended = 1; + $server->save(); + + $client = Models\Node::guzzleRequest($server->node); + $client->request('POST', '/server/suspend', [ + 'headers' => [ + 'X-Access-Token' => $node->daemonSecret, + 'X-Access-Server' => $server->uuid + ] + ]); + + return DB::commit(); + } catch (\GuzzleHttp\Exception\TransferException $ex) { + DB::rollBack(); + throw new DisplayException('An error occured while attempting to suspend this server.', $ex); + } catch (\Exception $ex) { + DB::rollBack(); + throw $ex; + } } /** @@ -740,9 +762,31 @@ class ServerRepository */ public function unsuspend($id) { - // @TODO: Implement logic; not doing it now since that is outside of the - // scope of this API brance. - return true; + $server = Models\Server::findOrFail($id); + $node = Models\Node::findOrFail($server->node); + + DB::beginTransaction(); + + try { + $server->suspended = 0; + $server->save(); + + $client = Models\Node::guzzleRequest($server->node); + $client->request('POST', '/server/unsuspend', [ + 'headers' => [ + 'X-Access-Token' => $node->daemonSecret, + 'X-Access-Server' => $server->uuid + ] + ]); + + return DB::commit(); + } catch (\GuzzleHttp\Exception\TransferException $ex) { + DB::rollBack(); + throw new DisplayException('An error occured while attempting to un-suspend this server.', $ex); + } catch (\Exception $ex) { + DB::rollBack(); + throw $ex; + } } public function updateSFTPPassword($id, $password) diff --git a/database/migrations/2016_09_01_193520_add_suspension_for_servers.php b/database/migrations/2016_09_01_193520_add_suspension_for_servers.php new file mode 100644 index 000000000..39717253b --- /dev/null +++ b/database/migrations/2016_09_01_193520_add_suspension_for_servers.php @@ -0,0 +1,31 @@ +tinyInteger('suspended')->unsigned()->default(0)->after('active'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('servers', function (Blueprint $table) { + $table->dropColumn('suspended'); + }); + } +} diff --git a/resources/views/admin/servers/index.blade.php b/resources/views/admin/servers/index.blade.php index 23dd5dcb6..54eda116e 100644 --- a/resources/views/admin/servers/index.blade.php +++ b/resources/views/admin/servers/index.blade.php @@ -42,8 +42,8 @@ @foreach ($servers as $server) - - {{ $server->name }} + + {{ $server->name }}@if($server->suspended === 1) Suspended@endif {{ $server->a_ownerEmail }} {{ $server->a_nodeName }} {{ $server->ip_alias }}:{{ $server->port }} @if($server->ip !== $server->ip_alias)alias@endif diff --git a/resources/views/admin/servers/view.blade.php b/resources/views/admin/servers/view.blade.php index dd5cb3f46..a2f879f9a 100644 --- a/resources/views/admin/servers/view.blade.php +++ b/resources/views/admin/servers/view.blade.php @@ -30,6 +30,11 @@
  • Servers
  • {{ $server->name }} ({{ $server->uuidShort}})
  • + @if($server->suspended === 1) +
    + This server is suspended and has no user access. Processes cannot be started and files cannot be modified. All API access is disabled unless using a master token. +
    + @endif @if($server->installed === 0)
    This server is still running through the install process and is not avaliable for use just yet. This message will disappear once this process is completed. @@ -89,7 +94,7 @@ OOM Killer - {!! ($server->oom_disabled === 0) ? 'enabled' : 'disabled' !!} + {!! ($server->oom_disabled === 0) ? 'Enabled' : 'Disabled' !!} Disk Space @@ -121,6 +126,10 @@ Installed {!! ($server->installed === 1) ? 'Yes' : 'No' !!} + + Suspended + {!! ($server->suspended === 1) ? 'Suspended' : 'No' !!} +
    @@ -437,6 +446,32 @@ @endif +
    +
    +
    + @if($server->suspended === 0) +
    +
    + {!! csrf_field() !!} + +
    +
    +
    +

    This will suspend the server, stop any running processes, and immediately block the user from being able to access their files or otherwise manage the server through the panel or API.

    +
    + @else +
    +
    + {!! csrf_field() !!} + +
    +
    +
    +

    This will unsuspend the server and restore normal user access.

    +
    + @endif +
    +
    @endif diff --git a/resources/views/base/index.blade.php b/resources/views/base/index.blade.php index 1027a1604..a80836256 100644 --- a/resources/views/base/index.blade.php +++ b/resources/views/base/index.blade.php @@ -47,7 +47,7 @@ @foreach ($servers as $server) - + @if (Auth::user()->root_admin == 1) @if ($server->owner === Auth::user()->id) @@ -63,7 +63,7 @@ -- -- / {{ $server->memory === 0 ? '∞' : $server->memory }} MB -- % - -- + @if($server->suspended === 1)Suspended@else--@endif @endforeach diff --git a/resources/views/errors/suspended.blade.php b/resources/views/errors/suspended.blade.php new file mode 100644 index 000000000..442f85ec0 --- /dev/null +++ b/resources/views/errors/suspended.blade.php @@ -0,0 +1,36 @@ +{{-- Copyright (c) 2015 - 2016 Dane Everitt --}} + +{{-- Permission is hereby granted, free of charge, to any person obtaining a copy --}} +{{-- of this software and associated documentation files (the "Software"), to deal --}} +{{-- in the Software without restriction, including without limitation the rights --}} +{{-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell --}} +{{-- copies of the Software, and to permit persons to whom the Software is --}} +{{-- furnished to do so, subject to the following conditions: --}} + +{{-- The above copyright notice and this permission notice shall be included in all --}} +{{-- copies or substantial portions of the Software. --}} + +{{-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR --}} +{{-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, --}} +{{-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE --}} +{{-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER --}} +{{-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, --}} +{{-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE --}} +{{-- SOFTWARE. --}} +@extends('layouts.master') + +@section('title', '503: Server Temporarily Unavaliable') + +@section('content') +
    +
    +
    +

    HTTP 403: Access Denied

    +
    +
    +

    This server has been suspended and cannot be accessed.

    +
    +
    +

    Take me back or go home.

    +
    +@endsection From 8e657a0bf0c146ce143f90c4f72a6277b4e74827 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 1 Sep 2016 21:21:01 -0400 Subject: [PATCH 2/2] Remove old 'active' column and replace some references with 'suspended' in place --- app/Console/Commands/RunTasks.php | 2 +- app/Http/Controllers/Admin/UserController.php | 1 - app/Models/Server.php | 9 +++--- app/Repositories/ServerRepository.php | 2 +- ...2016_09_01_211924_remove_active_column.php | 31 +++++++++++++++++++ 5 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 database/migrations/2016_09_01_211924_remove_active_column.php diff --git a/app/Console/Commands/RunTasks.php b/app/Console/Commands/RunTasks.php index 351dabd68..cb69cc15c 100644 --- a/app/Console/Commands/RunTasks.php +++ b/app/Console/Commands/RunTasks.php @@ -67,7 +67,7 @@ class RunTasks extends Command */ public function handle() { - $tasks = Models\Task::where('queued', 0)->where('active', 1)->where('next_run', '<=', (Carbon::now())->toAtomString())->get(); + $tasks = Models\Task::where('queued', 0)->where('suspended', 0)->where('next_run', '<=', (Carbon::now())->toAtomString())->get(); $this->info(sprintf('Preparing to queue %d tasks.', count($tasks))); $bar = $this->output->createProgressBar(count($tasks)); diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index da0aeac9b..e46e8f03e 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -69,7 +69,6 @@ class UserController extends Controller ->join('nodes', 'servers.node', '=', 'nodes.id') ->join('locations', 'nodes.location', '=', 'locations.id') ->where('owner', $id) - ->where('active', 1) ->get(), ]); } diff --git a/app/Models/Server.php b/app/Models/Server.php index 02857590b..171e62194 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -59,7 +59,7 @@ class Server extends Model */ protected $casts = [ 'node' => 'integer', - 'active' => 'integer', + 'suspended' => 'integer', 'owner' => 'integer', 'memory' => 'integer', 'swap' => 'integer', @@ -117,7 +117,7 @@ class Server extends Model /** * Returns array of all servers owned by the logged in user. - * Returns all active servers if user is a root admin. + * Returns all users servers if user is a root admin. * * @return \Illuminate\Database\Eloquent\Collection */ @@ -132,8 +132,7 @@ class Server extends Model 'allocations.port' )->join('nodes', 'servers.node', '=', 'nodes.id') ->join('locations', 'nodes.location', '=', 'locations.id') - ->join('allocations', 'servers.allocation', '=', 'allocations.id') - ->where('active', 1); + ->join('allocations', 'servers.allocation', '=', 'allocations.id'); if (self::$user->root_admin !== 1) { $query->whereIn('servers.id', Subuser::accessServers()); @@ -164,7 +163,7 @@ class Server extends Model $query = self::select('servers.*', 'services.file as a_serviceFile') ->join('services', 'services.id', '=', 'servers.service') - ->where('uuidShort', $uuid)->where('active', 1); + ->where('uuidShort', $uuid); if (self::$user->root_admin !== 1) { $query->whereIn('servers.id', Subuser::accessServers()); diff --git a/app/Repositories/ServerRepository.php b/app/Repositories/ServerRepository.php index c5d0c6bcf..06d97e353 100644 --- a/app/Repositories/ServerRepository.php +++ b/app/Repositories/ServerRepository.php @@ -205,7 +205,7 @@ class ServerRepository 'uuidShort' => $uuid->generateShort('servers', 'uuidShort', $generatedUuid), 'node' => $data['node'], 'name' => $data['name'], - 'active' => 1, + 'suspended' => 0, 'owner' => $user->id, 'memory' => $data['memory'], 'swap' => $data['swap'], diff --git a/database/migrations/2016_09_01_211924_remove_active_column.php b/database/migrations/2016_09_01_211924_remove_active_column.php new file mode 100644 index 000000000..746559a80 --- /dev/null +++ b/database/migrations/2016_09_01_211924_remove_active_column.php @@ -0,0 +1,31 @@ +dropColumn('active'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('servers', function (Blueprint $table) { + $table->tinyInteger('active')->after('name')->unsigned()->default(0); + }); + } +}