deployAddedNodes.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "commandUtils.php";
  29. include_once "../orchestrator/HMC.php";
  30. include_once "../db/OrchestratorDB.php";
  31. include_once "../puppet/DBReader.php";
  32. include_once "../puppet/PuppetInvoker.php";
  33. include_once "../util/clusterState.php";
  34. $dbPath = $GLOBALS["DB_PATH"];
  35. $clusterName = $_GET['clusterName'];
  36. /* For returning in our JSON at the very end. */
  37. $result = 0;
  38. $error = "";
  39. $txnId = -1;
  40. $logger = new HMCLogger("deployAddedNodes");
  41. $dbAccessor = new HMCDBAccessor($dbPath);
  42. $clusterStateResponse = $dbAccessor->getClusterState($clusterName);
  43. if ($clusterStateResponse['result'] != 0) {
  44. print json_encode($clusterStateResponse);
  45. return;
  46. }
  47. $clusterState = json_decode($clusterStateResponse['state'], true);
  48. /* Perform the actual addition of nodes only if this cluster is in a
  49. * deployed state (regardless of whether the deploy was a success or failure).
  50. */
  51. if ($clusterState['state'] == 'DEPLOYED') {
  52. $readFromFile = getHostsFilePath($clusterName);
  53. $requestData = file_get_contents('php://input');
  54. $components = json_decode($requestData, true);
  55. $logger->log_debug("Components are ".json_encode($components));
  56. $hmc = new HMC($dbPath, $clusterName);
  57. // Get info about all nodes from hosts file
  58. // the host file has been pruned by the previous stage of addNodes
  59. $goodHosts = readHostsFile($readFromFile);
  60. $goodHosts = convertToLowerCase($goodHosts);
  61. $logger->log_debug("goodHosts: ".json_encode($goodHosts));
  62. /////// Insert roles for these nodes ////////
  63. foreach ($components as $componentName) {
  64. $addHostsToComponentResult = $dbAccessor->addHostsToComponent($clusterName, $componentName, $goodHosts, "UNKNOWN", "UNKNOWN");
  65. if ($addHostsToComponentResult["result"] != 0 ) {
  66. $logger->log_error("Got error adding component $componentName :" .$addHostsToComponentResult["error"]);
  67. print json_encode($addHostsToComponentResult);
  68. return;
  69. }
  70. }
  71. $deployAddedNodesResult = $hmc->deployNodes($goodHosts);
  72. $txnId = $deployAddedNodesResult["txnId"];
  73. /* (And when we kick off the node addition is the only time to update the
  74. * state of the cluster).
  75. */
  76. $state = "NODE_ADDITION_IN_PROGRESS";
  77. $displayName = "Node addition in progress";
  78. $context = array (
  79. 'txnId' => $txnId,
  80. /* We've come here only if the cluster is in the "DEPLOYED" state, so the
  81. * state we stash is the state at the end of the deploy - we'll restore
  82. * this state at the end of the service management action.
  83. */
  84. 'stashedDeployState' => $clusterState
  85. );
  86. $retval = updateClusterState($clusterName, $state, $displayName, $context);
  87. if ($retval['result'] != 0) {
  88. $result = $retval['result'];
  89. $error = $retval['error'];
  90. }
  91. }
  92. /* In case a node addition is already running, just return the txnId
  93. * from the DB instead of kicking off a fresh action - this is so
  94. * we can try and preclude multiple additions occurring in parallel.
  95. */
  96. elseif ($clusterState['state'] == 'NODE_ADDITION_IN_PROGRESS') {
  97. $txnId = $clusterState['context']['txnId'];
  98. }
  99. /* Create the output data... */
  100. $jsonOutput = array(
  101. 'clusterName' => $clusterName,
  102. 'txnId' => $txnId
  103. );
  104. /* ...and spit it out. */
  105. header("Content-type: application/json");
  106. print (json_encode(array("result" => $result, "error" => $error, "response" => $jsonOutput)));
  107. ?>