findSshableNodes.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /*
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. */
  22. include_once '../util/Logger.php';
  23. include_once '../conf/Config.inc';
  24. include_once 'localDirs.php';
  25. include_once "../util/lock.php";
  26. include_once '../db/HMCDBAccessor.php';
  27. include_once "../util/util.php";
  28. include_once "../util/HMCTxnUtils.php";
  29. include_once 'commandUtils.php';
  30. $logger = new HMCLogger("findSshableNodes");
  31. $dbAccessor = new HMCDBAccessor($GLOBALS["DB_PATH"]);
  32. function getCommandLine() {
  33. $cmdLine = "hostname ;";
  34. // uncomment following line for demo purposes.
  35. // $cmdLine = $cmdLine . 'sleep $[ $RANDOM % 5 ]; ';
  36. return $cmdLine;
  37. }
  38. $clusterName = $argv[1];
  39. $deployUser = $argv[2];
  40. $rootTxnId = $argv[3];
  41. $mySubTxnId = $argv[4];
  42. $parentSubTxnId = $argv[5];
  43. $readFromFile = $argv[6];
  44. $opStatus = "STARTED";
  45. $subTransactionReturnValue = $dbAccessor->updateSubTransactionOpStatus($clusterName, $parentSubTxnId, $mySubTxnId, $opStatus);
  46. if ($subTransactionReturnValue["result"] != 0 ) {
  47. $logger->log_error("Got error while updating subTxn: ".$subTransactionReturnValue["error"]);
  48. print json_encode($subTransactionReturnValue);
  49. return;
  50. }
  51. $stageName = "findSshableNodes";
  52. $cmdLine = getCommandLine();
  53. // $hosts = explode(",", $hostsStr);
  54. runPdsh($clusterName, $stageName, $deployUser, $readFromFile, $cmdLine);
  55. ////////////// now read the per-host output files to get ssh-able information about each node ////////////////
  56. $clusterDir = getClusterDir($clusterName);
  57. $commandOutputDir = $clusterDir . $stageName . "/";
  58. $allHosts = array();
  59. $finalOpStatus = "SUCCESS";
  60. $numTotalNodes = 0;
  61. $numNodesSucceeded = 0;
  62. $numNodesFailed = 0;
  63. if ($dirHandle = opendir($commandOutputDir)) {
  64. while (false !== ($entry = readdir($dirHandle))) {
  65. if ($entry == "." || $entry == "..") {
  66. continue;
  67. }
  68. $nodeStatus = "SUCCESS";
  69. // Only consider .out files
  70. if(!preg_match("/.out/", $entry)) {
  71. continue;
  72. }
  73. $nodeName = basename($entry, ".out");
  74. $nodeStatus = "SUCCESS";
  75. $doneFile = $commandOutputDir . $nodeName . ".done";
  76. if (file_exists($doneFile)) {
  77. // Read the contents of the done-file
  78. $doneFileContents = file_get_contents($doneFile);
  79. if (trim($doneFileContents) != "0") {
  80. $numNodesFailed += 1;
  81. $nodeStatus = "FAILED";
  82. $finalOpStatus = "FAILED";
  83. $logger->log_debug( "Contents of done file for $clusterName : $doneFileContents");
  84. }
  85. } else {
  86. $numNodesFailed += 1;
  87. $nodeStatus = "FAILED";
  88. $finalOpStatus = "FAILED";
  89. }
  90. // Initialize this host's array
  91. $thisHostArray = array();
  92. $thisHostArray["hostName"] = strtolower($nodeName);
  93. $thisHostArray["totalMem"] = 0;
  94. $thisHostArray["cpuCount"] = 0;
  95. $thisHostArray["osArch"] = "";
  96. $thisHostArray["disksInfo"] = json_encode(array());
  97. $thisHostArray["osType"] = "";
  98. $thisHostArray["os"] = "";
  99. $thisHostArray["ip"] = $nodeName; // To be unique
  100. if ($nodeStatus != "FAILED") {
  101. $sshContents = file_get_contents($commandOutputDir.$entry);
  102. if ($sshContents == "") {
  103. $numNodesFailed += 1;
  104. $finalOpStatus = "FAILED";
  105. $nodeStatus = "FAILED";
  106. }
  107. }
  108. // since node status can be updated in the above block as well.
  109. if ($nodeStatus != "FAILED") {
  110. $numNodesSucceeded += 1;
  111. } else {
  112. $thisHostArray["badHealthReason"] =
  113. rtrim(file_get_contents($commandOutputDir.$nodeName . ".err"));
  114. }
  115. $thisHostArray["discoveryStatus"] = $nodeStatus;
  116. array_push($allHosts, $thisHostArray);
  117. }
  118. closedir($dirHandle);
  119. }
  120. // Perisist the data to the db.
  121. $logger->log_info("Going to persist information sshAble nodes");
  122. $returnValue = $dbAccessor->addHostsToCluster($clusterName, $allHosts);
  123. if ($returnValue["result"] != 0 ) {
  124. $logger->log_error("Got error while adding hosts: ".$returnValue["error"]);
  125. print json_encode($returnValue);
  126. return;
  127. }
  128. if ($numNodesSucceeded == 0) {
  129. $finalOpStatus = "TOTALFAILURE";
  130. }
  131. $nodeFileOut = fopen($readFromFile, "w");
  132. if ($nodeFileOut == FALSE) {
  133. $subTransactionReturnValue = $dbAccessor->updateSubTransactionOpStatus($clusterName, $parentSubTxnId, $mySubTxnId, "TOTALFAILURE");
  134. $logger->log_error("Got error while trying to rewrite hosts file");
  135. return;
  136. }
  137. // foreach successfully discovered host write the host list to the readFromFile
  138. foreach ($allHosts as $hostInfo) {
  139. if ($hostInfo["discoveryStatus"] == "FAILED") {
  140. continue;
  141. }
  142. // write the nodename to the readFromFile file.
  143. fwrite($nodeFileOut, $hostInfo["hostName"]."\n");
  144. }
  145. fclose($nodeFileOut);
  146. $subTransactionReturnValue = $dbAccessor->updateSubTransactionOpStatus($clusterName, $parentSubTxnId, $mySubTxnId, $finalOpStatus);
  147. if ($subTransactionReturnValue["result"] != 0 ) {
  148. $logger->log_error("Got error while updating subTxn: ".$subTransactionReturnValue["error"]);
  149. print json_encode($subTransactionReturnValue);
  150. return;
  151. }
  152. ?>