Merge pull request #526 from SahrulGnwn/development
Fix checking free resource node
This commit is contained in:
commit
66de5f6474
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -104,26 +104,14 @@ 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 (
|
||||||
|
|
Loading…
Reference in a new issue