Skip to content

Commit 38eae88

Browse files
committed
Add support for suspension
1 parent 3ca7e4d commit 38eae88

File tree

9 files changed

+223
-25
lines changed

9 files changed

+223
-25
lines changed

app/Http/Controllers/Admin/ServersController.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,4 +424,42 @@ public function postDatabase(Request $request, $id)
424424
])->withInput();
425425
}
426426

427+
public function postSuspendServer(Request $request, $id)
428+
{
429+
try {
430+
$repo = new ServerRepository;
431+
$repo->suspend($id);
432+
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.');
433+
} catch (\Pterodactyl\Exceptions\DisplayException $e) {
434+
Alert::danger($e->getMessage())->flash();
435+
} catch(\Exception $e) {
436+
Log::error($e);
437+
Alert::danger('An unhandled exception occured while attemping to suspend this server. Please try again.')->flash();
438+
} finally {
439+
return redirect()->route('admin.servers.view', [
440+
'id' => $id,
441+
'tab' => 'tab_manage'
442+
]);
443+
}
444+
}
445+
446+
public function postUnsuspendServer(Request $request, $id)
447+
{
448+
try {
449+
$repo = new ServerRepository;
450+
$repo->unsuspend($id);
451+
Alert::success('Server has been unsuspended on the system. Access has been re-enabled.');
452+
} catch (\Pterodactyl\Exceptions\DisplayException $e) {
453+
Alert::danger($e->getMessage())->flash();
454+
} catch(\Exception $e) {
455+
Log::error($e);
456+
Alert::danger('An unhandled exception occured while attemping to unsuspend this server. Please try again.')->flash();
457+
} finally {
458+
return redirect()->route('admin.servers.view', [
459+
'id' => $id,
460+
'tab' => 'tab_manage'
461+
]);
462+
}
463+
}
464+
427465
}

app/Http/Middleware/CheckServer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,15 @@ public function handle($request, Closure $next)
4646

4747
$server = Server::getByUUID($request->route()->server);
4848
if (!$server) {
49-
return response()->view('errors.403', [], 403);
49+
return response()->view('errors.404', [], 404);
50+
}
51+
52+
if ($server->suspended === 1) {
53+
return response()->view('errors.suspended', [], 403);
5054
}
5155

5256
if ($server->installed !== 1) {
53-
return response()->view('errors.installing', [], 503);
57+
return response()->view('errors.installing', [], 403);
5458
}
5559

5660
return $next($request);

app/Http/Routes/AdminRoutes.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,21 @@ public function map(Router $router) {
129129
]);
130130

131131
// Assorted Page Helpers
132-
$router->post('/new/get-nodes', [
133-
'uses' => 'Admin\ServersController@postNewServerGetNodes'
134-
]);
132+
$router->post('/new/get-nodes', [
133+
'uses' => 'Admin\ServersController@postNewServerGetNodes'
134+
]);
135135

136-
$router->post('/new/get-ips', [
137-
'uses' => 'Admin\ServersController@postNewServerGetIps'
138-
]);
136+
$router->post('/new/get-ips', [
137+
'uses' => 'Admin\ServersController@postNewServerGetIps'
138+
]);
139139

140-
$router->post('/new/service-options', [
141-
'uses' => 'Admin\ServersController@postNewServerServiceOptions'
142-
]);
140+
$router->post('/new/service-options', [
141+
'uses' => 'Admin\ServersController@postNewServerServiceOptions'
142+
]);
143143

144-
$router->post('/new/service-variables', [
145-
'uses' => 'Admin\ServersController@postNewServerServiceVariables'
146-
]);
144+
$router->post('/new/service-variables', [
145+
'uses' => 'Admin\ServersController@postNewServerServiceVariables'
146+
]);
147147
// End Assorted Page Helpers
148148

149149
// View Specific Server
@@ -179,6 +179,16 @@ public function map(Router $router) {
179179
'uses' => 'Admin\ServersController@postUpdateServerUpdateBuild'
180180
]);
181181

182+
// Suspend Server
183+
$router->post('/view/{id}/suspend', [
184+
'uses' => 'Admin\ServersController@postSuspendServer'
185+
]);
186+
187+
// Unsuspend Server
188+
$router->post('/view/{id}/unsuspend', [
189+
'uses' => 'Admin\ServersController@postUnsuspendServer'
190+
]);
191+
182192
// Change Install Status
183193
$router->post('/view/{id}/installed', [
184194
'uses' => 'Admin\ServersController@postToggleInstall'

app/Repositories/ServerRepository.php

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -728,9 +728,31 @@ public function toggleInstall($id)
728728
*/
729729
public function suspend($id)
730730
{
731-
// @TODO: Implement logic; not doing it now since that is outside of the
732-
// scope of this API brance.
733-
return true;
731+
$server = Models\Server::findOrFail($id);
732+
$node = Models\Node::findOrFail($server->node);
733+
734+
DB::beginTransaction();
735+
736+
try {
737+
$server->suspended = 1;
738+
$server->save();
739+
740+
$client = Models\Node::guzzleRequest($server->node);
741+
$client->request('POST', '/server/suspend', [
742+
'headers' => [
743+
'X-Access-Token' => $node->daemonSecret,
744+
'X-Access-Server' => $server->uuid
745+
]
746+
]);
747+
748+
return DB::commit();
749+
} catch (\GuzzleHttp\Exception\TransferException $ex) {
750+
DB::rollBack();
751+
throw new DisplayException('An error occured while attempting to suspend this server.', $ex);
752+
} catch (\Exception $ex) {
753+
DB::rollBack();
754+
throw $ex;
755+
}
734756
}
735757

736758
/**
@@ -740,9 +762,31 @@ public function suspend($id)
740762
*/
741763
public function unsuspend($id)
742764
{
743-
// @TODO: Implement logic; not doing it now since that is outside of the
744-
// scope of this API brance.
745-
return true;
765+
$server = Models\Server::findOrFail($id);
766+
$node = Models\Node::findOrFail($server->node);
767+
768+
DB::beginTransaction();
769+
770+
try {
771+
$server->suspended = 0;
772+
$server->save();
773+
774+
$client = Models\Node::guzzleRequest($server->node);
775+
$client->request('POST', '/server/unsuspend', [
776+
'headers' => [
777+
'X-Access-Token' => $node->daemonSecret,
778+
'X-Access-Server' => $server->uuid
779+
]
780+
]);
781+
782+
return DB::commit();
783+
} catch (\GuzzleHttp\Exception\TransferException $ex) {
784+
DB::rollBack();
785+
throw new DisplayException('An error occured while attempting to un-suspend this server.', $ex);
786+
} catch (\Exception $ex) {
787+
DB::rollBack();
788+
throw $ex;
789+
}
746790
}
747791

748792
public function updateSFTPPassword($id, $password)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class AddSuspensionForServers extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::table('servers', function (Blueprint $table) {
16+
$table->tinyInteger('suspended')->unsigned()->default(0)->after('active');
17+
});
18+
}
19+
20+
/**
21+
* Reverse the migrations.
22+
*
23+
* @return void
24+
*/
25+
public function down()
26+
{
27+
Schema::table('servers', function (Blueprint $table) {
28+
$table->dropColumn('suspended');
29+
});
30+
}
31+
}

resources/views/admin/servers/index.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
</thead>
4343
<tbody>
4444
@foreach ($servers as $server)
45-
<tr class="dynUpdate @if($server->active !== 1)active @endif" id="{{ $server->uuidShort }}">
46-
<td><a href="/admin/servers/view/{{ $server->id }}">{{ $server->name }}</td>
45+
<tr class="dynUpdate @if($server->suspended === 1)warning @endif" id="{{ $server->uuidShort }}">
46+
<td><a href="/admin/servers/view/{{ $server->id }}">{{ $server->name }}</a>@if($server->suspended === 1) <span class="label label-warning">Suspended</span>@endif</td>
4747
<td><a href="/admin/users/view/{{ $server->owner }}">{{ $server->a_ownerEmail }}</a></td>
4848
<td class="hidden-xs"><a href="/admin/nodes/view/{{ $server->node }}">{{ $server->a_nodeName }}</a></td>
4949
<td><code>{{ $server->ip_alias }}:{{ $server->port }}</code> @if($server->ip !== $server->ip_alias)<span class="label label-default">alias</span>@endif</td>

resources/views/admin/servers/view.blade.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
<li><a href="/admin/servers">Servers</a></li>
3131
<li class="active">{{ $server->name }} ({{ $server->uuidShort}})</li>
3232
</ul>
33+
@if($server->suspended === 1)
34+
<div class="alert alert-warning">
35+
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.
36+
</div>
37+
@endif
3338
@if($server->installed === 0)
3439
<div class="alert alert-warning">
3540
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 @@
8994
</tr>
9095
<tr>
9196
<td><abbr title="Out of Memory">OOM</abbr> Killer</td>
92-
<td>{!! ($server->oom_disabled === 0) ? '<span class="label label-success">enabled</span>' : '<span class="label label-default">disabled</span>' !!}</td>
97+
<td>{!! ($server->oom_disabled === 0) ? '<span class="label label-success">Enabled</span>' : '<span class="label label-default">Disabled</span>' !!}</td>
9398
</tr>
9499
<tr>
95100
<td>Disk Space</td>
@@ -121,6 +126,10 @@
121126
<td>Installed</td>
122127
<td>{!! ($server->installed === 1) ? '<span class="label label-success">Yes</span>' : '<span class="label label-danger">No</span>' !!}</td>
123128
</tr>
129+
<tr>
130+
<td>Suspended</td>
131+
<td>{!! ($server->suspended === 1) ? '<span class="label label-warning">Suspended</span>' : '<span class="label label-success">No</span>' !!}</td>
132+
</tr>
124133
</tbody>
125134
</table>
126135
</div>
@@ -437,6 +446,32 @@
437446
</div>
438447
</div>
439448
@endif
449+
<div class="panel-heading" style="border-top: 1px solid #ddd;"></div>
450+
<div class="panel-body">
451+
<div class="row">
452+
@if($server->suspended === 0)
453+
<div class="col-md-4 text-center">
454+
<form action="/admin/servers/view/{{ $server->id }}/suspend" method="POST">
455+
{!! csrf_field() !!}
456+
<button type="submit" class="btn btn-sm btn-warning">Suspend Server</button>
457+
</form>
458+
</div>
459+
<div class="col-md-8">
460+
<p>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.</p>
461+
</div>
462+
@else
463+
<div class="col-md-4 text-center">
464+
<form action="/admin/servers/view/{{ $server->id }}/unsuspend" method="POST">
465+
{!! csrf_field() !!}
466+
<button type="submit" class="btn btn-sm btn-success">Unsuspend Server</button>
467+
</form>
468+
</div>
469+
<div class="col-md-8">
470+
<p>This will unsuspend the server and restore normal user access.</p>
471+
</div>
472+
@endif
473+
</div>
474+
</div>
440475
</div>
441476
</div>
442477
@endif

resources/views/base/index.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
</thead>
4848
<tbody>
4949
@foreach ($servers as $server)
50-
<tr class="dynUpdate" data-server="{{ $server->uuidShort }}">
50+
<tr class="dynUpdate @if($server->suspended === 1)warning @endif" data-server="{{ $server->uuidShort }}">
5151
@if (Auth::user()->root_admin == 1)
5252
<td style="width:26px;">
5353
@if ($server->owner === Auth::user()->id)
@@ -63,7 +63,7 @@
6363
<td class="text-center" data-action="players">--</td>
6464
<td class="text-center"><span data-action="memory">--</span> / {{ $server->memory === 0 ? '&infin;' : $server->memory }} MB</td>
6565
<td class="text-center"><span data-action="cpu" data-cpumax="{{ $server->cpu }}">--</span> %</td>
66-
<td class="text-center" data-action="status">--</td>
66+
<td class="text-center" data-action="status">@if($server->suspended === 1)<span class="label label-warning">Suspended</span>@else--@endif</td>
6767
</tr>
6868
@endforeach
6969
</tbody>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{{-- Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com> --}}
2+
3+
{{-- Permission is hereby granted, free of charge, to any person obtaining a copy --}}
4+
{{-- of this software and associated documentation files (the "Software"), to deal --}}
5+
{{-- in the Software without restriction, including without limitation the rights --}}
6+
{{-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell --}}
7+
{{-- copies of the Software, and to permit persons to whom the Software is --}}
8+
{{-- furnished to do so, subject to the following conditions: --}}
9+
10+
{{-- The above copyright notice and this permission notice shall be included in all --}}
11+
{{-- copies or substantial portions of the Software. --}}
12+
13+
{{-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR --}}
14+
{{-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, --}}
15+
{{-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE --}}
16+
{{-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER --}}
17+
{{-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, --}}
18+
{{-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE --}}
19+
{{-- SOFTWARE. --}}
20+
@extends('layouts.master')
21+
22+
@section('title', '503: Server Temporarily Unavaliable')
23+
24+
@section('content')
25+
<div class="col-md-12">
26+
<div class="panel panel-danger">
27+
<div class="panel-heading">
28+
<h3 class="panel-title">HTTP 403: Access Denied</h3>
29+
</div>
30+
<div class="panel-body">
31+
<p style="margin-bottom:0;">This server has been suspended and cannot be accessed.</p>
32+
</div>
33+
</div>
34+
<p style="text-align:center;"><a href="{{ URL::previous() }}">Take me back</a> or <a href="/">go home</a>.</p>
35+
</div>
36+
@endsection

0 commit comments

Comments
 (0)