selectServices.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "../util/clusterState.php";
  28. include_once '../util/selectNodes.php';
  29. // include_once 'install_puppet.php';
  30. $logger = new HMCLogger("InitializeCluster");
  31. $dbAccessor = new HMCDBAccessor($GLOBALS["DB_PATH"]);
  32. $selectNodes = new SelectNodes();
  33. header("Content-type: application/json");
  34. $clusterName = $_GET['clusterName'];
  35. // Validate clusterId: TODO; FIXME
  36. /////// only persist the yum repo stuff
  37. $serviceConfigResult = $dbAccessor->getServiceConfig($clusterName);
  38. if ($serviceConfigResult["result"] != 0) {
  39. $logger->log_error("Failed to get service config ".json_encode($serviceConfigResult));
  40. print (json_encode($serviceConfigResult));
  41. return;
  42. }
  43. // We need to clean up prior instances for this cluster name
  44. $dbAccessor->cleanupServices($clusterName);
  45. $yumInfoArray = array("using_local_repo" => $serviceConfigResult['properties']['using_local_repo'],
  46. "yum_repo_file" => $serviceConfigResult['properties']['yum_repo_file']
  47. );
  48. $dbResponse = $dbAccessor->updateServiceConfigs($clusterName, $yumInfoArray);
  49. if ($dbResponse["result"] != 0) {
  50. $logger->log_error("Got error while persisting configs: ".$dbResponse["error"]);
  51. print (json_encode($dbResponse));
  52. return;
  53. }
  54. ////// done persisting
  55. // Read from the input
  56. $requestdata = file_get_contents('php://input');
  57. $requestObj = json_decode($requestdata, true);
  58. // Persist the services enabled stuff
  59. $servicesCheckList = $requestObj["services"];
  60. // create local copy for easy lookup
  61. $tempLocalServicesCopy = array();
  62. foreach($servicesCheckList as $serviceInfo) {
  63. $tempLocalServicesCopy[$serviceInfo["serviceName"]] = array("isEnabled" => $serviceInfo["isEnabled"]);
  64. }
  65. $logger->log_debug("Temp local copy for services \n".json_encode($tempLocalServicesCopy));
  66. $svcDepsListArr = $dbAccessor->getAllServiceDependencies();
  67. if ($svcDepsListArr["result"] != 0) {
  68. $logger->log_error("Got error while getting service dependencies: ".$svcDepsListArr["error"]);
  69. print json_encode($svcDepsListArr);
  70. return;
  71. }
  72. $svcDepsList = $svcDepsListArr["serviceDependencies"];
  73. // generate full svc list based on dep tree
  74. $localServicesCopy = array();
  75. foreach ($tempLocalServicesCopy as $svcName => $svcInfo) {
  76. if (!$svcInfo["isEnabled"]
  77. && !isset($localServicesCopy[$svcName])) {
  78. $localServicesCopy[$svcName] = $svcInfo;
  79. continue;
  80. }
  81. $localServicesCopy[$svcName] = array("isEnabled" => TRUE);
  82. $svcDeps = $dbAccessor->getRecursiveServiceDependency(
  83. $svcDepsList, $svcName);
  84. if (is_array($svcDeps)) {
  85. foreach ($svcDeps as $svcDep) {
  86. $localServicesCopy[$svcDep] = array("isEnabled" => TRUE);
  87. }
  88. }
  89. }
  90. $servicesCheckList = array();
  91. foreach ($localServicesCopy as $svcName => $svcInfo) {
  92. $svcInfo["serviceName"] = $svcName;
  93. array_push($servicesCheckList, $svcInfo);
  94. }
  95. $logger->log_debug("svc list".json_encode($servicesCheckList)."========");
  96. $dbResponse = $dbAccessor->addServicesToCluster($clusterName, $servicesCheckList);
  97. if ($dbResponse["result"] != 0) {
  98. $logger->log_error("Got error while adding service: ".$dbResponse["error"]);
  99. print json_encode($dbResponse);
  100. return;
  101. }
  102. $jsonOutput["clusterName"] = $clusterName;
  103. // Populate all hosts for the UI
  104. $jsonOutput["allHosts"] = array();
  105. /*
  106. * FIXME need to get all hosts from select Nodes script
  107. */
  108. $allHostsInfo = $dbAccessor->getAllHostsInfo($clusterName,
  109. array("=" => array ( "discoveryStatus" => "SUCCESS")),
  110. array(
  111. array("sortColumn" => "totalMem", "sortOrder" => "DESC"),
  112. array("sortColumn" => "cpuCount", "sortOrder" => "DESC"),
  113. array("sortColumn" => "hostName", "sortOrder" => "ASC")
  114. )
  115. );
  116. if ($allHostsInfo["result"] != 0 ) {
  117. $logger->log_error("Got error while getting hostsInfo ".$allHostsInfo["error"]);
  118. print json_encode($allHostsInfo);
  119. return;
  120. }
  121. $logger->log_debug("HOSTS_INFO: ".json_encode($allHostsInfo["hosts"]));
  122. foreach($allHostsInfo["hosts"] as $hostInfo) {
  123. $logger->log_debug("HOST: ".json_encode($hostInfo));
  124. array_push($jsonOutput["allHosts"], $hostInfo);
  125. }
  126. // bootstrap($jsonOutput["allHosts"], getSshKeyFilePath($clusterName));
  127. $suggestedNodes = $selectNodes->selectNodes($clusterName,
  128. $dbAccessor);
  129. //////////////////// Populate service masters for the UI ///////////////////////////
  130. $jsonOutput["services"] = array();
  131. $allServicesArray = $dbAccessor->getAllServicesList();
  132. if ($allServicesArray["result"] != 0 ) {
  133. $logger->log_error("Got error while getting all services list ".$allServicesArray["error"]);
  134. print json_encode($allServicesArray);
  135. return;
  136. }
  137. $allServicesInfo = $dbAccessor->getAllServicesInfo($clusterName);
  138. if ($allServicesInfo["result"] != 0 ) {
  139. $logger->log_error("Got error while getting all services list ".$allServicesInfo["error"]);
  140. print json_encode($allServicesInfo);
  141. return;
  142. }
  143. foreach($allServicesArray["services"] as $service) {
  144. $thisService = array();
  145. $serviceName = $service["serviceName"];
  146. $thisService["enabled"] = $allServicesInfo["services"][$serviceName]["isEnabled"];
  147. $thisService["masters"] = array();
  148. $allServiceComponents = $dbAccessor->getAllServiceComponents($serviceName);
  149. if ($allServiceComponents["result"] != 0 ) {
  150. $logger->log_error("Got error while getting all service compoenents:".$allServiceComponents["error"]);
  151. print json_encode($allServiceComponents);
  152. return;
  153. }
  154. foreach($allServiceComponents["components"] as $componentName => $component) {
  155. if($component["isMaster"] == 0) {
  156. continue;
  157. }
  158. $thisMaster = array();
  159. $hosts = $suggestedNodes["mastersToHosts"][$componentName]["hostNames"];
  160. $thisMaster["name"] = $component["componentName"];
  161. $thisMaster["displayName"] = $component["displayName"];
  162. $thisMaster["description"] = $component["description"];
  163. $thisMaster["hostNames"] =
  164. $hosts;
  165. array_push($thisService["masters"], $thisMaster);
  166. }
  167. array_push($jsonOutput["services"], $thisService);
  168. }
  169. //////////////////// End of populate service masters for the UI ///////////////////////////
  170. $jsonOutput['managerHostName'] = strtolower(exec('hostname -f'));
  171. // Update the state of the cluster.
  172. $result = 0;
  173. $error = "";
  174. $state = "CONFIGURATION_IN_PROGRESS";
  175. $displayName = "Configuration in progress";
  176. $context = array (
  177. 'stage' => "SELECT_SERVICES"
  178. );
  179. $retval = updateClusterState($clusterName, $state, $displayName, $context);
  180. if ($retval['result'] != 0) {
  181. $result = $retval['result'];
  182. $error = $retval['error'];
  183. }
  184. print (json_encode(array("result" => $result, "error" => $error, "response" => $jsonOutput)));
  185. ?>