Cleanup allocation repository

This commit is contained in:
Dane Everitt 2020-09-13 12:47:05 -07:00
parent 9410a54c98
commit 7b57d65edf
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 27 additions and 79 deletions

View file

@ -6,14 +6,6 @@ use Illuminate\Support\Collection;
interface AllocationRepositoryInterface extends RepositoryInterface interface AllocationRepositoryInterface extends RepositoryInterface
{ {
/**
* Return all of the unique IPs that exist for a given node.
*
* @param int $node
* @return \Illuminate\Support\Collection
*/
public function getUniqueAllocationIpsForNode(int $node): Collection;
/** /**
* Return all of the allocations that exist for a node that are not currently * Return all of the allocations that exist for a node that are not currently
* allocated. * allocated.
@ -23,27 +15,6 @@ interface AllocationRepositoryInterface extends RepositoryInterface
*/ */
public function getUnassignedAllocationIds(int $node): array; public function getUnassignedAllocationIds(int $node): array;
/**
* Get an array of all allocations that are currently assigned to a given server.
*
* @param int $server
* @return array
*/
public function getAssignedAllocationIds(int $server): array;
/**
* 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.
*
* @param array $nodes
* @return array
*/
public function getDiscardableDedicatedAllocations(array $nodes = []): array;
/** /**
* Return a single allocation from those meeting the requirements. * Return a single allocation from those meeting the requirements.
* *

View file

@ -5,6 +5,7 @@ namespace Pterodactyl\Http\Controllers\Admin\Nodes;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Pterodactyl\Models\Node; use Pterodactyl\Models\Node;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Pterodactyl\Models\Allocation;
use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\Factory;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Repositories\Eloquent\NodeRepository; use Pterodactyl\Repositories\Eloquent\NodeRepository;
@ -134,7 +135,10 @@ class NodeViewController extends Controller
return $this->view->make('admin.nodes.view.allocation', [ return $this->view->make('admin.nodes.view.allocation', [
'node' => $node, 'node' => $node,
'allocations' => $this->allocationRepository->setColumns(['ip'])->getUniqueAllocationIpsForNode($node->id), 'allocations' => Allocation::query()->where('node_id', $node->id)
->groupBy('ip')
->orderByRaw('INET_ATON(ip) ASC')
->get(['ip']),
]); ]);
} }

View file

@ -2,7 +2,6 @@
namespace Pterodactyl\Repositories\Eloquent; namespace Pterodactyl\Repositories\Eloquent;
use Illuminate\Support\Collection;
use Pterodactyl\Models\Allocation; use Pterodactyl\Models\Allocation;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
@ -19,20 +18,6 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos
return Allocation::class; return Allocation::class;
} }
/**
* Return all of the unique IPs that exist for a given node.
*
* @param int $node
* @return \Illuminate\Support\Collection
*/
public function getUniqueAllocationIpsForNode(int $node): Collection
{
return $this->getBuilder()->where('node_id', $node)
->groupBy('ip')
->orderByRaw('INET_ATON(ip) ASC')
->get($this->getColumns());
}
/** /**
* Return all of the allocations that exist for a node that are not currently * Return all of the allocations that exist for a node that are not currently
* allocated. * allocated.
@ -42,22 +27,12 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos
*/ */
public function getUnassignedAllocationIds(int $node): array public function getUnassignedAllocationIds(int $node): array
{ {
$results = $this->getBuilder()->select('id')->whereNull('server_id')->where('node_id', $node)->get(); return Allocation::query()->select('id')
->whereNull('server_id')
return $results->pluck('id')->toArray(); ->where('node_id', $node)
} ->get()
->pluck('id')
/** ->toArray();
* Get an array of all allocations that are currently assigned to a given server.
*
* @param int $server
* @return array
*/
public function getAssignedAllocationIds(int $server): array
{
$results = $this->getBuilder()->select('id')->where('server_id', $server)->get();
return $results->pluck('id')->toArray();
} }
/** /**
@ -71,21 +46,19 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos
* @param array $nodes * @param array $nodes
* @return array * @return array
*/ */
public function getDiscardableDedicatedAllocations(array $nodes = []): array protected function getDiscardableDedicatedAllocations(array $nodes = []): array
{ {
$instance = $this->getBuilder()->select( $query = Allocation::query()->selectRaw('CONCAT_WS("-", node_id, ip) as result');
$this->getBuilder()->raw('CONCAT_WS("-", node_id, ip) as result')
);
if (! empty($nodes)) { if (! empty($nodes)) {
$instance->whereIn('node_id', $nodes); $query->whereIn('node_id', $nodes);
} }
$results = $instance->whereNotNull('server_id') return $query->whereNotNull('server_id')
->groupBy($this->getBuilder()->raw('CONCAT(node_id, ip)')) ->groupByRaw('CONCAT(node_id, ip)')
->get(); ->get()
->pluck('result')
return $results->pluck('result')->toArray(); ->toArray();
} }
/** /**
@ -98,18 +71,18 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos
*/ */
public function getRandomAllocation(array $nodes, array $ports, bool $dedicated = false) public function getRandomAllocation(array $nodes, array $ports, bool $dedicated = false)
{ {
$instance = $this->getBuilder()->whereNull('server_id'); $query = Allocation::query()->whereNull('server_id');
if (! empty($nodes)) { if (! empty($nodes)) {
$instance->whereIn('node_id', $nodes); $query->whereIn('node_id', $nodes);
} }
if (! empty($ports)) { if (! empty($ports)) {
$instance->where(function (Builder $query) use ($ports) { $query->where(function (Builder $inner) use ($ports) {
$whereIn = []; $whereIn = [];
foreach ($ports as $port) { foreach ($ports as $port) {
if (is_array($port)) { if (is_array($port)) {
$query->orWhereBetween('port', $port); $inner->orWhereBetween('port', $port);
continue; continue;
} }
@ -117,7 +90,7 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos
} }
if (! empty($whereIn)) { if (! empty($whereIn)) {
$query->orWhereIn('port', $whereIn); $inner->orWhereIn('port', $whereIn);
} }
}); });
} }
@ -128,12 +101,12 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos
$discard = $this->getDiscardableDedicatedAllocations($nodes); $discard = $this->getDiscardableDedicatedAllocations($nodes);
if (! empty($discard)) { if (! empty($discard)) {
$instance->whereNotIn( $query->whereNotIn(
$this->getBuilder()->raw('CONCAT_WS("-", node_id, ip)'), $discard $this->getBuilder()->raw('CONCAT_WS("-", node_id, ip)'), $discard
); );
} }
} }
return $instance->inRandomOrder()->first(); return $query->inRandomOrder()->first();
} }
} }

View file

@ -159,7 +159,7 @@ class BuildModificationService
// Handle removal of allocations from this server. // Handle removal of allocations from this server.
if (array_key_exists('remove_allocations', $data) && ! empty($data['remove_allocations'])) { if (array_key_exists('remove_allocations', $data) && ! empty($data['remove_allocations'])) {
$assigned = $this->allocationRepository->getAssignedAllocationIds($server->id); $assigned = $server->allocations->pluck('id')->toArray();
$updateIds = []; $updateIds = [];
foreach ($data['remove_allocations'] as $allocation) { foreach ($data['remove_allocations'] as $allocation) {