createCluster.php 5.5 KB

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