Cleanup allocation repository
This commit is contained in:
parent
9410a54c98
commit
7b57d65edf
4 changed files with 27 additions and 79 deletions
|
@ -6,14 +6,6 @@ use Illuminate\Support\Collection;
|
|||
|
||||
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
|
||||
* allocated.
|
||||
|
@ -23,27 +15,6 @@ interface AllocationRepositoryInterface extends RepositoryInterface
|
|||
*/
|
||||
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.
|
||||
*
|
||||
|
|
|
@ -5,6 +5,7 @@ namespace Pterodactyl\Http\Controllers\Admin\Nodes;
|
|||
use Illuminate\Http\Request;
|
||||
use Pterodactyl\Models\Node;
|
||||
use Illuminate\Support\Collection;
|
||||
use Pterodactyl\Models\Allocation;
|
||||
use Illuminate\Contracts\View\Factory;
|
||||
use Pterodactyl\Http\Controllers\Controller;
|
||||
use Pterodactyl\Repositories\Eloquent\NodeRepository;
|
||||
|
@ -134,7 +135,10 @@ class NodeViewController extends Controller
|
|||
|
||||
return $this->view->make('admin.nodes.view.allocation', [
|
||||
'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']),
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace Pterodactyl\Repositories\Eloquent;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Pterodactyl\Models\Allocation;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
||||
|
@ -19,20 +18,6 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos
|
|||
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
|
||||
* allocated.
|
||||
|
@ -42,22 +27,12 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos
|
|||
*/
|
||||
public function getUnassignedAllocationIds(int $node): array
|
||||
{
|
||||
$results = $this->getBuilder()->select('id')->whereNull('server_id')->where('node_id', $node)->get();
|
||||
|
||||
return $results->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();
|
||||
return Allocation::query()->select('id')
|
||||
->whereNull('server_id')
|
||||
->where('node_id', $node)
|
||||
->get()
|
||||
->pluck('id')
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -71,21 +46,19 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos
|
|||
* @param array $nodes
|
||||
* @return array
|
||||
*/
|
||||
public function getDiscardableDedicatedAllocations(array $nodes = []): array
|
||||
protected function getDiscardableDedicatedAllocations(array $nodes = []): array
|
||||
{
|
||||
$instance = $this->getBuilder()->select(
|
||||
$this->getBuilder()->raw('CONCAT_WS("-", node_id, ip) as result')
|
||||
);
|
||||
$query = Allocation::query()->selectRaw('CONCAT_WS("-", node_id, ip) as result');
|
||||
|
||||
if (! empty($nodes)) {
|
||||
$instance->whereIn('node_id', $nodes);
|
||||
$query->whereIn('node_id', $nodes);
|
||||
}
|
||||
|
||||
$results = $instance->whereNotNull('server_id')
|
||||
->groupBy($this->getBuilder()->raw('CONCAT(node_id, ip)'))
|
||||
->get();
|
||||
|
||||
return $results->pluck('result')->toArray();
|
||||
return $query->whereNotNull('server_id')
|
||||
->groupByRaw('CONCAT(node_id, ip)')
|
||||
->get()
|
||||
->pluck('result')
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -98,18 +71,18 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos
|
|||
*/
|
||||
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)) {
|
||||
$instance->whereIn('node_id', $nodes);
|
||||
$query->whereIn('node_id', $nodes);
|
||||
}
|
||||
|
||||
if (! empty($ports)) {
|
||||
$instance->where(function (Builder $query) use ($ports) {
|
||||
$query->where(function (Builder $inner) use ($ports) {
|
||||
$whereIn = [];
|
||||
foreach ($ports as $port) {
|
||||
if (is_array($port)) {
|
||||
$query->orWhereBetween('port', $port);
|
||||
$inner->orWhereBetween('port', $port);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -117,7 +90,7 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos
|
|||
}
|
||||
|
||||
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);
|
||||
|
||||
if (! empty($discard)) {
|
||||
$instance->whereNotIn(
|
||||
$query->whereNotIn(
|
||||
$this->getBuilder()->raw('CONCAT_WS("-", node_id, ip)'), $discard
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $instance->inRandomOrder()->first();
|
||||
return $query->inRandomOrder()->first();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -159,7 +159,7 @@ class BuildModificationService
|
|||
|
||||
// Handle removal of allocations from this server.
|
||||
if (array_key_exists('remove_allocations', $data) && ! empty($data['remove_allocations'])) {
|
||||
$assigned = $this->allocationRepository->getAssignedAllocationIds($server->id);
|
||||
$assigned = $server->allocations->pluck('id')->toArray();
|
||||
|
||||
$updateIds = [];
|
||||
foreach ($data['remove_allocations'] as $allocation) {
|
||||
|
|
Loading…
Reference in a new issue