sequentialScriptRunner.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. // all scripts run with base directory as frontend
  3. include_once '../util/Logger.php';
  4. include_once '../conf/Config.inc';
  5. include_once 'localDirs.php';
  6. include_once "../util/lock.php";
  7. include_once '../db/HMCDBAccessor.php';
  8. include_once "../util/HMCTxnUtils.php";
  9. include_once "commandUtils.php";
  10. include_once '../util/util.php';
  11. // this script will write to TransactionStatus table the following:
  12. // 1. txn-id -> describes state sshable, discoverable, bootstrapable
  13. // 2. status_info blob -> specific errors for the state
  14. // initial setup
  15. $logger = new HMCLogger("sequentialScriptExecutor");
  16. $dbHandle = new HMCDBAccessor($GLOBALS["DB_PATH"]);
  17. function updateProgressForStage($clusterName, $rootTxnId, $orchestratorTxnId, $mySubTxnId, $operationName) {
  18. global $logger, $dbHandle, $stagesInfo;
  19. $clusterDir = getClusterDir($clusterName);
  20. $commandOutputDir = $clusterDir . $operationName . "/";
  21. $numTotalNodes = 0;
  22. $numNodesFailed = 0;
  23. $numNodesSucceeded = 0;
  24. $additionalInfo = array();
  25. if (is_dir($commandOutputDir) && $dirHandle = opendir($commandOutputDir)) {
  26. $logger->log_debug($commandOutputDir . " exists finally ");
  27. while (false !== ($entry = readdir($dirHandle))) {
  28. if ($entry == "." || $entry == "..") {
  29. continue;
  30. }
  31. // Only consider .out files
  32. if(!preg_match("/.out/", $entry)) {
  33. continue;
  34. }
  35. $nodeName = basename($entry, ".out");
  36. $numTotalNodes++;
  37. $doneFile = $commandOutputDir . $nodeName . ".done";
  38. if (file_exists($doneFile)) {
  39. // Read the contents of the done-file
  40. $doneFileContents = file_get_contents($doneFile);
  41. if (trim($doneFileContents) == "0") {
  42. $numNodesSucceeded++;
  43. $additionalInfo[$nodeName] = "Successful with no errors.";
  44. } else {
  45. $numNodesFailed++;
  46. // Let's read the errors
  47. $errorFile = $commandOutputDir . $nodeName . ".err";
  48. $errorFileContents = file_get_contents($errorFile);
  49. $additionalInfo[$nodeName] = "Failed. Reason: $errorFileContents";
  50. }
  51. }
  52. }
  53. } else {
  54. $logger->log_debug( $commandOutputDir . " still doesn't exist");
  55. }
  56. $logger->log_debug(" Current operation is $operationName and commandOutput dir: $commandOutputDir");
  57. $logger->log_debug(" total nodes: $numTotalNodes, succeeded: $numNodesSucceeded, failed: $numNodesFailed");
  58. $progressState = generateNodeActionProgress($numTotalNodes, $numNodesSucceeded, $numNodesFailed, "succeeded");
  59. $logger->log_debug(" Current progressState is ".$progressState);
  60. $state = json_encode($additionalInfo); $description = $stagesInfo[$operationName]["description"]; $progress = $progressState; $subTxnType = "";
  61. $subTransactionReturnValue = $dbHandle->insertOrUpdateSubTransaction($clusterName, $orchestratorTxnId, $mySubTxnId, $orchestratorTxnId, $state, $description, $progress, $subTxnType );
  62. if ($subTransactionReturnValue["result"] != 0 ) {
  63. $logger->log_error("Got error while creating subTxn: ".$subTransactionReturnValue["error"]);
  64. print json_encode($subTransactionReturnValue);
  65. return;
  66. }
  67. }
  68. $clusterName = $argv[1];
  69. $deployUser = $argv[2];
  70. $rootTxnId = $argv[3];
  71. $readFromFile = $argv[4];
  72. $stagesFile = $argv[5];
  73. include_once $stagesFile;
  74. $logger->log_debug("Sequential runner params: $clusterName, $deployUser, $rootTxnId, $readFromFile, $stagesFile");
  75. // Create a sub-txn for the orchestrator
  76. $status = "";
  77. $createTxResult = HMCTxnUtils::createNewTransaction($dbHandle, $clusterName, $status);
  78. if ($createTxResult == FALSE) {
  79. $logger->log_error("Failed to create new transaction " . " in background: $createTxResult\n");
  80. return;
  81. }
  82. $orchestratorTxnId = $createTxResult;
  83. $logger->log_debug("Txn Id: $orchestratorTxnId\n");
  84. $state = ""; $description = "sequentialScriptExecutor"; $progress = ""; $subTxnType = "";
  85. $subTransactionReturnValue = $dbHandle->insertOrUpdateSubTransaction($clusterName, $rootTxnId, $orchestratorTxnId, $rootTxnId, $state, $description, $progress, $subTxnType );
  86. if ($subTransactionReturnValue["result"] != 0 ) {
  87. $logger->log_error("Got error while creating subTxn for sequentialScriptExecutor: ".$subTransactionReturnValue["error"]);
  88. print json_encode($subTransactionReturnValue);
  89. return;
  90. }
  91. $logger->log_debug("sequentialScriptExecutor txnId: $orchestratorTxnId\n");
  92. $startTime = time(0);
  93. $cmd = "";
  94. $currentStage = "";
  95. $statusInfo = "";
  96. $count = 0;
  97. $thisHostName = trim(strtolower(exec('hostname -f')));
  98. // Add the host which runs the server to the list
  99. function addThisHostToList($hosts, $logger, $thisHostName, $readFromFile) {
  100. $result = array();
  101. foreach($hosts as $host) {
  102. array_push($result, $host);
  103. if ($thisHostName == $host) {
  104. $logger->log_debug("Management host $thisHostName exists".
  105. " in the hosts file");
  106. return $hosts;
  107. }
  108. }
  109. $outFd = fopen($readFromFile, "a");
  110. if ($outFd == FALSE) {
  111. $logger->log_error("Failed to add the hmc host to the nodes file");
  112. return $result;
  113. }
  114. fwrite($outFd, "\n".$thisHostName);
  115. fclose($outFd);
  116. array_push($result, $thisHostName);
  117. return $result;
  118. }
  119. $hosts = readHostsFile($readFromFile);
  120. $hosts = convertToLowerCase($hosts);
  121. $logger->log_debug("The hosts after converting to lower case ".print_r($hosts, true));
  122. // Only add this host to list if the db does not have that host already.
  123. $checkThisHostInDB = $dbHandle->getHostInfo($clusterName, $thisHostName);
  124. $logger->log_debug("Host Info in DB ".print_r($checkThisHostInDB, true));
  125. if ($checkThisHostInDB["result"] != 0) {
  126. $logger->log_info($thisHostName . "not found in DB so adding it to the list of hosts");
  127. $hosts = addThisHostToList($hosts, $logger, $thisHostName, $readFromFile);
  128. }
  129. $hosts = convertToLowerCase($hosts);
  130. $logger->log_debug("The hosts that are being sshed to are ".print_r($hosts, true));
  131. $allHosts = $hosts; // Keep a copy in case
  132. foreach ($stagesInfo as $stage => $stageInfo) {
  133. // create a new subtransaction for each stage
  134. $mySubTxnId = 100 + $count;
  135. $state = ""; $description = $stagesInfo[$stage]["description"]; $progress = " ( starting )"; $subTxnType = "";
  136. $subTransactionReturnValue = $dbHandle->insertOrUpdateSubTransaction($clusterName, $orchestratorTxnId, $mySubTxnId, $orchestratorTxnId, $state, $description, $progress, $subTxnType );
  137. if ($subTransactionReturnValue["result"] != 0 ) {
  138. $logger->log_error("Got error while creating subTxn: ".$subTransactionReturnValue["error"]);
  139. print json_encode($subTransactionReturnValue);
  140. return;
  141. }
  142. $currentStage = $stage;
  143. // SubTxn is created. Set start op_status
  144. $updateSubTransactionStatusResult = $dbHandle->updateSubTransactionOpStatus($clusterName, $orchestratorTxnId, $mySubTxnId, $opStatus);
  145. if ($updateSubTransactionStatusResult["result"] != 0 ) {
  146. $logger->log_error("Got error while updating subTxn: ".$updateSubTransactionStatusResult["error"]);
  147. print json_encode($updateSubTransactionStatusResult);
  148. return;
  149. }
  150. // If the host list is empty, say because of failures in previous stage, no point carrying it on..
  151. if (count($hosts) == 0) {
  152. $logger->log_info("Skipping stage " . $stage . " as no valid hosts available");
  153. continue; // so that all stages can get marked as failures
  154. }
  155. $cmd = $GLOBALS["PHP_EXEC_PATH"]." ".$stagesInfo[$stage]["scriptName"];
  156. $args = "$clusterName $deployUser $rootTxnId $mySubTxnId $orchestratorTxnId " . $readFromFile;
  157. $count++;
  158. $execBackgroundResult = HMCTxnUtils::execBackgroundProcess($dbHandle, $clusterName, $rootTxnId, $cmd, $args, "" );
  159. if ($execBackgroundResult == FALSE) {
  160. $logger->log_error("Failed to execute $currentStage".
  161. " in background: $execBackgroundResult\n");
  162. return;
  163. }
  164. $allSubTransactionsInfoResult = $dbHandle->getAllSubTransactionsInfo($clusterName, $orchestratorTxnId);
  165. if ($allSubTransactionsInfoResult["result"] != 0 ) {
  166. $logger->log_error("Got error while getting subTxnInfo: ".$allSubTransactionsInfoResult["error"]);
  167. print json_encode($allSubTransactionsInfoResult);
  168. return;
  169. }
  170. /*
  171. if (count($allSubTransactionsInfoResult["subTxns"]) != $count) {
  172. $logger->log_info("Still waiting ");
  173. }
  174. */
  175. //$mySubTxnId = '"'.$mySubTxnId.'"';
  176. // $logger->log_error(" sequentialScriptExecutors sub txns " . json_encode($allSubTransactionsInfoResult));
  177. $successStatus = "SUCCESS";
  178. $errorStatus = "FAILED";
  179. $totalFailedStatus = "TOTALFAILURE";
  180. $currentStatus = $allSubTransactionsInfoResult["subTxns"][$mySubTxnId]["opStatus"];
  181. while ( $currentStatus != $successStatus && $currentStatus != $errorStatus
  182. && $currentStatus != $totalFailedStatus) {
  183. sleep(1);
  184. $allSubTransactionsInfoResult = $dbHandle->getAllSubTransactionsInfo($clusterName, $orchestratorTxnId);
  185. if ($allSubTransactionsInfoResult["result"] != 0 ) {
  186. $logger->log_error("Got error while getting subTxnInfo: ".$allSubTransactionsInfoResult["error"]);
  187. print json_encode($allSubTransactionsInfoResult);
  188. return;
  189. }
  190. $currentStatus = $allSubTransactionsInfoResult["subTxns"][$mySubTxnId]["opStatus"];
  191. //$logger->log_debug(" sequentialScriptExecutors sub txns " . json_encode($allSubTransactionsInfoResult));
  192. if ($currentStatus != $successStatus && $currentStatus != $errorStatus
  193. && $currentStatus != $totalFailedStatus) {
  194. updateProgressForStage($clusterName, $rootTxnId,
  195. $orchestratorTxnId, $mySubTxnId, $stage);
  196. }
  197. //$logger->log_debug("Status we are seeing: " . $currentStatus . " txnId: " . $orchestratorTxnId . " subTxnId " . $mySubTxnId);
  198. }
  199. // Just in case, the command finished too fast and the while loop is skipped.
  200. updateProgressForStage($clusterName, $rootTxnId, $orchestratorTxnId, $mySubTxnId, $stage);
  201. $logger->log_debug("Came out of the launch for stage " . $currentStage . "\n");
  202. unset($subTxn);
  203. if ($currentStatus == $totalFailedStatus) {
  204. $logger->log_error("Encountered total failure in transaction $mySubTxnId" .
  205. " while running cmd: $cmd with args: $args");
  206. return;
  207. }
  208. ////////// Construct the host list needed for next stage ////////////
  209. $nextStageHosts = array();
  210. foreach ($hosts as $host) {
  211. ////// Get info about this node from db ////
  212. $hostInfo = $dbHandle->getHostInfo($clusterName,$host);
  213. if ($hostInfo["result"] != 0 ) {
  214. $logger->log_error("Got error while getting hostInfo for $host :" .$hostInfo["error"]);
  215. continue;
  216. }
  217. if ($hostInfo["discoveryStatus"] == "SUCCESS") {
  218. $nextStageHosts[] = $host;
  219. }
  220. }
  221. // Change the host list to weed-out bad nodes.
  222. $hosts = $nextStageHosts;
  223. ////////// End of constructructing the host list needed for next stage ////////////
  224. }
  225. ?>