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
* @return \Illuminate\Http\JsonResponse
*
* @throws \Throwable
* @throws \Illuminate\Validation\ValidationException
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException

View file

@ -2,6 +2,22 @@
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
{
/**

View file

@ -29,4 +29,26 @@ class DaemonServerRepository extends DaemonRepository
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
// 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 ($data->find ?? [] as &$value) {

View file

@ -47,14 +47,76 @@ class ServerConfigurationStructureService
* daemon, if you modify the structure eggs will break unexpectedly.
*
* @param \Pterodactyl\Models\Server $server
* @param bool $legacy
* @return array
*
* @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);
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 [
'uuid' => $server->uuid,
'build' => [

View file

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