ClusterMain.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. include_once '../util/Logger.php';
  3. include_once '../conf/Config.inc';
  4. include_once "../util/lock.php";
  5. include_once '../db/HMCDBAccessor.php';
  6. include_once "Cluster.php";
  7. include_once "Service.php";
  8. include_once "../puppet/DBReader.php";
  9. include_once "../puppet/PuppetInvoker.php";
  10. include_once "../util/HMCTxnUtils.php";
  11. $defaultLogger = new HMCLogger("ClusterMain");
  12. function usage() {
  13. $usageStr = <<<USAGESTR
  14. Usage:
  15. -c/--cluster-name Cluster Name
  16. -x/--txn-id Transaction ID
  17. -d/--db-path DB Path
  18. -a/--action Action - deploy, deployNode, startAll, stopAll,
  19. uninstallAll, reconfigure, wipeout
  20. -s/--service Service - names of services
  21. -n/--node Node - hostname of node to take action on
  22. -r/--dry-run Dry-run only
  23. -h/--help Print usage message
  24. USAGESTR;
  25. print "\n".$usageStr."\n";
  26. }
  27. /*
  28. $ACTIONS = array (
  29. "deploy",
  30. "deployNode",
  31. "startAll",
  32. "start",
  33. "stopAll",
  34. "stop",
  35. "installAll",
  36. "install",
  37. "uninstallAll",
  38. "uninstall",
  39. "reconfigureAll",
  40. "reconfigure"
  41. );
  42. */
  43. $ACTIONS = array (
  44. "deploy",
  45. "deployNode",
  46. "startAll",
  47. "stopAll",
  48. "uninstallAll",
  49. "start",
  50. "stop",
  51. "reconfigure",
  52. "wipeout"
  53. );
  54. function validateAndCreateDBHandle($dbPath) {
  55. global $logger;
  56. if ($dbPath == "") {
  57. $logger->log_error("Invalid DB Path provided");
  58. exit (1);
  59. }
  60. if (!file_exists($dbPath)) {
  61. $logger->log_error("DB Path provided does not exist, dbpath=" . $dbPath);
  62. exit (1);
  63. }
  64. $dbHandle = new HMCDBAccessor($dbPath);
  65. if (isset($dbHandle)) {
  66. return $dbHandle;
  67. }
  68. $logger->log_error("Could not initialize handle to DB, dbPath=" . $dbPath);
  69. exit (1);
  70. }
  71. function validateCluster($dbHandle, $clusterName) {
  72. global $logger;
  73. if ($clusterName == "") {
  74. $logger->log_error("Invalid cluster name provided");
  75. exit (1);
  76. }
  77. $clusters = $dbHandle->getAllClusters();
  78. if ($clusters === FALSE || $clusters["result"] != 0
  79. || !isset($clusters["clusters"])
  80. || !is_array($clusters["clusters"])
  81. || !isset($clusters["clusters"][$clusterName])
  82. || !is_array($clusters["clusters"][$clusterName])) {
  83. $logger->log_error("Could not retrieve cluster info from DB"
  84. . ", clusterName=" . $clusterName
  85. . ", error=" . $clusters["error"]);
  86. exit (1);
  87. }
  88. }
  89. function validateService($dbHandle, $serviceName) {
  90. global $logger;
  91. $services = $dbHandle->getAllServicesList();
  92. if ($services === FALSE || $services["result"] != 0
  93. || !isset($services["services"])
  94. || !is_array($services["services"])
  95. || !isset($services["services"][$serviceName])
  96. || !is_array($services["services"][$serviceName])) {
  97. $logger->log_error("Could not retrieve service info from DB"
  98. . ", serviceName=" . $serviceName
  99. . ", error=" . $services["error"]);
  100. exit (1);
  101. }
  102. }
  103. function validateNode($dbHandle, $clusterName, $nodeName) {
  104. global $logger;
  105. $hostInfo = $dbHandle->getHostInfo($clusterName, $nodeName);
  106. if ($hostInfo === FALSE || $hostInfo["result"] != 0) {
  107. $logger->log_error("Could not find node info in DB"
  108. . ", clusterName=" . $clusterName
  109. . ", hostName=" . $nodeName
  110. . ", error=" . $hostInfo["error"]);
  111. exit (1);
  112. }
  113. }
  114. function validateAction($dbHandle, $clusterName, $action, $serviceNames,
  115. $nodeNames) {
  116. global $ACTIONS;
  117. global $logger;
  118. if (array_search($action, $ACTIONS) === FALSE) {
  119. $logger->log_error("Invalid action provided, action=" . $action);
  120. exit (1);
  121. }
  122. if ($action == "start" || $action == "stop" || $action == "uninstall"
  123. || $action == "reconfigure") {
  124. if (count($serviceNames) == 0) {
  125. $logger->log_error("No services listed for given action, action="
  126. . $action);
  127. exit (1);
  128. }
  129. foreach ($serviceNames as $serviceName) {
  130. validateService($dbHandle, $serviceName);
  131. }
  132. }
  133. if ($action == "deployNode") {
  134. foreach ($nodeNames as $nodeName) {
  135. validateNode($dbHandle, $clusterName, $nodeName);
  136. }
  137. }
  138. }
  139. function validateTxn($dbHandle, $clusterName, $txnId) {
  140. global $logger;
  141. $txnInfo = $dbHandle->getTransactionStatusInfo($clusterName, $txnId);
  142. if ($txnInfo === FALSE || $txnInfo["result"] != 0) {
  143. $logger->log_error("Could not find txn info in DB"
  144. . ", clusterName=" . $clusterName
  145. . ", txnId=" . $txnId
  146. . ", error=" . $txnInfo["error"]);
  147. exit (1);
  148. }
  149. }
  150. function createServiceObj($dbHandle, $clusterName, $serviceName,
  151. $odb, $puppetInvoker) {
  152. global $logger;
  153. $services = $dbHandle->getAllServicesInfo($clusterName);
  154. if ($services === FALSE || $services["result"] != 0) {
  155. $logger->log_error("Failed to get service list from DB"
  156. . ", error=" . $services["error"]);
  157. exit (1);
  158. }
  159. foreach ($services["services"] as $svc) {
  160. if ($svc["serviceName"] != $serviceName) {
  161. continue;
  162. }
  163. $svcObj = new Service($svc["serviceName"], $svc["state"],
  164. $odb, $puppetInvoker);
  165. return $svcObj;
  166. }
  167. $logger->log_error("Could not find service in DB"
  168. . ", serviceName=" . $serviceName);
  169. exit (1);
  170. }
  171. $params = array(
  172. "c:" => "cluster-name:",
  173. "x:" => "txn-id",
  174. "d:" => "db-path:",
  175. "a:" => "action",
  176. "s:" => "service:",
  177. "n:" => "node:",
  178. "r" => "dry-run",
  179. "h" => "help"
  180. );
  181. $argsStr = implode(" ", $argv);
  182. $options = @getopt(implode("", array_keys($params)), array_values ($params));
  183. $clusterName = "";
  184. $dbPath = "";
  185. $action = "";
  186. $serviceNames = array();
  187. $nodeNames = array();
  188. $dryRun = FALSE;
  189. $txnId = "";
  190. foreach ($options as $opt => $val) {
  191. if ($opt == "c" || $opt == "cluster-name") {
  192. $clusterName = $val;
  193. }
  194. else if ($opt == "x" || $opt == "txn-id") {
  195. $txnId = $val;
  196. }
  197. else if ($opt == "d" || $opt == "db-path") {
  198. $dbPath = $val;
  199. }
  200. else if ($opt == "a" || $opt == "action") {
  201. $action = $val;
  202. }
  203. else if ($opt == "s" || $opt == "service") {
  204. if (is_array($val)) {
  205. $serviceNames = $val;
  206. }
  207. else {
  208. array_push($serviceNames, $val);
  209. }
  210. }
  211. else if ($opt == "n" || $opt == "node") {
  212. if (is_array($val)) {
  213. $nodeNames = $val;
  214. }
  215. else {
  216. array_push($nodeNames, $val);
  217. }
  218. }
  219. else if ($opt == "r" || $opt == "dry-run") {
  220. $dryRun = TRUE;
  221. }
  222. else if ($opt == "h" || $opt == "help") {
  223. usage();
  224. exit (0);
  225. }
  226. else {
  227. $defaultLogger->log_error("Invalid command line option provided, opt=$opt");
  228. $defaultLogger->log_error("ClusterMain execution failed, args=".$argsStr);
  229. exit (1);
  230. }
  231. }
  232. if ($clusterName == "") {
  233. $defaultLogger->log_error("ClusterMain execution failed"
  234. . ", no cluster name defined, args=".$argsStr);
  235. exit (1);
  236. }
  237. if ($action == "") {
  238. $defaultLogger->log_error("ClusterMain execution failed"
  239. . ", no action defined, args=".$argsStr);
  240. exit (1);
  241. }
  242. if ($txnId == "") {
  243. $defaultLogger->log_error("ClusterMain execution failed"
  244. . ", no txnId defined, args=".$argsStr);
  245. exit (1);
  246. }
  247. if ($dbPath == "") {
  248. $defaultLogger->log_error("ClusterMain execution failed"
  249. . ", no db path defined, args=".$argsStr);
  250. exit (1);
  251. }
  252. $logger = new HMCLogger("ClusterMain:TxnId=".$txnId);
  253. $dbHandle = validateAndCreateDBHandle($dbPath);
  254. validateCluster($dbHandle, $clusterName);
  255. validateTxn($dbHandle, $clusterName, $txnId);
  256. validateAction($dbHandle, $clusterName, $action, $serviceNames, $nodeNames);
  257. $puppetInvoker = new PuppetInvoker($dbPath);
  258. $odb = new OrchestratorDB($dbPath, $clusterName, $puppetInvoker);
  259. $cluster = new Cluster($clusterName, $odb, $puppetInvoker);
  260. $transaction = new Transaction($txnId, 0, 0);
  261. // reset sub txn id
  262. // treat 0 as no-parent
  263. $GLOBALS["SUB_TXN_ID"] = 1;
  264. if ($dryRun) {
  265. print "DEBUG: DRYRUN: ARGS: ".implode(" ",$argv)."\n";
  266. print "DEBUG: DRYRUN: DB PATH: $dbPath\n";
  267. print "DEBUG: DRYRUN: CLUSTER NAME: $clusterName\n";
  268. print "DEBUG: DRYRUN: TXN : ".$transaction->toString()."\n";
  269. print "DEBUG: DRYRUN: ACTION: $action\n";
  270. exit(0);
  271. }
  272. $logger->log_info("Taking action=$action on cluster=$clusterName, txn="
  273. . $transaction->toString());
  274. $result = array( "error" => "", "result" => 0);
  275. if ($action == "deploy") {
  276. $result = $cluster->deployHDP($transaction);
  277. } else if ($action == "deployNode") {
  278. $result = $cluster->deployNodes($transaction, $nodeNames);
  279. } else if ($action == "startAll") {
  280. $result = $cluster->startAllServices($transaction);
  281. } else if ($action == "stopAll") {
  282. $result = $cluster->stopAllServices($transaction);
  283. } else if ($action == "uninstallAll") {
  284. $wipeoutData = FALSE;
  285. $result = $cluster->uninstallHDP($transaction, $wipeoutData);
  286. } else if ($action == "wipeout") {
  287. $wipeoutData = TRUE;
  288. $result = $cluster->uninstallHDP($transaction, $wipeoutData);
  289. } else if ($action == "start") {
  290. $result = $cluster->startServices($transaction, $serviceNames);
  291. } else if ($action == "stop") {
  292. $result = $cluster->stopServices($transaction, $serviceNames);
  293. } else if ($action == "reconfigure") {
  294. $result = $cluster->reconfigureServices($transaction, $serviceNames);
  295. } else {
  296. $result = array ( "result" => -1,
  297. "error" => "Unsupported action, action=$action");
  298. }
  299. $logger->log_info("Completed action=$action on cluster=$clusterName, txn="
  300. . $transaction->toString()
  301. . ", result=" . $result["result"]
  302. . ", error=" . $result["error"]);
  303. exit ($result["result"]);
  304. ?>