mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-20 19:14:45 +02:00
parent
7674ee0e2b
commit
f6325c07c4
@ -24,8 +24,8 @@ class NodeDeploymentController extends ApplicationApiController
|
|||||||
$data = $request->validated();
|
$data = $request->validated();
|
||||||
|
|
||||||
$nodes = $this->viableNodesService->handle(
|
$nodes = $this->viableNodesService->handle(
|
||||||
$data['disk'] ?? 0,
|
|
||||||
$data['memory'] ?? 0,
|
$data['memory'] ?? 0,
|
||||||
|
$data['disk'] ?? 0,
|
||||||
$data['cpu'] ?? 0,
|
$data['cpu'] ?? 0,
|
||||||
$data['tags'] ?? $data['location_ids'] ?? [],
|
$data['tags'] ?? $data['location_ids'] ?? [],
|
||||||
);
|
);
|
||||||
|
@ -63,10 +63,6 @@ class Node extends Model
|
|||||||
*/
|
*/
|
||||||
protected $hidden = ['daemon_token_id', 'daemon_token'];
|
protected $hidden = ['daemon_token_id', 'daemon_token'];
|
||||||
|
|
||||||
public int $sum_memory;
|
|
||||||
public int $sum_disk;
|
|
||||||
public int $sum_cpu;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fields that are mass assignable.
|
* Fields that are mass assignable.
|
||||||
*/
|
*/
|
||||||
@ -250,11 +246,28 @@ class Node extends Model
|
|||||||
*/
|
*/
|
||||||
public function isViable(int $memory, int $disk, int $cpu): bool
|
public function isViable(int $memory, int $disk, int $cpu): bool
|
||||||
{
|
{
|
||||||
|
if ($this->memory_overallocate >= 0) {
|
||||||
$memoryLimit = $this->memory * (1 + ($this->memory_overallocate / 100));
|
$memoryLimit = $this->memory * (1 + ($this->memory_overallocate / 100));
|
||||||
$diskLimit = $this->disk * (1 + ($this->disk_overallocate / 100));
|
if ($this->servers_sum_memory + $memory > $memoryLimit) {
|
||||||
$cpuLimit = $this->cpu * (1 + ($this->cpu_overallocate / 100));
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return ($this->sum_memory + $memory) <= $memoryLimit && ($this->sum_disk + $disk) <= $diskLimit && ($this->sum_cpu + $cpu) <= $cpuLimit;
|
if ($this->disk_overallocate >= 0) {
|
||||||
|
$diskLimit = $this->disk * (1 + ($this->disk_overallocate / 100));
|
||||||
|
if ($this->servers_sum_disk + $disk > $diskLimit) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->cpu_overallocate >= 0) {
|
||||||
|
$cpuLimit = $this->cpu * (1 + ($this->cpu_overallocate / 100));
|
||||||
|
if ($this->servers_sum_cpu + $cpu > $cpuLimit) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getForServerCreation()
|
public static function getForServerCreation()
|
||||||
|
@ -17,19 +17,17 @@ class FindViableNodesService
|
|||||||
* are tossed out, as are any nodes marked as non-public, meaning automatic
|
* are tossed out, as are any nodes marked as non-public, meaning automatic
|
||||||
* deployments should not be done against them.
|
* deployments should not be done against them.
|
||||||
*/
|
*/
|
||||||
public function handle(int $disk = 0, int $memory = 0, int $cpu = 0, $tags = []): Collection
|
public function handle(int $memory = 0, int $disk = 0, int $cpu = 0, $tags = []): Collection
|
||||||
{
|
{
|
||||||
$nodes = Node::query()
|
$nodes = Node::query()
|
||||||
->withSum('servers', 'disk')
|
|
||||||
->withSum('servers', 'memory')
|
->withSum('servers', 'memory')
|
||||||
|
->withSum('servers', 'disk')
|
||||||
->withSum('servers', 'cpu')
|
->withSum('servers', 'cpu')
|
||||||
->where('public', true)
|
->where('public', true)
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
return $nodes
|
return $nodes
|
||||||
->filter(fn (Node $node) => !$tags || collect($node->tags)->intersect($tags))
|
->filter(fn (Node $node) => !$tags || collect($node->tags)->intersect($tags))
|
||||||
->filter(fn (Node $node) => $node->servers_sum_disk + $disk <= $node->disk * (1 + $node->disk_overallocate / 100))
|
->filter(fn (Node $node) => $node->isViable($memory, $disk, $cpu));
|
||||||
->filter(fn (Node $node) => $node->servers_sum_memory + $memory <= $node->memory * (1 + $node->memory_overallocate / 100))
|
|
||||||
->filter(fn (Node $node) => $node->servers_sum_cpu + $cpu <= $node->cpu * (1 + $node->cpu_overallocate / 100));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -109,8 +109,8 @@ class ServerCreationService
|
|||||||
{
|
{
|
||||||
/** @var Collection<\App\Models\Node> $nodes */
|
/** @var Collection<\App\Models\Node> $nodes */
|
||||||
$nodes = $this->findViableNodesService->handle(
|
$nodes = $this->findViableNodesService->handle(
|
||||||
Arr::get($data, 'disk', 0),
|
|
||||||
Arr::get($data, 'memory', 0),
|
Arr::get($data, 'memory', 0),
|
||||||
|
Arr::get($data, 'disk', 0),
|
||||||
Arr::get($data, 'cpu', 0),
|
Arr::get($data, 'cpu', 0),
|
||||||
Arr::get($data, 'tags', []),
|
Arr::get($data, 'tags', []),
|
||||||
);
|
);
|
||||||
|
@ -58,7 +58,9 @@ class TransferServerService
|
|||||||
// Check if the node is viable for the transfer.
|
// Check if the node is viable for the transfer.
|
||||||
$node = Node::query()
|
$node = Node::query()
|
||||||
->select(['nodes.id', 'nodes.fqdn', 'nodes.scheme', 'nodes.daemon_token', 'nodes.daemon_listen', 'nodes.memory', 'nodes.disk', 'nodes.cpu', 'nodes.memory_overallocate', 'nodes.disk_overallocate', 'nodes.cpu_overallocate'])
|
->select(['nodes.id', 'nodes.fqdn', 'nodes.scheme', 'nodes.daemon_token', 'nodes.daemon_listen', 'nodes.memory', 'nodes.disk', 'nodes.cpu', 'nodes.memory_overallocate', 'nodes.disk_overallocate', 'nodes.cpu_overallocate'])
|
||||||
->selectRaw('IFNULL(SUM(servers.memory), 0) as sum_memory, IFNULL(SUM(servers.disk), 0) as sum_disk, IFNULL(SUM(servers.cpu), 0) as sum_cpu')
|
->withSum('servers', 'disk')
|
||||||
|
->withSum('servers', 'memory')
|
||||||
|
->withSum('servers', 'cpu')
|
||||||
->leftJoin('servers', 'servers.node_id', '=', 'nodes.id')
|
->leftJoin('servers', 'servers.node_id', '=', 'nodes.id')
|
||||||
->where('nodes.id', $node_id)
|
->where('nodes.id', $node_id)
|
||||||
->first();
|
->first();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user