selectServices.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. include_once '../util/Logger.php';
  3. include_once '../conf/Config.inc';
  4. include_once 'localDirs.php';
  5. include_once "../util/lock.php";
  6. include_once '../db/HMCDBAccessor.php';
  7. include_once "../util/clusterState.php";
  8. include_once '../util/selectNodes.php';
  9. // include_once 'install_puppet.php';
  10. $logger = new HMCLogger("InitializeCluster");
  11. $dbAccessor = new HMCDBAccessor($GLOBALS["DB_PATH"]);
  12. $selectNodes = new SelectNodes();
  13. header("Content-type: application/json");
  14. $clusterName = $_GET['clusterName'];
  15. // Validate clusterId: TODO; FIXME
  16. // We need to clean up prior instances for this cluster name
  17. $dbAccessor->cleanupServices($clusterName);
  18. // Read from the input
  19. $requestdata = file_get_contents('php://input');
  20. $requestObj = json_decode($requestdata, true);
  21. $deployUser = $requestObj['ClusterDeployUser'];
  22. // Persist the services enabled stuff
  23. $servicesCheckList = $requestObj["services"];
  24. // create local copy for easy lookup
  25. $tempLocalServicesCopy = array();
  26. foreach($servicesCheckList as $serviceInfo) {
  27. $tempLocalServicesCopy[$serviceInfo["serviceName"]] = array("isEnabled" => $serviceInfo["isEnabled"]);
  28. }
  29. $logger->log_debug("temp local".json_encode($tempLocalServicesCopy)."========");
  30. $svcDepsListArr = $dbAccessor->getAllServiceDependencies();
  31. if ($svcDepsListArr["result"] != 0) {
  32. $logger->log_error("Got error while getting service dependencies: ".$svcDepsListArr["error"]);
  33. print json_encode($svcDepsListArr);
  34. return;
  35. }
  36. $svcDepsList = $svcDepsListArr["serviceDependencies"];
  37. // generate full svc list based on dep tree
  38. $localServicesCopy = array();
  39. foreach ($tempLocalServicesCopy as $svcName => $svcInfo) {
  40. if (!$svcInfo["isEnabled"]
  41. && !isset($localServicesCopy[$svcName])) {
  42. $localServicesCopy[$svcName] = $svcInfo;
  43. continue;
  44. }
  45. $localServicesCopy[$svcName] = array("isEnabled" => TRUE);
  46. $svcDeps = $dbAccessor->getRecursiveServiceDependency(
  47. $svcDepsList, $svcName);
  48. if (is_array($svcDeps)) {
  49. foreach ($svcDeps as $svcDep) {
  50. $localServicesCopy[$svcDep] = array("isEnabled" => TRUE);
  51. }
  52. }
  53. }
  54. $servicesCheckList = array();
  55. foreach ($localServicesCopy as $svcName => $svcInfo) {
  56. $svcInfo["serviceName"] = $svcName;
  57. array_push($servicesCheckList, $svcInfo);
  58. }
  59. $logger->log_debug("svc list".json_encode($servicesCheckList)."========");
  60. $dbResponse = $dbAccessor->addServicesToCluster($clusterName, $servicesCheckList);
  61. if ($dbResponse["result"] != 0) {
  62. $logger->log_error("Got error while adding service: ".$dbResponse["error"]);
  63. print json_encode($dbResponse);
  64. return;
  65. }
  66. $jsonOutput["clusterName"] = $clusterName;
  67. // Populate all hosts for the UI
  68. $jsonOutput["allHosts"] = array();
  69. /*
  70. * FIXME need to get all hosts from select Nodes script
  71. */
  72. $allHostsInfo = $dbAccessor->getAllHostsInfo($clusterName,
  73. array("=" => array ( "discoveryStatus" => "SUCCESS")),
  74. array(
  75. array("sortColumn" => "totalMem", "sortOrder" => "DESC"),
  76. array("sortColumn" => "cpuCount", "sortOrder" => "DESC"),
  77. array("sortColumn" => "hostName", "sortOrder" => "ASC")
  78. )
  79. );
  80. if ($allHostsInfo["result"] != 0 ) {
  81. $logger->log_error("Got error while getting hostsInfo ".$allHostsInfo["error"]);
  82. print json_encode($allHostsInfo);
  83. return;
  84. }
  85. $logger->log_debug("HOSTS_INFO: ".json_encode($allHostsInfo["hosts"]));
  86. foreach($allHostsInfo["hosts"] as $hostInfo) {
  87. $logger->log_debug("HOST: ".json_encode($hostInfo));
  88. array_push($jsonOutput["allHosts"], $hostInfo);
  89. }
  90. // bootstrap($jsonOutput["allHosts"], getSshKeyFilePath($clusterName));
  91. $suggestedNodes = $selectNodes->selectNodes($clusterName,
  92. $dbAccessor);
  93. //////////////////// Populate service masters for the UI ///////////////////////////
  94. $jsonOutput["services"] = array();
  95. $allServicesArray = $dbAccessor->getAllServicesList();
  96. if ($allServicesArray["result"] != 0 ) {
  97. $logger->log_error("Got error while getting all services list ".$allServicesArray["error"]);
  98. print json_encode($allServicesArray);
  99. return;
  100. }
  101. $allServicesInfo = $dbAccessor->getAllServicesInfo($clusterName);
  102. if ($allServicesInfo["result"] != 0 ) {
  103. $logger->log_error("Got error while getting all services list ".$allServicesInfo["error"]);
  104. print json_encode($allServicesInfo);
  105. return;
  106. }
  107. foreach($allServicesArray["services"] as $service) {
  108. $thisService = array();
  109. $serviceName = $service["serviceName"];
  110. $thisService["enabled"] = $allServicesInfo["services"][$serviceName]["isEnabled"];
  111. $thisService["masters"] = array();
  112. $allServiceComponents = $dbAccessor->getAllServiceComponents($serviceName);
  113. if ($allServiceComponents["result"] != 0 ) {
  114. $logger->log_error("Got error while getting all service compoenents:".$allServiceComponents["error"]);
  115. print json_encode($allServiceComponents);
  116. return;
  117. }
  118. foreach($allServiceComponents["components"] as $componentName => $component) {
  119. if($component["isMaster"] == 0) {
  120. continue;
  121. }
  122. $thisMaster = array();
  123. $hosts = $suggestedNodes["mastersToHosts"][$componentName]["hostNames"];
  124. $thisMaster["name"] = $component["componentName"];
  125. $thisMaster["displayName"] = $component["displayName"];
  126. $thisMaster["description"] = $component["description"];
  127. $thisMaster["hostNames"] =
  128. $hosts;
  129. array_push($thisService["masters"], $thisMaster);
  130. }
  131. array_push($jsonOutput["services"], $thisService);
  132. }
  133. //////////////////// End of populate service masters for the UI ///////////////////////////
  134. // Update the state of the cluster.
  135. $result = 0;
  136. $error = "";
  137. $state = "CONFIGURATION_IN_PROGRESS";
  138. $displayName = "Configuration in progress";
  139. $context = array (
  140. 'stage' => "SELECT_SERVICES"
  141. );
  142. $retval = updateClusterState($clusterName, $state, $displayName, $context);
  143. if ($retval['result'] != 0) {
  144. $result = $retval['result'];
  145. $error = $retval['error'];
  146. }
  147. print (json_encode(array("result" => $result, "error" => $error, "response" => $jsonOutput)));
  148. ?>