uninstallCleanup.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "./uninstall/stages.php";
  8. include_once 'commandUtils.php';
  9. include_once "../util/HMCTxnUtils.php";
  10. $logger = new HMCLogger("uninstallCleanup");
  11. $dbAccessor = new HMCDBAccessor($GLOBALS["DB_PATH"]);
  12. // this function will launch the stages in the background
  13. // and poll for completion.
  14. // upon completion, does the nodesActionProgress piece to update
  15. // status of the completed state
  16. function deBootStrap ($clusterName, $deployUser, $txnId, $progressInfo)
  17. {
  18. global $logger, $dbAccessor, $stagesInfo;
  19. /////// launch the stages in the background.
  20. $stagesFile = "./uninstall/stages.php";
  21. $logger->log_debug("ClusterName: $clusterName\n");
  22. $logger->log_debug("Deploy User: $deployUser\n");
  23. $status = "";
  24. $createTxResult = HMCTxnUtils::createNewTransaction($dbAccessor, $clusterName, $status);
  25. if ($createTxResult == FALSE) {
  26. $msg = "SETUP: Failed to create new transaction in background: $createTxResult\n";
  27. $logger->log_error($msg);
  28. return array("result" => 1, "error" => "$msg");
  29. }
  30. $rootTxnId = $createTxResult;
  31. $logger->log_error("Txn Id ===== $rootTxnId\n");
  32. $logger->log_error("Clustername ===== $clusterName");
  33. $cmd = $GLOBALS["PHP_EXEC_PATH"] . " " . "../util/sequentialScriptRunner.php";
  34. // $cmd = $GLOBALS["PHP_EXEC_PATH"] . " " . "./addNodes/addNodesOrchestrator.php";
  35. $hostsFile = getHostsFilePath($clusterName);
  36. $args = "$clusterName $deployUser $rootTxnId $hostsFile $stagesFile";
  37. $execBackgroundResult = HMCTxnUtils::execBackgroundProcess($dbAccessor, $clusterName, $rootTxnId, $cmd, $args, "");
  38. if ($execBackgroundResult == FALSE) {
  39. $msg = "Failed to execute addNodesOrchestrator in background: $execBackgroundResult\n";
  40. $logger->log_error($msg);
  41. return array("result" => 1, "error" => "$msg");
  42. }
  43. /////// done launching stages
  44. /////// now monitor the stages
  45. $allDone = false;
  46. while ($allDone == false) {
  47. // use the txn id for finding the status to pass back to the user
  48. // the orchestrator txn id could potentially return an error because
  49. // there is a potential race condition before the orchestrator txnId has
  50. // been committed to the db by the backgrounded process.
  51. $dbHandle = new HMCDBAccessor($GLOBALS["DB_PATH"]);
  52. $orchestratorTxnId = $dbHandle->getAllSubTransactionsInfo(
  53. $clusterName, $rootTxnId);
  54. if ($orchestratorTxnId['result'] != 0) { // encountered error
  55. $logger->log_error("Empty orchestrator txn id " .
  56. json_encode($orchestratorTxnId));
  57. // Check if this should return error FIXME
  58. // $jsonOutput['encounteredError'] = true;
  59. return (array('result' => 1, 'error' => $orchestratorTxnId['error']));
  60. }
  61. // as soon as DB is read, sleep for 2 seconds because we want to
  62. // avoid continuous polling of the db
  63. sleep(2);
  64. //$logger->log_error(" ==== subTxns info is " . json_encode($orchestratorTxnId));
  65. $keys = array_keys($orchestratorTxnId['subTxns']);
  66. if (count($keys) == 0) {
  67. $logger->log_debug("No keys found in orchestrator's child transactions");
  68. continue;
  69. }
  70. $firstKey = $keys[0];
  71. if ($firstKey == '') {
  72. continue;
  73. }
  74. $allSubTxns = $dbHandle->getAllSubTransactionsInfo(
  75. $clusterName, $firstKey);
  76. // the all sub txn ids could potentially return an error because
  77. // there is a potential race condition before the orchestrator's subTxnId has
  78. // been committed to the db by the backgrounded process.
  79. if ($allSubTxns['result'] != 0) {
  80. $logger->log_error("Empty orchestrator txn id " .
  81. json_encode($allSubTxns));
  82. return ($jsonOutput);
  83. }
  84. $logger->log_debug("allsubtxn count is ".json_encode(count($allSubTxns['subTxns'])));
  85. $logger->log_debug("stages count is ".json_encode(count($stagesInfo)));
  86. $logger->log_debug("subtxns ".json_encode($allSubTxns));
  87. // allDone must be set once the processes launched have completed running
  88. if ((count($allSubTxns['subTxns']) == count($stagesInfo))) {
  89. $lastTxn = end($allSubTxns['subTxns']);
  90. if (($lastTxn['opStatus'] == "SUCCESS")) {
  91. $allDone = true;
  92. $result = 0;
  93. $error = "";
  94. } else if (($lastTxn['opStatus'] == "FAILED") ||
  95. ($lastTxn['opStatus'] == "TOTALFAILURE")) {
  96. $logger->log_debug("");
  97. $allDone = true;
  98. $error = json_encode($lastTxn['state']);
  99. $result = 1;
  100. }
  101. }
  102. }
  103. /////// done monitoring return back to the uninstall
  104. // need to cleanup db for this cluster
  105. $dbAccessor->cleanupCluster($clusterName);
  106. return array('result' => $result, 'error' => $error);
  107. }
  108. ?>