Merge pull request #526 from SahrulGnwn/development
Fix checking free resource node
This commit is contained in:
commit
66de5f6474
2 changed files with 47 additions and 22 deletions
|
@ -308,6 +308,12 @@ class Pterodactyl
|
|||
return $response->json()['attributes'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update Server Resources
|
||||
* @param Server $server
|
||||
* @param Product $product
|
||||
* @return boolean
|
||||
*/
|
||||
public static function updateServer(Server $server, Product $product)
|
||||
{
|
||||
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)
|
||||
{
|
||||
return self::clientAdmin()->post("/client/servers/{$server->identifier}/power", [
|
||||
"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,27 +104,15 @@ class ServerController extends Controller
|
|||
// minimum credits && Check for Allocation
|
||||
if (FacadesRequest::has("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'));
|
||||
$nodeMem = $node['memory'];
|
||||
$nodeDisk = $node['disk'];
|
||||
$nodeName = $node['name'];
|
||||
$currServers = Auth::user()->servers();
|
||||
$currMem = 0;
|
||||
$currDisk = 0;
|
||||
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.");
|
||||
|
||||
|
||||
// Get node resource allocation info
|
||||
$node = $product->nodes()->findOrFail(FacadesRequest::input('node'));
|
||||
$nodeName = $node->name;
|
||||
|
||||
// Check if node has enough memory and disk space
|
||||
$checkResponse = Pterodactyl::checkNodeResources($node, $product->memory, $product->disk);
|
||||
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."));
|
||||
|
||||
// Min. Credits
|
||||
if (
|
||||
Auth::user()->credits <
|
||||
|
|
Loading…
Reference in a new issue