commandUtils.php 5.3 KB

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