From 5144e0126baceefcd885fd9a52161b9674f75eb7 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 23 Jul 2017 14:51:18 -0500 Subject: [PATCH] Add support for more server functionality --- .../Daemon/ServerRepositoryInterface.php | 28 +++ .../LocationRepositoryInterface.php | 17 ++ .../Controllers/Admin/LocationController.php | 33 +-- .../Controllers/Admin/ServersController.php | 187 ++++++++++------- app/Repositories/Daemon/ServerRepository.php | 32 +++ .../Eloquent/LocationRepository.php | 22 ++ .../Servers/ContainerRebuildService.php | 80 +++++++ .../Servers/DetailsModificationService.php | 2 +- app/Services/Servers/ReinstallService.php | 94 +++++++++ app/Services/Servers/SuspensionService.php | 114 ++++++++++ resources/lang/en/admin/server.php | 5 + routes/admin.php | 34 +-- .../Servers/ContainerRebuildServiceTest.php | 139 ++++++++++++ .../DetailsModificationServiceTest.php | 6 +- .../Services/Servers/ReinstallServiceTest.php | 177 ++++++++++++++++ .../Servers/SuspensionServiceTest.php | 198 ++++++++++++++++++ 16 files changed, 1049 insertions(+), 119 deletions(-) create mode 100644 app/Services/Servers/ContainerRebuildService.php create mode 100644 app/Services/Servers/ReinstallService.php create mode 100644 app/Services/Servers/SuspensionService.php create mode 100644 tests/Unit/Services/Servers/ContainerRebuildServiceTest.php create mode 100644 tests/Unit/Services/Servers/ReinstallServiceTest.php create mode 100644 tests/Unit/Services/Servers/SuspensionServiceTest.php diff --git a/app/Contracts/Repository/Daemon/ServerRepositoryInterface.php b/app/Contracts/Repository/Daemon/ServerRepositoryInterface.php index a69e1bb65..d8659de3d 100644 --- a/app/Contracts/Repository/Daemon/ServerRepositoryInterface.php +++ b/app/Contracts/Repository/Daemon/ServerRepositoryInterface.php @@ -43,4 +43,32 @@ interface ServerRepositoryInterface extends BaseRepositoryInterface * @return \Psr\Http\Message\ResponseInterface */ public function update(array $data); + + /** + * Mark a server to be reinstalled on the system. + * + * @return \Psr\Http\Message\ResponseInterface + */ + public function reinstall(); + + /** + * Mark a server as needing a container rebuild the next time the server is booted. + * + * @return \Psr\Http\Message\ResponseInterface + */ + public function rebuild(); + + /** + * Suspend a server on the daemon. + * + * @return \Psr\Http\Message\ResponseInterface + */ + public function suspend(); + + /** + * Un-suspend a server on the daemon. + * + * @return \Psr\Http\Message\ResponseInterface + */ + public function unsuspend(); } diff --git a/app/Contracts/Repository/LocationRepositoryInterface.php b/app/Contracts/Repository/LocationRepositoryInterface.php index 9a52e2988..a5d0c665a 100644 --- a/app/Contracts/Repository/LocationRepositoryInterface.php +++ b/app/Contracts/Repository/LocationRepositoryInterface.php @@ -38,4 +38,21 @@ interface LocationRepositoryInterface extends RepositoryInterface, SearchableInt * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ public function deleteIfNoNodes($id); + + /** + * Return locations with a count of nodes and servers attached to it. + * + * @return mixed + */ + public function allWithDetails(); + + /** + * Return all of the nodes and their respective count of servers for a location. + * + * @param int $id + * @return mixed + * + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException + */ + public function getWithNodes($id); } diff --git a/app/Http/Controllers/Admin/LocationController.php b/app/Http/Controllers/Admin/LocationController.php index db358e5c3..4c33368a9 100644 --- a/app/Http/Controllers/Admin/LocationController.php +++ b/app/Http/Controllers/Admin/LocationController.php @@ -24,12 +24,13 @@ namespace Pterodactyl\Http\Controllers\Admin; +use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; use Pterodactyl\Models\Location; use Prologue\Alerts\AlertsMessageBag; use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Requests\Admin\LocationRequest; -use Pterodactyl\Services\Administrative\LocationService; +use Pterodactyl\Services\LocationService; class LocationController extends Controller { @@ -39,29 +40,29 @@ class LocationController extends Controller protected $alert; /** - * @var \Pterodactyl\Models\Location + * @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface */ - protected $locationModel; + protected $repository; /** - * @var \Pterodactyl\Services\Administrative\\LocationService + * @var \Pterodactyl\Services\\LocationService */ protected $service; /** * LocationController constructor. * - * @param \Prologue\Alerts\AlertsMessageBag $alert - * @param \Pterodactyl\Models\Location $locationModel - * @param \Pterodactyl\Services\Administrative\LocationService $service + * @param \Prologue\Alerts\AlertsMessageBag $alert + * @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $repository + * @param \Pterodactyl\Services\LocationService $service */ public function __construct( AlertsMessageBag $alert, - Location $locationModel, + LocationRepositoryInterface $repository, LocationService $service ) { $this->alert = $alert; - $this->locationModel = $locationModel; + $this->repository = $repository; $this->service = $service; } @@ -73,21 +74,21 @@ class LocationController extends Controller public function index() { return view('admin.locations.index', [ - 'locations' => $this->locationModel->withCount('nodes', 'servers')->get(), + 'locations' => $this->repository->allWithDetails(), ]); } /** * Return the location view page. * - * @param \Pterodactyl\Models\Location $location + * @param int $id * @return \Illuminate\View\View */ - public function view(Location $location) + public function view($id) { - $location->load('nodes.servers'); - - return view('admin.locations.view', ['location' => $location]); + return view('admin.locations.view', [ + 'location' => $this->repository->getWithNodes($id), + ]); } /** @@ -132,7 +133,7 @@ class LocationController extends Controller /** * Delete a location from the system. * - * @param \Pterodactyl\Models\Location $location + * @param \Pterodactyl\Models\Location $location * @return \Illuminate\Http\RedirectResponse * * @throws \Exception diff --git a/app/Http/Controllers/Admin/ServersController.php b/app/Http/Controllers/Admin/ServersController.php index 16747c014..5b7508d64 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -36,7 +36,6 @@ use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServiceRepositoryInterface; use Pterodactyl\Http\Requests\Admin\ServerFormRequest; -use Pterodactyl\Models; use Pterodactyl\Models\Server; use Illuminate\Http\Request; use GuzzleHttp\Exception\TransferException; @@ -44,8 +43,12 @@ use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository; use Pterodactyl\Exceptions\DisplayValidationException; +use Pterodactyl\Services\Database\CreationService as DatabaseCreationService; +use Pterodactyl\Services\Servers\ContainerRebuildService; use Pterodactyl\Services\Servers\CreationService; use Pterodactyl\Services\Servers\DetailsModificationService; +use Pterodactyl\Services\Servers\ReinstallService; +use Pterodactyl\Services\Servers\SuspensionService; class ServersController extends Controller { @@ -64,6 +67,11 @@ class ServersController extends Controller */ protected $config; + /** + * @var \Pterodactyl\Services\Servers\ContainerRebuildService + */ + protected $containerRebuildService; + /** * @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface */ @@ -94,6 +102,11 @@ class ServersController extends Controller */ protected $nodeRepository; + /** + * @var \Pterodactyl\Services\Servers\ReinstallService + */ + protected $reinstallService; + /** * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface */ @@ -109,32 +122,62 @@ class ServersController extends Controller */ protected $serviceRepository; + /** + * @var \Pterodactyl\Services\Servers\SuspensionService + */ + protected $suspensionService; + + /** + * ServersController constructor. + * + * @param \Prologue\Alerts\AlertsMessageBag $alert + * @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository + * @param \Illuminate\Contracts\Config\Repository $config + * @param \Pterodactyl\Services\Servers\ContainerRebuildService $containerRebuildService + * @param \Pterodactyl\Services\Servers\CreationService $service + * @param \Pterodactyl\Services\Database\CreationService $databaseCreationService + * @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $databaseRepository + * @param \Pterodactyl\Repositories\Eloquent\DatabaseHostRepository $databaseHostRepository + * @param \Pterodactyl\Services\Servers\DetailsModificationService $detailsModificationService + * @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $locationRepository + * @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $nodeRepository + * @param \Pterodactyl\Services\Servers\ReinstallService $reinstallService + * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository + * @param \Pterodactyl\Contracts\Repository\ServiceRepositoryInterface $serviceRepository + * @param \Pterodactyl\Services\Servers\SuspensionService $suspensionService + */ public function __construct( AlertsMessageBag $alert, AllocationRepositoryInterface $allocationRepository, ConfigRepository $config, + ContainerRebuildService $containerRebuildService, CreationService $service, - \Pterodactyl\Services\Database\CreationService $databaseCreationService, + DatabaseCreationService $databaseCreationService, DatabaseRepositoryInterface $databaseRepository, DatabaseHostRepository $databaseHostRepository, DetailsModificationService $detailsModificationService, LocationRepositoryInterface $locationRepository, NodeRepositoryInterface $nodeRepository, + ReinstallService $reinstallService, ServerRepositoryInterface $repository, - ServiceRepositoryInterface $serviceRepository + ServiceRepositoryInterface $serviceRepository, + SuspensionService $suspensionService ) { $this->alert = $alert; $this->allocationRepository = $allocationRepository; $this->config = $config; + $this->containerRebuildService = $containerRebuildService; $this->databaseCreationService = $databaseCreationService; $this->databaseRepository = $databaseRepository; $this->databaseHostRepository = $databaseHostRepository; $this->detailsModificationService = $detailsModificationService; $this->locationRepository = $locationRepository; $this->nodeRepository = $nodeRepository; + $this->reinstallService = $reinstallService; $this->repository = $repository; $this->service = $service; $this->serviceRepository = $serviceRepository; + $this->suspensionService = $suspensionService; } /** @@ -192,7 +235,8 @@ class ServersController extends Controller return redirect()->route('admin.servers.view', $server->id); } catch (TransferException $ex) { Log::warning($ex); - Alert::danger('A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.')->flash(); + Alert::danger('A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.') + ->flash(); } return redirect()->route('admin.servers.new')->withInput(); @@ -375,47 +419,41 @@ class ServersController extends Controller /** * Toggles the install status for a server. * - * @param \Illuminate\Http\Request $request - * @param int $id + * @param \Pterodactyl\Models\Server $server * @return \Illuminate\Http\RedirectResponse + * + * @throws \Pterodactyl\Exceptions\DisplayException + * @throws \Pterodactyl\Exceptions\Model\DataValidationException */ - public function toggleInstall(Request $request, $id) + public function toggleInstall(Server $server) { - $repo = new ServerRepository; - try { - $repo->toggleInstall($id); - - Alert::success('Server install status was successfully toggled.')->flash(); - } catch (DisplayException $ex) { - Alert::danger($ex->getMessage())->flash(); - } catch (\Exception $ex) { - Log::error($ex); - Alert::danger('An unhandled exception occured while attemping to toggle this servers status. This error has been logged.')->flash(); + if ($server->installed > 1) { + throw new DisplayException(trans('admin/server.exceptions.marked_as_failed')); } - return redirect()->route('admin.servers.view.manage', $id); + $this->repository->update($server->id, [ + 'installed' => ! $server->installed, + ]); + + $this->alert->success(trans('admin/server.alerts.install_toggled'))->flash(); + + return redirect()->route('admin.servers.view.manage', $server->id); } /** * Reinstalls the server with the currently assigned pack and service. * - * @param \Illuminate\Http\Request $request - * @param int $id + * @param int $id * @return \Illuminate\Http\RedirectResponse + * + * @throws \Pterodactyl\Exceptions\DisplayException + * @throws \Pterodactyl\Exceptions\Model\DataValidationException + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ - public function reinstallServer(Request $request, $id) + public function reinstallServer($id) { - $repo = new ServerRepository; - try { - $repo->reinstall($id); - - Alert::success('Server successfully marked for reinstallation.')->flash(); - } catch (DisplayException $ex) { - Alert::danger($ex->getMessage())->flash(); - } catch (\Exception $ex) { - Log::error($ex); - Alert::danger('An unhandled exception occured while attemping to perform this reinstallation. This error has been logged.')->flash(); - } + $this->reinstallService->reinstall($id); + $this->alert->success(trans('admin/server.alerts.server_reinstalled'))->flash(); return redirect()->route('admin.servers.view.manage', $id); } @@ -423,60 +461,37 @@ class ServersController extends Controller /** * Setup a server to have a container rebuild. * - * @param \Illuminate\Http\Request $request - * @param int $id + * @param \Pterodactyl\Models\Server $server * @return \Illuminate\Http\RedirectResponse + * + * @throws \Pterodactyl\Exceptions\DisplayException */ - public function rebuildContainer(Request $request, $id) + public function rebuildContainer(Server $server) { - $server = Models\Server::with('node')->findOrFail($id); + $this->containerRebuildService->rebuild($server); + $this->alert->success(trans('admin/server.alerts.rebuild_on_boot'))->flash(); - try { - $server->node->guzzleClient([ - 'X-Access-Server' => $server->uuid, - 'X-Access-Token' => $server->node->daemonSecret, - ])->request('POST', '/server/rebuild'); - - Alert::success('A rebuild has been queued successfully. It will run the next time this server is booted.')->flash(); - } catch (TransferException $ex) { - Log::warning($ex); - Alert::danger('A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.')->flash(); - } - - return redirect()->route('admin.servers.view.manage', $id); + return redirect()->route('admin.servers.view.manage', $server->id); } /** * Manage the suspension status for a server. * - * @param \Illuminate\Http\Request $request - * @param int $id + * @param \Illuminate\Http\Request $request + * @param \Pterodactyl\Models\Server $server * @return \Illuminate\Http\RedirectResponse + * + * @throws \Pterodactyl\Exceptions\DisplayException + * @throws \Pterodactyl\Exceptions\Model\DataValidationException */ - public function manageSuspension(Request $request, $id) + public function manageSuspension(Request $request, Server $server) { - $repo = new ServerRepository; - $action = $request->input('action'); + $this->suspensionService->toggle($server, $request->input('action')); + $this->alert->success(trans('admin/server.alerts.suspension_toggled', [ + 'status' => $request->input('action') . 'ed', + ]))->flash(); - if (! in_array($action, ['suspend', 'unsuspend'])) { - Alert::danger('Invalid action was passed to function.')->flash(); - - return redirect()->route('admin.servers.view.manage', $id); - } - - try { - $repo->toggleAccess($id, ($action === 'unsuspend')); - - Alert::success('Server has been ' . $action . 'ed.'); - } catch (TransferException $ex) { - Log::warning($ex); - Alert::danger('A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.')->flash(); - } catch (\Exception $ex) { - Log::error($ex); - Alert::danger('An unhandled exception occured while attemping to ' . $action . ' this server. This error has been logged.')->flash(); - } - - return redirect()->route('admin.servers.view.manage', $id); + return redirect()->route('admin.servers.view.manage', $server->id); } /** @@ -498,15 +513,20 @@ class ServersController extends Controller Alert::success('Server details were successfully updated.')->flash(); } catch (DisplayValidationException $ex) { - return redirect()->route('admin.servers.view.build', $id)->withErrors(json_decode($ex->getMessage()))->withInput(); + return redirect() + ->route('admin.servers.view.build', $id) + ->withErrors(json_decode($ex->getMessage())) + ->withInput(); } catch (DisplayException $ex) { Alert::danger($ex->getMessage())->flash(); } catch (TransferException $ex) { Log::warning($ex); - Alert::danger('A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.')->flash(); + Alert::danger('A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.') + ->flash(); } catch (\Exception $ex) { Log::error($ex); - Alert::danger('An unhandled exception occured while attemping to add this server. This error has been logged.')->flash(); + Alert::danger('An unhandled exception occured while attemping to add this server. This error has been logged.') + ->flash(); } return redirect()->route('admin.servers.view.build', $id); @@ -532,10 +552,12 @@ class ServersController extends Controller Alert::danger($ex->getMessage())->flash(); } catch (TransferException $ex) { Log::warning($ex); - Alert::danger('A TransferException occurred while attempting to delete this server from the daemon, please ensure it is running. This error has been logged.')->flash(); + Alert::danger('A TransferException occurred while attempting to delete this server from the daemon, please ensure it is running. This error has been logged.') + ->flash(); } catch (\Exception $ex) { Log::error($ex); - Alert::danger('An unhandled exception occured while attemping to delete this server. This error has been logged.')->flash(); + Alert::danger('An unhandled exception occured while attemping to delete this server. This error has been logged.') + ->flash(); } return redirect()->route('admin.servers.view.delete', $id); @@ -554,7 +576,8 @@ class ServersController extends Controller try { if ($repo->updateStartup($id, $request->except('_token'), true)) { - Alert::success('Service configuration successfully modfied for this server, reinstalling now.')->flash(); + Alert::success('Service configuration successfully modfied for this server, reinstalling now.') + ->flash(); return redirect()->route('admin.servers.view', $id); } else { @@ -566,10 +589,12 @@ class ServersController extends Controller Alert::danger($ex->getMessage())->flash(); } catch (TransferException $ex) { Log::warning($ex); - Alert::danger('A TransferException occurred while attempting to update the startup for this server, please ensure the daemon is running. This error has been logged.')->flash(); + Alert::danger('A TransferException occurred while attempting to update the startup for this server, please ensure the daemon is running. This error has been logged.') + ->flash(); } catch (\Exception $ex) { Log::error($ex); - Alert::danger('An unhandled exception occured while attemping to update startup variables for this server. This error has been logged.')->flash(); + Alert::danger('An unhandled exception occured while attemping to update startup variables for this server. This error has been logged.') + ->flash(); } return redirect()->route('admin.servers.view.startup', $id); diff --git a/app/Repositories/Daemon/ServerRepository.php b/app/Repositories/Daemon/ServerRepository.php index f398abfa6..e3e197dbf 100644 --- a/app/Repositories/Daemon/ServerRepository.php +++ b/app/Repositories/Daemon/ServerRepository.php @@ -93,4 +93,36 @@ class ServerRepository extends BaseRepository implements ServerRepositoryInterfa 'json' => $data, ]); } + + /** + * {@inheritdoc} + */ + public function reinstall() + { + return $this->getHttpClient()->request('POST', '/server/reinstall'); + } + + /** + * {@inheritdoc} + */ + public function rebuild() + { + return $this->getHttpClient()->request('POST', '/server/rebuild'); + } + + /** + * {@inheritdoc} + */ + public function suspend() + { + return $this->getHttpClient()->request('POST', '/server/suspend'); + } + + /** + * {@inheritdoc} + */ + public function unsuspend() + { + return $this->getHttpClient()->request('POST', '/server/unsuspend'); + } } diff --git a/app/Repositories/Eloquent/LocationRepository.php b/app/Repositories/Eloquent/LocationRepository.php index 43e2e15d6..50d400730 100644 --- a/app/Repositories/Eloquent/LocationRepository.php +++ b/app/Repositories/Eloquent/LocationRepository.php @@ -76,4 +76,26 @@ class LocationRepository extends EloquentRepository implements LocationRepositor return $location->delete(); } + + /** + * {@inheritdoc} + */ + public function allWithDetails() + { + return $this->getBuilder()->withCount('nodes', 'servers')->get($this->getColumns()); + } + + /** + * {@inheritdoc} + */ + public function getWithNodes($id) + { + $instance = $this->getBuilder()->with('nodes.servers')->find($id, $this->getColumns()); + + if (! $instance) { + throw new RecordNotFoundException(); + } + + return $instance; + } } diff --git a/app/Services/Servers/ContainerRebuildService.php b/app/Services/Servers/ContainerRebuildService.php new file mode 100644 index 000000000..4a0224269 --- /dev/null +++ b/app/Services/Servers/ContainerRebuildService.php @@ -0,0 +1,80 @@ +. + * + * 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. + */ + +namespace Pterodactyl\Services\Servers; + +use GuzzleHttp\Exception\RequestException; +use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; +use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; +use Pterodactyl\Exceptions\DisplayException; +use Pterodactyl\Models\Server; + +class ContainerRebuildService +{ + /** + * @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface + */ + protected $daemonServerRepository; + + /** + * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface + */ + protected $repository; + + /** + * ContainerRebuildService constructor. + * + * @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository + * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository + */ + public function __construct( + DaemonServerRepositoryInterface $daemonServerRepository, + ServerRepositoryInterface $repository + ) { + $this->daemonServerRepository = $daemonServerRepository; + $this->repository = $repository; + } + + /** + * Mark a server for rebuild on next boot cycle. + * + * @param int|\Pterodactyl\Models\Server $server + * + * @throws \Pterodactyl\Exceptions\DisplayException + */ + public function rebuild($server) + { + if (! $server instanceof Server) { + $server = $this->repository->find($server); + } + + try { + $this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->rebuild(); + } catch (RequestException $exception) { + throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [ + 'code' => $exception->getResponse()->getStatusCode(), + ])); + } + } +} diff --git a/app/Services/Servers/DetailsModificationService.php b/app/Services/Servers/DetailsModificationService.php index f1fb3d275..ea2759702 100644 --- a/app/Services/Servers/DetailsModificationService.php +++ b/app/Services/Servers/DetailsModificationService.php @@ -146,7 +146,7 @@ class DetailsModificationService ], ]); - return $this->database->commit(); + $this->database->commit(); } catch (RequestException $exception) { throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [ 'code' => $exception->getResponse()->getStatusCode(), diff --git a/app/Services/Servers/ReinstallService.php b/app/Services/Servers/ReinstallService.php new file mode 100644 index 000000000..b99fafdec --- /dev/null +++ b/app/Services/Servers/ReinstallService.php @@ -0,0 +1,94 @@ +. + * + * 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. + */ + +namespace Pterodactyl\Services\Servers; + +use GuzzleHttp\Exception\RequestException; +use Pterodactyl\Exceptions\DisplayException; +use Pterodactyl\Models\Server; +use Illuminate\Database\ConnectionInterface; +use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; +use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; + +class ReinstallService +{ + /** + * @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface + */ + protected $daemonServerRepository; + + /** + * @var \Illuminate\Database\ConnectionInterface + */ + protected $database; + + /** + * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface + */ + protected $repository; + + /** + * ReinstallService constructor. + * + * @param \Illuminate\Database\ConnectionInterface $database + * @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository + * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository + */ + public function __construct( + ConnectionInterface $database, + DaemonServerRepositoryInterface $daemonServerRepository, + ServerRepositoryInterface $repository + ) { + $this->daemonServerRepository = $daemonServerRepository; + $this->database = $database; + $this->repository = $repository; + } + + /** + * @param int|\Pterodactyl\Models\Server $server + * + * @throws \Pterodactyl\Exceptions\DisplayException + * @throws \Pterodactyl\Exceptions\Model\DataValidationException + */ + public function reinstall($server) + { + if (! $server instanceof Server) { + $server = $this->repository->find($server); + } + + $this->database->beginTransaction(); + $this->repository->withoutFresh()->update($server->id, [ + 'installed' => 0, + ]); + + try { + $this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->reinstall(); + $this->database->commit(); + } catch (RequestException $exception) { + throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [ + 'code' => $exception->getResponse()->getStatusCode(), + ])); + } + } +} diff --git a/app/Services/Servers/SuspensionService.php b/app/Services/Servers/SuspensionService.php new file mode 100644 index 000000000..b272d06db --- /dev/null +++ b/app/Services/Servers/SuspensionService.php @@ -0,0 +1,114 @@ +. + * + * 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. + */ + +namespace Pterodactyl\Services\Servers; + +use GuzzleHttp\Exception\RequestException; +use Illuminate\Database\ConnectionInterface; +use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; +use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; +use Pterodactyl\Exceptions\DisplayException; +use Pterodactyl\Models\Server; + +class SuspensionService +{ + /** + * @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface + */ + protected $daemonServerRepository; + + /** + * @var \Illuminate\Database\ConnectionInterface + */ + protected $database; + + /** + * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface + */ + protected $repository; + + /** + * SuspensionService constructor. + * + * @param \Illuminate\Database\ConnectionInterface $database + * @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository + * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository + */ + public function __construct( + ConnectionInterface $database, + DaemonServerRepositoryInterface $daemonServerRepository, + ServerRepositoryInterface $repository + ) { + $this->daemonServerRepository = $daemonServerRepository; + $this->database = $database; + $this->repository = $repository; + } + + /** + * Suspends a server on the system. + * + * @param int|\Pterodactyl\Models\Server $server + * @param string $action + * @return bool + * + * @throws \Pterodactyl\Exceptions\DisplayException + * @throws \Pterodactyl\Exceptions\Model\DataValidationException + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException + */ + public function toggle($server, $action = 'suspend') + { + if (! $server instanceof Server) { + $server = $this->repository->find($server); + } + + if (! in_array($action, ['suspend', 'unsuspend'])) { + throw new \InvalidArgumentException(sprintf( + 'Action must be either suspend or unsuspend, %s passed.', $action + )); + } + + if ( + $action === 'suspend' && $server->suspended || + $action === 'unsuspend' && ! $server->suspended + ) { + return true; + } + + $this->database->beginTransaction(); + $this->repository->withoutFresh()->update($server->id, [ + 'suspended' => $action === 'suspend', + ]); + + try { + $this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->$action(); + $this->database->commit(); + + return true; + } catch (RequestException $exception) { + throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [ + 'code' => $exception->getResponse()->getStatusCode(), + ])); + } + } +} diff --git a/resources/lang/en/admin/server.php b/resources/lang/en/admin/server.php index 6ded58801..9b977ece3 100644 --- a/resources/lang/en/admin/server.php +++ b/resources/lang/en/admin/server.php @@ -24,10 +24,15 @@ return [ 'exceptions' => [ + 'marked_as_failed' => 'This server was marked as having failed a previous installation. Current status cannot be toggled in this state.', 'bad_variable' => 'There was a validation error with the :name variable.', 'daemon_exception' => 'There was an exception while attempting to communicate with the daemon resulting in a HTTP/:code response code. This exception has been logged.', ], 'alerts' => [ + 'suspension_toggled' => 'Server suspension status has been changed to :status.', + 'rebuild_on_boot' => 'This server has been marked as requiring a Docker Container rebuild. This will happen the next time the server is started.', + 'install_toggled' => 'The installation status for this server has been toggled.', + 'server_reinstalled' => 'This server has been queued for a reinstallation beginning now.', 'details_updated' => 'Server details have been successfully updated.', 'docker_image_updated' => 'Successfully changed the default Docker image to use for this server. A reboot is required to apply this change.', ], diff --git a/routes/admin.php b/routes/admin.php index 38785ee90..157109c13 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -100,30 +100,30 @@ Route::group(['prefix' => 'users'], function () { Route::group(['prefix' => 'servers'], function () { Route::get('/', 'ServersController@index')->name('admin.servers'); Route::get('/new', 'ServersController@create')->name('admin.servers.new'); - Route::get('/view/{id}', 'ServersController@viewIndex')->name('admin.servers.view'); - Route::get('/view/{id}/details', 'ServersController@viewDetails')->name('admin.servers.view.details'); - Route::get('/view/{id}/build', 'ServersController@viewBuild')->name('admin.servers.view.build'); - Route::get('/view/{id}/startup', 'ServersController@viewStartup')->name('admin.servers.view.startup'); - Route::get('/view/{id}/database', 'ServersController@viewDatabase')->name('admin.servers.view.database'); - Route::get('/view/{id}/manage', 'ServersController@viewManage')->name('admin.servers.view.manage'); - Route::get('/view/{id}/delete', 'ServersController@viewDelete')->name('admin.servers.view.delete'); + Route::get('/view/{server}', 'ServersController@viewIndex')->name('admin.servers.view'); + Route::get('/view/{server}/details', 'ServersController@viewDetails')->name('admin.servers.view.details'); + Route::get('/view/{server}/build', 'ServersController@viewBuild')->name('admin.servers.view.build'); + Route::get('/view/{server}/startup', 'ServersController@viewStartup')->name('admin.servers.view.startup'); + Route::get('/view/{server}/database', 'ServersController@viewDatabase')->name('admin.servers.view.database'); + Route::get('/view/{server}/manage', 'ServersController@viewManage')->name('admin.servers.view.manage'); + Route::get('/view/{server}/delete', 'ServersController@viewDelete')->name('admin.servers.view.delete'); Route::post('/new', 'ServersController@store'); Route::post('/new/nodes', 'ServersController@nodes')->name('admin.servers.new.nodes'); - Route::post('/view/{id}/build', 'ServersController@updateBuild'); - Route::post('/view/{id}/startup', 'ServersController@saveStartup'); - Route::post('/view/{id}/database', 'ServersController@newDatabase'); - Route::post('/view/{id}/manage/toggle', 'ServersController@toggleInstall')->name('admin.servers.view.manage.toggle'); - Route::post('/view/{id}/manage/rebuild', 'ServersController@rebuildContainer')->name('admin.servers.view.manage.rebuild'); - Route::post('/view/{id}/manage/suspension', 'ServersController@manageSuspension')->name('admin.servers.view.manage.suspension'); - Route::post('/view/{id}/manage/reinstall', 'ServersController@reinstallServer')->name('admin.servers.view.manage.reinstall'); - Route::post('/view/{id}/delete', 'ServersController@delete'); + Route::post('/view/{server}/build', 'ServersController@updateBuild'); + Route::post('/view/{server}/startup', 'ServersController@saveStartup'); + Route::post('/view/{server}/database', 'ServersController@newDatabase'); + Route::post('/view/{server}/manage/toggle', 'ServersController@toggleInstall')->name('admin.servers.view.manage.toggle'); + Route::post('/view/{server}/manage/rebuild', 'ServersController@rebuildContainer')->name('admin.servers.view.manage.rebuild'); + Route::post('/view/{server}/manage/suspension', 'ServersController@manageSuspension')->name('admin.servers.view.manage.suspension'); + Route::post('/view/{server}/manage/reinstall', 'ServersController@reinstallServer')->name('admin.servers.view.manage.reinstall'); + Route::post('/view/{server}/delete', 'ServersController@delete'); Route::patch('/view/{server}/details', 'ServersController@setDetails'); Route::patch('/view/{server}/details/container', 'ServersController@setContainer')->name('admin.servers.view.details.container'); - Route::patch('/view/{id}/database', 'ServersController@resetDatabasePassword'); + Route::patch('/view/{server}/database', 'ServersController@resetDatabasePassword'); - Route::delete('/view/{id}/database/{database}/delete', 'ServersController@deleteDatabase')->name('admin.servers.view.database.delete'); + Route::delete('/view/{server}/database/{database}/delete', 'ServersController@deleteDatabase')->name('admin.servers.view.database.delete'); }); /* diff --git a/tests/Unit/Services/Servers/ContainerRebuildServiceTest.php b/tests/Unit/Services/Servers/ContainerRebuildServiceTest.php new file mode 100644 index 000000000..2ccd487fc --- /dev/null +++ b/tests/Unit/Services/Servers/ContainerRebuildServiceTest.php @@ -0,0 +1,139 @@ +. + * + * 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. + */ + +namespace Tests\Unit\Services\Servers; + +use Exception; +use GuzzleHttp\Exception\RequestException; +use Mockery as m; +use Pterodactyl\Exceptions\DisplayException; +use Pterodactyl\Models\Server; +use Pterodactyl\Services\Servers\ContainerRebuildService; +use Tests\TestCase; +use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; +use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; + +class ContainerRebuildServiceTest extends TestCase +{ + /** + * @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface + */ + protected $daemonServerRepository; + + /** + * @var \GuzzleHttp\Exception\RequestException + */ + protected $exception; + + /** + * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface + */ + protected $repository; + + /** + * @var \Pterodactyl\Models\Server + */ + protected $server; + + /** + * @var \Pterodactyl\Services\Servers\ContainerRebuildService + */ + protected $service; + + /** + * Setup tests. + */ + public function setUp() + { + parent::setUp(); + + $this->daemonServerRepository = m::mock(DaemonServerRepositoryInterface::class); + $this->exception = m::mock(RequestException::class)->makePartial(); + $this->repository = m::mock(ServerRepositoryInterface::class); + $this->server = factory(Server::class)->make(['node_id' => 1]); + + $this->service = new ContainerRebuildService($this->daemonServerRepository, $this->repository); + } + + /** + * Test that a server is marked for rebuild when it's model is passed to the function. + */ + public function testServerShouldBeMarkedForARebuildWhenModelIsPassed() + { + $this->repository->shouldNotReceive('find'); + $this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf() + ->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf() + ->shouldReceive('rebuild')->withNoArgs()->once()->andReturnNull(); + + $this->service->rebuild($this->server); + } + + /** + * Test that a server is marked for rebuild when the ID of the server is passed to the function. + */ + public function testServerShouldBeMarkedForARebuildWhenServerIdIsPassed() + { + $this->repository->shouldReceive('find')->with($this->server->id)->once()->andReturn($this->server); + + $this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf() + ->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf() + ->shouldReceive('rebuild')->withNoArgs()->once()->andReturnNull(); + + $this->service->rebuild($this->server->id); + } + + /** + * Test that an exception thrown by guzzle is rendered as a displayable exception. + */ + public function testExceptionThrownByGuzzleShouldBeReRenderedAsDisplayable() + { + $this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id) + ->once()->andThrow($this->exception); + + $this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf() + ->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400); + + try { + $this->service->rebuild($this->server); + } catch (Exception $exception) { + $this->assertInstanceOf(DisplayException::class, $exception); + $this->assertEquals( + trans('admin/server.exceptions.daemon_exception', ['code' => 400,]), $exception->getMessage() + ); + } + } + + /** + * Test that an exception thrown by something other than guzzle is not transformed to a displayable. + * + * @expectedException \Exception + */ + public function testExceptionNotThrownByGuzzleShouldNotBeTransformedToDisplayable() + { + $this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id) + ->once()->andThrow(new Exception()); + + $this->service->rebuild($this->server); + } +} diff --git a/tests/Unit/Services/Servers/DetailsModificationServiceTest.php b/tests/Unit/Services/Servers/DetailsModificationServiceTest.php index 3ed8208a2..581b5e6db 100644 --- a/tests/Unit/Services/Servers/DetailsModificationServiceTest.php +++ b/tests/Unit/Services/Servers/DetailsModificationServiceTest.php @@ -310,8 +310,7 @@ class DetailsModificationServiceTest extends TestCase $this->database->shouldReceive('commit')->withNoArgs()->once()->andReturn(true); - $response = $this->service->setDockerImage($server, 'new/image'); - $this->assertTrue($response); + $this->service->setDockerImage($server, 'new/image'); } /** @@ -338,8 +337,7 @@ class DetailsModificationServiceTest extends TestCase $this->database->shouldReceive('commit')->withNoArgs()->once()->andReturn(true); - $response = $this->service->setDockerImage($server->id, 'new/image'); - $this->assertTrue($response); + $this->service->setDockerImage($server->id, 'new/image'); } /** diff --git a/tests/Unit/Services/Servers/ReinstallServiceTest.php b/tests/Unit/Services/Servers/ReinstallServiceTest.php new file mode 100644 index 000000000..471ff3f99 --- /dev/null +++ b/tests/Unit/Services/Servers/ReinstallServiceTest.php @@ -0,0 +1,177 @@ +. + * + * 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. + */ + +namespace Tests\Unit\Services\Servers; + +use Exception; +use GuzzleHttp\Exception\RequestException; +use Illuminate\Database\ConnectionInterface; +use Mockery as m; +use Pterodactyl\Exceptions\DisplayException; +use Pterodactyl\Models\Server; +use Pterodactyl\Services\Servers\ReinstallService; +use Tests\TestCase; +use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; +use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; + +class ReinstallServiceTest extends TestCase +{ + /** + * @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface + */ + protected $daemonServerRepository; + + /** + * @var \Illuminate\Database\ConnectionInterface + */ + protected $database; + + /** + * @var \GuzzleHttp\Exception\RequestException + */ + protected $exception; + + /** + * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface + */ + protected $repository; + + /** + * @var \Pterodactyl\Models\Server + */ + protected $server; + + /** + * @var \Pterodactyl\Services\Servers\ReinstallService + */ + protected $service; + + /** + * Setup tests. + */ + public function setUp() + { + parent::setUp(); + + $this->daemonServerRepository = m::mock(DaemonServerRepositoryInterface::class); + $this->database = m::mock(ConnectionInterface::class); + $this->exception = m::mock(RequestException::class)->makePartial(); + $this->repository = m::mock(ServerRepositoryInterface::class); + $this->server = factory(Server::class)->make(['node_id' => 1]); + + $this->service = new ReinstallService( + $this->database, + $this->daemonServerRepository, + $this->repository + ); + } + + /** + * Test that a server is reinstalled when it's model is passed to the function. + */ + public function testServerShouldBeReinstalledWhenModelIsPassed() + { + $this->repository->shouldNotReceive('find'); + + $this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); + $this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf() + ->shouldReceive('update')->with($this->server->id, [ + 'installed' => 0, + ])->once()->andReturnNull(); + + $this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf() + ->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf() + ->shouldReceive('reinstall')->withNoArgs()->once()->andReturnNull(); + $this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); + + $this->service->reinstall($this->server); + } + + /** + * Test that a server is reinstalled when the ID of the server is passed to the function. + */ + public function testServerShouldBeReinstalledWhenServerIdIsPassed() + { + $this->repository->shouldReceive('find')->with($this->server->id)->once()->andReturn($this->server); + + $this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); + $this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf() + ->shouldReceive('update')->with($this->server->id, [ + 'installed' => 0, + ])->once()->andReturnNull(); + + $this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf() + ->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf() + ->shouldReceive('reinstall')->withNoArgs()->once()->andReturnNull(); + $this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); + + $this->service->reinstall($this->server->id); + } + + /** + * Test that an exception thrown by guzzle is rendered as a displayable exception. + */ + public function testExceptionThrownByGuzzleShouldBeReRenderedAsDisplayable() + { + $this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); + $this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf() + ->shouldReceive('update')->with($this->server->id, [ + 'installed' => 0, + ])->once()->andReturnNull(); + + $this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id) + ->once()->andThrow($this->exception); + + $this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf() + ->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400); + + try { + $this->service->reinstall($this->server); + } catch (Exception $exception) { + $this->assertInstanceOf(DisplayException::class, $exception); + $this->assertEquals( + trans('admin/server.exceptions.daemon_exception', ['code' => 400,]), $exception->getMessage() + ); + } + } + + /** + * Test that an exception thrown by something other than guzzle is not transformed to a displayable. + * + * @expectedException \Exception + */ + public function testExceptionNotThrownByGuzzleShouldNotBeTransformedToDisplayable() + { + $this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); + $this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf() + ->shouldReceive('update')->with($this->server->id, [ + 'installed' => 0, + ])->once()->andReturnNull(); + + $this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id) + ->once()->andThrow(new Exception()); + + $this->service->reinstall($this->server); + } +} diff --git a/tests/Unit/Services/Servers/SuspensionServiceTest.php b/tests/Unit/Services/Servers/SuspensionServiceTest.php new file mode 100644 index 000000000..ff8beeb14 --- /dev/null +++ b/tests/Unit/Services/Servers/SuspensionServiceTest.php @@ -0,0 +1,198 @@ +. + * + * 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. + */ + +namespace Tests\Unit\Services\Servers; + +use Exception; +use GuzzleHttp\Exception\RequestException; +use Illuminate\Database\ConnectionInterface; +use Mockery as m; +use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; +use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; +use Pterodactyl\Exceptions\DisplayException; +use Pterodactyl\Models\Server; +use Pterodactyl\Services\Servers\SuspensionService; +use Tests\TestCase; + +class SuspensionServiceTest extends TestCase +{ + /** + * @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface + */ + protected $daemonServerRepository; + + /** + * @var \Illuminate\Database\ConnectionInterface + */ + protected $database; + + /** + * @var \GuzzleHttp\Exception\RequestException + */ + protected $exception; + + /** + * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface + */ + protected $repository; + + /** + * @var \Pterodactyl\Models\Server + */ + protected $server; + + /** + * @var \Pterodactyl\Services\Servers\SuspensionService + */ + protected $service; + + /** + * Setup tests. + */ + public function setUp() + { + parent::setUp(); + + $this->daemonServerRepository = m::mock(DaemonServerRepositoryInterface::class); + $this->database = m::mock(ConnectionInterface::class); + $this->exception = m::mock(RequestException::class)->makePartial(); + $this->repository = m::mock(ServerRepositoryInterface::class); + + $this->server = factory(Server::class)->make(['suspended' => 0, 'node_id' => 1]); + + $this->service = new SuspensionService( + $this->database, + $this->daemonServerRepository, + $this->repository + ); + } + + /** + * Test that the function accepts an integer in place of the server model. + * + * @expectedException \Exception + */ + public function testFunctionShouldAcceptAnIntegerInPlaceOfAServerModel() + { + $this->repository->shouldReceive('find')->with($this->server->id)->once()->andThrow(new Exception()); + + $this->service->toggle($this->server->id); + } + + /** + * Test that no action being passed suspends a server. + */ + public function testServerShouldBeSuspendedWhenNoActionIsPassed() + { + $this->server->suspended = 0; + + $this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); + $this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf() + ->shouldReceive('update')->with($this->server->id, ['suspended' => true])->once()->andReturnNull(); + + $this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf() + ->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf() + ->shouldReceive('suspend')->withNoArgs()->once()->andReturnNull(); + $this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); + + $this->assertTrue($this->service->toggle($this->server)); + } + + + /** + * Test that server is unsuspended if action=unsuspend + */ + public function testServerShouldBeUnsuspendedWhenUnsuspendActionIsPassed() + { + $this->server->suspended = 1; + + $this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); + $this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf() + ->shouldReceive('update')->with($this->server->id, ['suspended' => false])->once()->andReturnNull(); + + $this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf() + ->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf() + ->shouldReceive('unsuspend')->withNoArgs()->once()->andReturnNull(); + $this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); + + $this->assertTrue($this->service->toggle($this->server, 'unsuspend')); + } + + /** + * Test that nothing happens if a server is already unsuspended and action=unsuspend + */ + public function testNoActionShouldHappenIfServerIsAlreadyUnsuspendedAndActionIsUnsuspend() + { + $this->server->suspended = 0; + + $this->assertTrue($this->service->toggle($this->server, 'unsuspend')); + } + + /** + * Test that nothing happens if a server is already suspended and action=suspend + */ + public function testNoActionShouldHappenIfServerIsAlreadySuspendedAndActionIsSuspend() + { + $this->server->suspended = 1; + + $this->assertTrue($this->service->toggle($this->server, 'suspend')); + } + + /** + * Test that an exception thrown by Guzzle is caught and transformed to a displayable exception. + */ + public function testExceptionThrownByGuzzleShouldBeCaughtAndTransformedToDisplayable() + { + $this->server->suspended = 0; + + $this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); + $this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf() + ->shouldReceive('update')->with($this->server->id, ['suspended' => true])->once()->andReturnNull(); + + $this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id) + ->once()->andThrow($this->exception); + + $this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf() + ->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400); + + try { + $this->service->toggle($this->server); + } catch (Exception $exception) { + $this->assertInstanceOf(DisplayException::class, $exception); + $this->assertEquals( + trans('admin/server.exceptions.daemon_exception', ['code' => 400,]), $exception->getMessage() + ); + } + } + + /** + * Test that if action is not suspend or unsuspend an exception is thrown. + * + * @expectedException \InvalidArgumentException + */ + public function testExceptionShouldBeThrownIfActionIsNotValid() + { + $this->service->toggle($this->server, 'random'); + } +}