allocationRepository = $allocationRepository; $this->configurationStructureService = $configurationStructureService; $this->connection = $connection; $this->daemonServerRepository = $daemonServerRepository; $this->nodeRepository = $nodeRepository; $this->repository = $repository; $this->serverVariableRepository = $serverVariableRepository; $this->userRepository = $userRepository; $this->validatorService = $validatorService; } /** * Create a server on both the panel and daemon. * * @param array $data * @return mixed * * @throws \Pterodactyl\Exceptions\DisplayException * @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @throws \Illuminate\Validation\ValidationException */ public function create(array $data) { // @todo auto-deployment $this->connection->beginTransaction(); $server = $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']), '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' => false, '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, 'docker_image'), ]); // 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. $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 [ 'server_id' => $server->id, 'variable_id' => $result->id, 'variable_value' => $result->value, ]; })->toArray(); 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; } }