From f842aae3d31ebe87a5ec0bb6eac6fbfa97f96484 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 23 Jul 2017 19:57:43 -0500 Subject: [PATCH] Add build modification settings, fix exception handling to log to file --- .../Repository/RepositoryInterface.php | 13 +- .../Controllers/Admin/ServersController.php | 46 ++-- .../Eloquent/EloquentRepository.php | 8 + .../Servers/BuildModificationService.php | 244 ++++++++++++++++++ .../Servers/ContainerRebuildService.php | 16 +- app/Services/Servers/CreationService.php | 26 +- .../Servers/DetailsModificationService.php | 22 +- app/Services/Servers/ReinstallService.php | 16 +- app/Services/Servers/SuspensionService.php | 16 +- resources/lang/en/admin/server.php | 3 + .../Servers/ContainerRebuildServiceTest.php | 16 +- .../Services/Servers/CreationServiceTest.php | 10 +- .../DetailsModificationServiceTest.php | 16 +- .../Services/Servers/ReinstallServiceTest.php | 13 +- .../Servers/SuspensionServiceTest.php | 12 +- 15 files changed, 427 insertions(+), 50 deletions(-) create mode 100644 app/Services/Servers/BuildModificationService.php diff --git a/app/Contracts/Repository/RepositoryInterface.php b/app/Contracts/Repository/RepositoryInterface.php index b32260952..cb8241e38 100644 --- a/app/Contracts/Repository/RepositoryInterface.php +++ b/app/Contracts/Repository/RepositoryInterface.php @@ -83,7 +83,7 @@ interface RepositoryInterface /** * Delete a given record from the database. * - * @param int $id + * @param int $id * @return bool|null */ public function delete($id); @@ -130,6 +130,17 @@ interface RepositoryInterface */ public function update($id, array $fields, $validate = true, $force = false); + /** + * Perform a mass update where matching records are updated using whereIn. + * This does not perform any model data validation. + * + * @param string $column + * @param array $values + * @param array $fields + * @return int + */ + public function updateWhereIn($column, array $values, array $fields); + /** * Update multiple records matching the passed clauses. * diff --git a/app/Http/Controllers/Admin/ServersController.php b/app/Http/Controllers/Admin/ServersController.php index ca5b862d4..851cd287c 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -44,6 +44,7 @@ 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\BuildModificationService; use Pterodactyl\Services\Servers\ContainerRebuildService; use Pterodactyl\Services\Servers\CreationService; use Pterodactyl\Services\Servers\DetailsModificationService; @@ -62,6 +63,11 @@ class ServersController extends Controller */ protected $allocationRepository; + /** + * @var \Pterodactyl\Services\Servers\BuildModificationService + */ + protected $buildModificationService; + /** * @var \Illuminate\Contracts\Config\Repository */ @@ -132,6 +138,7 @@ class ServersController extends Controller * * @param \Prologue\Alerts\AlertsMessageBag $alert * @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository + * @param \Pterodactyl\Services\Servers\BuildModificationService $buildModificationService * @param \Illuminate\Contracts\Config\Repository $config * @param \Pterodactyl\Services\Servers\ContainerRebuildService $containerRebuildService * @param \Pterodactyl\Services\Servers\CreationService $service @@ -149,6 +156,7 @@ class ServersController extends Controller public function __construct( AlertsMessageBag $alert, AllocationRepositoryInterface $allocationRepository, + BuildModificationService $buildModificationService, ConfigRepository $config, ContainerRebuildService $containerRebuildService, CreationService $service, @@ -165,6 +173,7 @@ class ServersController extends Controller ) { $this->alert = $alert; $this->allocationRepository = $allocationRepository; + $this->buildModificationService = $buildModificationService; $this->config = $config; $this->containerRebuildService = $containerRebuildService; $this->databaseCreationService = $databaseCreationService; @@ -490,39 +499,22 @@ class ServersController extends Controller /** * Update the build configuration 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 + * @internal param int $id */ - public function updateBuild(Request $request, $id) + public function updateBuild(Request $request, Server $server) { - $repo = new ServerRepository; - - try { - $repo->changeBuild($id, $request->intersect([ + $this->buildModificationService->handle($server, $request->intersect([ 'allocation_id', 'add_allocations', 'remove_allocations', 'memory', 'swap', 'io', 'cpu', 'disk', - ])); + ])); + $this->alert->success(trans('admin/server.alerts.build_updated'))->flash(); - 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(); - } 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(); - } catch (\Exception $ex) { - Log::error($ex); - 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); + return redirect()->route('admin.servers.view.build', $server->id); } /** diff --git a/app/Repositories/Eloquent/EloquentRepository.php b/app/Repositories/Eloquent/EloquentRepository.php index 994647df9..5746a562c 100644 --- a/app/Repositories/Eloquent/EloquentRepository.php +++ b/app/Repositories/Eloquent/EloquentRepository.php @@ -145,6 +145,14 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf return ($this->withFresh) ? $instance->fresh() : $saved; } + /** + * {@inheritdoc} + */ + public function updateWhereIn($column, array $values, array $fields) + { + return $this->getBuilder()->whereIn($column, $values)->update($fields); + } + /** * {@inheritdoc} */ diff --git a/app/Services/Servers/BuildModificationService.php b/app/Services/Servers/BuildModificationService.php new file mode 100644 index 000000000..26f64632f --- /dev/null +++ b/app/Services/Servers/BuildModificationService.php @@ -0,0 +1,244 @@ +. + * + * 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 Illuminate\Log\Writer; +use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; +use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; +use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; +use Pterodactyl\Exceptions\DisplayException; +use Pterodactyl\Exceptions\Repository\RecordNotFoundException; +use Pterodactyl\Models\Server; + +class BuildModificationService +{ + /** + * @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface + */ + protected $allocationRepository; + + /** + * @var array + */ + protected $build = []; + + /** + * @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface + */ + protected $daemonServerRepository; + + /** + * @var \Illuminate\Database\ConnectionInterface + */ + protected $database; + + /** + * @var null|int + */ + protected $firstAllocationId = null; + + /** + * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface + */ + protected $repository; + + /** + * @var \Illuminate\Log\Writer + */ + protected $writer; + + /** + * BuildModificationService constructor. + * + * @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository + * @param \Illuminate\Database\ConnectionInterface $database + * @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository + * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository + * @param \Illuminate\Log\Writer $writer + */ + public function __construct( + AllocationRepositoryInterface $allocationRepository, + ConnectionInterface $database, + DaemonServerRepositoryInterface $daemonServerRepository, + ServerRepositoryInterface $repository, + Writer $writer + ) { + $this->allocationRepository = $allocationRepository; + $this->daemonServerRepository = $daemonServerRepository; + $this->database = $database; + $this->repository = $repository; + $this->writer = $writer; + } + + /** + * Set build array parameters. + * + * @param string $key + * @param mixed $value + */ + public function setBuild($key, $value) + { + $this->build[$key] = $value; + } + + /** + * Return the build array. + * + * @return array + */ + public function getBuild() + { + return $this->build; + } + + /** + * Change the build details for a specified server. + * + * @param int|\Pterodactyl\Models\Server $server + * @param array $data + * + * @throws \Pterodactyl\Exceptions\DisplayException + * @throws \Pterodactyl\Exceptions\Model\DataValidationException + */ + public function handle($server, array $data) + { + if (! $server instanceof Server) { + $server = $this->repository->find($server); + } + + $data['allocation_id'] = array_get($data, 'allocation_id', $server->allocation_id); + $this->database->beginTransaction(); + + $this->setBuild('memory', (int) array_get($data, 'memory', $server->memory)); + $this->setBuild('swap', (int) array_get($data, 'swap', $server->swap)); + $this->setBuild('io', (int) array_get($data, 'io', $server->io)); + $this->setBuild('cpu', (int) array_get($data, 'cpu', $server->cpu)); + $this->setBuild('disk', (int) array_get($data, 'disk', $server->disk)); + + $this->processAllocations($server, $data); + $allocations = $this->allocationRepository->findWhere([ + ['server_id', '=', $server->id], + ]); + + if (isset($data['allocation_id']) && $data['allocation_id'] != $server->allocation_id) { + try { + $allocation = $this->allocationRepository->findFirstWhere([ + ['id', '=', $data['allocation_id']], + ['server_id', '=', $server->id], + ]); + } catch (RecordNotFoundException $ex) { + throw new DisplayException(trans('admin/server.exceptions.default_allocation_not_found')); + } + + $this->setBuild('default', ['ip' => $allocation->ip, 'port' => $allocation->port]); + } + + $this->setBuild('ports|overwrite', $allocations->groupBy('ip')->map(function ($item) { + return $item->pluck('port'); + })->toArray()); + + try { + $this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->update([ + 'build' => $this->getBuild(), + ]); + + $this->database->commit(); + } catch (RequestException $exception) { + $response = $exception->getResponse(); + $this->writer->warning($exception); + + throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [ + 'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(), + ])); + } + } + + /** + * Process the allocations being assigned in the data and ensure they are available for a server. + * + * @param \Pterodactyl\Models\Server $server + * @param array $data + * + * @throws \Pterodactyl\Exceptions\DisplayException + */ + public function processAllocations(Server $server, array &$data) + { + if (! array_key_exists('add_allocations', $data) && ! array_key_exists('remove_allocations', $data)) { + return; + } + + // Loop through allocations to add. + if (array_key_exists('add_allocations', $data) && ! empty($data['add_allocations'])) { + $unassigned = $this->allocationRepository->findWhere([ + ['server_id', '=', null], + ['node_id', '=', $server->node_id], + ])->pluck('id')->toArray(); + + foreach ($data['add_allocations'] as $allocation) { + if (! in_array($allocation, $unassigned)) { + continue; + } + + $this->firstAllocationId = $this->firstAllocationId ?? $allocation; + $toUpdate[] = [$allocation]; + } + + if (isset($toUpdate)) { + $this->allocationRepository->updateWhereIn('id', $toUpdate, ['server_id' => $server->id]); + unset($toUpdate); + } + } + + // Loop through allocations to remove. + if (array_key_exists('remove_allocations', $data) && ! empty($data['remove_allocations'])) { + $assigned = $this->allocationRepository->findWhere([ + ['server_id', '=', $server->id], + ])->pluck('id')->toArray(); + + foreach ($data['remove_allocations'] as $allocation) { + if (! in_array($allocation, $assigned)) { + continue; + } + + if ($allocation == $data['allocation_id']) { + if (is_null($this->firstAllocationId)) { + throw new DisplayException(trans('admin/server.exceptions.no_new_default_allocation')); + } + + $data['allocation_id'] = $this->firstAllocationId; + } + + $toUpdate[] = [$allocation]; + } + + if (isset($toUpdate)) { + $this->allocationRepository->updateWhereIn('id', $toUpdate, ['server_id' => null]); + unset($toUpdate); + } + } + } +} diff --git a/app/Services/Servers/ContainerRebuildService.php b/app/Services/Servers/ContainerRebuildService.php index 4a0224269..20ce9e0b9 100644 --- a/app/Services/Servers/ContainerRebuildService.php +++ b/app/Services/Servers/ContainerRebuildService.php @@ -25,6 +25,7 @@ namespace Pterodactyl\Services\Servers; use GuzzleHttp\Exception\RequestException; +use Illuminate\Log\Writer; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; use Pterodactyl\Exceptions\DisplayException; @@ -42,18 +43,26 @@ class ContainerRebuildService */ protected $repository; + /** + * @var \Illuminate\Log\Writer + */ + protected $writer; + /** * ContainerRebuildService constructor. * * @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository + * @param \Illuminate\Log\Writer $writer */ public function __construct( DaemonServerRepositoryInterface $daemonServerRepository, - ServerRepositoryInterface $repository + ServerRepositoryInterface $repository, + Writer $writer ) { $this->daemonServerRepository = $daemonServerRepository; $this->repository = $repository; + $this->writer = $writer; } /** @@ -72,8 +81,11 @@ class ContainerRebuildService try { $this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->rebuild(); } catch (RequestException $exception) { + $response = $exception->getResponse(); + $this->writer->warning($exception); + throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [ - 'code' => $exception->getResponse()->getStatusCode(), + 'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(), ])); } } diff --git a/app/Services/Servers/CreationService.php b/app/Services/Servers/CreationService.php index 290f68a80..fbe895052 100644 --- a/app/Services/Servers/CreationService.php +++ b/app/Services/Servers/CreationService.php @@ -24,6 +24,9 @@ namespace Pterodactyl\Services\Servers; +use GuzzleHttp\Exception\RequestException; +use Illuminate\Log\Writer; +use Pterodactyl\Exceptions\DisplayException; use Ramsey\Uuid\Uuid; use Illuminate\Database\DatabaseManager; use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; @@ -80,6 +83,11 @@ class CreationService */ protected $validatorService; + /** + * @var \Illuminate\Log\Writer + */ + protected $writer; + /** * CreationService constructor. * @@ -92,6 +100,7 @@ class CreationService * @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $userRepository * @param \Pterodactyl\Services\Servers\UsernameGenerationService $usernameService * @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService + * @param \Illuminate\Log\Writer $writer */ public function __construct( AllocationRepositoryInterface $allocationRepository, @@ -102,7 +111,8 @@ class CreationService ServerVariableRepositoryInterface $serverVariableRepository, UserRepositoryInterface $userRepository, UsernameGenerationService $usernameService, - VariableValidatorService $validatorService + VariableValidatorService $validatorService, + Writer $writer ) { $this->allocationRepository = $allocationRepository; $this->daemonServerRepository = $daemonServerRepository; @@ -113,6 +123,7 @@ class CreationService $this->userRepository = $userRepository; $this->usernameService = $usernameService; $this->validatorService = $validatorService; + $this->writer = $writer; } /** @@ -121,6 +132,7 @@ class CreationService * @param array $data * @return mixed * + * @throws \Pterodactyl\Exceptions\DisplayException * @throws \Pterodactyl\Exceptions\Model\DataValidationException */ public function create(array $data) @@ -178,9 +190,17 @@ class CreationService $this->serverVariableRepository->insert($records); // Create the server on the daemon & commit it to the database. - $this->daemonServerRepository->setNode($server->node_id)->create($server->id); + try { + $this->daemonServerRepository->setNode($server->node_id)->create($server->id); + $this->database->commit(); + } catch (RequestException $exception) { + $response = $exception->getResponse(); + $this->writer->warning($exception); - $this->database->commit(); + throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [ + 'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(), + ])); + } return $server; } diff --git a/app/Services/Servers/DetailsModificationService.php b/app/Services/Servers/DetailsModificationService.php index ea2759702..c28970213 100644 --- a/app/Services/Servers/DetailsModificationService.php +++ b/app/Services/Servers/DetailsModificationService.php @@ -26,6 +26,7 @@ namespace Pterodactyl\Services\Servers; use GuzzleHttp\Exception\RequestException; use Illuminate\Database\DatabaseManager; +use Illuminate\Log\Writer; use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Models\Server; use Pterodactyl\Repositories\Eloquent\ServerRepository; @@ -48,21 +49,29 @@ class DetailsModificationService */ protected $repository; + /** + * @var \Illuminate\Log\Writer + */ + protected $writer; + /** * DetailsModificationService constructor. * * @param \Illuminate\Database\DatabaseManager $database * @param \Pterodactyl\Repositories\Daemon\ServerRepository $daemonServerRepository * @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository + * @param \Illuminate\Log\Writer $writer */ public function __construct( DatabaseManager $database, DaemonServerRepository $daemonServerRepository, - ServerRepository $repository + ServerRepository $repository, + Writer $writer ) { $this->database = $database; $this->daemonServerRepository = $daemonServerRepository; $this->repository = $repository; + $this->writer = $writer; } /** @@ -114,8 +123,11 @@ class DetailsModificationService return $this->database->commit(); } catch (RequestException $exception) { + $response = $exception->getResponse(); + $this->writer->warning($exception); + throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [ - 'code' => $exception->getResponse()->getStatusCode(), + 'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(), ])); } } @@ -125,7 +137,6 @@ class DetailsModificationService * * @param int|\Pterodactyl\Models\Server $server * @param string $image - * @return bool * * @throws \Pterodactyl\Exceptions\DisplayException * @throws \Pterodactyl\Exceptions\Model\DataValidationException @@ -148,8 +159,11 @@ class DetailsModificationService $this->database->commit(); } catch (RequestException $exception) { + $response = $exception->getResponse(); + $this->writer->warning($exception); + throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [ - 'code' => $exception->getResponse()->getStatusCode(), + 'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(), ])); } } diff --git a/app/Services/Servers/ReinstallService.php b/app/Services/Servers/ReinstallService.php index b99fafdec..5b1b24dde 100644 --- a/app/Services/Servers/ReinstallService.php +++ b/app/Services/Servers/ReinstallService.php @@ -25,6 +25,7 @@ namespace Pterodactyl\Services\Servers; use GuzzleHttp\Exception\RequestException; +use Illuminate\Log\Writer; use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Models\Server; use Illuminate\Database\ConnectionInterface; @@ -48,21 +49,29 @@ class ReinstallService */ protected $repository; + /** + * @var \Illuminate\Log\Writer + */ + protected $writer; + /** * ReinstallService constructor. * * @param \Illuminate\Database\ConnectionInterface $database * @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository + * @param \Illuminate\Log\Writer $writer */ public function __construct( ConnectionInterface $database, DaemonServerRepositoryInterface $daemonServerRepository, - ServerRepositoryInterface $repository + ServerRepositoryInterface $repository, + Writer $writer ) { $this->daemonServerRepository = $daemonServerRepository; $this->database = $database; $this->repository = $repository; + $this->writer = $writer; } /** @@ -86,8 +95,11 @@ class ReinstallService $this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->reinstall(); $this->database->commit(); } catch (RequestException $exception) { + $response = $exception->getResponse(); + $this->writer->warning($exception); + throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [ - 'code' => $exception->getResponse()->getStatusCode(), + 'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(), ])); } } diff --git a/app/Services/Servers/SuspensionService.php b/app/Services/Servers/SuspensionService.php index b272d06db..96462db3b 100644 --- a/app/Services/Servers/SuspensionService.php +++ b/app/Services/Servers/SuspensionService.php @@ -26,6 +26,7 @@ namespace Pterodactyl\Services\Servers; use GuzzleHttp\Exception\RequestException; use Illuminate\Database\ConnectionInterface; +use Illuminate\Log\Writer; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; use Pterodactyl\Exceptions\DisplayException; @@ -48,21 +49,29 @@ class SuspensionService */ protected $repository; + /** + * @var \Illuminate\Log\Writer + */ + protected $writer; + /** * SuspensionService constructor. * * @param \Illuminate\Database\ConnectionInterface $database * @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository + * @param \Illuminate\Log\Writer $writer */ public function __construct( ConnectionInterface $database, DaemonServerRepositoryInterface $daemonServerRepository, - ServerRepositoryInterface $repository + ServerRepositoryInterface $repository, + Writer $writer ) { $this->daemonServerRepository = $daemonServerRepository; $this->database = $database; $this->repository = $repository; + $this->writer = $writer; } /** @@ -106,8 +115,11 @@ class SuspensionService return true; } catch (RequestException $exception) { + $response = $exception->getResponse(); + $this->writer->warning($exception); + throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [ - 'code' => $exception->getResponse()->getStatusCode(), + 'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(), ])); } } diff --git a/resources/lang/en/admin/server.php b/resources/lang/en/admin/server.php index 9b977ece3..ab4057d9e 100644 --- a/resources/lang/en/admin/server.php +++ b/resources/lang/en/admin/server.php @@ -24,11 +24,14 @@ return [ 'exceptions' => [ + 'no_new_default_allocation' => 'You are attempting to delete the default allocation for this server but there is no fallback allocation to use.', '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.', + 'default_allocation_not_found' => 'The requested default allocation was not found in this server\'s allocations.', ], 'alerts' => [ + 'build_updated' => 'The build details for this server have been updated. Some changes may require a restart to take effect.', '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.', diff --git a/tests/Unit/Services/Servers/ContainerRebuildServiceTest.php b/tests/Unit/Services/Servers/ContainerRebuildServiceTest.php index 2ccd487fc..fac637c5c 100644 --- a/tests/Unit/Services/Servers/ContainerRebuildServiceTest.php +++ b/tests/Unit/Services/Servers/ContainerRebuildServiceTest.php @@ -26,6 +26,7 @@ namespace Tests\Unit\Services\Servers; use Exception; use GuzzleHttp\Exception\RequestException; +use Illuminate\Log\Writer; use Mockery as m; use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Models\Server; @@ -61,6 +62,11 @@ class ContainerRebuildServiceTest extends TestCase */ protected $service; + /** + * @var \Illuminate\Log\Writer + */ + protected $writer; + /** * Setup tests. */ @@ -71,9 +77,15 @@ class ContainerRebuildServiceTest extends TestCase $this->daemonServerRepository = m::mock(DaemonServerRepositoryInterface::class); $this->exception = m::mock(RequestException::class)->makePartial(); $this->repository = m::mock(ServerRepositoryInterface::class); + $this->writer = m::mock(Writer::class); + $this->server = factory(Server::class)->make(['node_id' => 1]); - $this->service = new ContainerRebuildService($this->daemonServerRepository, $this->repository); + $this->service = new ContainerRebuildService( + $this->daemonServerRepository, + $this->repository, + $this->writer + ); } /** @@ -114,6 +126,8 @@ class ContainerRebuildServiceTest extends TestCase $this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf() ->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400); + $this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull(); + try { $this->service->rebuild($this->server); } catch (Exception $exception) { diff --git a/tests/Unit/Services/Servers/CreationServiceTest.php b/tests/Unit/Services/Servers/CreationServiceTest.php index 3f1ac7c83..1ff7c3abc 100644 --- a/tests/Unit/Services/Servers/CreationServiceTest.php +++ b/tests/Unit/Services/Servers/CreationServiceTest.php @@ -24,6 +24,7 @@ namespace Tests\Unit\Services\Servers; +use Illuminate\Log\Writer; use Mockery as m; use phpmock\phpunit\PHPMock; use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; @@ -98,6 +99,11 @@ class CreationServiceTest extends TestCase */ protected $uuid; + /** + * @var \Illuminate\Log\Writer + */ + protected $writer; + /** * Setup tests. */ @@ -115,6 +121,7 @@ class CreationServiceTest extends TestCase $this->usernameService = m::mock(UsernameGenerationService::class); $this->validatorService = m::mock(VariableValidatorService::class); $this->uuid = m::mock('overload:Ramsey\Uuid\Uuid'); + $this->writer = m::mock(Writer::class); $this->getFunctionMock('\\Pterodactyl\\Services\\Servers', 'bin2hex') ->expects($this->any())->willReturn('randomstring'); @@ -131,7 +138,8 @@ class CreationServiceTest extends TestCase $this->serverVariableRepository, $this->userRepository, $this->usernameService, - $this->validatorService + $this->validatorService, + $this->writer ); } diff --git a/tests/Unit/Services/Servers/DetailsModificationServiceTest.php b/tests/Unit/Services/Servers/DetailsModificationServiceTest.php index 581b5e6db..37164b4fb 100644 --- a/tests/Unit/Services/Servers/DetailsModificationServiceTest.php +++ b/tests/Unit/Services/Servers/DetailsModificationServiceTest.php @@ -25,6 +25,7 @@ namespace Tests\Unit\Services\Servers; use Exception; +use Illuminate\Log\Writer; use Mockery as m; use Tests\TestCase; use phpmock\phpunit\PHPMock; @@ -65,6 +66,11 @@ class DetailsModificationServiceTest extends TestCase */ protected $service; + /** + * @var \Illuminate\Log\Writer + */ + protected $writer; + /** * Setup tests. */ @@ -76,6 +82,7 @@ class DetailsModificationServiceTest extends TestCase $this->exception = m::mock(RequestException::class)->makePartial(); $this->daemonServerRepository = m::mock(DaemonServerRepository::class); $this->repository = m::mock(ServerRepository::class); + $this->writer = m::mock(Writer::class); $this->getFunctionMock('\\Pterodactyl\\Services\\Servers', 'bin2hex') ->expects($this->any())->willReturn('randomString'); @@ -83,7 +90,8 @@ class DetailsModificationServiceTest extends TestCase $this->service = new DetailsModificationService( $this->database, $this->daemonServerRepository, - $this->repository + $this->repository, + $this->writer ); } @@ -243,7 +251,7 @@ class DetailsModificationServiceTest extends TestCase $this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf() ->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400); - $this->database->shouldNotReceive('commit'); + $this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull(); try { $this->service->edit($server, $data); @@ -281,7 +289,6 @@ class DetailsModificationServiceTest extends TestCase ], true, true)->once()->andReturnNull(); $this->daemonServerRepository->shouldReceive('setNode')->andThrow(new Exception()); - $this->database->shouldNotReceive('commit'); $this->service->edit($server, $data); } @@ -357,7 +364,7 @@ class DetailsModificationServiceTest extends TestCase $this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf() ->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400); - $this->database->shouldNotReceive('commit'); + $this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull(); try { $this->service->setDockerImage($server, 'new/image'); @@ -385,7 +392,6 @@ class DetailsModificationServiceTest extends TestCase ])->once()->andReturnNull(); $this->daemonServerRepository->shouldReceive('setNode')->andThrow(new Exception()); - $this->database->shouldNotReceive('commit'); $this->service->setDockerImage($server, 'new/image'); } diff --git a/tests/Unit/Services/Servers/ReinstallServiceTest.php b/tests/Unit/Services/Servers/ReinstallServiceTest.php index 471ff3f99..ee023012e 100644 --- a/tests/Unit/Services/Servers/ReinstallServiceTest.php +++ b/tests/Unit/Services/Servers/ReinstallServiceTest.php @@ -27,6 +27,7 @@ namespace Tests\Unit\Services\Servers; use Exception; use GuzzleHttp\Exception\RequestException; use Illuminate\Database\ConnectionInterface; +use Illuminate\Log\Writer; use Mockery as m; use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Models\Server; @@ -67,6 +68,11 @@ class ReinstallServiceTest extends TestCase */ protected $service; + /** + * @var \Illuminate\Log\Writer + */ + protected $writer; + /** * Setup tests. */ @@ -78,12 +84,15 @@ class ReinstallServiceTest extends TestCase $this->database = m::mock(ConnectionInterface::class); $this->exception = m::mock(RequestException::class)->makePartial(); $this->repository = m::mock(ServerRepositoryInterface::class); + $this->writer = m::mock(Writer::class); + $this->server = factory(Server::class)->make(['node_id' => 1]); $this->service = new ReinstallService( $this->database, $this->daemonServerRepository, - $this->repository + $this->repository, + $this->writer ); } @@ -146,6 +155,8 @@ class ReinstallServiceTest extends TestCase $this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf() ->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400); + $this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull(); + try { $this->service->reinstall($this->server); } catch (Exception $exception) { diff --git a/tests/Unit/Services/Servers/SuspensionServiceTest.php b/tests/Unit/Services/Servers/SuspensionServiceTest.php index ff8beeb14..0347038e3 100644 --- a/tests/Unit/Services/Servers/SuspensionServiceTest.php +++ b/tests/Unit/Services/Servers/SuspensionServiceTest.php @@ -27,6 +27,7 @@ namespace Tests\Unit\Services\Servers; use Exception; use GuzzleHttp\Exception\RequestException; use Illuminate\Database\ConnectionInterface; +use Illuminate\Log\Writer; use Mockery as m; use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; @@ -67,6 +68,11 @@ class SuspensionServiceTest extends TestCase */ protected $service; + /** + * @var \Illuminate\Log\Writer + */ + protected $writer; + /** * Setup tests. */ @@ -78,13 +84,15 @@ class SuspensionServiceTest extends TestCase $this->database = m::mock(ConnectionInterface::class); $this->exception = m::mock(RequestException::class)->makePartial(); $this->repository = m::mock(ServerRepositoryInterface::class); + $this->writer = m::mock(Writer::class); $this->server = factory(Server::class)->make(['suspended' => 0, 'node_id' => 1]); $this->service = new SuspensionService( $this->database, $this->daemonServerRepository, - $this->repository + $this->repository, + $this->writer ); } @@ -176,6 +184,8 @@ class SuspensionServiceTest extends TestCase $this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf() ->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400); + $this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull(); + try { $this->service->toggle($this->server); } catch (Exception $exception) {