addNodes.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/util.php';
  29. include_once './commandUtils.php';
  30. /*
  31. sleep(3);
  32. */
  33. $logger = new HMCLogger("UploadFiles");
  34. $dbAccessor = new HMCDBAccessor($GLOBALS["DB_PATH"]);
  35. $clusterName = $_GET['clusterName'];
  36. /* Figure out whether it's a fresh install or an AddNodesWizard-ish flow. */
  37. $clusterStateResponse = $dbAccessor->getClusterState($clusterName);
  38. if ($clusterStateResponse['result'] != 0) {
  39. print json_encode($clusterStateResponse);
  40. return;
  41. }
  42. $clusterState = json_decode($clusterStateResponse['state'], true);
  43. $freshInstall = ($clusterState['state'] == 'CONFIGURATION_IN_PROGRESS') ? true : false;
  44. $clusterDir = getClusterDir($clusterName);
  45. $logString = "Cluster Name: $clusterName Cleanup required? $freshInstall and type: ".gettype($freshInstall);
  46. $logger->log_info($logString);
  47. // Validate clusterName: TODO; FIXME
  48. // We need to clean up prior instances for this
  49. // cluster name if this is a fresh install
  50. if ($freshInstall) {
  51. $dbAccessor->cleanupHosts($clusterName);
  52. }
  53. // Read from the input
  54. $deployUser = $_POST['ClusterDeployUser'];
  55. /////// Remove the cluster directory before copying the files
  56. $clusterDir = getClusterDir($clusterName);
  57. rrmdir($clusterDir);
  58. if (!is_dir($clusterDir) && !mkdir($clusterDir, 0700, true)) {
  59. print json_encode(array( "result" => 1, "error" => 'Failed to create directory...'));
  60. return;
  61. }
  62. ////// end of directory removal
  63. $identityFileDestination = getSshKeyFilePath($clusterName);
  64. $hostsFileDestination = getHostsFilePath($clusterName);
  65. // TODO: FIXME: Change echos to return error codes.
  66. //echo '<pre>';
  67. if (move_uploaded_file($_FILES['clusterDeployUserIdentityFile']['tmp_name'], $identityFileDestination)) {
  68. //echo "File is valid, and was successfully uploaded.\n";
  69. // Set the permissions
  70. chmod($identityFileDestination, 0400);// TODO: Error conditions
  71. } else {
  72. $logger->log_error("Identity file copy to loc ".$identityFileDestination." failed");
  73. //echo "Possible file upload attack!\n";
  74. }
  75. if (move_uploaded_file($_FILES['clusterHostsFile']['tmp_name'], $hostsFileDestination)) {
  76. //echo "File is valid, and was successfully uploaded.\n";
  77. } else {
  78. $logger->log_error("Hosts file copy to loc ".$hostsFileDestination." failed");
  79. //echo "Possible file upload attack!\n";
  80. }
  81. removeCarriageReturn($hostsFileDestination);
  82. header("Content-type: application/json");
  83. // Validate that there are no nodes that are already in use in case of addNodesWizard
  84. $logger->log_info("Doing a fresh install: $freshInstall");
  85. if (!$freshInstall) {
  86. // Get the list of current nodes
  87. $allHostsInfoResult = $dbAccessor->getAllHostsInfo("", array());
  88. if ($allHostsInfoResult["result"] != 0 ) {
  89. $logger->log_error("Got error while getting hostInfo for $host :" .$allHostsInfoResult["error"]);
  90. print json_encode($allHostsInfoResult);
  91. return;
  92. }
  93. $unassignedHostResult = $dbAccessor->getAllUnassignedHosts($clusterName);
  94. if ($unassignedHostResult["result"] != 0) {
  95. print json_encode($unassignedHostResult);
  96. return;
  97. }
  98. $unassignedHostList = $unassignedHostResult["hosts"];
  99. // See if they are duplicates
  100. $newHosts = readHostsFile($hostsFileDestination);
  101. $duplicateHosts = array();
  102. $logger->log_debug("Checking for Duplicate Hosts.");
  103. foreach ($allHostsInfoResult["hosts"] as $hostInfo) {
  104. $logger->log_debug("Checking for Duplicate Hosts. hostname = ".
  105. $hostInfo["hostName"]. " key does not exist? " .
  106. array_key_exists($hostInfo["hostName"], $unassignedHostList).
  107. " incoming cluster name: $clusterName, host is part of cluster "
  108. .$hostInfo["clusterName"]);
  109. if (in_array($hostInfo["hostName"], $newHosts) &&
  110. (!(array_key_exists($hostInfo["hostName"], $unassignedHostList))
  111. || ($hostInfo["clusterName"] != $clusterName))) {
  112. if (!array_key_exists($hostInfo["clusterName"], $duplicateHosts)) {
  113. $duplicateHosts[$hostInfo["clusterName"]] = array();
  114. }
  115. array_push($duplicateHosts[$hostInfo["clusterName"]],
  116. $hostInfo["hostName"]);
  117. }
  118. }
  119. $numDupHosts = count($duplicateHosts);
  120. $numNewHosts = count($newHosts);
  121. if ($numDupHosts != 0) {
  122. $logger->log_warn("Trying to add duplicate nodes to the cluster, dups="
  123. . print_r($duplicateHosts, true));
  124. print (json_encode(array("result" => 3, "error" => "Some hosts in the given file are already being used in cluster", "hosts" => $duplicateHosts)));
  125. return;
  126. }
  127. } else {
  128. // Update the state of the cluster.
  129. $state = "CONFIGURATION_IN_PROGRESS";
  130. $displayName = "Configuration in progress";
  131. $context = array (
  132. 'stage' => "ADD_NODES"
  133. );
  134. $retval = updateClusterState($clusterName, $state, $displayName, $context);
  135. }
  136. print (json_encode(array("result" => 0)));
  137. /*
  138. $outjson = array(
  139. "errorCode"=> $retval['result'],
  140. // "clusterName" => $clusterName,
  141. // "finalDestination" => $identityFileDestination,
  142. // "fileToBeMoved" => $_FILES['clusterDeployUserIdentityFile']['tmp_name'],
  143. );
  144. */
  145. // TODO: FIXME: Check file lengths.
  146. ?>