verifyAndUpdateNodesInfo.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. include_once '../util/Logger.php';
  3. include_once '../conf/Config.inc';
  4. include_once 'localDirs.php';
  5. include_once "../util/lock.php";
  6. include_once '../db/HMCDBAccessor.php';
  7. include_once 'commandUtils.php';
  8. include_once '../util/util.php';
  9. $logger = new HMCLogger("verifyAndUpdateNodesInfo");
  10. $dbAccessor = new HMCDBAccessor($GLOBALS["DB_PATH"]);
  11. $lineSeparatorPattern = "HDP-------HDP";
  12. $clusterName = $argv[1];
  13. $deployUser = $argv[2];
  14. $rootTxnId = $argv[3];
  15. $mySubTxnId = $argv[4];
  16. $parentSubTxnId = $argv[5];
  17. $readFromFile = $argv[6];
  18. $stageName = "verifyAndUpdateNodesInfo";
  19. $prevStageName = "obtainNodesInfo";
  20. $clusterDir = getClusterDir($clusterName);
  21. $prevOutputDir = $clusterDir . $prevStageName . "/";
  22. $outputDir = $clusterDir . $stageName . "/";
  23. $logger->log_debug("OutputDir is : $outputDir");
  24. if (is_dir($outputDir)) {
  25. rrmdir($outputDir);
  26. }
  27. mkdir($outputDir);
  28. $allHosts = array();
  29. $allBadHosts = array();
  30. function updateStatusForNode ($outDir, $nodeName, $status, $error = "")
  31. {
  32. global $logger;
  33. $outArray = array();
  34. $doneFileName = $outDir . $nodeName . ".done";
  35. $outFileName = $outDir . $nodeName . ".out";
  36. $errFileName = $outDir . $nodeName . ".err";
  37. $logger->log_debug("done file name is $doneFileName");
  38. $fd = fopen($doneFileName, "w");
  39. if ($fd == FALSE) {
  40. $outArray['success'] = FALSE;
  41. $outArray['reason'] = "Failed to update done status: $status for node: $nodeName";
  42. return $outArray;
  43. }
  44. $retval = fwrite($fd, $status);
  45. if ($retval == FALSE) {
  46. $outArray['success'] = FALSE;
  47. $outArray['reason'] = "Failed to write done status: $status for node: $nodeName";
  48. return $outArray;
  49. } else {
  50. $outArray['success'] = TRUE;
  51. }
  52. fclose($fd);
  53. $fd = fopen($outFileName, "w");
  54. if ($fd == FALSE) {
  55. $outArray['success'] = FALSE;
  56. $outArray['reason'] = "Failed to write out status: $status for node: $nodeName";
  57. return $outArray;
  58. }
  59. $retval = fwrite($fd, $status);
  60. if ($retval == FALSE) {
  61. $outArray['success'] = FALSE;
  62. $outArray['reason'] = "Failed to write out status: $status for node: $nodeName";
  63. } else {
  64. $outArray['success'] = TRUE;
  65. }
  66. fclose($fd);
  67. if ($status != 0) {
  68. $fd = fopen($errFileName, "w");
  69. if ($fd == FALSE) {
  70. $outArray['success'] = FALSE;
  71. $outArray['reason'] = "Failed to write err info: $error for node: $nodeName";
  72. return $outArray;
  73. }
  74. $retval = fwrite($fd, $error);
  75. if ($retval == FALSE) {
  76. $outArray['success'] = FALSE;
  77. $outArray['reason'] = "Failed to write err info: $error for node: $nodeName";
  78. } else {
  79. $outArray['success'] = TRUE;
  80. }
  81. fclose($fd);
  82. }
  83. return $outArray;
  84. }
  85. function updateFailedStatusForNode ($outDir, $nodeName, $error)
  86. {
  87. return updateStatusForNode($outDir, $nodeName, 255, $error);
  88. }
  89. function updateSuccessStatusForNode ($outDir, $nodeName)
  90. {
  91. return updateStatusForNode($outDir, $nodeName, 0);
  92. }
  93. function getBadNodeReason ($count)
  94. {
  95. switch ($count)
  96. {
  97. case 0:
  98. return "Failed to get memory info";
  99. case 1:
  100. return "Failed to get cpu count info";
  101. case 2:
  102. return "Failed to get OS architecture";
  103. case 3:
  104. return "Failed to get mount point info";
  105. case 4:
  106. return "Failed to get OS distribution type";
  107. case 5:
  108. return "Failed to get OS related information";
  109. case 6:
  110. return "Failed to get IP address";
  111. case 7:
  112. return "Failed to get public FQDN";
  113. case 8:
  114. return "Failed to get private FQDN";
  115. default:
  116. return "Unknown error in host discovery";
  117. }
  118. }
  119. function populateVal ($line, $count, $arr)
  120. {
  121. switch ($count)
  122. {
  123. case 0:
  124. $arr["totalMem"] = trim($line);
  125. break;
  126. case 1:
  127. $arr["cpuCount"] = trim($line);
  128. break;
  129. case 2:
  130. $arr["osArch"] = trim($line);
  131. break;
  132. case 3:
  133. $arr["disksInfo"][] = trim($line);
  134. break;
  135. case 4:
  136. if (!isset($arr["osType"])) {
  137. $arr["osType"] = "";
  138. }
  139. $arr["osType"] .= trim($line);
  140. break;
  141. case 5:
  142. if (!isset($arr["os"])) {
  143. $arr["os"] = "";
  144. }
  145. if ($arr["os"] != "") {
  146. $arr["os"] .= ";";
  147. }
  148. $arr["os"] .= trim($line);
  149. break;
  150. case 6:
  151. $arr["ip"] = trim($line);
  152. break;
  153. case 7:
  154. if (!isset($arr["attributes"])) {
  155. $arr["attributes"] = array();
  156. }
  157. $arr["attributes"]["publicFQDN"] = strtolower(trim($line));
  158. case 8:
  159. if (!isset($arr["attributes"])) {
  160. $arr["attributes"] = array();
  161. }
  162. $arr["attributes"]["privateFQDN"] = strtolower(trim($line));
  163. default:
  164. break;
  165. }
  166. return $arr;
  167. }
  168. $finalOpStatus = "SUCCESS";
  169. $failedCount = 0;
  170. $successCount = 0;
  171. if ($dirHandle = opendir($prevOutputDir)) {
  172. while (false !== ($entry = readdir($dirHandle))) {
  173. if ($entry == "." || $entry == "..") {
  174. continue;
  175. }
  176. $nodeStatus = "SUCCESS";
  177. // Only consider .out files
  178. if(!preg_match("/.out/", $entry)) {
  179. continue;
  180. }
  181. $nodeName = basename($entry, ".out");
  182. $nodeStatus = "SUCCESS";
  183. $doneFile = $prevOutputDir . $nodeName . ".done";
  184. if (file_exists($doneFile)) {
  185. // Read the contents of the done-file
  186. $doneFileContents = file_get_contents($doneFile);
  187. if (trim($doneFileContents) != "0") {
  188. $failedCount += 1;
  189. $nodeStatus = "FAILED";
  190. $finalOpStatus = "FAILED";
  191. updateFailedStatusForNode($outputDir, $nodeName,
  192. "Command to discover node information failed, exit_code=" . $doneFileContents);
  193. $logger->log_debug( "Contents of done file for $clusterName : $doneFileContents");
  194. }
  195. } else {
  196. $failedCount += 1;
  197. $nodeStatus = "FAILED";
  198. $finalOpStatus = "FAILED";
  199. updateFailedStatusForNode($outputDir, $nodeName,
  200. "Command to discover node information failed, no exit code found");
  201. $logger->log_debug("Update failed because file contents of $doneFile is empty");
  202. }
  203. // Initialize this host's array
  204. $thisHostArray = array();
  205. $thisHostArray["hostName"] = strtolower($nodeName);
  206. $thisHostArray["totalMem"] = 0;
  207. $thisHostArray["cpuCount"] = 0;
  208. $thisHostArray["osArch"] = "";
  209. $thisHostArray["disksInfo"] = array();
  210. $thisHostArray["osType"] = "";
  211. $thisHostArray["os"] = "";
  212. $thisHostArray["ip"] = $nodeName; // To be unique
  213. $thisHostArray["attributes"] = array();
  214. if ($nodeStatus != "FAILED") {
  215. // parse the file for the contents we need
  216. // if any exit value != 0, we need to set the host as bad
  217. $hostOutFd = fopen($prevOutputDir.$entry, "r");
  218. if ($hostOutFd === FALSE) {
  219. $logger->log_error("Failed to open file to read: ". $prevOutputDir.$entry);
  220. $thisHostArray["badHealthReason"] = "No data obtained for host";
  221. $finalOpStatus = "FAILED";
  222. $nodeStatus = "FAILED";
  223. updateFailedStatusForNode($outputDir, $nodeName,
  224. $thisHostArray["badHealthReason"]);
  225. } else {
  226. $goodReturnValCount = 0;
  227. while (!feof($hostOutFd)) {
  228. $line = fgets($hostOutFd, 4096);
  229. if (preg_match("/".$lineSeparatorPattern."0/", $line)) {
  230. $goodReturnValCount += 1;
  231. } else if (preg_match("/".$lineSeparatorPattern."/", $line)) {
  232. // this particular node is dead
  233. // add to db saying failed.
  234. $failedCount += 1;
  235. $thisHostArray["badHealthReason"] = getBadNodeReason($goodReturnValCount);
  236. $finalOpStatus = "FAILED";
  237. $nodeStatus = "FAILED";
  238. updateFailedStatusForNode($outputDir, $nodeName,
  239. $thisHostArray["badHealthReason"]);
  240. // write to file if bad so as to be shown as json in frontend.
  241. array_push($badHostsList, $thisHostArray);
  242. break;
  243. } else {
  244. $thisHostArray = populateVal($line, $goodReturnValCount, $thisHostArray);
  245. }
  246. }
  247. fclose($hostOutFd);
  248. }
  249. if ($nodeStatus == "SUCCESS") {
  250. if ($thisHostArray["osType"] != "redhatenterpriseserver5"
  251. && $thisHostArray["osType"] != "centos5") {
  252. $thisHostArray["badHealthReason"] = "Unsupported OS";
  253. $finalOpStatus = "FAILED";
  254. $nodeStatus = "FAILED";
  255. updateFailedStatusForNode($outputDir, $nodeName,
  256. $thisHostArray["badHealthReason"]);
  257. }
  258. }
  259. if ($nodeStatus == "SUCCESS") {
  260. $successCount += 1;
  261. updateSuccessStatusForNode($outputDir, $nodeName);
  262. }
  263. }
  264. $thisHostArray["discoveryStatus"] = $nodeStatus;
  265. $thisHostArray["disksInfo"] = json_encode($thisHostArray["disksInfo"]);
  266. array_push($allHosts, $thisHostArray);
  267. }
  268. closedir($dirHandle);
  269. }
  270. // Perisist the data to the db.
  271. $logger->log_debug("Going to persist discovered node properties");
  272. $returnValue = $dbAccessor->addHostsToCluster($clusterName, $allHosts);
  273. if ($returnValue["result"] != 0 ) {
  274. $logger->log_error("Got error while adding hosts: ".$returnValue["error"]);
  275. print json_encode($returnValue);
  276. return;
  277. }
  278. if ($successCount == 0) {
  279. $finalOpStatus = "TOTALFAILURE";
  280. }
  281. $nodeFileOut = fopen($readFromFile, "w");
  282. if ($nodeFileOut == FALSE) {
  283. $subTransactionReturnValue = $dbAccessor->updateSubTransactionOpStatus($clusterName, $parentSubTxnId, $mySubTxnId, "TOTALFAILURE");
  284. $logger->log_error("Got error while trying to rewrite hosts file");
  285. return;
  286. }
  287. // foreach successfully discovered host write the host list to the readFromFile
  288. foreach ($allHosts as $hostInfo) {
  289. if ($hostInfo["discoveryStatus"] == "FAILED") {
  290. continue;
  291. }
  292. // write the nodename to the readFromFile file.
  293. fwrite($nodeFileOut, $hostInfo["hostName"]."\n");
  294. }
  295. fclose($nodeFileOut);
  296. $subTransactionReturnValue = $dbAccessor->updateSubTransactionOpStatus($clusterName, $parentSubTxnId, $mySubTxnId, $finalOpStatus);
  297. if ($subTransactionReturnValue["result"] != 0 ) {
  298. $logger->log_error("Got error while updating subTxn: ".$subTransactionReturnValue["error"]);
  299. print json_encode($subTransactionReturnValue);
  300. return;
  301. }
  302. ?>