Remove unnecessary API calls to daemon

This commit is contained in:
Dane Everitt 2019-12-22 13:45:40 -08:00
parent 59bfc212c9
commit 34ffcdae7a
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
10 changed files with 10 additions and 312 deletions

View file

@ -1,109 +0,0 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Console\Commands\Server;
use Webmozart\Assert\Assert;
use Illuminate\Console\Command;
use GuzzleHttp\Exception\RequestException;
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
class RebuildServerCommand extends Command
{
/**
* @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService
*/
protected $configurationStructureService;
/**
* @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
*/
protected $daemonRepository;
/**
* @var string
*/
protected $description = 'Rebuild a single server, all servers on a node, or all servers on the panel.';
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
*/
protected $repository;
/**
* @var string
*/
protected $signature = 'p:server:rebuild
{server? : The ID of the server to rebuild.}
{--node= : ID of the node to rebuild all servers on. Ignored if server is passed.}';
/**
* RebuildServerCommand constructor.
*
* @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $daemonRepository
* @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
*/
public function __construct(
DaemonServerRepository $daemonRepository,
ServerConfigurationStructureService $configurationStructureService,
ServerRepositoryInterface $repository
) {
parent::__construct();
$this->configurationStructureService = $configurationStructureService;
$this->daemonRepository = $daemonRepository;
$this->repository = $repository;
}
/**
* Handle command execution.
*/
public function handle()
{
$servers = $this->getServersToProcess();
$bar = $this->output->createProgressBar(count($servers));
$servers->each(function ($server) use ($bar) {
$bar->clear();
$json = array_merge($this->configurationStructureService->handle($server), ['rebuild' => true]);
try {
$this->daemonRepository->setServer($server)->update($json);
} catch (RequestException $exception) {
$this->output->error(trans('command/messages.server.rebuild_failed', [
'name' => $server->name,
'id' => $server->id,
'node' => $server->node->name,
'message' => $exception->getMessage(),
]));
}
$bar->advance();
$bar->display();
});
$this->line('');
}
/**
* Return the servers to be rebuilt.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
private function getServersToProcess()
{
Assert::nullOrIntegerish($this->argument('server'), 'Value passed in server argument must be null or an integer, received %s.');
Assert::nullOrIntegerish($this->option('node'), 'Value passed in node option must be null or integer, received %s.');
return $this->repository->getDataForRebuild($this->argument('server'), $this->option('node'));
}
}

View file

@ -18,7 +18,6 @@ use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Services\Servers\SuspensionService;
use Pterodactyl\Services\Servers\ServerDeletionService;
use Pterodactyl\Services\Servers\ReinstallServerService;
use Pterodactyl\Services\Servers\ContainerRebuildService;
use Pterodactyl\Services\Servers\BuildModificationService;
use Pterodactyl\Services\Databases\DatabasePasswordService;
use Pterodactyl\Services\Servers\DetailsModificationService;
@ -54,11 +53,6 @@ class ServersController extends Controller
*/
protected $config;
/**
* @var \Pterodactyl\Services\Servers\ContainerRebuildService
*/
protected $containerRebuildService;
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
*/
@ -121,7 +115,6 @@ class ServersController extends Controller
* @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\Databases\DatabaseManagementService $databaseManagementService
* @param \Pterodactyl\Services\Databases\DatabasePasswordService $databasePasswordService
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $databaseRepository
@ -139,7 +132,6 @@ class ServersController extends Controller
AllocationRepositoryInterface $allocationRepository,
BuildModificationService $buildModificationService,
ConfigRepository $config,
ContainerRebuildService $containerRebuildService,
DatabaseManagementService $databaseManagementService,
DatabasePasswordService $databasePasswordService,
DatabaseRepositoryInterface $databaseRepository,
@ -156,7 +148,6 @@ class ServersController extends Controller
$this->allocationRepository = $allocationRepository;
$this->buildModificationService = $buildModificationService;
$this->config = $config;
$this->containerRebuildService = $containerRebuildService;
$this->databaseHostRepository = $databaseHostRepository;
$this->databaseManagementService = $databaseManagementService;
$this->databasePasswordService = $databasePasswordService;
@ -235,21 +226,6 @@ class ServersController extends Controller
return redirect()->route('admin.servers.view.manage', $server->id);
}
/**
* Setup a server to have a container rebuild.
*
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\Http\RedirectResponse
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function rebuildContainer(Server $server)
{
$this->containerRebuildService->handle($server);
$this->alert->success(trans('admin/server.alerts.rebuild_on_boot'))->flash();
return redirect()->route('admin.servers.view.manage', $server->id);
}
/**
* Manage the suspension status for a server.
*
@ -302,7 +278,7 @@ class ServersController extends Controller
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Throwable
*/
public function delete(Request $request, Server $server)
{
@ -320,7 +296,6 @@ class ServersController extends Controller
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Illuminate\Validation\ValidationException
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/

View file

@ -63,15 +63,16 @@ class ServerDetailsController extends ApplicationApiController
* Update the build details for a specific server.
*
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\UpdateServerBuildConfigurationRequest $request
* @param \Pterodactyl\Models\Server $server
* @return array
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function build(UpdateServerBuildConfigurationRequest $request): array
public function build(UpdateServerBuildConfigurationRequest $request, Server $server): array
{
$server = $this->buildModificationService->handle($request->getModel(Server::class), $request->validated());
$server = $this->buildModificationService->handle($server, $request->validated());
return $this->fractal->item($server)
->transformWith($this->getTransformer(ServerTransformer::class))

View file

@ -6,17 +6,11 @@ use Illuminate\Http\Response;
use Pterodactyl\Models\Server;
use Pterodactyl\Services\Servers\SuspensionService;
use Pterodactyl\Services\Servers\ReinstallServerService;
use Pterodactyl\Services\Servers\ContainerRebuildService;
use Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest;
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
class ServerManagementController extends ApplicationApiController
{
/**
* @var \Pterodactyl\Services\Servers\ContainerRebuildService
*/
private $rebuildService;
/**
* @var \Pterodactyl\Services\Servers\ReinstallServerService
*/
@ -30,18 +24,15 @@ class ServerManagementController extends ApplicationApiController
/**
* SuspensionController constructor.
*
* @param \Pterodactyl\Services\Servers\ContainerRebuildService $rebuildService
* @param \Pterodactyl\Services\Servers\ReinstallServerService $reinstallServerService
* @param \Pterodactyl\Services\Servers\SuspensionService $suspensionService
*/
public function __construct(
ContainerRebuildService $rebuildService,
ReinstallServerService $reinstallServerService,
SuspensionService $suspensionService
) {
parent::__construct();
$this->rebuildService = $rebuildService;
$this->reinstallServerService = $reinstallServerService;
$this->suspensionService = $suspensionService;
}
@ -53,9 +44,7 @@ class ServerManagementController extends ApplicationApiController
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\Http\Response
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Throwable
*/
public function suspend(ServerWriteRequest $request, Server $server): Response
{
@ -71,9 +60,7 @@ class ServerManagementController extends ApplicationApiController
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\Http\Response
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Throwable
*/
public function unsuspend(ServerWriteRequest $request, Server $server): Response
{
@ -99,20 +86,4 @@ class ServerManagementController extends ApplicationApiController
return $this->returnNoContent();
}
/**
* Mark a server as needing its container rebuilt the next time it is started.
*
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest $request
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\Http\Response
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function rebuild(ServerWriteRequest $request, Server $server): Response
{
$this->rebuildService->handle($server);
return $this->returnNoContent();
}
}

View file

@ -107,9 +107,10 @@ class BuildModificationService
$updateData = $this->structureService->handle($server);
try {
$this->daemonServerRepository->setServer($server)->update(
Arr::only($updateData, ['allocations', 'build', 'container'])
);
$this->daemonServerRepository
->setServer($server)
->update(Arr::only($updateData, ['build']));
$this->connection->commit();
} catch (RequestException $exception) {
throw new DaemonConnectionException($exception);

View file

@ -1,44 +0,0 @@
<?php
namespace Pterodactyl\Services\Servers;
use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException;
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
class ContainerRebuildService
{
/**
* @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
*/
private $repository;
/**
* ContainerRebuildService constructor.
*
* @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $repository
*/
public function __construct(DaemonServerRepository $repository)
{
$this->repository = $repository;
}
/**
* Mark a server for rebuild on next boot cycle. This just makes an empty patch
* request to Wings which will automatically mark the container as requiring a rebuild
* on the next boot as a result.
*
* @param \Pterodactyl\Models\Server $server
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function handle(Server $server)
{
try {
$this->repository->setServer($server)->update([]);
} catch (RequestException $exception) {
throw new DaemonConnectionException($exception);
}
}
}

View file

@ -2,16 +2,12 @@
namespace Pterodactyl\Services\Servers;
use Illuminate\Support\Arr;
use Pterodactyl\Models\User;
use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Traits\Services\HasUserLevels;
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
class StartupModificationService
@ -48,11 +44,6 @@ class StartupModificationService
*/
private $validatorService;
/**
* @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
*/
private $daemonServerRepository;
/**
* @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService
*/
@ -62,7 +53,6 @@ class StartupModificationService
* StartupModificationService constructor.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $daemonServerRepository
* @param \Pterodactyl\Contracts\Repository\EggRepositoryInterface $eggRepository
* @param \Pterodactyl\Services\Servers\EnvironmentService $environmentService
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
@ -72,7 +62,6 @@ class StartupModificationService
*/
public function __construct(
ConnectionInterface $connection,
DaemonServerRepository $daemonServerRepository,
EggRepositoryInterface $eggRepository,
EnvironmentService $environmentService,
ServerRepositoryInterface $repository,
@ -86,7 +75,6 @@ class StartupModificationService
$this->repository = $repository;
$this->serverVariableRepository = $serverVariableRepository;
$this->validatorService = $validatorService;
$this->daemonServerRepository = $daemonServerRepository;
$this->structureService = $structureService;
}
@ -98,7 +86,6 @@ class StartupModificationService
* @return \Pterodactyl\Models\Server
*
* @throws \Illuminate\Validation\ValidationException
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
@ -123,17 +110,6 @@ class StartupModificationService
$this->updateAdministrativeSettings($data, $server);
}
$updateData = $this->structureService->handle($server);
try {
$this->daemonServerRepository->setServer($server)->update(
Arr::only($updateData, ['environment', 'invocation', 'service'])
);
} catch (RequestException $exception) {
$this->connection->rollBack();
throw new DaemonConnectionException($exception);
}
$this->connection->commit();
return $server;

View file

@ -123,7 +123,6 @@ Route::group(['prefix' => 'servers'], function () {
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');

View file

@ -84,7 +84,6 @@ Route::group(['prefix' => '/servers'], function () {
Route::post('/{server}/suspend', 'Servers\ServerManagementController@suspend')->name('api.application.servers.suspend');
Route::post('/{server}/unsuspend', 'Servers\ServerManagementController@unsuspend')->name('api.application.servers.unsuspend');
Route::post('/{server}/reinstall', 'Servers\ServerManagementController@reinstall')->name('api.application.servers.reinstall');
Route::post('/{server}/rebuild', 'Servers\ServerManagementController@rebuild')->name('api.application.servers.rebuild');
Route::delete('/{server}', 'Servers\ServerController@delete');
Route::delete('/{server}/{force?}', 'Servers\ServerController@delete');

View file

@ -1,71 +0,0 @@
<?php
namespace Tests\Unit\Services\Servers;
use Mockery as m;
use Tests\TestCase;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException;
use Pterodactyl\Services\Servers\ContainerRebuildService;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface;
class ContainerRebuildServiceTest extends TestCase
{
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
*/
protected $repository;
/**
* @var \GuzzleHttp\Exception\RequestException
*/
protected $exception;
/**
* @var \Pterodactyl\Models\Server
*/
protected $server;
/**
* @var \Pterodactyl\Services\Servers\ContainerRebuildService
*/
protected $service;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$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->repository);
}
/**
* Test that a server is marked for rebuild.
*/
public function testServerIsMarkedForRebuild()
{
$this->repository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf()
->shouldReceive('rebuild')->withNoArgs()->once()->andReturn(new Response);
$this->service->handle($this->server);
}
/**
* Test that an exception thrown by guzzle is rendered as a displayable exception.
*
* @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function testExceptionThrownByGuzzle()
{
$this->repository->shouldReceive('setServer')->with($this->server)->once()->andThrow($this->exception);
$this->service->handle($this->server);
}
}