public function createAction()
 {
     $node_id = $this->getRequest("node_id", "");
     $idc = $this->getRequest("idc", "");
     $op_user = $this->userInfo["userName"];
     // 1. parameter null
     if ($node_id === "" || $idc === "") {
         $res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_BAD_ARGS);
         QconfMgrLog::err(__FILE__, __LINE__, "paramter: 'path', 'main_buss_id' or 'idc' are required!");
         echo json_encode($res);
         return;
     }
     // 2. node is exist
     $node = NodeServ::getNode($node_id);
     if ($node === FALSE) {
         $res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_NODE_NOT_EXIST);
         QconfMgrLog::err(__FILE__, __LINE__, "node of node_id({$node_id}) not exist!");
         echo json_encode($res);
         return;
     }
     // 3. node level is smaller than 2
     $node_level = $node["node_level"];
     if ($node_level < 2) {
         $res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_SNAPSHOT_NODE_LEVEL_ERROR);
         QconfMgrLog::err(__FILE__, __LINE__, "snapshot mustn't be created on root node!");
         echo json_encode($res);
         return;
     }
     // 4. already exist snapshot
     $node_whole = $node["node_whole"];
     //[USERPERM CHECK]
     $userperm_ret = UserPermServ::checkPerm($op_user, $node_whole);
     if ($userperm_ret === FALSE) {
         QconfMgrLog::err(__FILE__, __LINE__, "Insufficient permission of {$op_user} on node {$node_whole}");
         $res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_USER_PERM_PERMISSION_DENIED);
         echo json_encode($res);
         return;
     }
     $main_buss_id = $node["main_buss_id"];
     $existed = SnapShotUtil::existSnapShotForSnapShot($main_buss_id, $node_whole, $snapshot_path);
     if ($existed === FALSE) {
         $res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_SNAPSHOT_MODULE_FAILED);
         QconfMgrLog::err(__FILE__, __LINE__, "check snapshot existed failed");
         echo json_encode($res);
         return;
     }
     if ($existed === InfoDescUtil::SNAPSHOT_FILE_EXIST) {
         $res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_SNAPSHOT_FILE_EXIST);
         $res["errmsg"] .= ": {$snapshot_path}";
         QconfMgrLog::err(__FILE__, __LINE__, "snapshot has already exist, snapshot_path({$snapshot_path})");
         echo json_encode($res);
         return;
     }
     // 5. idc validation
     $cur_idcs = $node["idc"];
     $check_res = self::idcValidation($idc, $cur_idcs);
     if ($check_res["errno"] != "0") {
         echo json_encode($check_res);
         return;
     }
     // 6. construct values
     $zk_web = new QconfZkWebBase(Log::INFO);
     $idc_list = preg_split("/[,]+/", trim($idc, ","));
     $descendant = NodeServ::getDescendant($node_whole);
     $descendant[] = $node;
     $idc_values = array();
     foreach ($idc_list as $idc_each) {
         $values_conf = array();
         $values_serv = array();
         $idc_host = ZkConf::getZkHost($idc_each);
         $zk_web->connect($idc_host);
         foreach ($descendant as $one_descendant) {
             $desc_node_whole = $one_descendant["node_whole"];
             $desc_node_type_code = $one_descendant["node_type_code"];
             $desc_node_idcs = $one_descendant["idc"];
             $exist_idc = in_array($idc_each, explode(',', $desc_node_idcs));
             if ($exist_idc === FALSE) {
                 continue;
             }
             if ($desc_node_type_code === InfoDescUtil::NODE_TYPE_SERV_FATHER) {
                 // get services
                 $get_ret = ServiceController::getServicesAndStatusWithLink($zk_web, $desc_node_whole, $idc_each, $services);
                 if ($get_ret != InfoDescUtil::QCONF_OK) {
                     $res = InfoDescUtil::getErrorMsg($get_ret);
                     $res["data"] .= $idc_each;
                     echo json_encode($res);
                     return;
                 }
                 $values_serv[$desc_node_whole] = $services;
             }
             // get conf
             $get_conf = NodeController::getConfWithLink($zk_web, $desc_node_whole, $idc_each, $node_val);
             if ($get_conf != InfoDescUtil::QCONF_OK) {
                 $res = InfoDescUtil::getErrorMsg($get_conf);
                 $res["data"] .= $idc_each;
                 echo json_encode($res);
                 return;
             }
             $values_conf[$desc_node_whole] = $node_val;
         }
         $values = array();
         $values["conf"] = $values_conf;
         $values["serv"] = $values_serv;
         $idc_values[$idc_each] = $values;
     }
     // 7. create snapshot
     $main_buss_id = $node["main_buss_id"];
     $sub_buss_id = $node["sub_buss_id"];
     $create_res = SnapShotUtil::createSnapShot($main_buss_id, $node_whole, $op_user, $idc_values);
     if ($create_res === FALSE) {
         $res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_SNAPSHOT_MODULE_FAILED);
         QconfMgrLog::err(__FILE__, __LINE__, "snapshot has already exist");
         echo json_encode($res);
         return;
     }
     OpServ::insert(InfoDescUtil::OP_TYPE_SNAPSHOT_ADD, $node_whole, $idc, InfoDescUtil::OP_STATUS_SUCCESS, $main_buss_id, $sub_buss_id, $op_user, "");
     // 8. return the right info
     $res = array("errno" => "0", "errmsg" => "", "data" => "create snapshot success!");
     $json = json_encode($res);
     echo $json;
 }
예제 #2
0
 public function addIdcRecursiveAction()
 {
     $node_id = $this->getRequest("node_id", "");
     $idc = $this->getRequest("idc", "");
     $template_idc = $this->getRequest("template_idc", "");
     $is_copy_servs = $this->getRequest("is_copy_servs", "");
     $op_user = $this->userInfo["userName"];
     QconfMgrLog::err(__FILE__, __LINE__, "parameter 'node_id({$node_id})' 'idc({$idc})' 'template_idc({$template_idc})' or 'copy_services({$is_copy_servs})' is null!");
     // 1. parameter is not right
     if ($node_id === "" || $idc === "" || $template_idc === "" || $is_copy_servs === "") {
         $res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_BAD_ARGS);
         QconfMgrLog::err(__FILE__, __LINE__, "parameter 'node_id' 'idc' 'template_idc' or 'copy_services' is null!");
         echo json_encode($res);
         return;
     }
     $is_copy_services = $is_copy_servs === "1" ? TRUE : FALSE;
     // 2. get the node info from database
     $row = NodeServ::getNode($node_id);
     if ($row === FALSE) {
         $res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_NODE_NOT_EXIST);
         echo json_encode($res);
         return;
     }
     $main_buss_id = $row["main_buss_id"];
     $db_node_whole = $row["node_whole"];
     //[USERPERM CHECK]
     $userperm_ret = UserPermServ::checkPerm($op_user, $db_node_whole);
     if ($userperm_ret === FALSE) {
         QconfMgrLog::err(__FILE__, __LINE__, "Insufficient permission of {$op_user} on node {$db_node_whole}");
         $res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_USER_PERM_PERMISSION_DENIED);
         echo json_encode($res);
         return;
     }
     //[SNAPSHOT CHECK]
     $snapshot_ret = SnapShotUtil::existSnapShotForNode($main_buss_id, $db_node_whole, $snapshot_path, InfoDescUtil::SNAPSHOT_FULL_PATH);
     if ($snapshot_ret === FALSE) {
         QconfMgrLog::err(__FILE__, __LINE__, "snapshot operation failed");
         $res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_SNAPSHOT_MODULE_FAILED);
         echo json_encode($res);
         return;
     }
     if ($snapshot_ret === InfoDescUtil::SNAPSHOT_FILE_EXIST) {
         QconfMgrLog::err(__FILE__, __LINE__, "snapshot existed on current node node_whole({$node_whole})");
         $res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_SNAPSHOT_LOCKED_NODE);
         $res["errmsg"] .= ": {$snapshot_path}";
         echo json_encode($res);
         return;
     }
     // 3. check target idc exist
     $idc = trim($idc, ",");
     $idc_list = preg_split("/[,]+/", $idc);
     $idc_to = $idc_list[0];
     $idc_host = ZkConf::getZkHost($idc_to);
     if ($idc_host === NULL) {
         $res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_IDC_NOT_EXIST);
         $res["data"] .= $idc_to;
         echo json_encode($res);
         return;
     }
     $zk_web_target = new QconfZkWebBase(Log::INFO);
     $zk_web_target->connect($idc_host);
     // 4. check template idc exist
     $template_idc = trim($template_idc, ",");
     $template_list = preg_split("/[,]+/", $template_idc);
     $idc_from = $template_list[0];
     $template_host = ZkConf::getZkHost($idc_from);
     if ($template_host === NULL) {
         $res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_IDC_NOT_EXIST);
         $res["data"] .= $idc_from;
         echo json_encode($res);
         return;
     }
     $zk_web_template = new QconfZkWebBase(Log::INFO);
     $zk_web_template->connect($template_host);
     $descendant = NodeServ::getDescendant($row["node_whole"]);
     // add the current node itself
     $descendant[] = $row;
     mysql_query("BEGIN");
     foreach ($descendant as $one_descendant) {
         $node_cur_id = $one_descendant["node_id"];
         $node_type_code = $one_descendant["node_type_code"];
         $node_whole = $one_descendant["node_whole"];
         $main_buss_id = $one_descendant["main_buss_id"];
         $sub_buss_id = $one_descendant["sub_buss_id"];
         //idc maybe update during the previous loop
         $cur_descendant = NodeServ::getNode($node_cur_id);
         if ($cur_descendant === FALSE) {
             QconfMgrLog::err(__FILE__, __LINE__, "faied to retrive node_id : {$node_cur_id}");
             $res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_MYSQL_FAIL);
             echo json_encode($res);
             mysql_query("ROLLBACK");
             return;
         }
         $descendant_idc = $cur_descendant["idc"];
         // 5. check wether descendant exist on template idc
         // 5.1 check exist
         $descendant_idcs_array = explode(',', $descendant_idc);
         $exist_on_template = in_array($idc_from, $descendant_idcs_array);
         if ($exist_on_template === FALSE) {
             QconfMgrLog::err(__FILE__, __LINE__, "expect {$node_whole} on " . $idc_from);
             continue;
         }
         // 5.2 get value
         $get_ret = self::getConfWithLink($zk_web_template, $node_whole, $idc_from, $node_val);
         if ($get_ret != InfoDescUtil::QCONF_OK) {
             $res = InfoDescUtil::getErrorMsg($get_ret);
             $res["data"] .= $idc_from;
             echo json_encode($res);
             mysql_query("ROLLBACK");
             return;
         }
         // 5.3 get services
         $node_services = NULL;
         if ($is_copy_services === TRUE && $node_type_code === InfoDescUtil::NODE_TYPE_SERV_FATHER) {
             $get_ret = ServiceController::getServicesAndStatusWithLink($zk_web_template, $node_whole, $idc_host, $node_services);
             if ($get_ret != InfoDescUtil::QCONF_OK) {
                 $res = InfoDescUtil::getErrorMsg($get_ret);
                 mysql_query("ROLLBACK");
                 echo json_encode($res);
                 return;
             }
         }
         // check wether descendant exist on target idc
         $exist_on_target = in_array($idc_to, $descendant_idcs_array);
         if ($exist_on_target === FALSE) {
             // 6. find all leaf descendants under given node and expand it to the new idc
             // 6.1 add in zookeeper
             $res_cur = self::addOneIdcWithLink($node_type_code, $node_whole, $idc_to, $node_val, $node_services, $zk_web_target);
             if ($res_cur === -1) {
                 OpServ::insert(InfoDescUtil::OP_TYPE_EXPANSION, $node_whole, $idc_to, InfoDescUtil::OP_STATUS_ZOO_FAILED, $main_buss_id, $sub_buss_id, $op_user, "");
                 $res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_ZOOKEEPER_FAIL);
                 $res["data"] .= $idc_to;
                 echo json_encode($res);
                 mysql_query("ROLLBACK");
                 return;
             }
             // 6.2 update database
             $ret = NodeServ::modIdc($node_cur_id, $idc_to);
             if ($ret === FALSE) {
                 $res = InfoDescUtil::getErrorMsg(InfoDescUtil::ERR_MYSQL_FAIL);
                 OpServ::insert(InfoDescUtil::OP_TYPE_EXPANSION, $node_whole, $idc_to, InfoDescUtil::OP_STATUS_MYSQL_FAILED, $main_buss_id, $sub_buss_id, $op_user, "");
                 echo json_encode($res);
                 mysql_query("ROLLBACK");
                 return;
             }
             OpServ::insert(InfoDescUtil::OP_TYPE_EXPANSION, $node_whole, $idc_to, InfoDescUtil::OP_STATUS_SUCCESS, $main_buss_id, $sub_buss_id, $op_user, $node_val);
         } else {
             // 7. sync value and services between target idc and template idc
             // 7.1 sync value
             $sync_ret = InfoDescUtil::QCONF_OK;
             if ($node_val != "") {
                 $sync_ret = self::setConfWithLink($zk_web_target, $node_whole, $idc_to, $node_val);
                 // modify the zookeeper failed
             }
             // 7.2 sync services
             if ($sync_ret === InfoDescUtil::QCONF_OK && $node_services != NULL) {
                 foreach ($node_services as $ser => $status) {
                     $sync_ret = ServiceController::addServiceWithLink($zk_web_target, $node_whole, $ser, $idc_to, $status);
                     if ($sync_ret === -1) {
                         break;
                     }
                 }
             }
             if ($sync_ret != InfoDescUtil::QCONF_OK) {
                 OpServ::insert(InfoDescUtil::OP_TYPE_MOD, $node_whole, $idc_to, InfoDescUtil::OP_STATUS_ZOO_FAILED, $main_buss_id, $sub_buss_id, $op_user, "");
                 $res = InfoDescUtil::getErrorMsg($sync_ret);
                 $res["data"] .= $idc_to;
                 echo json_encode($res);
                 mysql_query("ROLLBACK");
                 return;
             }
             OpServ::insert(InfoDescUtil::OP_TYPE_MOD, $node_whole, $idc_to, InfoDescUtil::OP_STATUS_SUCCESS, $main_buss_id, $sub_buss_id, $op_user, $node_val);
         }
     }
     mysql_query("COMMIT");
     // 8. return the data
     $res = array("errno" => "0", "errmsg" => "", "data" => "add idc success!");
     $json = json_encode($res);
     echo $json;
     return;
 }