bootstrap.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 "../util/util.php";
  27. include_once '../db/HMCDBAccessor.php';
  28. include_once "../util/HMCTxnUtils.php";
  29. include_once 'commandUtils.php';
  30. include_once "../util/YumRepoConfigParser.php";
  31. $logger = new HMCLogger("BootStrap");
  32. $dbAccessor = new HMCDBAccessor($GLOBALS["DB_PATH"]);
  33. function bootstrap($clusterName, $user, $rscript, $sshkey, $repo)
  34. {
  35. global $logger, $dbAccessoar, $hosts, $readFromFile;
  36. $master=strtolower(exec('hostname -f'));
  37. $repoFile = $repo['yumRepoFilePath'];
  38. $usingLocalRepo = $repo['usingLocalRepo'];
  39. $gpgKeyFiles = $repo['gpgKeyFiles'];
  40. exec ("/etc/init.d/iptables stop");
  41. $logger->log_info("List of hosts to BootStrap ".json_encode($hosts));
  42. $logger->log_info("Run script for pdsh ".$rscript);
  43. $scpCmd = "scp -o StrictHostKeyChecking=no ";
  44. foreach ($hosts as $host) {
  45. $host = trim($host);
  46. $filesToCopy = array_merge ( array ($rscript, $repoFile), $gpgKeyFiles);
  47. /* Copy puppet run script to all nodes */
  48. // Copy repo file to each node
  49. // Copy gpg keys to each node
  50. if (!empty($filesToCopy)) {
  51. $cmd = "$scpCmd -i $sshkey " . implode(" ", $filesToCopy)
  52. . " $user@$host:/tmp/ ";
  53. $logger->log_debug("Running scp command $cmd");
  54. exec($cmd);
  55. }
  56. }
  57. $remoteRepoFilePath = trim("/tmp/" . basename(trim($repoFile)));
  58. $remoteGpgKeyPaths = "";
  59. foreach ($gpgKeyFiles as $kFile) {
  60. $dFile = trim("/tmp/" . basename(trim($kFile)));
  61. if ($remoteGpgKeyPaths != "") {
  62. $remoteGpgKeyPaths .= ",";
  63. }
  64. $remoteGpgKeyPaths .= $dFile;
  65. }
  66. $rcmd = "/tmp/puppet_agent_install.sh --puppet-master=" . $master
  67. . " --repo-file=" . $remoteRepoFilePath
  68. . " --gpg-key-files=" . $remoteGpgKeyPaths;
  69. if ("true" == strtolower($usingLocalRepo)) {
  70. $rcmd .= " --using-local-repo ";
  71. }
  72. $logger->log_info("Running $rcmd to bootstrap each node");
  73. runPdsh($clusterName, "bootstrapNodes", $user, $readFromFile, $rcmd);
  74. $result = parseAndUpdateNodeInfo ($clusterName, "bootstrapNodes", $logger);
  75. $logger->log_debug("Puppet agent install pdsh result: "
  76. . print_r($result, true));
  77. $errNodes = array();
  78. foreach ($result["allHosts"] as $hostInfo) {
  79. $errNodes[$hostInfo["hostName"]] =
  80. array ( "discoveryStatus" => $hostInfo["discoveryStatus"],
  81. "badHealthReason" => $hostInfo["badHealthReason"]
  82. );
  83. }
  84. return $errNodes;
  85. }
  86. $clusterName = $argv[1];
  87. $deployUser = $argv[2];
  88. $rootTxnId = $argv[3];
  89. $mySubTxnId = $argv[4];
  90. $parentSubTxnId = $argv[5];
  91. $readFromFile = $argv[6];
  92. $hosts = readHostsFile($readFromFile);
  93. $hosts = convertToLowerCase($hosts);
  94. $totalHosts = count($hosts);
  95. $opStatus = "STARTED";
  96. $subTransactionReturnValue = $dbAccessor->updateSubTransactionOpStatus($clusterName, $parentSubTxnId, $mySubTxnId, $opStatus);
  97. if ($subTransactionReturnValue["result"] != 0 ) {
  98. $logger->log_error("Got error while updating subTxn: ".$subTransactionReturnValue["error"]);
  99. print json_encode($subTransactionReturnValue);
  100. return;
  101. }
  102. $sshkey = getSshKeyFilePath($clusterName);
  103. // $rscript = "/var/www/html/KickAssHDPUI/ShellScripts/puppet_agent_install.sh";
  104. $rscript = realpath("../../ShellScripts/puppet_agent_install.sh");
  105. $repository=array();
  106. $configs = $dbAccessor->getServiceConfig($clusterName);
  107. if ($configs["result"] != 0) {
  108. $subTransactionReturnValue = $dbAccessor->updateSubTransactionOpStatus($clusterName, $parentSubTxnId, $mySubTxnId, "TOTALFAILURE");
  109. $logger->log_error("Got error when trying to retrieve configs from DB");
  110. return;
  111. }
  112. $repoFile = $configs["properties"]["yum_repo_file"];
  113. $usingLocalRepo = $configs["properties"]["using_local_repo"];
  114. $gpgKeyLocations = getEnabledGpgKeyLocations($repoFile);
  115. if ($gpgKeyLocations === FALSE) {
  116. $subTransactionReturnValue = $dbAccessor->updateSubTransactionOpStatus($clusterName, $parentSubTxnId, $mySubTxnId, "TOTALFAILURE");
  117. $logger->log_error("Got error when trying to parse yum repo config");
  118. return;
  119. }
  120. $tmpDir = "/tmp/hmcDownloads-".time()."/";
  121. $retVal = 0;
  122. $output = array();
  123. exec("mkdir -p ".$tmpDir, $output, $retVal);
  124. if ($retVal != 0) {
  125. $subTransactionReturnValue = $dbAccessor->updateSubTransactionOpStatus($clusterName, $parentSubTxnId, $mySubTxnId, "TOTALFAILURE");
  126. $logger->log_error("Got error when trying to create tmp download dir"
  127. . ", dir=" . $tmpDir . ", output=" . print_r($output, true));
  128. return;
  129. }
  130. $gpgKeyFiles = array();
  131. foreach ($gpgKeyLocations as $repoId => $gpgInfo) {
  132. if (!isset($gpgInfo["gpgkey"])) {
  133. continue;
  134. }
  135. $loc = $gpgInfo["gpgkey"];
  136. $logger->log_info("Fetching gpg key for $repoId from location $loc");
  137. $info = parse_url($loc);
  138. if ($info === FALSE || !isset($info["path"])) {
  139. $logger->log_error("Skipping invalid url $loc");
  140. continue;
  141. }
  142. $fileName = basename($info["path"]);
  143. $destFilePath = $tmpDir . "/" . $fileName;
  144. $fetchCurlCmd = "curl --connect-timeout 30 --fail -s -o "
  145. . $destFilePath . " " . $loc;
  146. $logger->log_info("Fetching gpg key for $repoId from location $loc using "
  147. . $fetchCurlCmd);
  148. $retVal = 0;
  149. $output = array();
  150. exec($fetchCurlCmd, $output, $retVal);
  151. if ($retVal != 0) {
  152. $subTransactionReturnValue = $dbAccessor->updateSubTransactionOpStatus($clusterName, $parentSubTxnId, $mySubTxnId, "TOTALFAILURE");
  153. $logger->log_error("Error when trying to download gpg key using "
  154. . $fetchCurlCmd . ", output=" . print_r($output, true));
  155. return;
  156. }
  157. array_push($gpgKeyFiles, $destFilePath);
  158. }
  159. $repository = array( "yumRepoFilePath" => $repoFile,
  160. "usingLocalRepo" => $usingLocalRepo,
  161. "gpgKeyFiles" => $gpgKeyFiles);
  162. $logger->log_debug("BootStrapping with puppet");
  163. $boot_result = bootstrap($clusterName, $deployUser, $rscript,
  164. $sshkey, $repository);
  165. $logger->log_debug("Boot Result \n".print_r($boot_result, true));
  166. $nodeFileOut = fopen($readFromFile, "w");
  167. if ($nodeFileOut == FALSE) {
  168. $subTransactionReturnValue = $dbAccessor->updateSubTransactionOpStatus($clusterName, $parentSubTxnId, $mySubTxnId, "TOTALFAILURE");
  169. $logger->log_error("Got error while trying to rewrite hosts file");
  170. return;
  171. }
  172. $updateHosts = array();
  173. $failedHosts = 0;
  174. $successfulHosts = 0;
  175. foreach ($boot_result as $hostName => $hostInfo) {
  176. if ($hostInfo["discoveryStatus"] == "FAILED") {
  177. $updateHosts[$hostName] = $hostInfo;
  178. $failedHosts++;
  179. } else {
  180. $successfulHosts++;
  181. // write the nodename to the readFromFile file.
  182. fwrite($nodeFileOut, $hostName."\n");
  183. }
  184. }
  185. fclose($nodeFileOut);
  186. $ret = $dbAccessor->updateHostDiscoveryStatus($clusterName, $updateHosts);
  187. if ($ret["result"] != 0) {
  188. $logger->log_error("Failed to update DB for hosts status, error="
  189. . $ret["error"]);
  190. // TODO - handle failure?
  191. }
  192. $opStatus = "SUCCESS";
  193. if ($totalHosts > 0) {
  194. if ($successfulHosts == 0) {
  195. $opStatus = "TOTALFAILURE";
  196. } else if ($failedHosts > 0) {
  197. $opStatus = "FAILED";
  198. }
  199. }
  200. $subTransactionReturnValue = $dbAccessor->updateSubTransactionOpStatus($clusterName, $parentSubTxnId, $mySubTxnId, $opStatus);
  201. if ($subTransactionReturnValue["result"] != 0 ) {
  202. $logger->log_error("Got error while updating subTxn: ".$subTransactionReturnValue["error"]);
  203. print json_encode($subTransactionReturnValue);
  204. return;
  205. }
  206. $logger->log_info("Completed bootstrapping puppet agents, opStatus=" . $opStatus);
  207. ?>