2017-07-20 01:49:41 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
|
|
|
|
use Ramsey\Uuid\Uuid;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Illuminate\Log\Writer;
|
2017-07-23 01:15:01 +00:00
|
|
|
use Illuminate\Database\DatabaseManager;
|
2017-08-05 22:26:30 +00:00
|
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
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;
|
|
|
|
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
|
|
|
|
|
2017-07-22 02:17:42 +00:00
|
|
|
class CreationService
|
2017-07-20 01:49:41 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $allocationRepository;
|
|
|
|
|
2017-07-22 02:17:42 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $daemonServerRepository;
|
|
|
|
|
|
|
|
/**
|
2017-07-23 01:15:01 +00:00
|
|
|
* @var \Illuminate\Database\DatabaseManager
|
2017-07-22 02:17:42 +00:00
|
|
|
*/
|
|
|
|
protected $database;
|
|
|
|
|
2017-07-20 01:49:41 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $nodeRepository;
|
|
|
|
|
2017-07-22 02:17:42 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $serverVariableRepository;
|
|
|
|
|
2017-07-20 01:49:41 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $userRepository;
|
|
|
|
|
2017-07-22 02:17:42 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\UsernameGenerationService
|
|
|
|
*/
|
2017-07-20 01:49:41 +00:00
|
|
|
protected $usernameService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\VariableValidatorService
|
|
|
|
*/
|
|
|
|
protected $validatorService;
|
|
|
|
|
2017-07-24 00:57:43 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Log\Writer
|
|
|
|
*/
|
|
|
|
protected $writer;
|
|
|
|
|
2017-07-22 02:17:42 +00:00
|
|
|
/**
|
|
|
|
* CreationService constructor.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository
|
2017-07-23 01:15:01 +00:00
|
|
|
* @param \Illuminate\Database\DatabaseManager $database
|
2017-07-22 02:17:42 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $nodeRepository
|
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
|
2017-07-23 01:15:01 +00:00
|
|
|
* @param \Pterodactyl\Services\Servers\UsernameGenerationService $usernameService
|
2017-07-22 02:17:42 +00:00
|
|
|
* @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService
|
2017-07-24 00:57:43 +00:00
|
|
|
* @param \Illuminate\Log\Writer $writer
|
2017-07-22 02:17:42 +00:00
|
|
|
*/
|
2017-07-20 01:49:41 +00:00
|
|
|
public function __construct(
|
|
|
|
AllocationRepositoryInterface $allocationRepository,
|
|
|
|
DaemonServerRepositoryInterface $daemonServerRepository,
|
2017-07-23 01:15:01 +00:00
|
|
|
DatabaseManager $database,
|
2017-07-20 01:49:41 +00:00
|
|
|
NodeRepositoryInterface $nodeRepository,
|
2017-07-23 01:15:01 +00:00
|
|
|
ServerRepositoryInterface $repository,
|
|
|
|
ServerVariableRepositoryInterface $serverVariableRepository,
|
2017-07-20 01:49:41 +00:00
|
|
|
UserRepositoryInterface $userRepository,
|
2017-07-23 01:15:01 +00:00
|
|
|
UsernameGenerationService $usernameService,
|
2017-07-24 00:57:43 +00:00
|
|
|
VariableValidatorService $validatorService,
|
|
|
|
Writer $writer
|
2017-07-20 01:49:41 +00:00
|
|
|
) {
|
|
|
|
$this->allocationRepository = $allocationRepository;
|
2017-07-23 01:15:01 +00:00
|
|
|
$this->daemonServerRepository = $daemonServerRepository;
|
2017-07-20 01:49:41 +00:00
|
|
|
$this->database = $database;
|
|
|
|
$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->usernameService = $usernameService;
|
|
|
|
$this->validatorService = $validatorService;
|
2017-07-24 00:57:43 +00:00
|
|
|
$this->writer = $writer;
|
2017-07-20 01:49:41 +00:00
|
|
|
}
|
|
|
|
|
2017-07-22 02:17:42 +00:00
|
|
|
/**
|
|
|
|
* Create a server on both the panel and daemon.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @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-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-25 02:34:10 +00:00
|
|
|
$validator = $this->validatorService->isAdmin()->setFields($data['environment'])->validate($data['option_id']);
|
2017-07-20 01:49:41 +00:00
|
|
|
$uniqueShort = bin2hex(random_bytes(4));
|
|
|
|
|
|
|
|
$this->database->beginTransaction();
|
|
|
|
|
|
|
|
$server = $this->repository->create([
|
|
|
|
'uuid' => Uuid::uuid4()->toString(),
|
2017-07-23 01:15:01 +00:00
|
|
|
'uuidShort' => $uniqueShort,
|
2017-07-20 01:49:41 +00:00
|
|
|
'node_id' => $data['node_id'],
|
|
|
|
'name' => $data['name'],
|
|
|
|
'description' => $data['description'],
|
|
|
|
'skip_scripts' => isset($data['skip_scripts']),
|
|
|
|
'suspended' => false,
|
2017-07-25 02:34:10 +00:00
|
|
|
'owner_id' => $data['owner_id'],
|
2017-07-20 01:49:41 +00:00
|
|
|
'memory' => $data['memory'],
|
|
|
|
'swap' => $data['swap'],
|
|
|
|
'disk' => $data['disk'],
|
|
|
|
'io' => $data['io'],
|
|
|
|
'cpu' => $data['cpu'],
|
|
|
|
'oom_disabled' => isset($data['oom_disabled']),
|
|
|
|
'allocation_id' => $data['allocation_id'],
|
|
|
|
'service_id' => $data['service_id'],
|
|
|
|
'option_id' => $data['option_id'],
|
2017-07-23 01:15:01 +00:00
|
|
|
'pack_id' => (! isset($data['pack_id']) || $data['pack_id'] == 0) ? null : $data['pack_id'],
|
2017-07-20 01:49:41 +00:00
|
|
|
'startup' => $data['startup'],
|
|
|
|
'daemonSecret' => bin2hex(random_bytes(18)),
|
|
|
|
'image' => $data['docker_image'],
|
|
|
|
'username' => $this->usernameService->generate($data['name'], $uniqueShort),
|
|
|
|
'sftp_password' => null,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
$records = [];
|
|
|
|
foreach ($validator->getResults() as $result) {
|
|
|
|
$records[] = [
|
|
|
|
'server_id' => $server->id,
|
|
|
|
'variable_id' => $result['id'],
|
|
|
|
'variable_value' => $result['value'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->serverVariableRepository->insert($records);
|
|
|
|
|
|
|
|
// Create the server on the daemon & commit it to the database.
|
2017-07-24 00:57:43 +00:00
|
|
|
try {
|
|
|
|
$this->daemonServerRepository->setNode($server->node_id)->create($server->id);
|
|
|
|
$this->database->commit();
|
|
|
|
} catch (RequestException $exception) {
|
|
|
|
$response = $exception->getResponse();
|
|
|
|
$this->writer->warning($exception);
|
|
|
|
|
|
|
|
throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [
|
|
|
|
'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(),
|
|
|
|
]));
|
|
|
|
}
|
2017-07-20 01:49:41 +00:00
|
|
|
|
|
|
|
return $server;
|
|
|
|
}
|
|
|
|
}
|