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

@ -57,4 +57,26 @@ interface AllocationRepositoryInterface extends RepositoryInterface
* @return array * @return array
*/ */
public function getAssignedAllocationIds(int $server): array; public function getAssignedAllocationIds(int $server): array;
/**
* Return a concated result set of node ips that already have at least one
* server assigned to that IP. This allows for filtering out sets for
* dedicated allocation IPs.
*
* If an array of nodes is passed the results will be limited to allocations
* in those nodes.
*
* @param array $nodes
* @return array
*/
public function getDiscardableDedicatedAllocations(array $nodes = []): array;
/**
* Return a single allocation from those meeting the requirements.
*
* @param array $nodes
* @param array $ports
* @param bool $dedicated
* @return \Pterodactyl\Models\Allocation|null
public function getRandomAllocation(array $nodes, array $ports, bool $dedicated = false);
} }

View file

@ -2,6 +2,7 @@
namespace Pterodactyl\Contracts\Repository; namespace Pterodactyl\Contracts\Repository;
use Generator;
use Pterodactyl\Models\Node; use Pterodactyl\Models\Node;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Contracts\Pagination\LengthAwarePaginator;
@ -62,4 +63,15 @@ interface NodeRepositoryInterface extends RepositoryInterface, SearchableInterfa
* @return \Illuminate\Support\Collection * @return \Illuminate\Support\Collection
*/ */
public function getNodesForServerCreation(): Collection; public function getNodesForServerCreation(): Collection;
/**
* Return the IDs of all nodes that exist in the provided locations and have the space
* available to support the additional disk and memory provided.
*
* @param array $locations
* @param int $disk
* @param int $memory
* @return \Generator
*/
public function getNodesWithResourceUse(array $locations, int $disk, int $memory): Generator;
} }

View file

@ -0,0 +1,9 @@
<?php
namespace Pterodactyl\Exceptions\Service\Deployment;
use Pterodactyl\Exceptions\DisplayException;
class NoViableAllocationException extends DisplayException
{
}

View file

@ -0,0 +1,9 @@
<?php
namespace Pterodactyl\Exceptions\Service\Deployment;
use Pterodactyl\Exceptions\DisplayException;
class NoViableNodeException extends DisplayException
{
}

View file

@ -251,14 +251,17 @@ class ServersController extends Controller
* @param \Pterodactyl\Http\Requests\Admin\ServerFormRequest $request * @param \Pterodactyl\Http\Requests\Admin\ServerFormRequest $request
* @return \Illuminate\Http\RedirectResponse * @return \Illuminate\Http\RedirectResponse
* *
* @throws \Illuminate\Validation\ValidationException
* @throws \Pterodactyl\Exceptions\DisplayException * @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Illuminate\Validation\ValidationException * @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException
*/ */
public function store(ServerFormRequest $request) public function store(ServerFormRequest $request)
{ {
$server = $this->service->create($request->except('_token')); $server = $this->service->handle($request->except('_token'));
$this->alert->success(trans('admin/server.alerts.server_created'))->flash(); $this->alert->success(trans('admin/server.alerts.server_created'))->flash();
return redirect()->route('admin.servers.view', $server->id); return redirect()->route('admin.servers.view', $server->id);

View file

@ -4,15 +4,23 @@ namespace Pterodactyl\Http\Controllers\Api\Application\Servers;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Illuminate\Http\JsonResponse;
use Pterodactyl\Services\Servers\ServerCreationService;
use Pterodactyl\Services\Servers\ServerDeletionService; use Pterodactyl\Services\Servers\ServerDeletionService;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Transformers\Api\Application\ServerTransformer; use Pterodactyl\Transformers\Api\Application\ServerTransformer;
use Pterodactyl\Http\Requests\Api\Application\Servers\GetServersRequest; use Pterodactyl\Http\Requests\Api\Application\Servers\GetServersRequest;
use Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest; use Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest;
use Pterodactyl\Http\Requests\Api\Application\Servers\StoreServerRequest;
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController; use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
class ServerController extends ApplicationApiController class ServerController extends ApplicationApiController
{ {
/**
* @var \Pterodactyl\Services\Servers\ServerCreationService
*/
private $creationService;
/** /**
* @var \Pterodactyl\Services\Servers\ServerDeletionService * @var \Pterodactyl\Services\Servers\ServerDeletionService
*/ */
@ -26,13 +34,18 @@ class ServerController extends ApplicationApiController
/** /**
* ServerController constructor. * ServerController constructor.
* *
* @param \Pterodactyl\Services\Servers\ServerCreationService $creationService
* @param \Pterodactyl\Services\Servers\ServerDeletionService $deletionService * @param \Pterodactyl\Services\Servers\ServerDeletionService $deletionService
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
*/ */
public function __construct(ServerDeletionService $deletionService, ServerRepositoryInterface $repository) public function __construct(
{ ServerCreationService $creationService,
ServerDeletionService $deletionService,
ServerRepositoryInterface $repository
) {
parent::__construct(); parent::__construct();
$this->creationService = $creationService;
$this->deletionService = $deletionService; $this->deletionService = $deletionService;
$this->repository = $repository; $this->repository = $repository;
} }
@ -52,6 +65,29 @@ class ServerController extends ApplicationApiController
->toArray(); ->toArray();
} }
/**
* Create a new server on the system.
*
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\StoreServerRequest $request
* @return \Illuminate\Http\JsonResponse
*
* @throws \Illuminate\Validation\ValidationException
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException
*/
public function store(StoreServerRequest $request): JsonResponse
{
$server = $this->creationService->handle($request->validated(), $request->getDeploymentObject());
return $this->fractal->item($server)
->transformWith($this->getTransformer(ServerTransformer::class))
->respond(201);
}
/** /**
* Show a single server transformed for the application API. * Show a single server transformed for the application API.
* *

View file

@ -0,0 +1,148 @@
<?php
namespace Pterodactyl\Http\Requests\Api\Application\Servers;
use Pterodactyl\Models\Server;
use Illuminate\Validation\Rule;
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Illuminate\Contracts\Validation\Validator;
use Pterodactyl\Models\Objects\DeploymentObject;
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
class StoreServerRequest extends ApplicationApiRequest
{
/**
* @var string
*/
protected $resource = AdminAcl::RESOURCE_SERVERS;
/**
* @var int
*/
protected $permission = AdminAcl::WRITE;
/**
* Rules to be applied to this request.
*
* @return array
*/
public function rules(): array
{
$rules = Server::getCreateRules();
return [
'name' => $rules['name'],
'description' => array_merge(['nullable'], $rules['description']),
'user' => $rules['owner_id'],
'egg' => $rules['egg_id'],
'pack' => $rules['pack_id'],
'docker_image' => $rules['image'],
'startup' => $rules['startup'],
'environment' => 'required|array',
'skip_scripts' => 'sometimes|boolean',
// Resource limitations
'limits' => 'required|array',
'limits.memory' => $rules['memory'],
'limits.swap' => $rules['swap'],
'limits.disk' => $rules['disk'],
'limits.io' => $rules['io'],
'limits.cpu' => $rules['cpu'],
// Automatic deployment rules
'deploy' => 'sometimes|required|array',
'deploy.locations' => 'array',
'deploy.locations.*' => 'integer|min:1',
'deploy.dedicated_ip' => 'required_with:deploy,boolean',
'deploy.port_range' => 'array',
'deploy.port_range.*' => 'string',
'start_on_completion' => 'sometimes|boolean',
];
}
/**
* Normalize the data into a format that can be consumed by the service.
*
* @return array
*/
public function validated()
{
$data = parent::validated();
return [
'name' => array_get($data, 'name'),
'description' => array_get($data, 'description'),
'owner_id' => array_get($data, 'user'),
'egg_id' => array_get($data, 'egg'),
'pack_id' => array_get($data, 'pack'),
'image' => array_get($data, 'docker_image'),
'startup' => array_get($data, 'startup'),
'environment' => array_get($data, 'environment'),
'memory' => array_get($data, 'limits.memory'),
'swap' => array_get($data, 'limits.swap'),
'disk' => array_get($data, 'limits.disk'),
'io' => array_get($data, 'limits.io'),
'cpu' => array_get($data, 'limits.cpu'),
'skip_scripts' => array_get($data, 'skip_scripts', false),
'allocation_id' => array_get($data, 'allocation.default'),
'allocation_additional' => array_get($data, 'allocation.additional'),
'start_on_completion' => array_get($data, 'start_on_completion', false),
];
}
/*
* Run validation after the rules above have been applied.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
*/
public function withValidator(Validator $validator)
{
$validator->sometimes('allocation.default', [
'required', 'integer', 'bail',
Rule::exists('allocations', 'id')->where(function ($query) {
$query->where('node_id', $this->input('node_id'));
$query->whereNull('server_id');
}),
], function ($input) {
return ! ($input->deploy);
});
$validator->sometimes('allocation.additional.*', [
'integer',
Rule::exists('allocations', 'id')->where(function ($query) {
$query->where('node_id', $this->input('node_id'));
$query->whereNull('server_id');
}),
], function ($input) {
return ! ($input->deploy);
});
$validator->sometimes('deploy.locations', 'present', function ($input) {
return $input->deploy;
});
$validator->sometimes('deploy.port_range', 'present', function ($input) {
return $input->deploy;
});
}
/**
* Return a deployment object that can be passed to the server creation service.
*
* @return \Pterodactyl\Models\Objects\DeploymentObject|null
*/
public function getDeploymentObject()
{
if (is_null($this->input('deploy'))) {
return null;
}
$object = new DeploymentObject;
$object->setDedicated($this->input('deploy.dedicated_ip', false));
$object->setLocations($this->input('deploy.locations', []));
$object->setPorts($this->input('deploy.port_range', []));
return $object;
}
}

View file

@ -0,0 +1,78 @@
<?php
namespace Pterodactyl\Models\Objects;
class DeploymentObject
{
/**
* @var bool
*/
private $dedicated = false;
/**
* @var array
*/
private $locations = [];
/**
* @var array
*/
private $ports = [];
/**
* @return bool
*/
public function isDedicated(): bool
{
return $this->dedicated;
}
/**
* @param bool $dedicated
* @return $this
*/
public function setDedicated(bool $dedicated)
{
$this->dedicated = $dedicated;
return $this;
}
/**
* @return array
*/
public function getLocations(): array
{
return $this->locations;
}
/**
* @param array $locations
* @return $this
*/
public function setLocations(array $locations)
{
$this->locations = $locations;
return $this;
}
/**
* @return array
*/
public function getPorts(): array
{
return $this->ports;
}
/**
* @param array $ports
* @return $this
*/
public function setPorts(array $ports)
{
$this->ports = $ports;
return $this;
}
}

View file

@ -66,13 +66,15 @@ class Server extends Model implements CleansAttributes, ValidableContract
'allocation_id' => 'required', 'allocation_id' => 'required',
'pack_id' => 'sometimes', 'pack_id' => 'sometimes',
'skip_scripts' => 'sometimes', 'skip_scripts' => 'sometimes',
'image' => 'required',
'startup' => 'required',
]; ];
/** /**
* @var array * @var array
*/ */
protected static $dataIntegrityRules = [ protected static $dataIntegrityRules = [
'owner_id' => 'exists:users,id', 'owner_id' => 'integer|exists:users,id',
'name' => 'string|min:1|max:255', 'name' => 'string|min:1|max:255',
'node_id' => 'exists:nodes,id', 'node_id' => 'exists:nodes,id',
'description' => 'string', 'description' => 'string',
@ -85,8 +87,9 @@ class Server extends Model implements CleansAttributes, ValidableContract
'nest_id' => 'exists:nests,id', 'nest_id' => 'exists:nests,id',
'egg_id' => 'exists:eggs,id', 'egg_id' => 'exists:eggs,id',
'pack_id' => 'nullable|numeric|min:0', 'pack_id' => 'nullable|numeric|min:0',
'startup' => 'nullable|string', 'startup' => 'string',
'skip_scripts' => 'boolean', 'skip_scripts' => 'boolean',
'image' => 'string|max:255',
]; ];
/** /**

View file

@ -4,6 +4,7 @@ namespace Pterodactyl\Repositories\Eloquent;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Pterodactyl\Models\Allocation; use Pterodactyl\Models\Allocation;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
@ -94,4 +95,81 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos
return $results->pluck('id')->toArray(); return $results->pluck('id')->toArray();
} }
/**
* Return a concated result set of node ips that already have at least one
* server assigned to that IP. This allows for filtering out sets for
* dedicated allocation IPs.
*
* If an array of nodes is passed the results will be limited to allocations
* in those nodes.
*
* @param array $nodes
* @return array
*/
public function getDiscardableDedicatedAllocations(array $nodes = []): array
{
$instance = $this->getBuilder()->select(
$this->getBuilder()->raw('CONCAT_WS("-", node_id, ip) as result')
);
if (! empty($nodes)) {
$instance->whereIn('node_id', $nodes);
}
$results = $instance->whereNotNull('server_id')
->groupBy($this->getBuilder()->raw('CONCAT(node_id, ip)'))
->get();
return $results->pluck('result')->toArray();
}
/**
* Return a single allocation from those meeting the requirements.
*
* @param array $nodes
* @param array $ports
* @param bool $dedicated
* @return \Pterodactyl\Models\Allocation|null
*/
public function getRandomAllocation(array $nodes, array $ports, bool $dedicated = false)
{
$instance = $this->getBuilder()->whereNull('server_id');
if (! empty($nodes)) {
$instance->whereIn('node_id', $nodes);
}
if (! empty($ports)) {
$instance->where(function (Builder $query) use ($ports) {
$whereIn = [];
foreach ($ports as $port) {
if (is_array($port)) {
$query->orWhereBetween('port', $port);
continue;
}
$whereIn[] = $port;
}
if (! empty($whereIn)) {
$query->orWhereIn('port', $whereIn);
}
});
}
// If this allocation should not be shared with any other servers get
// the data and modify the query as necessary,
if ($dedicated) {
$discard = $this->getDiscardableDedicatedAllocations($nodes);
if (! empty($discard)) {
$instance->whereNotIn(
$this->getBuilder()->raw('CONCAT_WS("-", node_id, ip)'), $discard
);
}
}
return $instance->inRandomOrder()->first();
}
} }

View file

@ -2,6 +2,7 @@
namespace Pterodactyl\Repositories\Eloquent; namespace Pterodactyl\Repositories\Eloquent;
use Generator;
use Pterodactyl\Models\Node; use Pterodactyl\Models\Node;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Pterodactyl\Repositories\Concerns\Searchable; use Pterodactyl\Repositories\Concerns\Searchable;
@ -157,4 +158,28 @@ class NodeRepository extends EloquentRepository implements NodeRepositoryInterfa
]; ];
})->values(); })->values();
} }
/**
* Return the IDs of all nodes that exist in the provided locations and have the space
* available to support the additional disk and memory provided.
*
* @param array $locations
* @param int $disk
* @param int $memory
* @return \Generator
*/
public function getNodesWithResourceUse(array $locations, int $disk, int $memory): Generator
{
$instance = $this->getBuilder()
->select(['nodes.id', 'nodes.memory', 'nodes.disk', 'nodes.memory_overallocate', 'nodes.disk_overallocate'])
->selectRaw('IFNULL(SUM(servers.memory), 0) as sum_memory, IFNULL(SUM(servers.disk), 0) as sum_disk')
->join('servers', 'servers.node_id', '=', 'nodes.id')
->where('nodes.public', 1);
if (! empty($locations)) {
$instance->whereIn('nodes.location_id', $locations);
}
return $instance->cursor();
}
} }

View file

@ -70,7 +70,7 @@ class AssignmentService
$this->connection->beginTransaction(); $this->connection->beginTransaction();
foreach (Network::parse(gethostbyname($data['allocation_ip'])) as $ip) { foreach (Network::parse(gethostbyname($data['allocation_ip'])) as $ip) {
foreach ($data['allocation_ports'] as $port) { foreach ($data['allocation_ports'] as $port) {
if (! ctype_digit($port) && ! preg_match(self::PORT_RANGE_REGEX, $port)) { if (! is_digit($port) && ! preg_match(self::PORT_RANGE_REGEX, $port)) {
throw new DisplayException(trans('exceptions.allocations.invalid_mapping', ['port' => $port])); throw new DisplayException(trans('exceptions.allocations.invalid_mapping', ['port' => $port]));
} }

View file

@ -0,0 +1,123 @@
<?php
namespace Pterodactyl\Services\Deployment;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Services\Allocations\AssignmentService;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException;
class AllocationSelectionService
{
/**
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface
*/
private $repository;
/**
* @var bool
*/
protected $dedicated = false;
/**
* @var array
*/
protected $nodes = [];
/**
* @var array
*/
protected $ports = [];
/**
* AllocationSelectionService constructor.
*
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $repository
*/
public function __construct(AllocationRepositoryInterface $repository)
{
$this->repository = $repository;
}
/**
* Toggle if the selected allocation should be the only allocation belonging
* to the given IP address. If true an allocation will not be selected if an IP
* already has another server set to use on if its allocations.
*
* @param bool $dedicated
* @return $this
*/
public function setDedicated(bool $dedicated)
{
$this->dedicated = $dedicated;
return $this;
}
/**
* A list of node IDs that should be used when selecting an allocation. If empty, all
* nodes will be used to filter with.
*
* @param array $nodes
* @return $this
*/
public function setNodes(array $nodes)
{
$this->nodes = $nodes;
return $this;
}
/**
* An array of individual ports or port ranges to use when selecting an allocation. If
* empty, all ports will be considered when finding an allocation. If set, only ports appearing
* in the array or range will be used.
*
* @param array $ports
* @return $this
*
* @throws \Pterodactyl\Exceptions\DisplayException
*/
public function setPorts(array $ports)
{
$stored = [];
foreach ($ports as $port) {
if (is_digit($port)) {
$stored[] = $port;
}
// Ranges are stored in the ports array as an array which can be
// better processed in the repository.
if (preg_match(AssignmentService::PORT_RANGE_REGEX, $port, $matches)) {
if (abs($matches[2] - $matches[1]) > AssignmentService::PORT_RANGE_LIMIT) {
throw new DisplayException(trans('exceptions.allocations.too_many_ports'));
}
$stored[] = [$matches[1], $matches[2]];
}
}
$this->ports = $stored;
return $this;
}
/**
* Return a single allocation that should be used as the default allocation for a server.
*
* @return \Pterodactyl\Models\Allocation
*
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException
*/
public function handle(): Allocation
{
$allocation = $this->repository->getRandomAllocation($this->nodes, $this->ports, $this->dedicated);
if (is_null($allocation)) {
throw new NoViableAllocationException(trans('exceptions.deployment.no_viable_allocations'));
}
return $allocation;
}
}

View file

@ -0,0 +1,121 @@
<?php
namespace Pterodactyl\Services\Deployment;
use Webmozart\Assert\Assert;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException;
class FindViableNodesService
{
/**
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
*/
private $repository;
/**
* @var array
*/
protected $locations = [];
/**
* @var int
*/
protected $disk;
/**
* @var int
*/
protected $memory;
/**
* FindViableNodesService constructor.
*
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $repository
*/
public function __construct(NodeRepositoryInterface $repository)
{
$this->repository = $repository;
}
/**
* Set the locations that should be searched through to locate available nodes.
*
* @param array $locations
* @return $this
*/
public function setLocations(array $locations): self
{
$this->locations = $locations;
return $this;
}
/**
* Set the amount of disk that will be used by the server being created. Nodes will be
* filtered out if they do not have enough available free disk space for this server
* to be placed on.
*
* @param int $disk
* @return $this
*/
public function setDisk(int $disk): self
{
$this->disk = $disk;
return $this;
}
/**
* Set the amount of memory that this server will be using. As with disk space, nodes that
* do not have enough free memory will be filtered out.
*
* @param int $memory
* @return $this
*/
public function setMemory(int $memory): self
{
$this->memory = $memory;
return $this;
}
/**
* Returns an array of nodes that meet the provided requirements and can then
* be passed to the AllocationSelectionService to return a single allocation.
*
* This functionality is used for automatic deployments of servers and will
* attempt to find all nodes in the defined locations that meet the disk and
* memory availability requirements. Any nodes not meeting those requirements
* are tossed out, as are any nodes marked as non-public, meaning automatic
* deployments should not be done aganist them.
*
* @return int[]
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException
*/
public function handle(): array
{
Assert::integer($this->disk, 'Calls to ' . __METHOD__ . ' must have the disk space set as an integer, received %s');
Assert::integer($this->memory, 'Calls to ' . __METHOD__ . ' must have the memory usage set as an integer, received %s');
$nodes = $this->repository->getNodesWithResourceUse($this->locations, $this->disk, $this->memory);
$viable = [];
foreach ($nodes as $node) {
$memoryLimit = $node->memory * (1 + ($node->memory_overallocate / 100));
$diskLimit = $node->disk * (1 + ($node->disk_overallocate / 100));
if (($node->sum_memory + $this->memory) > $memoryLimit || ($node->sum_disk + $this->disk) > $diskLimit) {
continue;
}
$viable[] = $node->id;
}
if (empty($viable)) {
throw new NoViableNodeException(trans('exceptions.deployment.no_viable_nodes'));
}
return $viable;
}
}

View file

@ -5,11 +5,16 @@ namespace Pterodactyl\Services\Servers;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
use Pterodactyl\Models\Node; use Pterodactyl\Models\Node;
use Pterodactyl\Models\User; use Pterodactyl\Models\User;
use Pterodactyl\Models\Server;
use Illuminate\Support\Collection;
use Pterodactyl\Models\Allocation;
use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; use Pterodactyl\Models\Objects\DeploymentObject;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Services\Deployment\FindViableNodesService;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Services\Deployment\AllocationSelectionService;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
@ -22,6 +27,11 @@ class ServerCreationService
*/ */
private $allocationRepository; private $allocationRepository;
/**
* @var \Pterodactyl\Services\Deployment\AllocationSelectionService
*/
private $allocationSelectionService;
/** /**
* @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService * @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService
*/ */
@ -38,9 +48,14 @@ class ServerCreationService
private $daemonServerRepository; 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 * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
@ -52,11 +67,6 @@ class ServerCreationService
*/ */
private $serverVariableRepository; private $serverVariableRepository;
/**
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
*/
private $userRepository;
/** /**
* @var \Pterodactyl\Services\Servers\VariableValidatorService * @var \Pterodactyl\Services\Servers\VariableValidatorService
*/ */
@ -66,60 +76,139 @@ class ServerCreationService
* CreationService constructor. * CreationService constructor.
* *
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository * @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository
* @param \Pterodactyl\Services\Deployment\AllocationSelectionService $allocationSelectionService
* @param \Illuminate\Database\ConnectionInterface $connection * @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository * @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\Services\Servers\ServerConfigurationStructureService $configurationStructureService
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
* @param \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface $serverVariableRepository * @param \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface $serverVariableRepository
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $userRepository
* @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService * @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService
*/ */
public function __construct( public function __construct(
AllocationRepositoryInterface $allocationRepository, AllocationRepositoryInterface $allocationRepository,
AllocationSelectionService $allocationSelectionService,
ConnectionInterface $connection, ConnectionInterface $connection,
DaemonServerRepositoryInterface $daemonServerRepository, DaemonServerRepositoryInterface $daemonServerRepository,
NodeRepositoryInterface $nodeRepository, EggRepositoryInterface $eggRepository,
FindViableNodesService $findViableNodesService,
ServerConfigurationStructureService $configurationStructureService, ServerConfigurationStructureService $configurationStructureService,
ServerRepositoryInterface $repository, ServerRepositoryInterface $repository,
ServerVariableRepositoryInterface $serverVariableRepository, ServerVariableRepositoryInterface $serverVariableRepository,
UserRepositoryInterface $userRepository,
VariableValidatorService $validatorService VariableValidatorService $validatorService
) { ) {
$this->allocationSelectionService = $allocationSelectionService;
$this->allocationRepository = $allocationRepository; $this->allocationRepository = $allocationRepository;
$this->configurationStructureService = $configurationStructureService; $this->configurationStructureService = $configurationStructureService;
$this->connection = $connection; $this->connection = $connection;
$this->daemonServerRepository = $daemonServerRepository; $this->daemonServerRepository = $daemonServerRepository;
$this->nodeRepository = $nodeRepository; $this->eggRepository = $eggRepository;
$this->findViableNodesService = $findViableNodesService;
$this->repository = $repository; $this->repository = $repository;
$this->serverVariableRepository = $serverVariableRepository; $this->serverVariableRepository = $serverVariableRepository;
$this->userRepository = $userRepository;
$this->validatorService = $validatorService; $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 * @param array $data
* @return mixed * @param \Pterodactyl\Models\Objects\DeploymentObject|null $deployment
* @return \Pterodactyl\Models\Server
* *
* @throws \Pterodactyl\Exceptions\DisplayException * @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Illuminate\Validation\ValidationException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @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(); $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(), 'uuid' => Uuid::uuid4()->toString(),
'uuidShort' => str_random(8), 'uuidShort' => str_random(8),
'node_id' => array_get($data, 'node_id'), 'node_id' => array_get($data, 'node_id'),
'name' => array_get($data, 'name'), 'name' => array_get($data, 'name'),
'description' => array_get($data, 'description') ?? '', 'description' => array_get($data, 'description') ?? '',
'skip_scripts' => isset($data['skip_scripts']), 'skip_scripts' => array_get($data, 'skip_scripts') ?? isset($data['skip_scripts']),
'suspended' => false, 'suspended' => false,
'owner_id' => array_get($data, 'owner_id'), 'owner_id' => array_get($data, 'owner_id'),
'memory' => array_get($data, 'memory'), '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'], 'pack_id' => (! isset($data['pack_id']) || $data['pack_id'] == 0) ? null : $data['pack_id'],
'startup' => array_get($data, 'startup'), 'startup' => array_get($data, 'startup'),
'daemonSecret' => str_random(Node::DAEMON_SECRET_LENGTH), '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']]; $records = [$data['allocation_id']];
if (isset($data['allocation_additional']) && is_array($data['allocation_additional'])) { if (isset($data['allocation_additional']) && is_array($data['allocation_additional'])) {
$records = array_merge($records, $data['allocation_additional']); $records = array_merge($records, $data['allocation_additional']);
} }
$this->allocationRepository->assignAllocationsToServer($server->id, $records); $this->allocationRepository->assignAllocationsToServer($server->id, $records);
}
// Process the passed variables and store them in the database. /**
$this->validatorService->setUserLevel(User::USER_LEVEL_ADMIN); * Process environment variables passed for this server and store them in the database.
$results = $this->validatorService->handle(array_get($data, 'egg_id'), array_get($data, 'environment', [])); *
* @param \Pterodactyl\Models\Server $server
$records = $results->map(function ($result) use ($server) { * @param \Illuminate\Support\Collection $variables
*/
private function storeEggVariables(Server $server, Collection $variables)
{
$records = $variables->map(function ($result) use ($server) {
return [ return [
'server_id' => $server->id, 'server_id' => $server->id,
'variable_id' => $result->id, 'variable_id' => $result->id,
@ -160,20 +262,5 @@ class ServerCreationService
if (! empty($records)) { if (! empty($records)) {
$this->serverVariableRepository->insert($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;
} }
} }

View file

@ -13,10 +13,10 @@ use Illuminate\Log\Writer;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Services\Databases\DatabaseManagementService; use Pterodactyl\Services\Databases\DatabaseManagementService;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
class ServerDeletionService class ServerDeletionService
@ -101,28 +101,21 @@ class ServerDeletionService
* @param int|\Pterodactyl\Models\Server $server * @param int|\Pterodactyl\Models\Server $server
* *
* @throws \Pterodactyl\Exceptions\DisplayException * @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/ */
public function handle($server) public function handle($server)
{ {
if (! $server instanceof Server) {
$server = $this->repository->setColumns(['id', 'node_id', 'uuid'])->find($server);
}
try { try {
$this->daemonServerRepository->setServer($server)->delete(); $this->daemonServerRepository->setServer($server)->delete();
} catch (RequestException $exception) { } catch (RequestException $exception) {
$response = $exception->getResponse(); $response = $exception->getResponse();
if (is_null($response) || (! is_null($response) && $response->getStatusCode() !== 404)) { if (is_null($response) || (! is_null($response) && $response->getStatusCode() !== 404)) {
$this->writer->warning($exception);
// If not forcing the deletion, throw an exception, otherwise just log it and // If not forcing the deletion, throw an exception, otherwise just log it and
// continue with server deletion process in the panel. // continue with server deletion process in the panel.
if (! $this->force) { if (! $this->force) {
throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [ throw new DaemonConnectionException($exception);
'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(), } else {
])); $this->writer->warning($exception);
} }
} }
} }

View file

@ -73,29 +73,27 @@ class VariableValidatorService
public function handle(int $egg, array $fields = []): Collection public function handle(int $egg, array $fields = []): Collection
{ {
$variables = $this->optionVariableRepository->findWhere([['egg_id', '=', $egg]]); $variables = $this->optionVariableRepository->findWhere([['egg_id', '=', $egg]]);
$messages = $this->validator->make([], []);
$response = $variables->map(function ($item) use ($fields, $messages) { $data = $rules = $customAttributes = [];
// Skip doing anything if user is not an admin and foreach ($variables as $variable) {
// variable is not user viewable or editable. $data['environment'][$variable->env_variable] = array_get($fields, $variable->env_variable);
$rules['environment.' . $variable->env_variable] = $variable->rules;
$customAttributes['environment.' . $variable->env_variable] = trans('validation.internal.variable_value', ['env' => $variable->name]);
}
$validator = $this->validator->make($data, $rules, [], $customAttributes);
if ($validator->fails()) {
throw new ValidationException($validator);
}
$response = $variables->filter(function ($item) {
// Skip doing anything if user is not an admin and variable is not user viewable or editable.
if (! $this->isUserLevel(User::USER_LEVEL_ADMIN) && (! $item->user_editable || ! $item->user_viewable)) { if (! $this->isUserLevel(User::USER_LEVEL_ADMIN) && (! $item->user_editable || ! $item->user_viewable)) {
return false; return false;
} }
$v = $this->validator->make([ return true;
'variable_value' => array_get($fields, $item->env_variable), })->map(function ($item) use ($fields) {
], [
'variable_value' => $item->rules,
], [], [
'variable_value' => trans('validation.internal.variable_value', ['env' => $item->name]),
]);
if ($v->fails()) {
foreach ($v->getMessageBag()->all() as $message) {
$messages->getMessageBag()->add($item->env_variable, $message);
}
}
return (object) [ return (object) [
'id' => $item->id, 'id' => $item->id,
'key' => $item->env_variable, 'key' => $item->env_variable,
@ -105,10 +103,6 @@ class VariableValidatorService
return is_object($item); return is_object($item);
}); });
if (! empty($messages->getMessageBag()->all())) {
throw new ValidationException($messages);
}
return $response; return $response;
} }
} }

View file

@ -15,10 +15,13 @@ trait HasUserLevels
* Set the access level for running this function. * Set the access level for running this function.
* *
* @param int $level * @param int $level
* @return $this
*/ */
public function setUserLevel(int $level) public function setUserLevel(int $level)
{ {
$this->userLevel = $level; $this->userLevel = $level;
return $this;
} }
/** /**

View file

@ -56,4 +56,8 @@ return [
'users' => [ 'users' => [
'node_revocation_failed' => 'Failed to revoke keys on <a href=":link">Node #:node</a>. :error', 'node_revocation_failed' => 'Failed to revoke keys on <a href=":link">Node #:node</a>. :error',
], ],
'deployment' => [
'no_viable_nodes' => 'No nodes satisfying the requirements specified for automatic deployment could be found.',
'no_viable_allocations' => 'No allocations satisfying the requirements for automatic deployment were found.',
],
]; ];

View file

@ -91,12 +91,6 @@
<p class="small text-muted no-margin">Additional allocations to assign to this server on creation.</p> <p class="small text-muted no-margin">Additional allocations to assign to this server on creation.</p>
</div> </div>
</div> </div>
<div class="box-footer">
<p class="text-muted small no-margin">
<input type="checkbox" name="auto_deploy" value="yes" id="pAutoDeploy" @if(old('auto_deploy'))checked="checked"@endif/>
<label for="pAutoDeploy">Check this box if you want the panel to automatically select a node and allocation for this server in the given location.</label>
</p>
</div>
</div> </div>
</div> </div>
</div> </div>
@ -202,7 +196,7 @@
<div class="box-body row"> <div class="box-body row">
<div class="form-group col-xs-12"> <div class="form-group col-xs-12">
<label for="pDefaultContainer">Docker Image</label> <label for="pDefaultContainer">Docker Image</label>
<input id="pDefaultContainer" name="docker_image" value="{{ old('docker_image') }}" class="form-control" /> <input id="pDefaultContainer" name="image" value="{{ old('image') }}" class="form-control" />
<p class="small text-muted no-margin">This is the default Docker image that will be used to run this server.</p> <p class="small text-muted no-margin">This is the default Docker image that will be used to run this server.</p>
</div> </div>
</div> </div>

View file

@ -77,6 +77,7 @@ Route::group(['prefix' => '/servers'], function () {
Route::patch('/{server}/details', 'Servers\ServerDetailsController@details')->name('api.application.servers.details'); Route::patch('/{server}/details', 'Servers\ServerDetailsController@details')->name('api.application.servers.details');
Route::patch('/{server}/build', 'Servers\ServerDetailsController@build')->name('api.application.servers.build'); Route::patch('/{server}/build', 'Servers\ServerDetailsController@build')->name('api.application.servers.build');
Route::post('/', 'Servers\ServerController@store');
Route::post('/{server}/suspend', 'Servers\ServerManagementController@suspend')->name('api.application.servers.suspend'); Route::post('/{server}/suspend', 'Servers\ServerManagementController@suspend')->name('api.application.servers.suspend');
Route::post('/{server}/unsuspend', 'Servers\ServerManagementController@unsuspend')->name('api.application.servers.unsuspend'); Route::post('/{server}/unsuspend', 'Servers\ServerManagementController@unsuspend')->name('api.application.servers.unsuspend');
Route::post('/{server}/reinstall', 'Servers\ServerManagementController@reinstall')->name('api.application.servers.reinstall'); Route::post('/{server}/reinstall', 'Servers\ServerManagementController@reinstall')->name('api.application.servers.reinstall');

View file

@ -9,16 +9,15 @@ use Pterodactyl\Models\User;
use Tests\Traits\MocksUuids; use Tests\Traits\MocksUuids;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Tests\Traits\MocksRequestException; use Tests\Traits\MocksRequestException;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\PterodactylException;
use Pterodactyl\Services\Servers\ServerCreationService; use Pterodactyl\Services\Servers\ServerCreationService;
use Pterodactyl\Services\Servers\VariableValidatorService; use Pterodactyl\Services\Servers\VariableValidatorService;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; use Pterodactyl\Services\Deployment\FindViableNodesService;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Services\Deployment\AllocationSelectionService;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Services\Servers\ServerConfigurationStructureService; use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
@ -35,6 +34,11 @@ class ServerCreationServiceTest extends TestCase
*/ */
private $allocationRepository; private $allocationRepository;
/**
* @var \Pterodactyl\Services\Deployment\AllocationSelectionService
*/
private $allocationSelectionService;
/** /**
* @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService|\Mockery\Mock * @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService|\Mockery\Mock
*/ */
@ -51,14 +55,14 @@ class ServerCreationServiceTest extends TestCase
private $daemonServerRepository; private $daemonServerRepository;
/** /**
* @var \GuzzleHttp\Exception\RequestException|\Mockery\Mock * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
*/ */
private $exception; private $eggRepository;
/** /**
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface|\Mockery\Mock * @var \Pterodactyl\Services\Deployment\FindViableNodesService
*/ */
private $nodeRepository; private $findViableNodesService;
/** /**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
@ -88,11 +92,12 @@ class ServerCreationServiceTest extends TestCase
parent::setUp(); parent::setUp();
$this->allocationRepository = m::mock(AllocationRepositoryInterface::class); $this->allocationRepository = m::mock(AllocationRepositoryInterface::class);
$this->allocationSelectionService = m::mock(AllocationSelectionService::class);
$this->configurationStructureService = m::mock(ServerConfigurationStructureService::class); $this->configurationStructureService = m::mock(ServerConfigurationStructureService::class);
$this->connection = m::mock(ConnectionInterface::class); $this->connection = m::mock(ConnectionInterface::class);
$this->daemonServerRepository = m::mock(DaemonServerRepositoryInterface::class); $this->daemonServerRepository = m::mock(DaemonServerRepositoryInterface::class);
$this->exception = m::mock(RequestException::class); $this->eggRepository = m::mock(EggRepositoryInterface::class);
$this->nodeRepository = m::mock(NodeRepositoryInterface::class); $this->findViableNodesService = m::mock(FindViableNodesService::class);
$this->repository = m::mock(ServerRepositoryInterface::class); $this->repository = m::mock(ServerRepositoryInterface::class);
$this->serverVariableRepository = m::mock(ServerVariableRepositoryInterface::class); $this->serverVariableRepository = m::mock(ServerVariableRepositoryInterface::class);
$this->userRepository = m::mock(UserRepositoryInterface::class); $this->userRepository = m::mock(UserRepositoryInterface::class);
@ -119,7 +124,7 @@ class ServerCreationServiceTest extends TestCase
$this->allocationRepository->shouldReceive('assignAllocationsToServer')->with($model->id, [$model->allocation_id])->once()->andReturn(1); $this->allocationRepository->shouldReceive('assignAllocationsToServer')->with($model->id, [$model->allocation_id])->once()->andReturn(1);
$this->validatorService->shouldReceive('setUserLevel')->with(User::USER_LEVEL_ADMIN)->once()->andReturnNull(); $this->validatorService->shouldReceive('setUserLevel')->with(User::USER_LEVEL_ADMIN)->once()->andReturnSelf();
$this->validatorService->shouldReceive('handle')->with($model->egg_id, [])->once()->andReturn( $this->validatorService->shouldReceive('handle')->with($model->egg_id, [])->once()->andReturn(
collect([(object) ['id' => 123, 'value' => 'var1-value']]) collect([(object) ['id' => 123, 'value' => 'var1-value']])
); );
@ -133,20 +138,19 @@ class ServerCreationServiceTest extends TestCase
])->once()->andReturn(true); ])->once()->andReturn(true);
$this->configurationStructureService->shouldReceive('handle')->with($model)->once()->andReturn(['test' => 'struct']); $this->configurationStructureService->shouldReceive('handle')->with($model)->once()->andReturn(['test' => 'struct']);
$node = factory(Node::class)->make(); $this->daemonServerRepository->shouldReceive('setServer')->with($model)->once()->andReturnSelf();
$this->nodeRepository->shouldReceive('find')->with($model->node_id)->once()->andReturn($node);
$this->daemonServerRepository->shouldReceive('setNode')->with($node)->once()->andReturnSelf();
$this->daemonServerRepository->shouldReceive('create')->with(['test' => 'struct'], ['start_on_completion' => false])->once(); $this->daemonServerRepository->shouldReceive('create')->with(['test' => 'struct'], ['start_on_completion' => false])->once();
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->getService()->create($model->toArray()); $response = $this->getService()->handle($model->toArray());
$this->assertSame($model, $response); $this->assertSame($model, $response);
} }
/** /**
* Test handling of node timeout or other daemon error. * Test handling of node timeout or other daemon error.
*
* @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/ */
public function testExceptionShouldBeThrownIfTheRequestFails() public function testExceptionShouldBeThrownIfTheRequestFails()
{ {
@ -159,21 +163,14 @@ class ServerCreationServiceTest extends TestCase
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('create')->once()->andReturn($model); $this->repository->shouldReceive('create')->once()->andReturn($model);
$this->allocationRepository->shouldReceive('assignAllocationsToServer')->once()->andReturn(1); $this->allocationRepository->shouldReceive('assignAllocationsToServer')->once()->andReturn(1);
$this->validatorService->shouldReceive('setUserLevel')->once()->andReturnNull(); $this->validatorService->shouldReceive('setUserLevel')->once()->andReturnSelf();
$this->validatorService->shouldReceive('handle')->once()->andReturn(collect([])); $this->validatorService->shouldReceive('handle')->once()->andReturn(collect([]));
$this->configurationStructureService->shouldReceive('handle')->once()->andReturn([]); $this->configurationStructureService->shouldReceive('handle')->once()->andReturn([]);
$node = factory(Node::class)->make(); $this->daemonServerRepository->shouldReceive('setServer')->with($model)->once()->andThrow($this->getExceptionMock());
$this->nodeRepository->shouldReceive('find')->with($model->node_id)->once()->andReturn($node);
$this->daemonServerRepository->shouldReceive('setNode')->with($node)->once()->andThrow($this->exception);
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull(); $this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
try { $this->getService()->handle($model->toArray());
$this->getService()->create($model->toArray());
} catch (PterodactylException $exception) {
$this->assertInstanceOf(DaemonConnectionException::class, $exception);
$this->assertInstanceOf(RequestException::class, $exception->getPrevious());
}
} }
/** /**
@ -185,13 +182,14 @@ class ServerCreationServiceTest extends TestCase
{ {
return new ServerCreationService( return new ServerCreationService(
$this->allocationRepository, $this->allocationRepository,
$this->allocationSelectionService,
$this->connection, $this->connection,
$this->daemonServerRepository, $this->daemonServerRepository,
$this->nodeRepository, $this->eggRepository,
$this->findViableNodesService,
$this->configurationStructureService, $this->configurationStructureService,
$this->repository, $this->repository,
$this->serverVariableRepository, $this->serverVariableRepository,
$this->userRepository,
$this->validatorService $this->validatorService
); );
} }

View file

@ -1,23 +1,14 @@
<?php <?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Services\Servers; namespace Tests\Unit\Services\Servers;
use Exception;
use Mockery as m; use Mockery as m;
use Tests\TestCase; use Tests\TestCase;
use Illuminate\Log\Writer; use Illuminate\Log\Writer;
use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException; use Tests\Traits\MocksRequestException;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Services\Servers\ServerDeletionService; use Pterodactyl\Services\Servers\ServerDeletionService;
use Pterodactyl\Services\Databases\DatabaseManagementService; use Pterodactyl\Services\Databases\DatabaseManagementService;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
@ -26,50 +17,37 @@ use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonS
class ServerDeletionServiceTest extends TestCase class ServerDeletionServiceTest extends TestCase
{ {
/** use MocksRequestException;
* @var \Illuminate\Database\ConnectionInterface
*/
protected $connection;
/** /**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface * @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
*/ */
protected $daemonServerRepository; private $connection;
/** /**
* @var \Pterodactyl\Services\Databases\DatabaseManagementService * @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
*/ */
protected $databaseManagementService; private $daemonServerRepository;
/** /**
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface * @var \Pterodactyl\Services\Databases\DatabaseManagementService|\Mockery\Mock
*/ */
protected $databaseRepository; private $databaseManagementService;
/** /**
* @var \GuzzleHttp\Exception\RequestException * @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface|\Mockery\Mock
*/ */
protected $exception; private $databaseRepository;
/** /**
* @var \Pterodactyl\Models\Server * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
*/ */
protected $model; private $repository;
/** /**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface * @var \Illuminate\Log\Writer|\Mockery\Mock
*/ */
protected $repository; private $writer;
/**
* @var \Pterodactyl\Services\Servers\ServerDeletionService
*/
protected $service;
/**
* @var \Illuminate\Log\Writer
*/
protected $writer;
/** /**
* Setup tests. * Setup tests.
@ -82,19 +60,8 @@ class ServerDeletionServiceTest extends TestCase
$this->daemonServerRepository = m::mock(DaemonServerRepositoryInterface::class); $this->daemonServerRepository = m::mock(DaemonServerRepositoryInterface::class);
$this->databaseRepository = m::mock(DatabaseRepositoryInterface::class); $this->databaseRepository = m::mock(DatabaseRepositoryInterface::class);
$this->databaseManagementService = m::mock(DatabaseManagementService::class); $this->databaseManagementService = m::mock(DatabaseManagementService::class);
$this->exception = m::mock(RequestException::class);
$this->model = factory(Server::class)->make();
$this->repository = m::mock(ServerRepositoryInterface::class); $this->repository = m::mock(ServerRepositoryInterface::class);
$this->writer = m::mock(Writer::class); $this->writer = m::mock(Writer::class);
$this->service = new ServerDeletionService(
$this->connection,
$this->daemonServerRepository,
$this->databaseRepository,
$this->databaseManagementService,
$this->repository,
$this->writer
);
} }
/** /**
@ -102,7 +69,7 @@ class ServerDeletionServiceTest extends TestCase
*/ */
public function testForceParameterCanBeSet() public function testForceParameterCanBeSet()
{ {
$response = $this->service->withForce(true); $response = $this->getService()->withForce(true);
$this->assertInstanceOf(ServerDeletionService::class, $response); $this->assertInstanceOf(ServerDeletionService::class, $response);
} }
@ -112,20 +79,22 @@ class ServerDeletionServiceTest extends TestCase
*/ */
public function testServerCanBeDeletedWithoutForce() public function testServerCanBeDeletedWithoutForce()
{ {
$this->daemonServerRepository->shouldReceive('setServer')->with($this->model)->once()->andReturnSelf() $model = factory(Server::class)->make();
->shouldReceive('delete')->withNoArgs()->once()->andReturn(new Response);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); $this->daemonServerRepository->shouldReceive('setServer')->once()->with($model)->andReturnSelf();
$this->databaseRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf() $this->daemonServerRepository->shouldReceive('delete')->once()->withNoArgs()->andReturn(new Response);
->shouldReceive('findWhere')->with([
['server_id', '=', $this->model->id],
])->once()->andReturn(collect([(object) ['id' => 50]]));
$this->databaseManagementService->shouldReceive('delete')->with(50)->once()->andReturnNull(); $this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
$this->repository->shouldReceive('delete')->with($this->model->id)->once()->andReturn(1); $this->databaseRepository->shouldReceive('setColumns')->once()->with('id')->andReturnSelf();
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $this->databaseRepository->shouldReceive('findWhere')->once()->with([
['server_id', '=', $model->id],
])->andReturn(collect([(object) ['id' => 50]]));
$this->service->handle($this->model); $this->databaseManagementService->shouldReceive('delete')->once()->with(50)->andReturnNull();
$this->repository->shouldReceive('delete')->once()->with($model->id)->andReturn(1);
$this->connection->shouldReceive('commit')->once()->withNoArgs()->andReturnNull();
$this->getService()->handle($model);
} }
/** /**
@ -133,64 +102,55 @@ class ServerDeletionServiceTest extends TestCase
*/ */
public function testServerShouldBeDeletedEvenWhenFailureOccursIfForceIsSet() public function testServerShouldBeDeletedEvenWhenFailureOccursIfForceIsSet()
{ {
$this->daemonServerRepository->shouldReceive('setServer')->with($this->model)->once()->andReturnSelf() $this->configureExceptionMock();
->shouldReceive('delete')->withNoArgs()->once()->andThrow($this->exception); $model = factory(Server::class)->make();
$this->daemonServerRepository->shouldReceive('setServer')->once()->with($model)->andReturnSelf();
$this->daemonServerRepository->shouldReceive('delete')->once()->withNoArgs()->andThrow($this->getExceptionMock());
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull(); $this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->databaseRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf() $this->databaseRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf();
->shouldReceive('findWhere')->with([ $this->databaseRepository->shouldReceive('findWhere')->with([
['server_id', '=', $this->model->id], ['server_id', '=', $model->id],
])->once()->andReturn(collect([(object) ['id' => 50]])); ])->once()->andReturn(collect([(object) ['id' => 50]]));
$this->databaseManagementService->shouldReceive('delete')->with(50)->once()->andReturnNull(); $this->databaseManagementService->shouldReceive('delete')->with(50)->once()->andReturnNull();
$this->repository->shouldReceive('delete')->with($this->model->id)->once()->andReturn(1); $this->repository->shouldReceive('delete')->with($model->id)->once()->andReturn(1);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->withForce()->handle($this->model); $this->getService()->withForce()->handle($model);
} }
/** /**
* Test that an exception is thrown if a server cannot be deleted from the node and force is not set. * Test that an exception is thrown if a server cannot be deleted from the node and force is not set.
*
* @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/ */
public function testExceptionShouldBeThrownIfDaemonReturnsAnErrorAndForceIsNotSet() public function testExceptionShouldBeThrownIfDaemonReturnsAnErrorAndForceIsNotSet()
{ {
$this->daemonServerRepository->shouldReceive('setServer->delete')->once()->andThrow($this->exception); $this->configureExceptionMock();
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull(); $model = factory(Server::class)->make();
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
try { $this->daemonServerRepository->shouldReceive('setServer->delete')->once()->andThrow($this->getExceptionMock());
$this->service->handle($this->model);
} catch (Exception $exception) { $this->getService()->handle($model);
$this->assertInstanceOf(DisplayException::class, $exception);
$this->assertEquals(trans('admin/server.exceptions.daemon_exception', [
'code' => 'E_CONN_REFUSED',
]), $exception->getMessage());
}
} }
/** /**
* Test that an integer can be passed in place of the Server model. * Return an instance of the class with mocked dependencies.
*
* @return \Pterodactyl\Services\Servers\ServerDeletionService
*/ */
public function testIntegerCanBePassedInPlaceOfServerModel() private function getService(): ServerDeletionService
{ {
$this->repository->shouldReceive('setColumns')->with(['id', 'node_id', 'uuid'])->once()->andReturnSelf() return new ServerDeletionService(
->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model); $this->connection,
$this->daemonServerRepository,
$this->daemonServerRepository->shouldReceive('setServer')->with($this->model)->once()->andReturnSelf() $this->databaseRepository,
->shouldReceive('delete')->withNoArgs()->once()->andReturn(new Response); $this->databaseManagementService,
$this->repository,
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); $this->writer
$this->databaseRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf() );
->shouldReceive('findWhere')->with([
['server_id', '=', $this->model->id],
])->once()->andReturn(collect([(object) ['id' => 50]]));
$this->databaseManagementService->shouldReceive('delete')->with(50)->once()->andReturnNull();
$this->repository->shouldReceive('delete')->with($this->model->id)->once()->andReturn(1);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->handle($this->model->id);
} }
} }

View file

@ -128,10 +128,13 @@ class VariableValidatorServiceTest extends TestCase
$messages = $exception->validator->getMessageBag()->all(); $messages = $exception->validator->getMessageBag()->all();
$this->assertNotEmpty($messages); $this->assertNotEmpty($messages);
$this->assertSame(1, count($messages)); $this->assertSame(4, count($messages));
for ($i = 0; $i < 4; $i++) {
$this->assertSame(trans('validation.required', [ $this->assertSame(trans('validation.required', [
'attribute' => trans('validation.internal.variable_value', ['env' => $variables[0]->name]), 'attribute' => trans('validation.internal.variable_value', ['env' => $variables[$i]->name]),
]), $messages[0]); ]), $messages[$i]);
}
} }
} }