Add server build management to API

This commit is contained in:
Dane Everitt 2018-01-21 16:02:03 -06:00
parent d3dba3fcf9
commit aca0819bcd
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
8 changed files with 202 additions and 128 deletions

View File

@ -40,4 +40,21 @@ interface AllocationRepositoryInterface extends RepositoryInterface
* @return \Illuminate\Support\Collection * @return \Illuminate\Support\Collection
*/ */
public function getUniqueAllocationIpsForNode(int $node): Collection; public function getUniqueAllocationIpsForNode(int $node): Collection;
/**
* Return all of the allocations that exist for a node that are not currently
* allocated.
*
* @param int $node
* @return array
*/
public function getUnassignedAllocationIds(int $node): array;
/**
* Get an array of all allocations that are currently assigned to a given server.
*
* @param int $server
* @return array
*/
public function getAssignedAllocationIds(int $server): array;
} }

View File

@ -1,11 +1,4 @@
<?php <?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\Exceptions; namespace Pterodactyl\Exceptions;
@ -66,7 +59,7 @@ class DisplayException extends PterodactylException
if ($request->expectsJson()) { if ($request->expectsJson()) {
return response()->json(Handler::convertToArray($this, [ return response()->json(Handler::convertToArray($this, [
'detail' => $this->getMessage(), 'detail' => $this->getMessage(),
]), method_exists($this, 'getStatusCode') ? $this->getStatusCode() : Response::HTTP_INTERNAL_SERVER_ERROR); ]), method_exists($this, 'getStatusCode') ? $this->getStatusCode() : Response::HTTP_BAD_REQUEST);
} }
app()->make(AlertsMessageBag::class)->danger($this->getMessage())->flash(); app()->make(AlertsMessageBag::class)->danger($this->getMessage())->flash();

View File

@ -36,6 +36,8 @@ class Handler extends ExceptionHandler
* @var array * @var array
*/ */
protected $dontFlash = [ protected $dontFlash = [
'token',
'secret',
'password', 'password',
'password_confirmation', 'password_confirmation',
]; ];

View File

@ -3,31 +3,42 @@
namespace Pterodactyl\Http\Controllers\Api\Application\Servers; namespace Pterodactyl\Http\Controllers\Api\Application\Servers;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Pterodactyl\Services\Servers\BuildModificationService;
use Pterodactyl\Services\Servers\DetailsModificationService; use Pterodactyl\Services\Servers\DetailsModificationService;
use Pterodactyl\Transformers\Api\Application\ServerTransformer; use Pterodactyl\Transformers\Api\Application\ServerTransformer;
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController; use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
use Pterodactyl\Http\Requests\Api\Application\Servers\UpdateServerDetailsRequest; use Pterodactyl\Http\Requests\Api\Application\Servers\UpdateServerDetailsRequest;
use Pterodactyl\Http\Requests\Api\Application\Servers\UpdateServerBuildConfigurationRequest;
class ServerDetailsController extends ApplicationApiController class ServerDetailsController extends ApplicationApiController
{ {
/**
* @var \Pterodactyl\Services\Servers\BuildModificationService
*/
private $buildModificationService;
/** /**
* @var \Pterodactyl\Services\Servers\DetailsModificationService * @var \Pterodactyl\Services\Servers\DetailsModificationService
*/ */
private $modificationService; private $detailsModificationService;
/** /**
* ServerDetailsController constructor. * ServerDetailsController constructor.
* *
* @param \Pterodactyl\Services\Servers\DetailsModificationService $modificationService * @param \Pterodactyl\Services\Servers\BuildModificationService $buildModificationService
* @param \Pterodactyl\Services\Servers\DetailsModificationService $detailsModificationService
*/ */
public function __construct(DetailsModificationService $modificationService) public function __construct(BuildModificationService $buildModificationService, DetailsModificationService $detailsModificationService)
{ {
parent::__construct(); parent::__construct();
$this->modificationService = $modificationService; $this->buildModificationService = $buildModificationService;
$this->detailsModificationService = $detailsModificationService;
} }
/** /**
* Update the details for a specific server.
*
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\UpdateServerDetailsRequest $request * @param \Pterodactyl\Http\Requests\Api\Application\Servers\UpdateServerDetailsRequest $request
* @param \Pterodactyl\Models\Server $server * @param \Pterodactyl\Models\Server $server
* @return array * @return array
@ -38,7 +49,27 @@ class ServerDetailsController extends ApplicationApiController
*/ */
public function details(UpdateServerDetailsRequest $request, Server $server): array public function details(UpdateServerDetailsRequest $request, Server $server): array
{ {
$server = $this->modificationService->returnUpdatedModel()->handle($server, $request->validated()); $server = $this->detailsModificationService->returnUpdatedModel()->handle($server, $request->validated());
return $this->fractal->item($server)
->transformWith($this->getTransformer(ServerTransformer::class))
->toArray();
}
/**
* 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, Server $server): array
{
$server = $this->buildModificationService->handle($server, $request->validated());
return $this->fractal->item($server) return $this->fractal->item($server)
->transformWith($this->getTransformer(ServerTransformer::class)) ->transformWith($this->getTransformer(ServerTransformer::class))

View File

@ -0,0 +1,61 @@
<?php
namespace Pterodactyl\Http\Requests\Api\Application\Servers;
use Pterodactyl\Models\Server;
class UpdateServerBuildConfigurationRequest extends ServerWriteRequest
{
/**
* Return the rules to validate this request aganist.
*
* @return array
*/
public function rules(): array
{
$rules = Server::getUpdateRulesForId($this->route()->parameter('server')->id);
return [
'allocation' => $rules['allocation_id'],
'memory' => $rules['memory'],
'swap' => $rules['swap'],
'io' => $rules['io'],
'cpu' => $rules['cpu'],
'disk' => $rules['disk'],
'add_allocations' => 'bail|array',
'add_allocations.*' => 'integer',
'remove_allocations' => 'bail|array',
'remove_allocations.*' => 'integer',
];
}
/**
* Convert the allocation field into the expected format for the service handler.
*
* @return array
*/
public function validated()
{
$data = parent::validated();
$data['allocation_id'] = $data['allocation'];
unset($data['allocation']);
return $data;
}
/**
* Custom attributes to use in error message responses.
*
* @return array
*/
public function attributes()
{
return [
'add_allocations' => 'allocations to add',
'remove_allocations' => 'allocations to remove',
'add_allocations.*' => 'allocation to add',
'remove_allocations.*' => 'allocation to remove',
];
}
}

View File

@ -2,7 +2,6 @@
namespace Pterodactyl\Repositories\Eloquent; namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\Node;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Pterodactyl\Models\Allocation; use Pterodactyl\Models\Allocation;
use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Contracts\Pagination\LengthAwarePaginator;
@ -68,4 +67,31 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos
->orderByRaw('INET_ATON(ip) ASC') ->orderByRaw('INET_ATON(ip) ASC')
->get($this->getColumns()); ->get($this->getColumns());
} }
/**
* Return all of the allocations that exist for a node that are not currently
* allocated.
*
* @param int $node
* @return array
*/
public function getUnassignedAllocationIds(int $node): array
{
$results = $this->getBuilder()->select('id')->whereNull('server_id')->where('node_id', $node)->get();
return $results->pluck('id')->toArray();
}
/**
* Get an array of all allocations that are currently assigned to a given server.
*
* @param int $server
* @return array
*/
public function getAssignedAllocationIds(int $server): array
{
$results = $this->getBuilder()->select('id')->where('server_id', $server)->get();
return $results->pluck('id')->toArray();
}
} }

View File

@ -2,7 +2,6 @@
namespace Pterodactyl\Services\Servers; namespace Pterodactyl\Services\Servers;
use Illuminate\Log\Writer;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
@ -10,6 +9,7 @@ use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException; use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
class BuildModificationService class BuildModificationService
@ -17,105 +17,58 @@ class BuildModificationService
/** /**
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface * @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface
*/ */
protected $allocationRepository; private $allocationRepository;
/**
* @var array
*/
protected $build = [];
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
*/
protected $daemonServerRepository;
/** /**
* @var \Illuminate\Database\ConnectionInterface * @var \Illuminate\Database\ConnectionInterface
*/ */
protected $database; private $connection;
/** /**
* @var null|int * @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
*/ */
protected $firstAllocationId = null; private $daemonServerRepository;
/** /**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
*/ */
protected $repository; private $repository;
/**
* @var \Illuminate\Log\Writer
*/
protected $writer;
/** /**
* BuildModificationService constructor. * BuildModificationService constructor.
* *
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository * @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository
* @param \Illuminate\Database\ConnectionInterface $database * @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository * @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
* @param \Illuminate\Log\Writer $writer
*/ */
public function __construct( public function __construct(
AllocationRepositoryInterface $allocationRepository, AllocationRepositoryInterface $allocationRepository,
ConnectionInterface $database, ConnectionInterface $connection,
DaemonServerRepositoryInterface $daemonServerRepository, DaemonServerRepositoryInterface $daemonServerRepository,
ServerRepositoryInterface $repository, ServerRepositoryInterface $repository
Writer $writer
) { ) {
$this->allocationRepository = $allocationRepository; $this->allocationRepository = $allocationRepository;
$this->daemonServerRepository = $daemonServerRepository; $this->daemonServerRepository = $daemonServerRepository;
$this->database = $database; $this->connection = $connection;
$this->repository = $repository; $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 or an item out of the build array.
*
* @param string|null $attribute
* @return array|mixed|null
*/
public function getBuild($attribute = null)
{
if (is_null($attribute)) {
return $this->build;
}
return array_get($this->build, $attribute);
} }
/** /**
* Change the build details for a specified server. * Change the build details for a specified server.
* *
* @param int|\Pterodactyl\Models\Server $server * @param \Pterodactyl\Models\Server $server
* @param array $data * @param array $data
* @return \Pterodactyl\Models\Server
* *
* @throws \Pterodactyl\Exceptions\DisplayException * @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/ */
public function handle($server, array $data) public function handle(Server $server, array $data)
{ {
if (! $server instanceof Server) { $build = [];
$server = $this->repository->find($server); $this->connection->beginTransaction();
}
$data['allocation_id'] = array_get($data, 'allocation_id', $server->allocation_id);
$this->database->beginTransaction();
$this->processAllocations($server, $data); $this->processAllocations($server, $data);
if (isset($data['allocation_id']) && $data['allocation_id'] != $server->allocation_id) { if (isset($data['allocation_id']) && $data['allocation_id'] != $server->allocation_id) {
@ -128,108 +81,98 @@ class BuildModificationService
throw new DisplayException(trans('admin/server.exceptions.default_allocation_not_found')); throw new DisplayException(trans('admin/server.exceptions.default_allocation_not_found'));
} }
$this->setBuild('default', ['ip' => $allocation->ip, 'port' => $allocation->port]); $build['default'] = ['ip' => $allocation->ip, 'port' => $allocation->port];
} }
$server = $this->repository->update($server->id, [ $server = $this->repository->withFreshModel()->update($server->id, [
'memory' => (int) array_get($data, 'memory', $server->memory), 'memory' => array_get($data, 'memory'),
'swap' => (int) array_get($data, 'swap', $server->swap), 'swap' => array_get($data, 'swap'),
'io' => (int) array_get($data, 'io', $server->io), 'io' => array_get($data, 'io'),
'cpu' => (int) array_get($data, 'cpu', $server->cpu), 'cpu' => array_get($data, 'cpu'),
'disk' => (int) array_get($data, 'disk', $server->disk), 'disk' => array_get($data, 'disk'),
'allocation_id' => array_get($data, 'allocation_id', $server->allocation_id), 'allocation_id' => array_get($data, 'allocation_id'),
]); ]);
$allocations = $this->allocationRepository->findWhere([ $allocations = $this->allocationRepository->findWhere([['server_id', '=', $server->id]]);
['server_id', '=', $server->id],
]);
$this->setBuild('memory', (int) $server->memory); $build['memory'] = (int) $server->memory;
$this->setBuild('swap', (int) $server->swap); $build['swap'] = (int) $server->swap;
$this->setBuild('io', (int) $server->io); $build['io'] = (int) $server->io;
$this->setBuild('cpu', (int) $server->cpu); $build['cpu'] = (int) $server->cpu;
$this->setBuild('disk', (int) $server->disk); $build['disk'] = (int) $server->disk;
$this->setBuild('ports|overwrite', $allocations->groupBy('ip')->map(function ($item) { $build['ports|overwrite'] = $allocations->groupBy('ip')->map(function ($item) {
return $item->pluck('port'); return $item->pluck('port');
})->toArray()); })->toArray();
try { try {
$this->daemonServerRepository->setServer($server)->update([ $this->daemonServerRepository->setServer($server)->update(['build' => $build]);
'build' => $this->getBuild(), $this->connection->commit();
]);
$this->database->commit();
} catch (RequestException $exception) { } catch (RequestException $exception) {
$response = $exception->getResponse(); throw new DaemonConnectionException($exception);
$this->writer->warning($exception);
throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [
'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(),
]));
} }
return $server;
} }
/** /**
* Process the allocations being assigned in the data and ensure they are available for a server. * Process the allocations being assigned in the data and ensure they
* are available for a server.
* *
* @param \Pterodactyl\Models\Server $server * @param \Pterodactyl\Models\Server $server
* @param array $data * @param array $data
* *
* @throws \Pterodactyl\Exceptions\DisplayException * @throws \Pterodactyl\Exceptions\DisplayException
*/ */
public function processAllocations(Server $server, array &$data) private function processAllocations(Server $server, array &$data)
{ {
$firstAllocationId = null;
if (! array_key_exists('add_allocations', $data) && ! array_key_exists('remove_allocations', $data)) { if (! array_key_exists('add_allocations', $data) && ! array_key_exists('remove_allocations', $data)) {
return; return;
} }
// Loop through allocations to add. // Handle the addition of allocations to this server.
if (array_key_exists('add_allocations', $data) && ! empty($data['add_allocations'])) { if (array_key_exists('add_allocations', $data) && ! empty($data['add_allocations'])) {
$unassigned = $this->allocationRepository->findWhere([ $unassigned = $this->allocationRepository->getUnassignedAllocationIds($server->node_id);
['server_id', '=', null],
['node_id', '=', $server->node_id],
])->pluck('id')->toArray();
$updateIds = [];
foreach ($data['add_allocations'] as $allocation) { foreach ($data['add_allocations'] as $allocation) {
if (! in_array($allocation, $unassigned)) { if (! in_array($allocation, $unassigned)) {
continue; continue;
} }
$this->firstAllocationId = $this->firstAllocationId ?? $allocation; $firstAllocationId = $firstAllocationId ?? $allocation;
$toUpdate[] = [$allocation]; $updateIds[] = $allocation;
} }
if (isset($toUpdate)) { if (! empty($updateIds)) {
$this->allocationRepository->updateWhereIn('id', $toUpdate, ['server_id' => $server->id]); $this->allocationRepository->updateWhereIn('id', $updateIds, ['server_id' => $server->id]);
unset($toUpdate);
} }
} }
// Loop through allocations to remove. // Handle removal of allocations from this server.
if (array_key_exists('remove_allocations', $data) && ! empty($data['remove_allocations'])) { if (array_key_exists('remove_allocations', $data) && ! empty($data['remove_allocations'])) {
$assigned = $this->allocationRepository->findWhere([ $assigned = $this->allocationRepository->getAssignedAllocationIds($server->id);
['server_id', '=', $server->id],
])->pluck('id')->toArray();
$updateIds = [];
foreach ($data['remove_allocations'] as $allocation) { foreach ($data['remove_allocations'] as $allocation) {
if (! in_array($allocation, $assigned)) { if (! in_array($allocation, $assigned)) {
continue; continue;
} }
if ($allocation == $data['allocation_id']) { if ($allocation == $data['allocation_id']) {
if (is_null($this->firstAllocationId)) { if (is_null($firstAllocationId)) {
throw new DisplayException(trans('admin/server.exceptions.no_new_default_allocation')); throw new DisplayException(trans('admin/server.exceptions.no_new_default_allocation'));
} }
$data['allocation_id'] = $this->firstAllocationId; $data['allocation_id'] = $firstAllocationId;
} }
$toUpdate[] = [$allocation]; $updateIds[] = $allocation;
} }
if (isset($toUpdate)) { if (! empty($updateIds)) {
$this->allocationRepository->updateWhereIn('id', $toUpdate, ['server_id' => null]); $this->allocationRepository->updateWhereIn('id', $updateIds, ['server_id' => null]);
unset($toUpdate);
} }
} }
} }

View File

@ -73,6 +73,7 @@ Route::group(['prefix' => '/servers'], function () {
Route::get('/{server}', 'Servers\ServerController@view')->name('api.application.servers.view'); Route::get('/{server}', 'Servers\ServerController@view')->name('api.application.servers.view');
Route::patch('/{server}/details', 'Servers\ServerDetailsController@details')->name('api.application.servers.details'); Route::patch('/{server}/details', 'Servers\ServerDetailsController@details')->name('api.application.servers.details');
Route::patch('/{server}/build', 'Servers\ServerDetailsController@build')->name('api.application.servers.build');
Route::post('/{server}/suspend', 'Servers\ServerManagementController@suspend')->name('api.application.servers.suspend'); 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}/unsuspend', 'Servers\ServerManagementController@unsuspend')->name('api.application.servers.unsuspend');