Update server creation data logic

This commit is contained in:
Dane Everitt 2019-11-16 13:33:01 -08:00
parent 2848d182ef
commit 8f0044575f
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
6 changed files with 187 additions and 86 deletions

View file

@ -72,9 +72,9 @@ class ServerController extends ApplicationApiController
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\StoreServerRequest $request * @param \Pterodactyl\Http\Requests\Api\Application\Servers\StoreServerRequest $request
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
* *
* @throws \Throwable
* @throws \Illuminate\Validation\ValidationException * @throws \Illuminate\Validation\ValidationException
* @throws \Pterodactyl\Exceptions\DisplayException * @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException * @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException

View file

@ -2,6 +2,22 @@
namespace Pterodactyl\Models; namespace Pterodactyl\Models;
/**
* @property int $id
* @property int $node_id
* @property string $ip
* @property string|null $ip_alias
* @property int $port
* @property int|null $server_id
* @property \Carbon\Carbon|null $created_at
* @property \Carbon\Carbon|null $updated_at
*
* @property string $alias
* @property bool $has_alias
*
* @property \Pterodactyl\Models\Server|null $server
* @property \Pterodactyl\Models\Node $node
*/
class Allocation extends Validable class Allocation extends Validable
{ {
/** /**

View file

@ -29,4 +29,26 @@ class DaemonServerRepository extends DaemonRepository
return json_decode($response->getBody()->__toString(), true); return json_decode($response->getBody()->__toString(), true);
} }
/**
* Creates a new server on the Wings daemon.
*
* @param array $data
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function create(array $data): void
{
Assert::isInstanceOf($this->server, Server::class);
try {
$this->getHttpClient()->post(
'/api/servers', [
'json' => $data,
]
);
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
}
}
} }

View file

@ -107,7 +107,7 @@ class EggConfigurationService
{ {
// Get the legacy configuration structure for the server so that we // Get the legacy configuration structure for the server so that we
// can property map the egg placeholders to values. // can property map the egg placeholders to values.
$structure = $this->configurationStructureService->handle($server); $structure = $this->configurationStructureService->handle($server, true);
foreach ($configs as $file => $data) { foreach ($configs as $file => $data) {
foreach ($data->find ?? [] as &$value) { foreach ($data->find ?? [] as &$value) {

View file

@ -47,14 +47,76 @@ class ServerConfigurationStructureService
* daemon, if you modify the structure eggs will break unexpectedly. * daemon, if you modify the structure eggs will break unexpectedly.
* *
* @param \Pterodactyl\Models\Server $server * @param \Pterodactyl\Models\Server $server
* @param bool $legacy
* @return array * @return array
* *
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/ */
public function handle(Server $server): array public function handle(Server $server, bool $legacy = false): array
{ {
$server->loadMissing(self::REQUIRED_RELATIONS); $server->loadMissing(self::REQUIRED_RELATIONS);
return $legacy ?
$this->returnLegacyFormat($server)
: $this->returnCurrentFormat($server);
}
/**
* Returns the new data format used for the Wings daemon.
*
* @param \Pterodactyl\Models\Server $server
* @return array
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
protected function returnCurrentFormat(Server $server)
{
return [
'uuid' => $server->uuid,
'suspended' => $server->suspended,
'environment' => $this->environment->handle($server),
'build' => [
'oom_disabled' => $server->oom_disabled,
'memory' => $server->memory,
'swap' => $server->swap,
'io' => $server->io,
'cpu' => $server->cpu,
'disk' => $server->disk,
],
'service' => [
'egg' => $server->egg->uuid,
'pack' => $server->pack ? $server->pack->uuid : null,
'skip_scripts' => $server->skip_scripts,
],
'container' => [
'image' => $server->image,
'requires_rebuild' => false,
],
'allocations' => [
'default' => [
'ip' => $server->allocation->ip,
'port' => $server->allocation->port,
],
'mappings' => [
$server->allocations->groupBy('ip')->map(function ($item) {
return $item->pluck('port');
})->toArray(),
],
],
];
}
/**
* Returns the legacy server data format to continue support for old egg configurations
* that have not yet been updated.
*
* @param \Pterodactyl\Models\Server $server
* @return array
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
protected function returnLegacyFormat(Server $server)
{
return [ return [
'uuid' => $server->uuid, 'uuid' => $server->uuid,
'build' => [ 'build' => [

View file

@ -3,27 +3,27 @@
namespace Pterodactyl\Services\Servers; namespace Pterodactyl\Services\Servers;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Pterodactyl\Models\Node; use Pterodactyl\Models\Node;
use Pterodactyl\Models\User; use Pterodactyl\Models\User;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Pterodactyl\Models\Allocation; use Pterodactyl\Models\Allocation;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Models\Objects\DeploymentObject; use Pterodactyl\Models\Objects\DeploymentObject;
use Pterodactyl\Repositories\Eloquent\EggRepository;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
use Pterodactyl\Repositories\Eloquent\AllocationRepository;
use Pterodactyl\Services\Deployment\FindViableNodesService; use Pterodactyl\Services\Deployment\FindViableNodesService;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Pterodactyl\Repositories\Eloquent\ServerVariableRepository;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Services\Deployment\AllocationSelectionService; use Pterodactyl\Services\Deployment\AllocationSelectionService;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
class ServerCreationService class ServerCreationService
{ {
/** /**
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface * @var \Pterodactyl\Repositories\Eloquent\AllocationRepository
*/ */
private $allocationRepository; private $allocationRepository;
@ -42,72 +42,72 @@ class ServerCreationService
*/ */
private $connection; private $connection;
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
*/
private $daemonServerRepository;
/**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
*/
private $eggRepository;
/** /**
* @var \Pterodactyl\Services\Deployment\FindViableNodesService * @var \Pterodactyl\Services\Deployment\FindViableNodesService
*/ */
private $findViableNodesService; private $findViableNodesService;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
*/
private $repository;
/**
* @var \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface
*/
private $serverVariableRepository;
/** /**
* @var \Pterodactyl\Services\Servers\VariableValidatorService * @var \Pterodactyl\Services\Servers\VariableValidatorService
*/ */
private $validatorService; private $validatorService;
/**
* @var \Pterodactyl\Repositories\Eloquent\EggRepository
*/
private $eggRepository;
/**
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
*/
private $repository;
/**
* @var \Pterodactyl\Repositories\Eloquent\ServerVariableRepository
*/
private $serverVariableRepository;
/**
* @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
*/
private $daemonServerRepository;
/** /**
* CreationService constructor. * CreationService constructor.
* *
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository * @param \Pterodactyl\Repositories\Eloquent\AllocationRepository $allocationRepository
* @param \Pterodactyl\Services\Deployment\AllocationSelectionService $allocationSelectionService * @param \Pterodactyl\Services\Deployment\AllocationSelectionService $allocationSelectionService
* @param \Illuminate\Database\ConnectionInterface $connection * @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository * @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $daemonServerRepository
* @param \Pterodactyl\Contracts\Repository\EggRepositoryInterface $eggRepository * @param \Pterodactyl\Repositories\Eloquent\EggRepository $eggRepository
* @param \Pterodactyl\Services\Deployment\FindViableNodesService $findViableNodesService * @param \Pterodactyl\Services\Deployment\FindViableNodesService $findViableNodesService
* @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService * @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository * @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
* @param \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface $serverVariableRepository * @param \Pterodactyl\Repositories\Eloquent\ServerVariableRepository $serverVariableRepository
* @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService * @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService
*/ */
public function __construct( public function __construct(
AllocationRepositoryInterface $allocationRepository, AllocationRepository $allocationRepository,
AllocationSelectionService $allocationSelectionService, AllocationSelectionService $allocationSelectionService,
ConnectionInterface $connection, ConnectionInterface $connection,
DaemonServerRepositoryInterface $daemonServerRepository, DaemonServerRepository $daemonServerRepository,
EggRepositoryInterface $eggRepository, EggRepository $eggRepository,
FindViableNodesService $findViableNodesService, FindViableNodesService $findViableNodesService,
ServerConfigurationStructureService $configurationStructureService, ServerConfigurationStructureService $configurationStructureService,
ServerRepositoryInterface $repository, ServerRepository $repository,
ServerVariableRepositoryInterface $serverVariableRepository, ServerVariableRepository $serverVariableRepository,
VariableValidatorService $validatorService VariableValidatorService $validatorService
) { ) {
$this->allocationSelectionService = $allocationSelectionService; $this->allocationSelectionService = $allocationSelectionService;
$this->allocationRepository = $allocationRepository; $this->allocationRepository = $allocationRepository;
$this->configurationStructureService = $configurationStructureService; $this->configurationStructureService = $configurationStructureService;
$this->connection = $connection; $this->connection = $connection;
$this->daemonServerRepository = $daemonServerRepository;
$this->eggRepository = $eggRepository;
$this->findViableNodesService = $findViableNodesService; $this->findViableNodesService = $findViableNodesService;
$this->validatorService = $validatorService;
$this->eggRepository = $eggRepository;
$this->repository = $repository; $this->repository = $repository;
$this->serverVariableRepository = $serverVariableRepository; $this->serverVariableRepository = $serverVariableRepository;
$this->validatorService = $validatorService; $this->daemonServerRepository = $daemonServerRepository;
} }
/** /**
@ -120,12 +120,12 @@ class ServerCreationService
* @param \Pterodactyl\Models\Objects\DeploymentObject|null $deployment * @param \Pterodactyl\Models\Objects\DeploymentObject|null $deployment
* @return \Pterodactyl\Models\Server * @return \Pterodactyl\Models\Server
* *
* @throws \Throwable
* @throws \Pterodactyl\Exceptions\DisplayException * @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Illuminate\Validation\ValidationException * @throws \Illuminate\Validation\ValidationException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException * @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException * @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException
*/ */
public function handle(array $data, DeploymentObject $deployment = null): Server public function handle(array $data, DeploymentObject $deployment = null): Server
@ -142,18 +142,19 @@ class ServerCreationService
// Auto-configure the node based on the selected allocation // Auto-configure the node based on the selected allocation
// if no node was defined. // if no node was defined.
if (is_null(array_get($data, 'node_id'))) { if (is_null(Arr::get($data, 'node_id'))) {
$data['node_id'] = $this->getNodeFromAllocation($data['allocation_id']); $data['node_id'] = $this->getNodeFromAllocation($data['allocation_id']);
} }
if (is_null(array_get($data, 'nest_id'))) { if (is_null(Arr::get($data, 'nest_id'))) {
$egg = $this->eggRepository->setColumns(['id', 'nest_id'])->find(array_get($data, 'egg_id')); /** @var \Pterodactyl\Models\Egg $egg */
$egg = $this->eggRepository->setColumns(['id', 'nest_id'])->find(Arr::get($data, 'egg_id'));
$data['nest_id'] = $egg->nest_id; $data['nest_id'] = $egg->nest_id;
} }
$eggVariableData = $this->validatorService $eggVariableData = $this->validatorService
->setUserLevel(User::USER_LEVEL_ADMIN) ->setUserLevel(User::USER_LEVEL_ADMIN)
->handle(array_get($data, 'egg_id'), array_get($data, 'environment', [])); ->handle(Arr::get($data, 'egg_id'), Arr::get($data, 'environment', []));
// Create the server and assign any additional allocations to it. // Create the server and assign any additional allocations to it.
$server = $this->createModel($data); $server = $this->createModel($data);
@ -162,16 +163,9 @@ class ServerCreationService
$structure = $this->configurationStructureService->handle($server); $structure = $this->configurationStructureService->handle($server);
try { $this->connection->transaction(function () use ($server, $structure) {
$this->daemonServerRepository->setServer($server)->create($structure, [ $this->daemonServerRepository->setServer($server)->create($structure);
'start_on_completion' => (bool) array_get($data, 'start_on_completion', false), });
]);
$this->connection->commit();
} catch (RequestException $exception) {
$this->connection->rollBack();
throw new DaemonConnectionException($exception);
}
return $server; return $server;
} }
@ -190,8 +184,8 @@ class ServerCreationService
private function configureDeployment(array $data, DeploymentObject $deployment): Allocation private function configureDeployment(array $data, DeploymentObject $deployment): Allocation
{ {
$nodes = $this->findViableNodesService->setLocations($deployment->getLocations()) $nodes = $this->findViableNodesService->setLocations($deployment->getLocations())
->setDisk(array_get($data, 'disk')) ->setDisk(Arr::get($data, 'disk'))
->setMemory(array_get($data, 'memory')) ->setMemory(Arr::get($data, 'memory'))
->handle(); ->handle();
return $this->allocationSelectionService->setDedicated($deployment->isDedicated()) return $this->allocationSelectionService->setDedicated($deployment->isDedicated())
@ -212,32 +206,35 @@ class ServerCreationService
{ {
$uuid = $this->generateUniqueUuidCombo(); $uuid = $this->generateUniqueUuidCombo();
return $this->repository->create([ /** @var \Pterodactyl\Models\Server $model */
'external_id' => array_get($data, 'external_id'), $model = $this->repository->create([
'external_id' => Arr::get($data, 'external_id'),
'uuid' => $uuid, 'uuid' => $uuid,
'uuidShort' => substr($uuid, 0, 8), 'uuidShort' => substr($uuid, 0, 8),
'node_id' => array_get($data, 'node_id'), 'node_id' => Arr::get($data, 'node_id'),
'name' => array_get($data, 'name'), 'name' => Arr::get($data, 'name'),
'description' => array_get($data, 'description') ?? '', 'description' => Arr::get($data, 'description') ?? '',
'skip_scripts' => array_get($data, 'skip_scripts') ?? isset($data['skip_scripts']), 'skip_scripts' => Arr::get($data, 'skip_scripts') ?? isset($data['skip_scripts']),
'suspended' => false, 'suspended' => false,
'owner_id' => array_get($data, 'owner_id'), 'owner_id' => Arr::get($data, 'owner_id'),
'memory' => array_get($data, 'memory'), 'memory' => Arr::get($data, 'memory'),
'swap' => array_get($data, 'swap'), 'swap' => Arr::get($data, 'swap'),
'disk' => array_get($data, 'disk'), 'disk' => Arr::get($data, 'disk'),
'io' => array_get($data, 'io'), 'io' => Arr::get($data, 'io'),
'cpu' => array_get($data, 'cpu'), 'cpu' => Arr::get($data, 'cpu'),
'oom_disabled' => array_get($data, 'oom_disabled', true), 'oom_disabled' => Arr::get($data, 'oom_disabled', true),
'allocation_id' => array_get($data, 'allocation_id'), 'allocation_id' => Arr::get($data, 'allocation_id'),
'nest_id' => array_get($data, 'nest_id'), 'nest_id' => Arr::get($data, 'nest_id'),
'egg_id' => array_get($data, 'egg_id'), 'egg_id' => Arr::get($data, 'egg_id'),
'pack_id' => (! isset($data['pack_id']) || $data['pack_id'] == 0) ? null : $data['pack_id'], 'pack_id' => empty($data['pack_id']) ? null : $data['pack_id'],
'startup' => array_get($data, 'startup'), 'startup' => Arr::get($data, 'startup'),
'daemonSecret' => str_random(Node::DAEMON_SECRET_LENGTH), 'daemonSecret' => Str::random(Node::DAEMON_SECRET_LENGTH),
'image' => array_get($data, 'image'), 'image' => Arr::get($data, 'image'),
'database_limit' => array_get($data, 'database_limit'), 'database_limit' => Arr::get($data, 'database_limit'),
'allocation_limit' => array_get($data, 'allocation_limit'), 'allocation_limit' => Arr::get($data, 'allocation_limit'),
]); ]);
return $model;
} }
/** /**
@ -280,18 +277,21 @@ class ServerCreationService
/** /**
* Get the node that an allocation belongs to. * Get the node that an allocation belongs to.
* *
* @param int $allocation * @param int $id
* @return int * @return int
* *
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/ */
private function getNodeFromAllocation(int $allocation): int private function getNodeFromAllocation(int $id): int
{ {
$allocation = $this->allocationRepository->setColumns(['id', 'node_id'])->find($allocation); /** @var \Pterodactyl\Models\Allocation $allocation */
$allocation = $this->allocationRepository->setColumns(['id', 'node_id'])->find($id);
return $allocation->node_id; return $allocation->node_id;
} }
/** @noinspection PhpDocMissingThrowsInspection */
/** /**
* Create a unique UUID and UUID-Short combo for a server. * Create a unique UUID and UUID-Short combo for a server.
* *
@ -299,6 +299,7 @@ class ServerCreationService
*/ */
private function generateUniqueUuidCombo(): string private function generateUniqueUuidCombo(): string
{ {
/** @noinspection PhpUnhandledExceptionInspection */
$uuid = Uuid::uuid4()->toString(); $uuid = Uuid::uuid4()->toString();
if (! $this->repository->isUniqueUuidCombo($uuid, substr($uuid, 0, 8))) { if (! $this->repository->isUniqueUuidCombo($uuid, substr($uuid, 0, 8))) {