manageServices.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "../orchestrator/HMC.php";
  7. include_once "../orchestrator/Service.php";
  8. include_once "../orchestrator/ServiceComponent.php";
  9. include_once "../db/Transaction.php";
  10. include_once "../db/OrchestratorDB.php";
  11. include_once "../puppet/DBReader.php";
  12. include_once "../puppet/PuppetInvoker.php";
  13. include_once "configUtils.php";
  14. include_once '../util/suggestProperties.php';
  15. $logger = new HMCLogger("ManageServices");
  16. $dbPath = $GLOBALS["DB_PATH"];
  17. $clusterName = $_GET['clusterName'];
  18. $dbAccessor = new HMCDBAccessor($dbPath);
  19. header("Content-type: application/json");
  20. function performServiceManagement( $hmc, $requestObj )
  21. {
  22. global $logger;
  23. global $dbAccessor;
  24. global $clusterName;
  25. /* What we're here to return. */
  26. $result = array();
  27. $action = $requestObj['action'];
  28. $serviceNames = array_keys($requestObj['services']);
  29. switch( $action )
  30. {
  31. case 'startAll':
  32. $result = $hmc->startAllServices();
  33. break;
  34. case 'stopAll':
  35. $result = $hmc->stopAllServices();
  36. break;
  37. case 'start':
  38. if( count($serviceNames) > 0 ) {
  39. $result = $hmc->startServices( $serviceNames );
  40. }
  41. break;
  42. case 'stop':
  43. if( count($serviceNames) > 0 ) {
  44. $result = $hmc->stopServices( $serviceNames );
  45. }
  46. break;
  47. case 'reconfigure':
  48. if( count($serviceNames) > 0 ) {
  49. /* Read additional data from $requestObj and update the DB
  50. * accordingly before attempting to call $hmc->reconfigureServices().
  51. *
  52. */
  53. /*
  54. $configsToUpdate = array ();
  55. foreach ($requestObj['services'] as $svcName => $svcInfo) {
  56. if (!isset($svcInfo["properties"])
  57. || !is_array($svcInfo["properties"])) {
  58. continue;
  59. }
  60. // TODO: Stupid translation, FIXME says tshooter to hitman
  61. $finalProperties = array();
  62. foreach ($svcInfo["properties"] as $key => $valueObj) {
  63. $finalProperties[$key] = $valueObj["value"];
  64. }
  65. $configsToUpdate = array_merge($configsToUpdate, $finalProperties);
  66. }
  67. $result = $dbAccessor->updateServiceConfigs($clusterName, $configsToUpdate);
  68. if ($result['result'] != 0) {
  69. $logger->log_error("Failed to update the configs in DB, error=" . $result["error"]);
  70. return $result;
  71. }
  72. */
  73. // re-using persistConfigs code
  74. $result = validateAndPersistConfigsFromUser($dbAccessor, $logger, $clusterName, $requestObj['services']);
  75. if ($result['result'] != 0) {
  76. $logger->log_error("Failed to validate configs from user, error=" . $result["error"]);
  77. return $result;
  78. }
  79. /*
  80. * Also, remember to save a snapshot of the outgoing service
  81. * config in the 'ConfigHistory' table (thanks, @tshooter).
  82. */
  83. $changeMsg = "Reconfiguring services, list=" . implode(",", $serviceNames);
  84. $result = $dbAccessor->createServiceConfigSnapshot($clusterName, $changeMsg);
  85. if ($result['result'] != 0) {
  86. $logger->log_error("Failed to create config snapshot in DB, error=" . $result["error"]);
  87. return $result;
  88. }
  89. $result = $hmc->reconfigureServices( $serviceNames );
  90. }
  91. break;
  92. default:
  93. $logger->log_error( "Unrecognized action '" . $action . "' requested" );
  94. $result = array ( "result" => -1 , "error" => "Invalid action, action=" . $action);
  95. break;
  96. }
  97. return $result;
  98. }
  99. $hmc = new HMC($dbPath, $clusterName);
  100. /* Slurp in the POST body. */
  101. $requestData = file_get_contents('php://input');
  102. $requestObj = json_decode($requestData, true);
  103. /* The Main Event. */
  104. $result = performServiceManagement($hmc, $requestObj);
  105. /* Augment $result with 'clusterName'. */
  106. $result["clusterName"] = $clusterName;
  107. if ($result["result"] != 0 ) {
  108. $logger->log_error("Failed to take an action in manage services, error=" . $result["error"]);
  109. }
  110. print (json_encode($result));
  111. ?>