Replace allocation repository
This commit is contained in:
parent
860b2d890b
commit
cd49324d46
10 changed files with 95 additions and 164 deletions
|
@ -2,10 +2,10 @@
|
|||
|
||||
namespace Pterodactyl\Services\Deployment;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
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
|
||||
|
@ -16,13 +16,6 @@ class AllocationSelectionService
|
|||
|
||||
protected array $ports = [];
|
||||
|
||||
/**
|
||||
* AllocationSelectionService constructor.
|
||||
*/
|
||||
public function __construct(private AllocationRepositoryInterface $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
|
||||
|
@ -84,7 +77,7 @@ class AllocationSelectionService
|
|||
*/
|
||||
public function handle(): Allocation
|
||||
{
|
||||
$allocation = $this->repository->getRandomAllocation($this->nodes, $this->ports, $this->dedicated);
|
||||
$allocation = $this->getRandomAllocation($this->nodes, $this->ports, $this->dedicated);
|
||||
|
||||
if (is_null($allocation)) {
|
||||
throw new NoViableAllocationException(trans('exceptions.deployment.no_viable_allocations'));
|
||||
|
@ -92,4 +85,72 @@ class AllocationSelectionService
|
|||
|
||||
return $allocation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a single allocation from those meeting the requirements.
|
||||
*/
|
||||
private function getRandomAllocation(array $nodes = [], array $ports = [], bool $dedicated = false): ?Allocation
|
||||
{
|
||||
$query = Allocation::query()->whereNull('server_id');
|
||||
|
||||
if (!empty($nodes)) {
|
||||
$query->whereIn('node_id', $nodes);
|
||||
}
|
||||
|
||||
if (!empty($ports)) {
|
||||
$query->where(function (Builder $inner) use ($ports) {
|
||||
$whereIn = [];
|
||||
foreach ($ports as $port) {
|
||||
if (is_array($port)) {
|
||||
$inner->orWhereBetween('port', $port);
|
||||
continue;
|
||||
}
|
||||
|
||||
$whereIn[] = $port;
|
||||
}
|
||||
|
||||
if (!empty($whereIn)) {
|
||||
$inner->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)) {
|
||||
$query->whereNotIn(
|
||||
$this->getBuilder()->raw('CONCAT_WS("-", node_id, ip)'),
|
||||
$discard
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $query->inRandomOrder()->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a concatenated 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.
|
||||
*/
|
||||
private function getDiscardableDedicatedAllocations(array $nodes = []): array
|
||||
{
|
||||
$query = Allocation::query()->selectRaw('CONCAT_WS("-", node_id, ip) as result');
|
||||
|
||||
if (!empty($nodes)) {
|
||||
$query->whereIn('node_id', $nodes);
|
||||
}
|
||||
|
||||
return $query->whereNotNull('server_id')
|
||||
->groupByRaw('CONCAT(node_id, ip)')
|
||||
->get()
|
||||
->pluck('result')
|
||||
->toArray();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue