public static function addConfAfterParamValidation($user, $path, $idc, $main_buss_id, $sub_buss_id, $desc, $node_val) { // 1.check whether node already exist $add_node = NodeServ::getNodeByNodeWhole($path, $main_buss_id); if ($add_node != FALSE) { $current_idc_array = explode(",", $add_node["idc"]); $new_idc_array = explode(",", $idc); $intersect = array_intersect($current_idc_array, $new_idc_array); if (count($intersect) != 0) { return InfoDescUtil::ERR_NODE_EXIST; } } // 2.cascade add node in database mysql_query("BEGIN"); $cur_path = ""; $parent_id = 0; $ret = TRUE; $path = "/" . trim($path, "/"); $node_list = preg_split("/[\\/]+/", trim($path, "/")); for ($i = 0; $i < count($node_list); $i++) { $currentp = $node_list[$i]; $cur_path .= "/" . $node_list[$i]; $exist = NodeServ::checkNode($cur_path, $main_buss_id); if ($exist == FALSE) { if ($i === 0) { // root path $perm_path = $node_list[$i]; QconfMgrLog::err(__FILE__, __LINE__, "root node :{$perm_path} is not exist on {$main_buss_id} "); return InfoDescUtil::ERR_NODE_NOT_EXIST; } else { //normal path if ($i == count($node_list) - 1) { $ret = NodeServ::insert($parent_id, $node_list[$i], InfoDescUtil::NODE_TYPE_NORMAL, $cur_path, $main_buss_id, $sub_buss_id, $idc, $desc); } else { $ret = NodeServ::insert($parent_id, $node_list[$i], InfoDescUtil::NODE_TYPE_NORMAL, $cur_path, $main_buss_id, $sub_buss_id, $idc, ""); } $cur_node = NodeServ::getNodeByNodeWhole($cur_path, $main_buss_id); if ($cur_node != FALSE) { $parent_id = $cur_node["node_id"]; } else { $ret = FALSE; QconfMgrLog::err(__FILE__, __LINE__, "get node with path:{$cur_path} and " . "main_buss_id:{$main_buss_id} after insert node, failed!"); break; } } } else { //already exist $cur_node = NodeServ::getNodeByNodeWhole($cur_path, $main_buss_id); if ($cur_node === FALSE) { QconfMgrLog::err(__FILE__, __LINE__, "get node of path:{$cur_path}, main_buss_id:{$main_buss_id} failed!"); $ret = FALSE; break; } else { // judge node type $cur_node_type = $cur_node["node_type_code"]; if ($cur_node_type == InfoDescUtil::NODE_TYPE_SERV_FATHER && $cur_path != $path) { mysql_query("ROLLBACK"); QconfMgrLog::err(__FILE__, __LINE__, "path:{$cur_path} " . "is already service father type of main_buss_id:{$main_buss_id} "); return InfoDescUtil::ERR_IMPORT_ADD_UNDER_SERV_FATHER; } //modify idc since idc expansion may happened $current_id = $cur_node["node_id"]; $current_idc_array = explode(",", $cur_node["idc"]); $new_idc_array = explode(",", $idc); $set_idc_array = array_merge($current_idc_array, $new_idc_array); $set_idc = implode(",", $set_idc_array); if (NodeServ::modIdc($current_id, $set_idc) === FALSE) { QconfMgrLog::err(__FILE__, __LINE__, "modify Idc:{$set_idc} of node_id:{$current_id} failed!"); } $parent_id = $current_id; } } } // any mysql error happened in current loop if ($ret === FALSE) { mysql_query("ROLLBACK"); //$res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_MYSQL_FAIL); QconfMgrLog::err(__FILE__, __LINE__, "node:{$path} didn't exist in database"); //echo json_encode($res); return InfoDescUtil::ERR_MYSQL_FAIL; } // 3.add node into each idc $idc_list = preg_split("/[,]+/", $idc); $zk_web = new QconfZkWebBase(Log::INFO); foreach ($idc_list as $idc_each) { // certainly exist which has been validation in the CommonArgusProcess function $idc_host = ZkConf::getZkHost($idc_each); $zk_web->connect($idc_host); $qconf_path = PathUtil::getQconfPath($path); $retry_time = 0; while ($retry_time++ < 3 && $zk_web->add_conf($qconf_path, $node_val) === -1) { QconfMgrLog::err(__FILE__, __LINE__, "fail to add conf into zookeeper! retry time {$retry_time}"); } if ($retry_time >= 3) { mysql_query("ROLLBACK"); //$res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_ZOOKEEPER_FAIL); QconfMgrLog::err(__FILE__, __LINE__, "add in zookeeper failed on idc: {$idc_each}"); OpServ::insert(InfoDescUtil::OP_TYPE_ADD, $path, $idc, InfoDescUtil::OP_STATUS_ZOO_FAILED, $main_buss_id, $sub_buss_id, $user, $node_val); //echo json_encode($res); return InfoDescUtil::ERR_ZOOKEEPER_FAIL; } } OpServ::insert(InfoDescUtil::OP_TYPE_ADD, $path, $idc, InfoDescUtil::OP_STATUS_SUCCESS, $main_buss_id, $sub_buss_id, $user, $node_val); mysql_query("COMMIT"); return 0; }
public function addNormalAction() { $main_buss_id = $this->getRequest("main_buss_id", ""); $user_name = $this->getRequest("user_name", ""); $node_whole = trim($this->getRequest("node_whole", "")); $op_user = $this->userInfo["userName"]; if ($main_buss_id === "" || $user_name === "" || $node_whole === "") { $res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_BAD_ARGS); QconfMgrLog::err(__FILE__, __LINE__, "parameter 'main_buss_id' or 'user_name' or 'node_whole' is null!"); echo json_encode($res); return; } // Check whether op_user is admin $is_admin = UserPermServ::checkAdmin($op_user, $main_buss_id); if ($is_admin !== InfoDescUtil::QCONF_OK) { if ($is_admin === FALSE) { $res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_MYSQL_FAIL); } else { $res = InfoDescUtil::getErrorMsg($is_admin); } QconfMgrLog::err(__FILE__, __LINE__, "admin:{$op_user} of main_buss_id:{$main_buss_id} list normals failed!"); echo json_encode($res); return; } // Check whether the $node_whole of $main_buss_id exist $node_whole = "/" . trim($node_whole, "/"); $flags = NodeServ::checkNode($node_whole, $main_buss_id); if ($flags === FALSE) { $res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_NODE_NOT_EXIST); QconfMgrLog::err(__FILE__, __LINE__, "{$node_whole} is not in qconf_node table!"); echo json_encode($res); return; } $flags = UserPermServ::addNormal($op_user, $user_name, $node_whole, $main_buss_id); if ($flags !== InfoDescUtil::QCONF_OK) { OpServ::insert(InfoDescUtil::OP_TYPE_USER_PERM_NORMAL_ADD, $node_whole, "", InfoDescUtil::OP_STATUS_MYSQL_FAILED, $main_buss_id, "NULL", $op_user, "add {$user_name} perm failed!"); if ($flags === FALSE) { $res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_MYSQL_FAIL); } else { $res = InfoDescUtil::getErrorMsg($flags); } QconfMgrLog::err(__FILE__, __LINE__, "admin:{$op_user} of main_buss_id:{$main_buss_id} add user:{$user_name} of node:{$node_whole}; {$flags} failed!"); echo json_encode($res); return; } OpServ::insert(InfoDescUtil::OP_TYPE_USER_PERM_NORMAL_ADD, $node_whole, "", InfoDescUtil::OP_STATUS_SUCCESS, $main_buss_id, "NULL", $op_user, "add {$user_name} perm success!"); $res = array("errno" => "0", "errmsg" => "", "data" => "add normal user success"); $json = json_encode($res); echo $json; }