Merge pull request #526 from SahrulGnwn/development

Fix checking free resource node
This commit is contained in:
Dennis 2022-08-11 17:27:45 +02:00 committed by GitHub
commit 66de5f6474
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 22 deletions

View file

@ -308,6 +308,12 @@ class Pterodactyl
return $response->json()['attributes']; return $response->json()['attributes'];
} }
/**
* Update Server Resources
* @param Server $server
* @param Product $product
* @return boolean
*/
public static function updateServer(Server $server, Product $product) public static function updateServer(Server $server, Product $product)
{ {
return self::client()->patch("/application/servers/{$server->pterodactyl_id}/build", [ return self::client()->patch("/application/servers/{$server->pterodactyl_id}/build", [
@ -325,11 +331,42 @@ class Pterodactyl
] ]
]); ]);
} }
/**
* Power Action Specific Server
* @param Server $server
* @param string $action
* @return boolean
*/
public static function powerAction(Server $server, $action) public static function powerAction(Server $server, $action)
{ {
return self::clientAdmin()->post("/client/servers/{$server->identifier}/power", [ return self::clientAdmin()->post("/client/servers/{$server->identifier}/power", [
"signal" => $action "signal" => $action
]); ]);
} }
/**
* Check if node has enough free resources to allocate the given resources
* @param Node $node
* @param int $requireMemory
* @param int $requireDisk
* @return boolean
*/
public static function checkNodeResources(Node $node, int $requireMemory, int $requireDisk)
{
try {
$response = self::client()->get("/application/nodes/{$node->id}");
} catch (Exception $e) {
throw self::getException($e->getMessage());
}
$node = $response['attributes'];
$freeMemory = $node['memory'] - $node['allocated_resources']['memory'];
$freeDisk = $node['disk'] - $node['allocated_resources']['disk'];
if ($freeMemory < $requireMemory) {
return false;
}
if ($freeDisk < $requireDisk) {
return false;
}
return true;
}
} }

View file

@ -104,27 +104,15 @@ class ServerController extends Controller
// minimum credits && Check for Allocation // minimum credits && Check for Allocation
if (FacadesRequest::has("product")) { if (FacadesRequest::has("product")) {
$product = Product::findOrFail(FacadesRequest::input("product")); $product = Product::findOrFail(FacadesRequest::input("product"));
// Can we allocate the node? If not then error lol(I don't like overallocation)
$node = Pterodactyl::getNode(FacadesRequest::input('node')); // Get node resource allocation info
$nodeMem = $node['memory']; $node = $product->nodes()->findOrFail(FacadesRequest::input('node'));
$nodeDisk = $node['disk']; $nodeName = $node->name;
$nodeName = $node['name'];
$currServers = Auth::user()->servers(); // Check if node has enough memory and disk space
$currMem = 0; $checkResponse = Pterodactyl::checkNodeResources($node, $product->memory, $product->disk);
$currDisk = 0; if ($checkResponse == False) return redirect()->route('servers.index')->with('error', __("The node '" . $nodeName . "' doesn't have the required memory or disk left to allocate this product."));
foreach($currServers as $currServer) {
$pteroServer = $currServer->getPterodactylServer();
$psvAttr = $pteroServer['attributes'];
if($psvAttr['node'] != $node['id'])
continue;
$currMem += $psvAttr['limits']['memory'];
$currDisk += $psvAttr['limits']['disk'];
}
$currMem += $product->memory;
$currDisk += $product->disk;
if($currMem > $nodeMem || $currDisk > $nodeDisk)
return redirect()->route('servers.index')->with('error', "The node '" . $nodeName . "' doesn't have the required memory or disk left to allocate this product.");
// Min. Credits // Min. Credits
if ( if (
Auth::user()->credits < Auth::user()->credits <