manageServices.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 "../util/lock.php";
  25. include_once '../db/HMCDBAccessor.php';
  26. include_once "../orchestrator/HMC.php";
  27. include_once "../orchestrator/Service.php";
  28. include_once "../orchestrator/ServiceComponent.php";
  29. include_once "../db/Transaction.php";
  30. include_once "../db/OrchestratorDB.php";
  31. include_once "../puppet/DBReader.php";
  32. include_once "../puppet/PuppetInvoker.php";
  33. include_once "configUtils.php";
  34. include_once '../util/suggestProperties.php';
  35. include_once "../util/clusterState.php";
  36. function performServiceManagement( $hmc, $requestObj )
  37. {
  38. global $logger;
  39. global $dbAccessor;
  40. global $clusterName;
  41. /* What we're here to return. */
  42. $serviceManagementResult = array();
  43. $action = $requestObj['action'];
  44. $serviceNames = array_keys($requestObj['services']);
  45. switch( $action )
  46. {
  47. case 'startAll':
  48. $serviceManagementResult = $hmc->startAllServices();
  49. break;
  50. case 'stopAll':
  51. $serviceManagementResult = $hmc->stopAllServices();
  52. break;
  53. case 'start':
  54. if( count($serviceNames) > 0 ) {
  55. $serviceManagementResult = $hmc->startServices( $serviceNames );
  56. }
  57. break;
  58. case 'stop':
  59. if( count($serviceNames) > 0 ) {
  60. $serviceManagementResult = $hmc->stopServices( $serviceNames );
  61. }
  62. break;
  63. case 'reconfigure':
  64. if( count($serviceNames) > 0 ) {
  65. /* Read additional data from $requestObj and update the DB
  66. * accordingly before attempting to call $hmc->reconfigureServices().
  67. */
  68. // re-using persistConfigs code
  69. $serviceManagementResult = validateAndPersistConfigsFromUser($dbAccessor, $logger, $clusterName, $requestObj['services']);
  70. if ($serviceManagementResult['result'] != 0) {
  71. $logger->log_error("Failed to validate configs from user, error=" . $serviceManagementResult["error"]);
  72. return $serviceManagementResult;
  73. }
  74. /* Also, remember to save a snapshot of the outgoing service
  75. * config in the 'ConfigHistory' table.
  76. */
  77. $changeMsg = "Reconfiguring services, list=" . implode(",", $serviceNames);
  78. $serviceManagementResult = $dbAccessor->createServiceConfigSnapshot($clusterName, $changeMsg);
  79. if ($serviceManagementResult['result'] != 0) {
  80. $logger->log_error("Failed to create config snapshot in DB, error=" . $serviceManagementResult["error"]);
  81. return $serviceManagementResult;
  82. }
  83. $serviceManagementResult = $hmc->reconfigureServices( $serviceNames );
  84. }
  85. break;
  86. default:
  87. $logger->log_error( "Unrecognized action '" . $action . "' requested" );
  88. $serviceManagementResult = array ( "result" => -1 , "error" => "Invalid action, action=" . $action);
  89. break;
  90. }
  91. return $serviceManagementResult;
  92. }
  93. $dbPath = $GLOBALS["DB_PATH"];
  94. $clusterName = $_GET['clusterName'];
  95. /* For returning in our JSON at the very end. */
  96. $result = 0;
  97. $error = "";
  98. $txnId = -1;
  99. $logger = new HMCLogger("ManageServices");
  100. $dbAccessor = new HMCDBAccessor($dbPath);
  101. $clusterStateResponse = $dbAccessor->getClusterState($clusterName);
  102. if ($clusterStateResponse['result'] != 0) {
  103. print json_encode($clusterStateResponse);
  104. return;
  105. }
  106. $clusterState = json_decode($clusterStateResponse['state'], true);
  107. /* Perform the actual management only if this cluster is in a deployed state
  108. * (regardless of whether the deploy was a success or failure).
  109. */
  110. if ($clusterState['state'] == 'DEPLOYED') {
  111. $hmc = new HMC($dbPath, $clusterName);
  112. /* Slurp in the POST body. */
  113. $requestData = file_get_contents('php://input');
  114. $requestObj = json_decode($requestData, true);
  115. /* The Main Event. */
  116. $serviceManagementResult = performServiceManagement($hmc, $requestObj);
  117. if ($serviceManagementResult["result"] != 0 ) {
  118. $logger->log_error("Failed to take an action in manage services, error=" . $serviceManagementResult["error"]);
  119. print json_encode($serviceManagementResult);
  120. return;
  121. }
  122. $txnId = $serviceManagementResult["txnId"];
  123. /* (And when we kick off the management action is the only time to update the
  124. * state of the cluster).
  125. */
  126. $state = "SERVICE_MANAGEMENT_IN_PROGRESS";
  127. $displayName = "Service management in progress";
  128. $context = array (
  129. 'txnId' => $txnId,
  130. /* We've come here only if the cluster is in the "DEPLOYED" state, so the
  131. * state we stash is the state at the end of the deploy - we'll restore
  132. * this state at the end of the service management action.
  133. */
  134. 'stashedDeployState' => $clusterState
  135. );
  136. $retval = updateClusterState($clusterName, $state, $displayName, $context);
  137. if ($retval['result'] != 0) {
  138. $result = $retval['result'];
  139. $error = $retval['error'];
  140. }
  141. }
  142. /* In case a management operation is already running, just return the txnId
  143. * from the DB instead of kicking off a fresh managemen action - this is so
  144. * we can try and preclude multiple managements occurring in parallel.
  145. */
  146. elseif ($clusterState['state'] == 'SERVICE_MANAGEMENT_IN_PROGRESS') {
  147. $txnId = $clusterState['context']['txnId'];
  148. }
  149. /* Create the output data... */
  150. $jsonOutput = array(
  151. 'clusterName' => $clusterName,
  152. 'txnId' => $txnId
  153. );
  154. /* ...and spit it out. */
  155. header("Content-type: application/json");
  156. print (json_encode(array("result" => $result, "error" => $error, "response" => $jsonOutput)));
  157. ?>