commandUtils.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. function launchCmd ($cmd)
  3. {
  4. global $logger;
  5. $logger->log_debug("WCOLL is ".json_encode(getenv("WCOLL")));
  6. $handle = popen($cmd, "r");
  7. while (!feof($handle)) {
  8. $read = fread($handle, 4096);
  9. }
  10. pclose($handle);
  11. // FIXME return some sort of status
  12. return 0;
  13. }
  14. function readHostsFile($hostsFile) {
  15. $hostsFd = fopen($hostsFile, "r");
  16. $hosts = array();
  17. while (($buffer = fgets($hostsFd, 4096)) !== false) {
  18. $line = trim($buffer);
  19. if ($line == "") {
  20. continue;
  21. }
  22. if (substr($line, 0, 1) == "#") {
  23. continue;
  24. }
  25. $splitHosts = preg_split("/[\s,]+/", $line);
  26. foreach ($splitHosts as $splitHost) {
  27. $h = trim($splitHost);
  28. if ($h != "") {
  29. $matches = array();
  30. $count = preg_match("/(.*)\[(\d+)-(\d+)\](.*)/", $h, $matches);
  31. if ($count == 1 && count($matches) == 5) {
  32. $hostPrefix = $matches[1];
  33. $hostSuffix = $matches[4];
  34. for ($i = $matches[2]; $i <= $matches[3]; $i++) {
  35. $newH = $hostPrefix . $i . $hostSuffix;
  36. $hosts[] = $newH;
  37. }
  38. } else {
  39. $hosts[] = $h;
  40. }
  41. }
  42. }
  43. }
  44. return array_unique($hosts);
  45. }
  46. function runPdsh($clusterName, $operationName, $deployUser, $hosts, $cmdLine) {
  47. global $logger;
  48. $sshCmd = "ssh -o ConnectTimeOut=3 -o StrictHostKeyChecking=no";
  49. if (getSshKeyFilePath($clusterName)) {
  50. $sshCmd = $sshCmd." -i ".getSshKeyFilePath($clusterName)." -l ".$deployUser." $1";
  51. } else {
  52. $sshCmd = $sshCmd." -l ".$deployUser." $1";
  53. }
  54. $clusterDir = getClusterDir($clusterName);
  55. $myDir = $clusterDir . $operationName . "/";
  56. if (is_dir($myDir)) {
  57. rrmdir($myDir);
  58. }
  59. mkdir($myDir);
  60. $fullSshCmd = $sshCmd . " \"".$cmdLine."\" 1>" . $myDir . "$1.out 2>" . $myDir . "$1.err ";
  61. $sshCmdFile = $myDir."/ssh.sh";
  62. $sshCmdFileHdl = fopen($sshCmdFile, 'w');
  63. fwrite($sshCmdFileHdl, $fullSshCmd ." ; \n");
  64. fwrite($sshCmdFileHdl, " echo $? > " . $myDir . "$1.done ; \n");
  65. fclose($sshCmdFileHdl);
  66. chmod($sshCmdFile, 0555);
  67. $logger->log_info("Hosts for this operation: ".json_encode($hosts));
  68. putenv("WCOLL=$hosts");
  69. $pdshCmd = "pdsh -R exec ".$sshCmdFile." %h ";
  70. $logger->log_info("Going to execute " . $operationName . " : ".$pdshCmd);
  71. $retval = launchCmd($pdshCmd);
  72. }
  73. function parseAndUpdateNodeInfo ($clusterName, $operationName, $logger) {
  74. $clusterDir = getClusterDir($clusterName);
  75. $commandOutputDir = $clusterDir . $operationName . "/";
  76. $allHosts = array();
  77. $finalOpStatus = "SUCCESS";
  78. $nodeStatus = "SUCCESS";
  79. $nodeCount = 0;
  80. $successNodeCount = 0;
  81. $failedNodeCount = 0;
  82. if ($dirHandle = opendir($commandOutputDir)) {
  83. while (false !== ($entry = readdir($dirHandle))) {
  84. if ($entry == "." || $entry == "..") {
  85. continue;
  86. }
  87. // Only consider .out files
  88. if(!preg_match("/.out/", $entry)) {
  89. continue;
  90. }
  91. $nodeCount += 1;
  92. $nodeName = basename($entry, ".out");
  93. $doneFile = $commandOutputDir . $nodeName . ".done";
  94. if (file_exists($doneFile)) {
  95. $nodeStatus = "SUCCESS";
  96. // Read the contents of the done-file
  97. $doneFileContents = file_get_contents($doneFile);
  98. if (trim($doneFileContents) != "0") {
  99. $failedNodeCount += 1;
  100. $nodeStatus = "FAILED";
  101. $finalOpStatus = "FAILED";
  102. $logger->log_debug( "Contents of done file for $clusterName : $doneFileContents");
  103. }
  104. } else {
  105. $failedNodeCount += 1;
  106. $nodeStatus = "FAILED";
  107. $finalOpStatus = "FAILED";
  108. }
  109. // Initialize this host's array
  110. $thisHostArray = array();
  111. $thisHostArray["hostName"] = $nodeName;
  112. $thisHostArray["totalMem"] = 0;
  113. $thisHostArray["cpuCount"] = 0;
  114. $thisHostArray["osArch"] = "";
  115. $thisHostArray["disksInfo"] = json_encode(array());
  116. $thisHostArray["osType"] = "";
  117. $thisHostArray["os"] = "";
  118. $thisHostArray["ip"] = $nodeName; // To be unique
  119. $thisHostArray["badHealthReason"] = "";
  120. if ($nodeStatus == "FAILED") {
  121. $thisHostArray["badHealthReason"] =
  122. rtrim(file_get_contents($commandOutputDir.$nodeName . ".err"));
  123. } else {
  124. $successNodeCount += 1;
  125. }
  126. $thisHostArray["discoveryStatus"] = $nodeStatus;
  127. array_push($allHosts, $thisHostArray);
  128. }
  129. closedir($dirHandle);
  130. }
  131. if ($failedNodeCount == $nodeCount) {
  132. $finalOpStatus = "TOTALFAILURE";
  133. }
  134. $retArr = array(
  135. "allHosts" => $allHosts,
  136. "finalOpStatus" => $finalOpStatus
  137. );
  138. return $retArr;
  139. }
  140. ?>