diff --git a/app/Http/Controllers/Admin/ServersController.php b/app/Http/Controllers/Admin/ServersController.php index c7a647924..69509d334 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -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' + ]); + } + } diff --git a/app/Http/Routes/AdminRoutes.php b/app/Http/Routes/AdminRoutes.php index 9ec7858f0..f073f9e56 100644 --- a/app/Http/Routes/AdminRoutes.php +++ b/app/Http/Routes/AdminRoutes.php @@ -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' ]); diff --git a/app/Repositories/ServerRepository.php b/app/Repositories/ServerRepository.php index 4c8fdbd9a..648ff077f 100644 --- a/app/Repositories/ServerRepository.php +++ b/app/Repositories/ServerRepository.php @@ -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; + } + } + } diff --git a/resources/views/admin/servers/index.blade.php b/resources/views/admin/servers/index.blade.php index d37335e29..6b5ebdcd0 100644 --- a/resources/views/admin/servers/index.blade.php +++ b/resources/views/admin/servers/index.blade.php @@ -10,6 +10,14 @@
- - - - | -
- 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. - |
-
- - | -
- This will toggle the install status for the server. -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.
- |
-
- - | -
- 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. -A rebuild will automatically occur whenever you edit build configuration settings for the server.
- |
-
- - | -
- Deleting a server is an irreversible action. All data will be immediately removed relating to this server.
- |
-
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.
+This will toggle the install status for the server.
+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.
+