bootstrap.php 7.2 KB

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