Implement server creation though the API.

Also implements auto-deployment to specific locations and ports.
This commit is contained in:
Dane Everitt 2018-01-28 17:14:14 -06:00
parent 97ee95b4da
commit 5ed164e13e
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
24 changed files with 927 additions and 223 deletions

View file

@ -5,11 +5,16 @@ namespace Pterodactyl\Services\Servers;
use Ramsey\Uuid\Uuid;
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\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Models\Objects\DeploymentObject;
use Pterodactyl\Services\Deployment\FindViableNodesService;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Services\Deployment\AllocationSelectionService;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
@ -22,6 +27,11 @@ class ServerCreationService
*/
private $allocationRepository;
/**
* @var \Pterodactyl\Services\Deployment\AllocationSelectionService
*/
private $allocationSelectionService;
/**
* @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService
*/
@ -38,9 +48,14 @@ class ServerCreationService
private $daemonServerRepository;
/**
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
*/
private $nodeRepository;
private $eggRepository;
/**
* @var \Pterodactyl\Services\Deployment\FindViableNodesService
*/
private $findViableNodesService;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
@ -52,11 +67,6 @@ class ServerCreationService
*/
private $serverVariableRepository;
/**
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
*/
private $userRepository;
/**
* @var \Pterodactyl\Services\Servers\VariableValidatorService
*/
@ -66,60 +76,139 @@ class ServerCreationService
* CreationService constructor.
*
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository
* @param \Pterodactyl\Services\Deployment\AllocationSelectionService $allocationSelectionService
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $nodeRepository
* @param \Pterodactyl\Contracts\Repository\EggRepositoryInterface $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\Contracts\Repository\UserRepositoryInterface $userRepository
* @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService
*/
public function __construct(
AllocationRepositoryInterface $allocationRepository,
AllocationSelectionService $allocationSelectionService,
ConnectionInterface $connection,
DaemonServerRepositoryInterface $daemonServerRepository,
NodeRepositoryInterface $nodeRepository,
EggRepositoryInterface $eggRepository,
FindViableNodesService $findViableNodesService,
ServerConfigurationStructureService $configurationStructureService,
ServerRepositoryInterface $repository,
ServerVariableRepositoryInterface $serverVariableRepository,
UserRepositoryInterface $userRepository,
VariableValidatorService $validatorService
) {
$this->allocationSelectionService = $allocationSelectionService;
$this->allocationRepository = $allocationRepository;
$this->configurationStructureService = $configurationStructureService;
$this->connection = $connection;
$this->daemonServerRepository = $daemonServerRepository;
$this->nodeRepository = $nodeRepository;
$this->eggRepository = $eggRepository;
$this->findViableNodesService = $findViableNodesService;
$this->repository = $repository;
$this->serverVariableRepository = $serverVariableRepository;
$this->userRepository = $userRepository;
$this->validatorService = $validatorService;
}
/**
* Create a server on both the panel and daemon.
* Create a server on the Panel and trigger a request to the Daemon to begin the server
* creation process.
*
* @param array $data
* @return mixed
* @param array $data
* @param \Pterodactyl\Models\Objects\DeploymentObject|null $deployment
* @return \Pterodactyl\Models\Server
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Illuminate\Validation\ValidationException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Illuminate\Validation\ValidationException
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException
*/
public function create(array $data)
public function handle(array $data, DeploymentObject $deployment = null): Server
{
// @todo auto-deployment
$this->connection->beginTransaction();
$server = $this->repository->create([
// If a deployment object has been passed we need to get the allocation
// that the server should use, and assign the node from that allocation.
if ($deployment instanceof DeploymentObject) {
$allocation = $this->configureDeployment($data, $deployment);
$data['allocation_id'] = $allocation->id;
$data['node_id'] = $allocation->node_id;
}
if (is_null(array_get($data, 'nest_id'))) {
$egg = $this->eggRepository->setColumns(['id', 'nest_id'])->find(array_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', []));
// Create the server and assign any additional allocations to it.
$server = $this->createModel($data);
$this->storeAssignedAllocations($server, $data);
$this->storeEggVariables($server, $eggVariableData);
$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);
}
return $server;
}
/**
* Gets an allocation to use for automatic deployment.
*
* @param array $data
* @param \Pterodactyl\Models\Objects\DeploymentObject $deployment
*
* @return \Pterodactyl\Models\Allocation
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException
*/
private function configureDeployment(array $data, DeploymentObject $deployment): Allocation
{
$nodes = $this->findViableNodesService->setLocations($deployment->getLocations())
->setDisk(array_get($data, 'disk'))
->setMemory(array_get($data, 'memory'))
->handle();
return $this->allocationSelectionService->setDedicated($deployment->isDedicated())
->setNodes($nodes)
->setPorts($deployment->getPorts())
->handle();
}
/**
* Store the server in the database and return the model.
*
* @param array $data
* @return \Pterodactyl\Models\Server
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
private function createModel(array $data): Server
{
return $this->repository->create([
'uuid' => Uuid::uuid4()->toString(),
'uuidShort' => str_random(8),
'node_id' => array_get($data, 'node_id'),
'name' => array_get($data, 'name'),
'description' => array_get($data, 'description') ?? '',
'skip_scripts' => isset($data['skip_scripts']),
'skip_scripts' => array_get($data, 'skip_scripts') ?? isset($data['skip_scripts']),
'suspended' => false,
'owner_id' => array_get($data, 'owner_id'),
'memory' => array_get($data, 'memory'),
@ -134,22 +223,35 @@ class ServerCreationService
'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, 'docker_image'),
'image' => array_get($data, 'image'),
]);
}
// Process allocations and assign them to the server in the database.
/**
* Configure the allocations assigned to this server.
*
* @param \Pterodactyl\Models\Server $server
* @param array $data
*/
private function storeAssignedAllocations(Server $server, array $data)
{
$records = [$data['allocation_id']];
if (isset($data['allocation_additional']) && is_array($data['allocation_additional'])) {
$records = array_merge($records, $data['allocation_additional']);
}
$this->allocationRepository->assignAllocationsToServer($server->id, $records);
}
// Process the passed variables and store them in the database.
$this->validatorService->setUserLevel(User::USER_LEVEL_ADMIN);
$results = $this->validatorService->handle(array_get($data, 'egg_id'), array_get($data, 'environment', []));
$records = $results->map(function ($result) use ($server) {
/**
* Process environment variables passed for this server and store them in the database.
*
* @param \Pterodactyl\Models\Server $server
* @param \Illuminate\Support\Collection $variables
*/
private function storeEggVariables(Server $server, Collection $variables)
{
$records = $variables->map(function ($result) use ($server) {
return [
'server_id' => $server->id,
'variable_id' => $result->id,
@ -160,20 +262,5 @@ class ServerCreationService
if (! empty($records)) {
$this->serverVariableRepository->insert($records);
}
$structure = $this->configurationStructureService->handle($server);
// Create the server on the daemon & commit it to the database.
$node = $this->nodeRepository->find($server->node_id);
try {
$this->daemonServerRepository->setNode($node)->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;
}
}