createCluster.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. header("Content-type: application/json");
  29. $logger = new HMCLogger("CreateCluster");
  30. $dbAccessor = new HMCDBAccessor($GLOBALS["DB_PATH"]);
  31. $multipleClustersSupported = false;
  32. $allClustersResult = $dbAccessor->getAllClusters();
  33. $listOfClusters = array();
  34. if ($allClustersResult["result"] != 0) {
  35. print(json_encode($allClustersResult));
  36. return;
  37. }
  38. // Read from the input
  39. $requestdata = file_get_contents('php://input');
  40. $requestObj = json_decode($requestdata, true);
  41. $clusterName = trim($requestObj["clusterName"]);
  42. if ($clusterName == "") {
  43. print json_encode(array("result" => 1, "error" => "Cluster name cannot be empty"));
  44. return;
  45. }
  46. if (preg_match("/[\s]+/", $clusterName) > 0) {
  47. print json_encode(array("result" => 1, "error" => "Cluster name cannot contain whitespaces"));
  48. return;
  49. }
  50. // do not allow ? ! * + - | " [ ] / ( ) { } ! @ # $ % ^ & * ( ) ' ` ~ , .
  51. if (preg_match('/(\?|\+|\-|\||\"|\[|\]|\/|\{|\}|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\'|\`|\~|\,|\.)/', $clusterName) > 0) {
  52. print json_encode(array("result" => 1, "error" => "Cluster name cannot contain special characters"));
  53. return;
  54. }
  55. // Validate clusterName: TODO; FIXME
  56. /*
  57. if (!array_key_exists($clusterName, $allClustersResult["clusters"])) {
  58. if (!$multipleClustersSupported && count($allClustersResult["clusters"]) != 0 ) {
  59. print (json_encode(array( "result" => 1, "error" => "Multiple clusters are not supported and you already have a cluster installed" )));
  60. return;
  61. }
  62. }
  63. */
  64. // create the lockfile in the clusterDir
  65. $fileHdl = fopen($GLOBALS["HMC_CLUSTER_PATH"]."/lockfile", "w");
  66. if ($fileHdl == false) {
  67. print json_encode(array( "result" => 1, "error" => 'Failed to create lock file...'));
  68. return;
  69. }
  70. fclose($fileHdl);
  71. // if user re-enters this page, we need to delete and restart the cluster conf
  72. $dbAccessor->wipeOutClusters($clusterName);
  73. $logger->log_info("Completed deletion of cluster: ".$clusterName);
  74. $hdpVersion="1.0"; // TODO: hardcoded
  75. $state="Configuration in progress";
  76. $response = $dbAccessor->createCluster($clusterName, $hdpVersion, $state);
  77. // Return errors from response
  78. if ($response["result"] != 0) {
  79. $logger->log_debug(print_r($response, true));
  80. print json_encode($response);
  81. return;
  82. }
  83. $clusterName = $response["clusterName"];
  84. // Populate the ServiceComponentInfo table
  85. $allServiceComps = array();
  86. $allServicesArray = $dbAccessor->getAllServicesList();
  87. if ($allServicesArray["result"] != 0 ) {
  88. $logger->log_error("Got error while getting all services list ".$allServicesArray["error"]);
  89. print json_encode($allServicesArray);
  90. return;
  91. }
  92. $allServicesInfo = $dbAccessor->getAllServicesInfo($clusterName);
  93. if ($allServicesInfo["result"] != 0 ) {
  94. $logger->log_error("Got error while getting all services list ".$allServicesInfo["error"]);
  95. print json_encode($allServicesInfo);
  96. return;
  97. }
  98. foreach($allServicesArray["services"] as $service) {
  99. $thisService = array();
  100. $serviceName = $service["serviceName"];
  101. $componentsStaticTableDBResult = $dbAccessor->getAllServiceComponents($serviceName);
  102. if ($componentsStaticTableDBResult["result"] != 0 ) {
  103. $logger->log_error("Got error while getting all service components:".$componentsStaticTableDBResult["error"]);
  104. print json_encode($componentsStaticTableDBResult);
  105. return;
  106. }
  107. foreach($componentsStaticTableDBResult["components"] as $componentName => $component) {
  108. $componentArray = array();
  109. $componentArray["state"] = "UNKNOWN";
  110. $componentArray["desiredState"] = "UNKNOWN";
  111. $thisService[$componentName] = $componentArray;
  112. }
  113. $allServiceComps[$serviceName] = $thisService;
  114. }
  115. $result = $dbAccessor->addServiceComponentsToCluster($clusterName, $allServiceComps);
  116. if ($result["result"] != 0 ) {
  117. $logger->log_error("Got error while adding all service components:".$result["error"]);
  118. print json_encode($result);
  119. return;
  120. }
  121. // end of populating the ServiceComponentInfo table
  122. $propertiesArr = $dbAccessor->getConfigPropertiesMetaInfo();
  123. if ($propertiesArr["result"] != 0) {
  124. print json_encode(array( "result" => 1, "error" => "Error in config properties meta info"));
  125. return;
  126. }
  127. $result = 0;
  128. $error = "";
  129. $state = "CONFIGURATION_IN_PROGRESS";
  130. $displayName = "Configuration in progress";
  131. $context = array (
  132. 'stage' => "CREATE_CLUSTER"
  133. );
  134. // update state of the cluster to be configuration in progress
  135. $retval = updateClusterState($clusterName, $state, $displayName, $context);
  136. if ($retval['result'] != 0) {
  137. $result = $retval['result'];
  138. $error = $retval['error'];
  139. }
  140. $output = array(
  141. "result" => $result,
  142. "error" => $error,
  143. "response" => array(
  144. "clusterName" => $response["clusterName"]
  145. )
  146. );
  147. print (json_encode($output));
  148. ?>