Add delete server support as well as fix a few other bugs

Also a few JS fixes to make things work better and not clear the
console every time the server is booted
This commit is contained in:
Dane Everitt 2016-01-03 23:16:03 -05:00
parent 9d2d726992
commit 4fe4881f77
7 changed files with 167 additions and 53 deletions

View file

@ -65,6 +65,10 @@ class ServersController extends Controller
->where('servers.id', $id)
->first();
if (!$server) {
return abort(404);
}
return view('admin.servers.view', [
'server' => $server,
'assigned' => Models\Allocation::select('id', 'ip', 'port')->where('assigned_to', $id)->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(),
@ -289,4 +293,23 @@ class ServersController extends Controller
]);
}
public function deleteServer(Request $request, $id, $force = null)
{
try {
$server = new ServerRepository;
$server->deleteServer($id, $force);
Alert::success('Server was successfully deleted from the panel and the daemon.')->flash();
return redirect()->route('admin.servers');
} 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 add this server. Please try again.')->flash();
}
return redirect()->route('admin.servers.view', [
'id' => $id,
'tab' => 'tab_delete'
]);
}
}

View file

@ -33,6 +33,7 @@ class AdminRoutes {
$router->post('/view/{id}/details', [ 'uses' => 'Admin\ServersController@postUpdateServerDetails' ]);
$router->post('/view/{id}/rebuild', [ 'uses' => 'Admin\ServersController@postUpdateServerToggleBuild' ]);
$router->post('/view/{id}/build', [ 'uses' => 'Admin\ServersController@postUpdateServerUpdateBuild' ]);
$router->delete('/view/{id}/{force?}', [ 'uses' => 'Admin\ServersController@deleteServer' ]);
$router->post('/new', [ 'uses' => 'Admin\ServersController@postNewServer']);
$router->post('/new/get-nodes', [ 'uses' => 'Admin\ServersController@postNewServerGetNodes' ]);

View file

@ -524,4 +524,55 @@ class ServerRepository
}
public function deleteServer($id, $force)
{
$server = Models\Server::findOrFail($id);
$node = Models\Node::findOrFail($server->node);
DB::beginTransaction();
// Delete Allocations
Models\Allocation::where('assigned_to', $server->id)->update([
'assigned_to' => null
]);
// Remove Variables
Models\ServerVariables::where('server_id', $server->id)->delete();
// Remove SubUsers
Models\Subuser::where('server_id', $server->id)->delete();
// Remove Permissions
Models\Permission::where('server_id', $server->id)->delete();
// Remove Downloads
Models\Download::where('server', $server->uuid)->delete();
try {
$client = Models\Node::guzzleRequest($server->node);
$client->request('DELETE', '/servers', [
'headers' => [
'X-Access-Token' => $node->daemonSecret,
'X-Access-Server' => $server->uuid
]
]);
$server->delete();
DB::commit();
return true;
} catch (\GuzzleHttp\Exception\TransferException $ex) {
if ($force === 'force') {
$server->delete();
DB::commit();
return true;
} else {
DB::rollBack();
Log::error($ex);
throw new DisplayException('An error occured while attempting to delete the server on the daemon: ' . $ex->getMessage());
}
} catch (\Exception $ex) {
DB::rollBack();
throw $ex;
}
}
}

View file

@ -10,6 +10,14 @@
<li><a href="/admin">Admin Control</a></li>
<li class="active">Servers</li>
</ul>
@foreach (Alert::getMessages() as $type => $messages)
@foreach ($messages as $message)
<div class="alert alert-{{ $type }} alert-dismissable" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
{!! $message !!}
</div>
@endforeach
@endforeach
<h3>All Servers</h3><hr />
<table class="table table-bordered table-hover">
<thead>

View file

@ -397,7 +397,7 @@ $(document).ready(function () {
$.each(data, function (i, item) {
var isRequired = (item.required === 1) ? '<code>Required</code>' : '';
var dataAppend = ' \
<div class="form-group col-md-6">\
<div class="form-group col-md-12">\
<label for="var_ref_' + item.id + '" class="control-label">' + item.name + '</label> ' + isRequired + '\
<div>\
<input type="text" autocomplete="off" name="env_' + item.env_variable + '" class="form-control" value="' + item.default_value + '" />\

View file

@ -33,8 +33,9 @@
<li class="active"><a href="#tab_about" data-toggle="tab">About</a></li>
<li><a href="#tab_details" data-toggle="tab">Details</a></li>
<li><a href="#tab_build" data-toggle="tab">Build Configuration</a></li>
<li><a href="#tab_startup" data-toggle="tab">Startup Settings</a></li>
<li><a href="#tab_startup" data-toggle="tab">Startup</a></li>
<li><a href="#tab_manage" data-toggle="tab">Manage</a></li>
<li><a href="#tab_delete" data-toggle="tab">Delete</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="tab_about">
@ -251,55 +252,80 @@
<div class="panel panel-default">
<div class="panel-heading"></div>
<div class="panel-body">
<table class="table table-striped" style="margin-bottom: 0;">
<tbody>
<tr>
<td class="text-center" style="vertical-align:middle;">
<a href="/server/{{ $server->uuidShort }}/">
<button type="submit" class="btn btn-sm btn-primary">Manage Server</button>
</a>
</td>
<td>
<p>This will take you to the server management page that users normally see and allow you to manage server files as well as check the console and data usage.</p>
</td>
</tr>
<tr>
<td class="text-center" style="vertical-align:middle;">
<form action="/admin/servers/view/{{ $server->id }}/installed" method="POST">
{!! csrf_field() !!}
<button type="submit" class="btn btn-sm btn-primary">Toggle Install Status</button>
</form>
</td>
<td>
<p>This will toggle the install status for the server.</p>
<div class="alert alert-warning">If you have just created this server it is ill advised to perform this action as the daemon will contact the panel when finished which could cause the install status to be wrongly set.</div>
</td>
</tr>
<tr>
<td class="text-center" style="vertical-align:middle;">
<form action="/admin/servers/view/{{ $server->id }}/rebuild" method="POST">
{!! csrf_field() !!}
<button type="submit" class="btn btn-sm btn-primary">Rebuild Server Container</button>
</form>
</td>
<td>
<p>This will trigger a rebuild of the server container when it next starts up. This is useful if you modified the server configuration file manually, or something just didn't work out correctly. Please be aware: if you manually updated the server's configuration file, you will need to restart the daemon before doing this, or it will be overwritten.</p>
<div class="alert alert-info">A rebuild will automatically occur whenever you edit build configuration settings for the server.</div>
</td>
</tr>
<tr>
<td class="text-center" style="vertical-align:middle;">
<form action="/admin/servers/view/{{ $server->id }}/delete" method="POST">
{!! csrf_field() !!}
<button type="submit" class="btn btn-sm btn-danger">Delete Server</button>
</form>
</td>
<td>
<div class="alert alert-danger" style="margin-top:21px;">Deleting a server is an irreversible action. <strong>All data will be immediately removed relating to this server.</strong></div>
</td>
</tr>
</tbody>
</table>
<div class="row">
<div class="col-md-4 text-center">
<a href="/server/{{ $server->uuidShort }}/" target="_blank">
<button type="submit" class="btn btn-sm btn-primary">Manage Server</button>
</a>
</div>
<div class="col-md-8">
<p>This will take you to the server management page that users normally see and allow you to manage server files as well as check the console and data usage.</p>
</div>
</div>
</div>
<div class="panel-heading" style="border-top: 1px solid #ddd;"></div>
<div class="panel-body">
<div class="row">
<div class="col-md-4 text-center">
<form action="/admin/servers/view/{{ $server->id }}/installed" method="POST">
{!! csrf_field() !!}
<button type="submit" class="btn btn-sm btn-primary">Toggle Install Status</button>
</form>
</div>
<div class="col-md-8">
<p>This will toggle the install status for the server.</p>
<div class="alert alert-warning">If you have just created this server it is ill advised to perform this action as the daemon will contact the panel when finished which could cause the install status to be wrongly set.</div>
</div>
</div>
</div>
<div class="panel-heading" style="border-top: 1px solid #ddd;"></div>
<div class="panel-body">
<div class="row">
<div class="col-md-4 text-center">
<form action="/admin/servers/view/{{ $server->id }}/rebuild" method="POST">
{!! csrf_field() !!}
<button type="submit" class="btn btn-sm btn-primary">Rebuild Server Container</button>
</form>
</div>
<div class="col-md-8">
<p>This will trigger a rebuild of the server container when it next starts up. This is useful if you modified the server configuration file manually, or something just didn't work out correctly. Please be aware: if you manually updated the server's configuration file, you will need to restart the daemon before doing this, or it will be overwritten.</p>
<div class="alert alert-info">A rebuild will automatically occur whenever you edit build configuration settings for the server.</div>
</div>
</div>
</div>
</div>
</div>
<div class="tab-pane" id="tab_delete">
<div class="panel panel-default">
<div class="panel-heading"></div>
<div class="panel-body">
<div class="row">
<div class="col-md-4 text-center">
<form action="/admin/servers/view/{{ $server->id }}" method="POST" data-attr="deleteServer">
{!! csrf_field() !!}
{!! method_field('DELETE') !!}
<button type="submit" class="btn btn-sm btn-danger">Delete Server</button>
</form>
</div>
<div class="col-md-8">
<div class="alert alert-danger">Deleting a server is an irreversible action. <strong>All data will be immediately removed relating to this server.</strong></div>
</div>
</div>
</div>
<div class="panel-heading" style="border-top: 1px solid #ddd;"></div>
<div class="panel-body">
<div class="row">
<div class="col-md-4 text-center">
<form action="/admin/servers/view/{{ $server->id }}/force" method="POST" data-attr="deleteServer">
{!! csrf_field() !!}
{!! method_field('DELETE') !!}
<button type="submit" class="btn btn-sm btn-danger">Force Delete Server</button>
</form>
</div>
<div class="col-md-8">
<div class="alert alert-danger">This is the same as deleting a server, however, if an error is returned by the daemon it is ignored and the server is still removed from the panel.</strong></div>
</div>
</div>
</div>
</div>
</div>
@ -312,6 +338,11 @@ $(document).ready(function () {
$('select[name="remove_additional[]"]').find('option:disabled').prop('disabled', false);
$('select[name="remove_additional[]"]').find('option[value="' + $(this).val() + '"]').prop('disabled', true).prop('selected', false);
});
$('form[data-attr="deleteServer"]').submit(function (event) {
if (confirm('Are you sure that you want to delete this server? There is no going back, all data will immediately be removed.')) {
event.submit();
}
});
});
</script>
@endsection

View file

@ -317,7 +317,6 @@ $(window).load(function () {
// New Console Data Recieved
socket.on('console', function (data) {
console.log(JSON.stringify(data));
$('#live_console').val($('#live_console').val() + data.line);
$('#live_console').scrollTop($('#live_console')[0].scrollHeight);
});
@ -445,7 +444,8 @@ $(window).load(function () {
// Reset Console Data
if (data === 2) {
$("#live_console").val('');
$('#live_console').val($('#live_console').val() + '\n --+ Server Detected as Booting + --\n');
$('#live_console').scrollTop($('#live_console')[0].scrollHeight);
}
// Server is On or Starting