2017-07-20 01:49:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
|
|
|
|
use Ramsey\Uuid\Uuid;
|
2017-10-27 04:49:54 +00:00
|
|
|
use Pterodactyl\Models\Node;
|
|
|
|
use Pterodactyl\Models\User;
|
2017-08-05 22:26:30 +00:00
|
|
|
use GuzzleHttp\Exception\RequestException;
|
2017-10-06 05:16:22 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2017-07-20 01:49:41 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
2017-10-06 05:16:22 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2017-07-20 01:49:41 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
|
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
class ServerCreationService
|
2017-07-20 01:49:41 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $allocationRepository;
|
2017-07-20 01:49:41 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
2017-10-06 05:16:22 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
|
2017-07-22 02:17:42 +00:00
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $daemonServerRepository;
|
2017-07-22 02:17:42 +00:00
|
|
|
|
2017-07-20 01:49:41 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $nodeRepository;
|
2017-07-20 01:49:41 +00:00
|
|
|
|
2017-07-22 02:17:42 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $repository;
|
2017-07-22 02:17:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $userRepository;
|
2017-07-20 01:49:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\VariableValidatorService
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $validatorService;
|
2017-07-20 01:49:41 +00:00
|
|
|
|
2017-07-22 02:17:42 +00:00
|
|
|
/**
|
|
|
|
* CreationService constructor.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository
|
2017-10-06 05:16:22 +00:00
|
|
|
* @param \Illuminate\Database\ConnectionInterface $connection
|
2017-07-22 02:17:42 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $nodeRepository
|
2017-10-06 04:09:43 +00:00
|
|
|
* @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService
|
2017-07-23 01:15:01 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface $serverVariableRepository
|
2017-07-22 02:17:42 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $userRepository
|
|
|
|
* @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService
|
|
|
|
*/
|
2017-07-20 01:49:41 +00:00
|
|
|
public function __construct(
|
|
|
|
AllocationRepositoryInterface $allocationRepository,
|
2017-10-06 05:16:22 +00:00
|
|
|
ConnectionInterface $connection,
|
2017-07-20 01:49:41 +00:00
|
|
|
DaemonServerRepositoryInterface $daemonServerRepository,
|
|
|
|
NodeRepositoryInterface $nodeRepository,
|
2017-10-06 04:09:43 +00:00
|
|
|
ServerConfigurationStructureService $configurationStructureService,
|
2017-07-23 01:15:01 +00:00
|
|
|
ServerRepositoryInterface $repository,
|
|
|
|
ServerVariableRepositoryInterface $serverVariableRepository,
|
2017-07-20 01:49:41 +00:00
|
|
|
UserRepositoryInterface $userRepository,
|
2017-10-06 05:16:22 +00:00
|
|
|
VariableValidatorService $validatorService
|
2017-07-20 01:49:41 +00:00
|
|
|
) {
|
|
|
|
$this->allocationRepository = $allocationRepository;
|
2017-10-06 04:09:43 +00:00
|
|
|
$this->configurationStructureService = $configurationStructureService;
|
2017-10-06 05:16:22 +00:00
|
|
|
$this->connection = $connection;
|
|
|
|
$this->daemonServerRepository = $daemonServerRepository;
|
2017-07-20 01:49:41 +00:00
|
|
|
$this->nodeRepository = $nodeRepository;
|
2017-07-23 01:15:01 +00:00
|
|
|
$this->repository = $repository;
|
|
|
|
$this->serverVariableRepository = $serverVariableRepository;
|
2017-07-20 01:49:41 +00:00
|
|
|
$this->userRepository = $userRepository;
|
|
|
|
$this->validatorService = $validatorService;
|
|
|
|
}
|
|
|
|
|
2017-07-22 02:17:42 +00:00
|
|
|
/**
|
|
|
|
* Create a server on both the panel and daemon.
|
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param array $data
|
2017-07-22 02:17:42 +00:00
|
|
|
* @return mixed
|
|
|
|
*
|
2017-07-24 00:57:43 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
2017-07-22 02:17:42 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
2017-09-14 04:07:02 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2018-01-01 18:23:45 +00:00
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
2017-07-22 02:17:42 +00:00
|
|
|
*/
|
2017-07-20 01:49:41 +00:00
|
|
|
public function create(array $data)
|
|
|
|
{
|
2017-07-22 02:17:42 +00:00
|
|
|
// @todo auto-deployment
|
2017-07-20 01:49:41 +00:00
|
|
|
|
2017-10-06 05:16:22 +00:00
|
|
|
$this->connection->beginTransaction();
|
2017-07-20 01:49:41 +00:00
|
|
|
$server = $this->repository->create([
|
|
|
|
'uuid' => Uuid::uuid4()->toString(),
|
2017-10-27 04:49:54 +00:00
|
|
|
'uuidShort' => str_random(8),
|
|
|
|
'node_id' => array_get($data, 'node_id'),
|
|
|
|
'name' => array_get($data, 'name'),
|
2018-01-01 18:23:45 +00:00
|
|
|
'description' => array_get($data, 'description', ''),
|
2017-07-20 01:49:41 +00:00
|
|
|
'skip_scripts' => isset($data['skip_scripts']),
|
|
|
|
'suspended' => false,
|
2017-10-27 04:49:54 +00:00
|
|
|
'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'),
|
2017-07-20 01:49:41 +00:00
|
|
|
'oom_disabled' => isset($data['oom_disabled']),
|
2017-10-27 04:49:54 +00:00
|
|
|
'allocation_id' => array_get($data, 'allocation_id'),
|
|
|
|
'nest_id' => array_get($data, 'nest_id'),
|
|
|
|
'egg_id' => array_get($data, 'egg_id'),
|
2017-07-23 01:15:01 +00:00
|
|
|
'pack_id' => (! isset($data['pack_id']) || $data['pack_id'] == 0) ? null : $data['pack_id'],
|
2017-10-27 04:49:54 +00:00
|
|
|
'startup' => array_get($data, 'startup'),
|
|
|
|
'daemonSecret' => str_random(Node::DAEMON_SECRET_LENGTH),
|
|
|
|
'image' => array_get($data, 'docker_image'),
|
2017-07-20 01:49:41 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Process allocations and assign them to the server in the database.
|
|
|
|
$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.
|
2017-10-27 04:49:54 +00:00
|
|
|
$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) {
|
|
|
|
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
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
if (! empty($records)) {
|
|
|
|
$this->serverVariableRepository->insert($records);
|
|
|
|
}
|
|
|
|
$structure = $this->configurationStructureService->handle($server);
|
2017-07-20 01:49:41 +00:00
|
|
|
|
|
|
|
// Create the server on the daemon & commit it to the database.
|
2018-01-06 00:27:47 +00:00
|
|
|
$node = $this->nodeRepository->find($server->node_id);
|
2017-07-24 00:57:43 +00:00
|
|
|
try {
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->daemonServerRepository->setNode($node)->create($structure, [
|
2017-10-06 05:16:22 +00:00
|
|
|
'start_on_completion' => (bool) array_get($data, 'start_on_completion', false),
|
|
|
|
]);
|
|
|
|
$this->connection->commit();
|
2017-07-24 00:57:43 +00:00
|
|
|
} catch (RequestException $exception) {
|
2017-10-06 05:16:22 +00:00
|
|
|
$this->connection->rollBack();
|
|
|
|
throw new DaemonConnectionException($exception);
|
2017-07-24 00:57:43 +00:00
|
|
|
}
|
2017-07-20 01:49:41 +00:00
|
|
|
|
|
|
|
return $server;
|
|
|
|
}
|
|
|
|
}
|