fetchClusterServices.php 7.8 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. $dbPath = $GLOBALS["DB_PATH"];
  28. header("Content-type: application/json");
  29. $logger = new HMCLogger("FetchClusterServices");
  30. $dbAccessor = new HMCDBAccessor($dbPath);
  31. $clusterName = $_GET['clusterName'];
  32. if (isset($_GET['getComponents'])) {
  33. $getComponents = $_GET['getComponents'];
  34. } else {
  35. $getComponents = "false";
  36. }
  37. if (isset($_GET['getConfigs'])) {
  38. $getConfigs = $_GET['getConfigs'];
  39. } else {
  40. $getConfigs = "false";
  41. }
  42. if (isset($_GET['serviceName'])) {
  43. $forService = $_GET['serviceName'];
  44. } else {
  45. $forService = "all";
  46. }
  47. function getAllComponentsForService ($serviceName)
  48. {
  49. global $logger, $dbAccessor, $clusterName;
  50. $logger->log_debug("Get all the service components for service $serviceName");
  51. $returnComponentsArray = array();
  52. // populate all components for the services
  53. // includes the isMaster information
  54. $componentResult = $dbAccessor->getAllServiceComponents($serviceName);
  55. if ($componentResult["result"] == 0) {
  56. foreach($componentResult["components"] as $componentName => $component) {
  57. $hostsForComponentDBResult = $dbAccessor->getHostsForComponent($clusterName, $componentName);
  58. if ($hostsForComponentDBResult["result"] != 0 ) {
  59. $logger->log_error("Got error while getting hosts for component ".$hostsForComponentDBResult["error"]);
  60. print json_encode($hostsForComponentDBResult);
  61. return;
  62. }
  63. $allHosts = array_keys($hostsForComponentDBResult["hosts"]);
  64. $component["hostNames"] = $allHosts;
  65. array_push($returnComponentsArray, $component);
  66. }
  67. } else {
  68. $gotError = "Error while getting component info: " . $componentResult["error"];
  69. $logger->log_error($gotError);
  70. print (json_encode( array("result" => 1, "error" => $gotError)));
  71. }
  72. return $returnComponentsArray;
  73. }
  74. /* The Main Event. */
  75. $services = array();
  76. $allServicesInfo = $dbAccessor->getAllServicesList();
  77. if( $allServicesInfo['result'] == 0 ) {
  78. /* This needs to be retrieved once at the beginning for repeatedly passing
  79. * in to getRecursiveServiceDependency() below.
  80. */
  81. $allServiceDependencies = $dbAccessor->getAllServiceDependencies();
  82. if( $allServiceDependencies['result'] == 0 ) {
  83. $clusterServicesInfo = $dbAccessor->getAllServicesInfo($clusterName);
  84. if( $clusterServicesInfo['result'] == 0 ) {
  85. /* Loop through the static catalog of services. */
  86. foreach( $allServicesInfo['services'] as $serviceName => $serviceInfo ) {
  87. /* Initialize each entry in $services with that from the static
  88. * services catalog.
  89. */
  90. if (($forService !== "all") && ($serviceName !== $forService)) {
  91. continue;
  92. }
  93. $services[$serviceName] = $serviceInfo;
  94. /* Next, augment the entry for $serviceName with the dependency list. */
  95. $services[$serviceName]['dependencies'] =
  96. $dbAccessor->getRecursiveServiceDependency
  97. ( $allServiceDependencies['serviceDependencies'], $serviceName );
  98. /* Next, augment the entry for $serviceName with the dependents list. */
  99. $services[$serviceName]['dependents'] =
  100. $dbAccessor->getRecursiveServiceDependents
  101. ( $allServiceDependencies['serviceDependencies'], $serviceName );
  102. /* Finally, if $serviceName has an entry in $clusterServicesInfo (which
  103. * means $serviceName has been selected for $clusterName), tack on some
  104. * additional install-specific data.
  105. */
  106. if( isset($clusterServicesInfo['services'][$serviceName]) ) {
  107. $services[$serviceName]['state'] = $clusterServicesInfo['services'][$serviceName]['state'];
  108. $services[$serviceName]['desiredState'] = $clusterServicesInfo['services'][$serviceName]['desiredState'];
  109. $services[$serviceName]['isEnabled'] = $clusterServicesInfo['services'][$serviceName]['isEnabled'];
  110. if ($getComponents == "true") {
  111. $services[$serviceName]["components"] = getAllComponentsForService($serviceName);
  112. }
  113. }
  114. }
  115. } else {
  116. $gotError = "Error while getting cluster services info: " . $clusterServicesInfo['error'] ;
  117. $logger->log_error($gotError);
  118. print (json_encode( array("result" => 1, "error" => $gotError)));
  119. return;
  120. }
  121. } else {
  122. $gotError = "Error while getting all service dependencies: " . $allServiceDependencies['error'] ;
  123. $logger->log_error($gotError);
  124. print (json_encode( array("result" => 1, "error" => $gotError)));
  125. return;
  126. }
  127. } else {
  128. $gotError = "Error while getting all services info: " . $allServicesInfo['error'] ;
  129. $logger->log_error($gotError);
  130. print (json_encode( array("result" => 1, "error" => $gotError)));
  131. return;
  132. }
  133. if ($getConfigs == "true") {
  134. // populating the properties for the services.
  135. $staticConfigProps = $dbAccessor->getConfigPropertiesMetaInfo();
  136. if ($staticConfigProps["result"] != 0) {
  137. $logger->log_error("Failed to get config properties");
  138. return;
  139. } else { // success case we found the meta info
  140. $dynamicConfigTableResult = $dbAccessor->getServiceConfig($clusterName);
  141. if ($dynamicConfigTableResult["result"] != 0) {
  142. print ("Error in getting configured properties for services"
  143. .$dynamicConfigTableResult["error"]);
  144. return;
  145. } else { // success case we found the configured properties.
  146. $dynamicConfigTable = $dynamicConfigTableResult["properties"];
  147. foreach ($staticConfigProps["configs"] as $key => $propInfo) {
  148. $serviceName = $propInfo["serviceName"];
  149. // if service specific config needs to be passed back,
  150. // bypass all other services.
  151. if (($forService !== "all") && ($forService !== $serviceName)) {
  152. continue;
  153. }
  154. /*
  155. $logger->log_debug("Service static config $serviceName keys ".
  156. json_encode($key));
  157. */
  158. if (!array_key_exists("properties", $services[$serviceName])) {
  159. $services[$serviceName]["properties"] = array();
  160. }
  161. if (isset($dynamicConfigTable[$key])) {
  162. $value = $dynamicConfigTable[$key];
  163. // $logger->log_debug("$value from service config for $key");
  164. } else {
  165. $value = $propInfo["value"];
  166. }
  167. $services[$serviceName]["properties"][$key] = array(
  168. "displayName" => $propInfo["displayName"],
  169. "description" => $propInfo["description"],
  170. "type" => $propInfo["displayType"],
  171. "unit" => $propInfo["displayAttributes"]["unit"],
  172. "value" => $value,
  173. "displayAttributes" => $propInfo["displayAttributes"]
  174. );
  175. }
  176. }
  177. }
  178. }
  179. $result = 0;
  180. $error = "";
  181. /* Create the output data... */
  182. $jsonOutput = array(
  183. 'result' => $result,
  184. 'error' => $error,
  185. 'response' => array(
  186. 'clusterName' => $clusterName,
  187. 'managerHostName' => strtolower(exec('hostname -f')),
  188. 'services' => $services )
  189. );
  190. /* ...and spit it out. */
  191. print (json_encode($jsonOutput));
  192. ?>