nodesAction.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 '../db/HMCDBAccessor.php';
  27. include_once 'commandUtils.php';
  28. include_once '../util/clusterState.php';
  29. include_once "../util/HMCTxnUtils.php";
  30. $logger = new HMCLogger("setupNodes");
  31. $dbAccessor = new HMCDBAccessor($GLOBALS["DB_PATH"]);
  32. $clusterName = $_GET['clusterName'];
  33. $action = $_GET['action'];
  34. $deployUser = $_POST['ClusterDeployUser'];
  35. $propertiesArr = $dbAccessor->getConfigPropertiesMetaInfo();
  36. if ($propertiesArr["result"] != 0) {
  37. $logger->log_error("Could not get config meta info from DB, error="
  38. . $propertiesArr["error"]);
  39. print json_encode(array( "result" => 1, "error" => "Error in config properties meta info"));
  40. return;
  41. }
  42. // Use meta info defaults
  43. // Override with current svc configs
  44. // Override with POST params
  45. $useLocalYumRepo = $propertiesArr["configs"]["using_local_repo"]["value"];
  46. $repoFilePath = $propertiesArr["configs"]["yum_repo_file"]["value"];
  47. $currentConfigs = $dbAccessor->getServiceConfig($clusterName);
  48. if ($currentConfigs["result"] != 0) {
  49. $logger->log_error("Could not get configs from DB, error="
  50. . $currentConfigs["error"]);
  51. print json_encode(array( "result" => 1, "error" => "Could not get configs from DB"));
  52. return;
  53. }
  54. if (isset($currentConfigs["properties"]["using_local_repo"]) &&
  55. $currentConfigs["properties"]["using_local_repo"] != "") {
  56. $useLocalYumRepo = $currentConfigs["properties"]["using_local_repo"];
  57. }
  58. if (isset($currentConfigs["properties"]["yum_repo_file"])
  59. && $currentConfigs["properties"]["yum_repo_file"] != "") {
  60. $repoFilePath = $currentConfigs["properties"]["yum_repo_file"];
  61. }
  62. if (isset($_POST['useLocalYumRepo']) && (trim($_POST['useLocalYumRepo']) != "")) {
  63. $useLocalYumRepo = trim($_POST['useLocalYumRepo']);
  64. }
  65. if (isset($_POST['yumRepoFilePath'])
  66. && trim($_POST['yumRepoFilePath']) != "") {
  67. $repoFilePath = trim($_POST['yumRepoFilePath']);
  68. }
  69. header("Content-type: application/json");
  70. if (!file_exists($repoFilePath)) {
  71. $logger->log_warn("Invalid repo file provided, file does not exist"
  72. . ", repoFile=" . $repoFilePath);
  73. print (json_encode(array(
  74. "result" => 1,
  75. "error" => "Invalid repo file path specified"
  76. )
  77. ));
  78. return;
  79. }
  80. // TODO - error checks for download urls
  81. /*
  82. if (parse_url($hdpArtifactsDownloadUrl) === FALSE
  83. || parse_url($gplArtifactsDownloadUrl) === FALSE) {
  84. $logger->log_warn("Invalid download urls provided, could not parse"
  85. . ", hdpArtifactsDownloadUrl=" . $hdpArtifactsDownloadUrl
  86. . ", gplArtifactsDownloadUrl=" . $gplArtifactsDownloadUrl);
  87. print (json_encode(array(
  88. "result" => 1,
  89. "error" => "Invalid download urls specified")));
  90. return;
  91. }
  92. */
  93. $configs = array ( "using_local_repo" => $useLocalYumRepo,
  94. "yum_repo_file" => $repoFilePath);
  95. $dbResponse = $dbAccessor->updateServiceConfigs($clusterName, $configs);
  96. if ($dbResponse["result"] != 0) {
  97. $logger->log_error("Got error while persisting configs: ".$dbResponse["error"]);
  98. return $dbResponse;
  99. }
  100. $stagesFiles = "";
  101. if ($action == "addNodes") {
  102. $stagesFile = "./addNodes/stages.php";
  103. } else if ($action == "uninstall") {
  104. $stagesFile = "./uninstall/stages.php";
  105. } else {
  106. print (json_encode(array(
  107. "result" => 1,
  108. "error" => "Invalid action",
  109. )
  110. ));
  111. return;
  112. }
  113. error_log("ClusterName: ".$clusterName);
  114. $logger->log_info("ClusterName: $clusterName\n");
  115. $logger->log_info("Deploy User: $deployUser\n");
  116. // this api just creates a new transaction id for the db
  117. $status = "";
  118. $createTxResult = HMCTxnUtils::createNewTransaction($dbAccessor, $clusterName, $status);
  119. if ($createTxResult == FALSE) {
  120. $msg = "SETUP: Failed to create new transaction in background: $createTxResult\n";
  121. $logger->log_error($msg);
  122. print (json_encode(array("result" => 1, "error" => "$msg")));
  123. return;
  124. }
  125. $rootTxnId = $createTxResult;
  126. $logger->log_info("Root Txn Id = $rootTxnId\n");
  127. $cmd = $GLOBALS["PHP_EXEC_PATH"] . " " . "../util/sequentialScriptRunner.php";
  128. // $cmd = $GLOBALS["PHP_EXEC_PATH"] . " " . "./addNodes/addNodesOrchestrator.php";
  129. $hostsFile = getHostsFilePath($clusterName);
  130. $args = "$clusterName $deployUser $rootTxnId $hostsFile $stagesFile";
  131. $execBackgroundResult = HMCTxnUtils::execBackgroundProcess($dbAccessor, $clusterName, $rootTxnId, $cmd, $args, "");
  132. if ($execBackgroundResult == FALSE) {
  133. $msg = "Failed to execute addNodesOrchestrator in background: $execBackgroundResult\n";
  134. $logger->log_error($msg);
  135. print (json_encode(array("result" => 1, "error" => "$msg")));
  136. return;
  137. }
  138. $result = 0;
  139. $error = "";
  140. $state = "CONFIGURATION_IN_PROGRESS";
  141. $displayName = "Configuration in progress";
  142. $context = array (
  143. 'stage' => "ADD_NODES",
  144. 'txnId' => $rootTxnId
  145. );
  146. /* Only updateClusterState() if we're in the middle of configuring the cluster
  147. * the first time through.
  148. */
  149. $clusterStateResponse = $dbAccessor->getClusterState($clusterName);
  150. if ($clusterStateResponse['result'] != 0) {
  151. print json_encode($clusterStateResponse);
  152. return;
  153. }
  154. $clusterState = json_decode($clusterStateResponse['state'], true);
  155. if ($clusterState['state'] == 'CONFIGURATION_IN_PROGRESS') {
  156. // update state of the cluster to be configuration in progress
  157. $retval = updateClusterState($clusterName, $state, $displayName, $context);
  158. if ($retval['result'] != 0) {
  159. $result = $retval['result'];
  160. $error = $retval['error'];
  161. }
  162. }
  163. print (json_encode(array(
  164. "result" => $result,
  165. "error" => $error,
  166. "response" => array(
  167. "clusterName" => $clusterName,
  168. "txnId" => $rootTxnId,
  169. )
  170. )
  171. ));
  172. ?>