misc_pterodactyl-panel/app/Repositories/Eloquent/AllocationRepository.php

101 lines
2.9 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\Allocation;
use Illuminate\Database\Eloquent\Builder;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
class AllocationRepository extends EloquentRepository implements AllocationRepositoryInterface
{
/**
* Return the model backing this repository.
*/
public function model(): string
{
return Allocation::class;
}
2018-01-21 22:02:03 +00:00
/**
* Return all the allocations that exist for a node that are not currently
2018-01-21 22:02:03 +00:00
* allocated.
*/
public function getUnassignedAllocationIds(int $node): array
{
2020-09-13 19:47:05 +00:00
return Allocation::query()->select('id')
->whereNull('server_id')
->where('node_id', $node)
->get()
->pluck('id')
->toArray();
2018-01-21 22:02:03 +00:00
}
/**
2018-05-13 14:50:56 +00:00
* 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.
*/
2020-09-13 19:47:05 +00:00
protected function getDiscardableDedicatedAllocations(array $nodes = []): array
{
$query = Allocation::query()->selectRaw('CONCAT_WS(\'-\', node_id, ip) as result');
2021-01-23 20:33:34 +00:00
if (!empty($nodes)) {
2020-09-13 19:47:05 +00:00
$query->whereIn('node_id', $nodes);
}
2020-09-13 19:47:05 +00:00
return $query->whereNotNull('server_id')
->groupByRaw('result')
2020-09-13 19:47:05 +00:00
->get()
->pluck('result')
->toArray();
}
/**
* Return a single allocation from those meeting the requirements.
*/
public function getRandomAllocation(array $nodes, array $ports, bool $dedicated = false): ?Allocation
{
2020-09-13 19:47:05 +00:00
$query = Allocation::query()->whereNull('server_id');
2021-01-23 20:33:34 +00:00
if (!empty($nodes)) {
2020-09-13 19:47:05 +00:00
$query->whereIn('node_id', $nodes);
}
2021-01-23 20:33:34 +00:00
if (!empty($ports)) {
2020-09-13 19:47:05 +00:00
$query->where(function (Builder $inner) use ($ports) {
$whereIn = [];
foreach ($ports as $port) {
if (is_array($port)) {
2020-09-13 19:47:05 +00:00
$inner->orWhereBetween('port', $port);
continue;
}
$whereIn[] = $port;
}
2021-01-23 20:33:34 +00:00
if (!empty($whereIn)) {
2020-09-13 19:47:05 +00:00
$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);
2021-01-23 20:33:34 +00:00
if (!empty($discard)) {
2020-09-13 19:47:05 +00:00
$query->whereNotIn(
$this->getBuilder()->raw('CONCAT_WS(\'-\', node_id, ip)'),
2021-01-23 20:33:34 +00:00
$discard
);
}
}
2020-09-13 19:47:05 +00:00
return $query->inRandomOrder()->first();
}
}