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

@ -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;
}
}