bootstrap.php 7.4 KB

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