BUG内容描述
给用户设置了CPU数量和内存大小限制后,该用户登入无法正常启动节点。
例如给test用户设置CPU最大数量:2个,内存最大大小:2000MB。 该用户在LAB中创建一个CPU数量:2,内存:2000MB的设备,点击启动后提示“错误信息CPU或内存超出限制”
创建一个2CPU,2000MB内存的设备
提示达到限制数量
临时修复方案
执行以下命令后替换指定内容
cp /opt/unetlab/html/includes/__node.php /opt/unetlab/html/includes/__node.php.back
vim /opt/unetlab/html/includes/__node.php
将updateRunning_start
方法中的以下内容进行替换
原始代码
if($checkMaxCpu){
$get = 'SELECT SUM(' . NODE_CPU . ') as consume_cpu FROM ' . NODE_SESSIONS_TABLE . ' WHERE ' . NODE_SESSION_POD . ' = :pod AND ' . NODE_SESSION_RUNNING . ' = 1';
$statement = $db->prepare($get);
$statement->execute([
'pod' => $pod
]);
$result = $statement->fetch(PDO::FETCH_ASSOC);
$consumeCpu = $result['consume_cpu'];
if( ($cpu + $consumeCpu ) > $hostLab[USER_MAX_CPU] ) throw new Exception('max cpu limit reached');
$query = 'UPDATE ' . NODE_SESSIONS_TABLE . ' SET ' .NODE_SESSION_RUNNING. '= :node_session_running,' . NODE_CPU . '=:cpu,' . NODE_RAM . '=:ram WHERE ' . NODE_SESSION_ID . '=:node_session_id';
$update = $db->prepare($query);
$update->execute([
'node_session_running' => $state,
'node_session_id' => $session_id,
'cpu' => $cpu,
'ram' => $ram,
]);
}
if($checkMaxRam){
$get = 'SELECT SUM(' . NODE_RAM . ') as consume_ram FROM ' . NODE_SESSIONS_TABLE . ' WHERE ' . NODE_SESSION_POD . ' = :pod AND ' . NODE_SESSION_RUNNING . ' = 1';
$statement = $db->prepare($get);
$statement->execute([
'pod' => $pod
]);
$result = $statement->fetch(PDO::FETCH_ASSOC);
$consumeRam = $result['consume_ram'];
if(($ram + $consumeRam ) > $hostLab[USER_MAX_RAM]) throw new Exception('max ram limit reached');
$query = 'UPDATE ' . NODE_SESSIONS_TABLE . ' SET ' .NODE_SESSION_RUNNING. '= :node_session_running,' . NODE_CPU . '=:cpu,' . NODE_RAM . '=:ram WHERE ' . NODE_SESSION_ID . '=:node_session_id';
$update = $db->prepare($query);
$update->execute([
'node_session_running' => $state,
'node_session_id' => $session_id,
'cpu' => $cpu,
'ram' => $ram,
]);
}
替换代码
//临时修复
if($checkMaxCpu && $checkMaxRam){
//获取当前用户运行节点的CPU使用个数和内存使用大小
$get = 'SELECT SUM(' . NODE_CPU . ') as consume_cpu, SUM(' . NODE_RAM . ') as consume_ram FROM ' . NODE_SESSIONS_TABLE . ' WHERE ' . NODE_SESSION_POD . ' = :pod AND ' . NODE_SESSION_RUNNING . ' = 1';
$statement = $db->prepare($get);
$statement->execute([
'pod' => $pod
]);
$result = $statement->fetch(PDO::FETCH_ASSOC);
//当前用户运行节点的CPU使用个数
$consumeCpu = $result['consume_cpu'];
//当前用户运行节点的内存使用大小
$consumeRam = $result['consume_ram'];
//判断CPU和内存是否超过最大限制数量
if( ($cpu + $consumeCpu ) > $hostLab[USER_MAX_CPU] ) throw new Exception('max cpu limit reached');
if( ($ram + $consumeRam ) > $hostLab[USER_MAX_RAM] ) throw new Exception('max ram limit reached');
//更新数据库
$query = 'UPDATE ' . NODE_SESSIONS_TABLE . ' SET ' .NODE_SESSION_RUNNING. '= :node_session_running,' . NODE_CPU . '=:cpu,' . NODE_RAM . '=:ram WHERE ' . NODE_SESSION_ID . '=:node_session_id';
$update = $db->prepare($query);
$update->execute([
'node_session_running' => $state,
'node_session_id' => $session_id,
'cpu' => $cpu,
'ram' => $ram,
]);
}
else if($checkMaxCpu){
$get = 'SELECT SUM(' . NODE_CPU . ') as consume_cpu FROM ' . NODE_SESSIONS_TABLE . ' WHERE ' . NODE_SESSION_POD . ' = :pod AND ' . NODE_SESSION_RUNNING . ' = 1';
$statement = $db->prepare($get);
$statement->execute([
'pod' => $pod
]);
$result = $statement->fetch(PDO::FETCH_ASSOC);
$consumeCpu = $result['consume_cpu'];
if( ($cpu + $consumeCpu ) > $hostLab[USER_MAX_CPU] ) throw new Exception('max cpu limit reached');
$query = 'UPDATE ' . NODE_SESSIONS_TABLE . ' SET ' .NODE_SESSION_RUNNING. '= :node_session_running,' . NODE_CPU . '=:cpu,' . NODE_RAM . '=:ram WHERE ' . NODE_SESSION_ID . '=:node_session_id';
$update = $db->prepare($query);
$update->execute([
'node_session_running' => $state,
'node_session_id' => $session_id,
'cpu' => $cpu,
'ram' => $ram,
]);
}
else if($checkMaxRam){
$get = 'SELECT SUM(' . NODE_RAM . ') as consume_ram FROM ' . NODE_SESSIONS_TABLE . ' WHERE ' . NODE_SESSION_POD . ' = :pod AND ' . NODE_SESSION_RUNNING . ' = 1';
$statement = $db->prepare($get);
$statement->execute([
'pod' => $pod
]);
$result = $statement->fetch(PDO::FETCH_ASSOC);
$consumeRam = $result['consume_ram'];
if(($ram + $consumeRam ) > $hostLab[USER_MAX_RAM]) throw new Exception('max ram limit reached');
$query = 'UPDATE ' . NODE_SESSIONS_TABLE . ' SET ' .NODE_SESSION_RUNNING. '= :node_session_running,' . NODE_CPU . '=:cpu,' . NODE_RAM . '=:ram WHERE ' . NODE_SESSION_ID . '=:node_session_id';
$update = $db->prepare($query);
$update->execute([
'node_session_running' => $state,
'node_session_id' => $session_id,
'cpu' => $cpu,
'ram' => $ram,
]);
}