123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- <?php
- /*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
- */
- include_once '../util/Logger.php';
- include_once '../conf/Config.inc';
- include_once 'localDirs.php';
- include_once "../util/lock.php";
- include_once '../db/HMCDBAccessor.php';
- include_once 'commandUtils.php';
- include_once '../util/util.php';
- $logger = new HMCLogger("verifyAndUpdateNodesInfo");
- $dbAccessor = new HMCDBAccessor($GLOBALS["DB_PATH"]);
- $lineSeparatorPattern = "HDP-------HDP";
- $clusterName = $argv[1];
- $deployUser = $argv[2];
- $rootTxnId = $argv[3];
- $mySubTxnId = $argv[4];
- $parentSubTxnId = $argv[5];
- $readFromFile = $argv[6];
- $stageName = "verifyAndUpdateNodesInfo";
- $prevStageName = "obtainNodesInfo";
- $clusterDir = getClusterDir($clusterName);
- $prevOutputDir = $clusterDir . $prevStageName . "/";
- $outputDir = $clusterDir . $stageName . "/";
- $logger->log_debug("OutputDir is : $outputDir");
- if (is_dir($outputDir)) {
- rrmdir($outputDir);
- }
- mkdir($outputDir);
- $allHosts = array();
- $allBadHosts = array();
- function updateStatusForNode ($outDir, $nodeName, $status, $error = "")
- {
- global $logger;
- $outArray = array();
- $doneFileName = $outDir . $nodeName . ".done";
- $outFileName = $outDir . $nodeName . ".out";
- $errFileName = $outDir . $nodeName . ".err";
- $logger->log_debug("done file name is $doneFileName");
- $fd = fopen($doneFileName, "w");
- if ($fd == FALSE) {
- $outArray['success'] = FALSE;
- $outArray['reason'] = "Failed to update done status: $status for node: $nodeName";
- return $outArray;
- }
- $retval = fwrite($fd, $status);
- if ($retval == FALSE) {
- $outArray['success'] = FALSE;
- $outArray['reason'] = "Failed to write done status: $status for node: $nodeName";
- return $outArray;
- } else {
- $outArray['success'] = TRUE;
- }
- fclose($fd);
- $fd = fopen($outFileName, "w");
- if ($fd == FALSE) {
- $outArray['success'] = FALSE;
- $outArray['reason'] = "Failed to write out status: $status for node: $nodeName";
- return $outArray;
- }
- $retval = fwrite($fd, $status);
- if ($retval == FALSE) {
- $outArray['success'] = FALSE;
- $outArray['reason'] = "Failed to write out status: $status for node: $nodeName";
- } else {
- $outArray['success'] = TRUE;
- }
- fclose($fd);
- if ($status != 0) {
- $fd = fopen($errFileName, "w");
- if ($fd == FALSE) {
- $outArray['success'] = FALSE;
- $outArray['reason'] = "Failed to write err info: $error for node: $nodeName";
- return $outArray;
- }
- $retval = fwrite($fd, $error);
- if ($retval == FALSE) {
- $outArray['success'] = FALSE;
- $outArray['reason'] = "Failed to write err info: $error for node: $nodeName";
- } else {
- $outArray['success'] = TRUE;
- }
- fclose($fd);
- }
- return $outArray;
- }
- function updateFailedStatusForNode ($outDir, $nodeName, $error)
- {
- return updateStatusForNode($outDir, $nodeName, 255, $error);
- }
- function updateSuccessStatusForNode ($outDir, $nodeName)
- {
- return updateStatusForNode($outDir, $nodeName, 0);
- }
- function getBadNodeReason ($count)
- {
- switch ($count)
- {
- case 0:
- return "Failed to get memory info";
- case 1:
- return "Failed to get cpu count info";
- case 2:
- return "Failed to get OS architecture";
- case 3:
- return "Failed to get mount point info";
- case 4:
- return "Failed to get OS distribution type";
- case 5:
- return "Failed to get OS related information";
- case 6:
- return "Failed to get IP address";
- case 7:
- return "Failed to get public FQDN";
- case 8:
- return "Failed to get private FQDN";
- default:
- return "Unknown error in host discovery";
- }
- }
- function populateVal ($line, $count, $arr)
- {
- switch ($count)
- {
- case 0:
- $arr["totalMem"] = trim($line);
- break;
- case 1:
- $arr["cpuCount"] = trim($line);
- break;
- case 2:
- $arr["osArch"] = trim($line);
- break;
- case 3:
- $arr["disksInfo"][] = trim($line);
- break;
- case 4:
- if (!isset($arr["osType"])) {
- $arr["osType"] = "";
- }
- $lline = strtolower(trim($line));
- if ($lline == "release") {
- break;
- }
- $matches = array();
- if (preg_match("/([0-9]+)(\.[0-9]+)?/", $lline, $matches) > 0) {
- $lline = $matches[1];
- }
- $arr["osType"] .= $lline;
- break;
- case 5:
- if (!isset($arr["os"])) {
- $arr["os"] = "";
- }
- if ($arr["os"] != "") {
- $arr["os"] .= ";";
- }
- $arr["os"] .= trim($line);
- break;
- case 6:
- $arr["ip"] = trim($line);
- break;
- case 7:
- if (!isset($arr["attributes"])) {
- $arr["attributes"] = array();
- }
- $arr["attributes"]["publicFQDN"] = strtolower(trim($line));
- case 8:
- if (!isset($arr["attributes"])) {
- $arr["attributes"] = array();
- }
- $arr["attributes"]["privateFQDN"] = strtolower(trim($line));
- default:
- break;
- }
- return $arr;
- }
- $finalOpStatus = "SUCCESS";
- $failedCount = 0;
- $successCount = 0;
- if ($dirHandle = opendir($prevOutputDir)) {
- while (false !== ($entry = readdir($dirHandle))) {
- if ($entry == "." || $entry == "..") {
- continue;
- }
- $nodeStatus = "SUCCESS";
- // Only consider .out files
- if(!preg_match("/.out/", $entry)) {
- continue;
- }
- $nodeName = basename($entry, ".out");
- $nodeStatus = "SUCCESS";
- $doneFile = $prevOutputDir . $nodeName . ".done";
- if (file_exists($doneFile)) {
- // Read the contents of the done-file
- $doneFileContents = file_get_contents($doneFile);
- if (trim($doneFileContents) != "0") {
- $failedCount += 1;
- $nodeStatus = "FAILED";
- $finalOpStatus = "FAILED";
- updateFailedStatusForNode($outputDir, $nodeName,
- "Command to discover node information failed, exit_code=" . $doneFileContents);
- $logger->log_debug( "Contents of done file for $clusterName : $doneFileContents");
- }
- } else {
- $failedCount += 1;
- $nodeStatus = "FAILED";
- $finalOpStatus = "FAILED";
- updateFailedStatusForNode($outputDir, $nodeName,
- "Command to discover node information failed, no exit code found");
- $logger->log_debug("Update failed because file contents of $doneFile is empty");
- }
- // Initialize this host's array
- $thisHostArray = array();
- $thisHostArray["hostName"] = strtolower($nodeName);
- $thisHostArray["totalMem"] = 0;
- $thisHostArray["cpuCount"] = 0;
- $thisHostArray["osArch"] = "";
- $thisHostArray["disksInfo"] = array();
- $thisHostArray["osType"] = "";
- $thisHostArray["os"] = "";
- $thisHostArray["ip"] = $nodeName; // To be unique
- $thisHostArray["attributes"] = array();
- if ($nodeStatus != "FAILED") {
- // parse the file for the contents we need
- // if any exit value != 0, we need to set the host as bad
- $hostOutFd = fopen($prevOutputDir.$entry, "r");
- if ($hostOutFd === FALSE) {
- $logger->log_error("Failed to open file to read: ". $prevOutputDir.$entry);
- $thisHostArray["badHealthReason"] = "No data obtained for host";
- $finalOpStatus = "FAILED";
- $nodeStatus = "FAILED";
- updateFailedStatusForNode($outputDir, $nodeName,
- $thisHostArray["badHealthReason"]);
- } else {
- $goodReturnValCount = 0;
- while (!feof($hostOutFd)) {
- $line = fgets($hostOutFd, 4096);
- if (preg_match("/".$lineSeparatorPattern."0/", $line)) {
- $goodReturnValCount += 1;
- } else if (preg_match("/".$lineSeparatorPattern."/", $line)) {
- // this particular node is dead
- // add to db saying failed.
- $failedCount += 1;
- $thisHostArray["badHealthReason"] = getBadNodeReason($goodReturnValCount);
- $finalOpStatus = "FAILED";
- $nodeStatus = "FAILED";
- updateFailedStatusForNode($outputDir, $nodeName,
- $thisHostArray["badHealthReason"]);
- // write to file if bad so as to be shown as json in frontend.
- array_push($badHostsList, $thisHostArray);
- break;
- } else {
- $thisHostArray = populateVal($line, $goodReturnValCount, $thisHostArray);
- }
- }
- fclose($hostOutFd);
- }
- if ($nodeStatus == "SUCCESS") {
- if ($thisHostArray["osType"] != "redhatenterpriselinuxserver5"
- && $thisHostArray["osType"] != "centos5"
- && $thisHostArray["osType"] != "redhatenterpriselinuxserver6"
- && $thisHostArray["osType"] != "centos6") {
- $thisHostArray["badHealthReason"] = "Unsupported OS";
- $finalOpStatus = "FAILED";
- $nodeStatus = "FAILED";
- updateFailedStatusForNode($outputDir, $nodeName,
- $thisHostArray["badHealthReason"]);
- }
- }
- if ($nodeStatus == "SUCCESS") {
- $successCount += 1;
- updateSuccessStatusForNode($outputDir, $nodeName);
- }
- }
- $thisHostArray["discoveryStatus"] = $nodeStatus;
- $thisHostArray["disksInfo"] = json_encode($thisHostArray["disksInfo"]);
- array_push($allHosts, $thisHostArray);
- }
- closedir($dirHandle);
- }
- // Perisist the data to the db.
- $logger->log_info("Going to persist discovered node properties");
- $returnValue = $dbAccessor->addHostsToCluster($clusterName, $allHosts);
- if ($returnValue["result"] != 0 ) {
- $logger->log_error("Got error while adding hosts: ".$returnValue["error"]);
- print json_encode($returnValue);
- return;
- }
- if ($successCount == 0) {
- $finalOpStatus = "TOTALFAILURE";
- }
- $nodeFileOut = fopen($readFromFile, "w");
- if ($nodeFileOut == FALSE) {
- $subTransactionReturnValue = $dbAccessor->updateSubTransactionOpStatus($clusterName, $parentSubTxnId, $mySubTxnId, "TOTALFAILURE");
- $logger->log_error("Got error while trying to rewrite hosts file");
- return;
- }
- // foreach successfully discovered host write the host list to the readFromFile
- foreach ($allHosts as $hostInfo) {
- if ($hostInfo["discoveryStatus"] == "FAILED") {
- continue;
- }
- // write the nodename to the readFromFile file.
- fwrite($nodeFileOut, $hostInfo["hostName"]."\n");
- }
- fclose($nodeFileOut);
- $subTransactionReturnValue = $dbAccessor->updateSubTransactionOpStatus($clusterName, $parentSubTxnId, $mySubTxnId, $finalOpStatus);
- if ($subTransactionReturnValue["result"] != 0 ) {
- $logger->log_error("Got error while updating subTxn: ".$subTransactionReturnValue["error"]);
- print json_encode($subTransactionReturnValue);
- return;
- }
- ?>
|