2017-07-20 01:49:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
|
|
|
|
use Ramsey\Uuid\Uuid;
|
2019-11-16 21:33:01 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2020-10-09 05:34:52 +00:00
|
|
|
use Pterodactyl\Models\Egg;
|
2017-10-27 04:49:54 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2020-10-09 05:34:52 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
2018-01-28 23:14:14 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Pterodactyl\Models\Allocation;
|
2017-10-06 05:16:22 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2018-01-28 23:14:14 +00:00
|
|
|
use Pterodactyl\Models\Objects\DeploymentObject;
|
2019-11-16 21:33:01 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\EggRepository;
|
|
|
|
use Pterodactyl\Repositories\Eloquent\ServerRepository;
|
|
|
|
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
|
2018-01-28 23:14:14 +00:00
|
|
|
use Pterodactyl\Services\Deployment\FindViableNodesService;
|
2019-11-16 21:33:01 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\ServerVariableRepository;
|
2018-01-28 23:14:14 +00:00
|
|
|
use Pterodactyl\Services\Deployment\AllocationSelectionService;
|
2019-12-17 05:02:30 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2017-07-20 01:49:41 +00:00
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
class ServerCreationService
|
2017-07-20 01:49:41 +00:00
|
|
|
{
|
2018-01-28 23:14:14 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Deployment\AllocationSelectionService
|
|
|
|
*/
|
|
|
|
private $allocationSelectionService;
|
|
|
|
|
2017-10-06 04:09:43 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $configurationStructureService;
|
2017-10-06 04:09:43 +00:00
|
|
|
|
2017-07-22 02:17:42 +00:00
|
|
|
/**
|
2017-10-06 05:16:22 +00:00
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
2017-07-22 02:17:42 +00:00
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $connection;
|
2017-07-22 02:17:42 +00:00
|
|
|
|
|
|
|
/**
|
2019-11-16 21:33:01 +00:00
|
|
|
* @var \Pterodactyl\Services\Deployment\FindViableNodesService
|
2017-07-22 02:17:42 +00:00
|
|
|
*/
|
2019-11-16 21:33:01 +00:00
|
|
|
private $findViableNodesService;
|
2017-07-22 02:17:42 +00:00
|
|
|
|
2017-07-20 01:49:41 +00:00
|
|
|
/**
|
2019-11-16 21:33:01 +00:00
|
|
|
* @var \Pterodactyl\Services\Servers\VariableValidatorService
|
2018-01-28 23:14:14 +00:00
|
|
|
*/
|
2019-11-16 21:33:01 +00:00
|
|
|
private $validatorService;
|
2018-01-28 23:14:14 +00:00
|
|
|
|
|
|
|
/**
|
2019-11-16 21:33:01 +00:00
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\EggRepository
|
2017-07-20 01:49:41 +00:00
|
|
|
*/
|
2019-11-16 21:33:01 +00:00
|
|
|
private $eggRepository;
|
2017-07-20 01:49:41 +00:00
|
|
|
|
2017-07-22 02:17:42 +00:00
|
|
|
/**
|
2019-11-16 21:33:01 +00:00
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
|
2017-07-22 02:17:42 +00:00
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $repository;
|
2017-07-22 02:17:42 +00:00
|
|
|
|
|
|
|
/**
|
2019-11-16 21:33:01 +00:00
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\ServerVariableRepository
|
2017-07-22 02:17:42 +00:00
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $serverVariableRepository;
|
2017-07-22 02:17:42 +00:00
|
|
|
|
2017-07-20 01:49:41 +00:00
|
|
|
/**
|
2019-11-16 21:33:01 +00:00
|
|
|
* @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
|
2017-07-20 01:49:41 +00:00
|
|
|
*/
|
2019-11-16 21:33:01 +00:00
|
|
|
private $daemonServerRepository;
|
2017-07-20 01:49:41 +00:00
|
|
|
|
2019-12-17 05:02:30 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\ServerDeletionService
|
|
|
|
*/
|
|
|
|
private $serverDeletionService;
|
|
|
|
|
2017-07-22 02:17:42 +00:00
|
|
|
/**
|
|
|
|
* CreationService constructor.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService
|
2021-01-23 20:33:34 +00:00
|
|
|
* @param \Pterodactyl\Services\Servers\ServerDeletionService $serverDeletionService
|
|
|
|
* @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService
|
2017-07-22 02:17:42 +00:00
|
|
|
*/
|
2017-07-20 01:49:41 +00:00
|
|
|
public function __construct(
|
2018-01-28 23:14:14 +00:00
|
|
|
AllocationSelectionService $allocationSelectionService,
|
2017-10-06 05:16:22 +00:00
|
|
|
ConnectionInterface $connection,
|
2019-11-16 21:33:01 +00:00
|
|
|
DaemonServerRepository $daemonServerRepository,
|
|
|
|
EggRepository $eggRepository,
|
2018-01-28 23:14:14 +00:00
|
|
|
FindViableNodesService $findViableNodesService,
|
2017-10-06 04:09:43 +00:00
|
|
|
ServerConfigurationStructureService $configurationStructureService,
|
2019-12-17 05:02:30 +00:00
|
|
|
ServerDeletionService $serverDeletionService,
|
2019-11-16 21:33:01 +00:00
|
|
|
ServerRepository $repository,
|
|
|
|
ServerVariableRepository $serverVariableRepository,
|
2017-10-06 05:16:22 +00:00
|
|
|
VariableValidatorService $validatorService
|
2017-07-20 01:49:41 +00:00
|
|
|
) {
|
2018-01-28 23:14:14 +00:00
|
|
|
$this->allocationSelectionService = $allocationSelectionService;
|
2017-10-06 04:09:43 +00:00
|
|
|
$this->configurationStructureService = $configurationStructureService;
|
2017-10-06 05:16:22 +00:00
|
|
|
$this->connection = $connection;
|
2018-01-28 23:14:14 +00:00
|
|
|
$this->findViableNodesService = $findViableNodesService;
|
2019-11-16 21:33:01 +00:00
|
|
|
$this->validatorService = $validatorService;
|
|
|
|
$this->eggRepository = $eggRepository;
|
2017-07-23 01:15:01 +00:00
|
|
|
$this->repository = $repository;
|
|
|
|
$this->serverVariableRepository = $serverVariableRepository;
|
2019-11-16 21:33:01 +00:00
|
|
|
$this->daemonServerRepository = $daemonServerRepository;
|
2019-12-17 05:02:30 +00:00
|
|
|
$this->serverDeletionService = $serverDeletionService;
|
2017-07-20 01:49:41 +00:00
|
|
|
}
|
|
|
|
|
2017-07-22 02:17:42 +00:00
|
|
|
/**
|
2018-01-28 23:14:14 +00:00
|
|
|
* Create a server on the Panel and trigger a request to the Daemon to begin the server
|
2018-02-10 20:01:49 +00:00
|
|
|
* creation process. This function will attempt to set as many additional values
|
|
|
|
* as possible given the input data. For example, if an allocation_id is passed with
|
|
|
|
* no node_id the node_is will be picked from the allocation.
|
2017-07-22 02:17:42 +00:00
|
|
|
*
|
2019-11-16 21:33:01 +00:00
|
|
|
* @throws \Throwable
|
2017-07-24 00:57:43 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
2018-01-28 23:14:14 +00:00
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
2017-09-14 04:07:02 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2018-01-28 23:14:14 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException
|
2017-07-22 02:17:42 +00:00
|
|
|
*/
|
2018-01-28 23:14:14 +00:00
|
|
|
public function handle(array $data, DeploymentObject $deployment = null): Server
|
2017-07-20 01:49:41 +00:00
|
|
|
{
|
2018-01-28 23:14:14 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2018-02-10 20:01:49 +00:00
|
|
|
// Auto-configure the node based on the selected allocation
|
|
|
|
// if no node was defined.
|
2020-10-09 05:34:52 +00:00
|
|
|
if (empty($data['node_id'])) {
|
|
|
|
Assert::false(empty($data['allocation_id']), 'Expected a non-empty allocation_id in server creation data.');
|
|
|
|
|
|
|
|
$data['node_id'] = Allocation::query()->findOrFail($data['allocation_id'])->node_id;
|
2018-02-10 20:01:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 05:34:52 +00:00
|
|
|
if (empty($data['nest_id'])) {
|
|
|
|
Assert::false(empty($data['egg_id']), 'Expected a non-empty egg_id in server creation data.');
|
|
|
|
|
|
|
|
$data['nest_id'] = Egg::query()->findOrFail($data['egg_id'])->nest_id;
|
2018-01-28 23:14:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$eggVariableData = $this->validatorService
|
|
|
|
->setUserLevel(User::USER_LEVEL_ADMIN)
|
2019-11-16 21:33:01 +00:00
|
|
|
->handle(Arr::get($data, 'egg_id'), Arr::get($data, 'environment', []));
|
2018-01-28 23:14:14 +00:00
|
|
|
|
2019-12-17 05:02:30 +00:00
|
|
|
// Due to the design of the Daemon, we need to persist this server to the disk
|
|
|
|
// before we can actually create it on the Daemon.
|
|
|
|
//
|
|
|
|
// If that connection fails out we will attempt to perform a cleanup by just
|
|
|
|
// deleting the server itself from the system.
|
2020-10-10 04:08:27 +00:00
|
|
|
/** @var \Pterodactyl\Models\Server $server */
|
|
|
|
$server = $this->connection->transaction(function () use ($data, $eggVariableData) {
|
|
|
|
// Create the server and assign any additional allocations to it.
|
|
|
|
$server = $this->createModel($data);
|
|
|
|
|
|
|
|
$this->storeAssignedAllocations($server, $data);
|
|
|
|
$this->storeEggVariables($server, $eggVariableData);
|
2019-12-17 05:02:30 +00:00
|
|
|
|
2020-10-10 04:08:27 +00:00
|
|
|
return $server;
|
2020-11-08 21:19:52 +00:00
|
|
|
}, 5);
|
2018-01-28 23:14:14 +00:00
|
|
|
|
2019-12-17 05:02:30 +00:00
|
|
|
try {
|
2020-10-10 04:08:27 +00:00
|
|
|
$this->daemonServerRepository->setServer($server)->create(
|
|
|
|
$this->configurationStructureService->handle($server)
|
|
|
|
);
|
2019-12-17 05:02:30 +00:00
|
|
|
} catch (DaemonConnectionException $exception) {
|
|
|
|
$this->serverDeletionService->withForce(true)->handle($server);
|
|
|
|
|
|
|
|
throw $exception;
|
|
|
|
}
|
2018-01-28 23:14:14 +00:00
|
|
|
|
|
|
|
return $server;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an allocation to use for automatic deployment.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException
|
|
|
|
*/
|
|
|
|
private function configureDeployment(array $data, DeploymentObject $deployment): Allocation
|
|
|
|
{
|
2021-01-10 21:08:43 +00:00
|
|
|
/** @var \Illuminate\Support\Collection $nodes */
|
2018-01-28 23:14:14 +00:00
|
|
|
$nodes = $this->findViableNodesService->setLocations($deployment->getLocations())
|
2019-11-16 21:33:01 +00:00
|
|
|
->setDisk(Arr::get($data, 'disk'))
|
|
|
|
->setMemory(Arr::get($data, 'memory'))
|
2018-01-28 23:14:14 +00:00
|
|
|
->handle();
|
|
|
|
|
|
|
|
return $this->allocationSelectionService->setDedicated($deployment->isDedicated())
|
2020-10-10 05:01:25 +00:00
|
|
|
->setNodes($nodes->pluck('id')->toArray())
|
2018-01-28 23:14:14 +00:00
|
|
|
->setPorts($deployment->getPorts())
|
|
|
|
->handle();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the server in the database and return the model.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
*/
|
|
|
|
private function createModel(array $data): Server
|
|
|
|
{
|
2018-03-04 04:20:53 +00:00
|
|
|
$uuid = $this->generateUniqueUuidCombo();
|
|
|
|
|
2019-11-16 21:33:01 +00:00
|
|
|
/** @var \Pterodactyl\Models\Server $model */
|
|
|
|
$model = $this->repository->create([
|
|
|
|
'external_id' => Arr::get($data, 'external_id'),
|
2018-03-04 04:20:53 +00:00
|
|
|
'uuid' => $uuid,
|
|
|
|
'uuidShort' => substr($uuid, 0, 8),
|
2019-11-16 21:33:01 +00:00
|
|
|
'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']),
|
2017-07-20 01:49:41 +00:00
|
|
|
'suspended' => false,
|
2019-11-16 21:33:01 +00:00
|
|
|
'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'),
|
2020-03-29 18:41:55 +00:00
|
|
|
'threads' => Arr::get($data, 'threads'),
|
2020-08-24 16:50:25 +00:00
|
|
|
'oom_disabled' => Arr::get($data, 'oom_disabled') ?? true,
|
2019-11-16 21:33:01 +00:00
|
|
|
'allocation_id' => Arr::get($data, 'allocation_id'),
|
|
|
|
'nest_id' => Arr::get($data, 'nest_id'),
|
|
|
|
'egg_id' => Arr::get($data, 'egg_id'),
|
|
|
|
'startup' => Arr::get($data, 'startup'),
|
|
|
|
'image' => Arr::get($data, 'image'),
|
2020-08-24 16:50:25 +00:00
|
|
|
'database_limit' => Arr::get($data, 'database_limit') ?? 0,
|
|
|
|
'allocation_limit' => Arr::get($data, 'allocation_limit') ?? 0,
|
|
|
|
'backup_limit' => Arr::get($data, 'backup_limit') ?? 0,
|
2017-07-20 01:49:41 +00:00
|
|
|
]);
|
2019-11-16 21:33:01 +00:00
|
|
|
|
|
|
|
return $model;
|
2018-01-28 23:14:14 +00:00
|
|
|
}
|
2017-07-20 01:49:41 +00:00
|
|
|
|
2018-01-28 23:14:14 +00:00
|
|
|
/**
|
|
|
|
* Configure the allocations assigned to this server.
|
|
|
|
*/
|
|
|
|
private function storeAssignedAllocations(Server $server, array $data)
|
|
|
|
{
|
2017-07-20 01:49:41 +00:00
|
|
|
$records = [$data['allocation_id']];
|
|
|
|
if (isset($data['allocation_additional']) && is_array($data['allocation_additional'])) {
|
|
|
|
$records = array_merge($records, $data['allocation_additional']);
|
|
|
|
}
|
|
|
|
|
2020-10-09 05:34:52 +00:00
|
|
|
Allocation::query()->whereIn('id', $records)->update([
|
2020-07-07 03:05:42 +00:00
|
|
|
'server_id' => $server->id,
|
|
|
|
]);
|
2018-01-28 23:14:14 +00:00
|
|
|
}
|
2017-07-20 01:49:41 +00:00
|
|
|
|
2018-01-28 23:14:14 +00:00
|
|
|
/**
|
|
|
|
* Process environment variables passed for this server and store them in the database.
|
|
|
|
*/
|
|
|
|
private function storeEggVariables(Server $server, Collection $variables)
|
|
|
|
{
|
|
|
|
$records = $variables->map(function ($result) use ($server) {
|
2017-10-27 04:49:54 +00:00
|
|
|
return [
|
2017-07-20 01:49:41 +00:00
|
|
|
'server_id' => $server->id,
|
2017-10-27 04:49:54 +00:00
|
|
|
'variable_id' => $result->id,
|
|
|
|
'variable_value' => $result->value,
|
2017-07-20 01:49:41 +00:00
|
|
|
];
|
2017-10-27 04:49:54 +00:00
|
|
|
})->toArray();
|
2017-07-20 01:49:41 +00:00
|
|
|
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!empty($records)) {
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->serverVariableRepository->insert($records);
|
|
|
|
}
|
2017-07-20 01:49:41 +00:00
|
|
|
}
|
2018-02-10 20:01:49 +00:00
|
|
|
|
2018-03-04 04:20:53 +00:00
|
|
|
/**
|
|
|
|
* Create a unique UUID and UUID-Short combo for a server.
|
|
|
|
*/
|
|
|
|
private function generateUniqueUuidCombo(): string
|
|
|
|
{
|
|
|
|
$uuid = Uuid::uuid4()->toString();
|
|
|
|
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->repository->isUniqueUuidCombo($uuid, substr($uuid, 0, 8))) {
|
2018-03-04 04:20:53 +00:00
|
|
|
return $this->generateUniqueUuidCombo();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $uuid;
|
|
|
|
}
|
2017-07-20 01:49:41 +00:00
|
|
|
}
|