OrchestratorDB.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. include_once "../orchestrator/State.php";
  20. include_once "../orchestrator/Service.php";
  21. include_once "../orchestrator/ServiceComponent.php";
  22. include_once "../orchestrator/Cluster.php";
  23. include_once '../util/Logger.php';
  24. include_once '../conf/Config.inc';
  25. include_once "../util/lock.php";
  26. include_once '../db/HMCDBAccessor.php';
  27. include_once "Transaction.php";
  28. class OrchestratorDB {
  29. // Cluster name
  30. public $clusterName;
  31. private $db;
  32. private $logger;
  33. private $puppet;
  34. private $exploreMode;
  35. private $servicesCache;
  36. private $serviceComponentsCache;
  37. /**
  38. * Initialize ODB
  39. * @param string $dbPath
  40. * @param string $clusterName
  41. * @param object $puppet PuppetInvoker
  42. */
  43. function __construct($dbPath, $clusterName, $puppet) {
  44. $this->clusterName = $clusterName;
  45. $this->db = new HMCDBAccessor($dbPath);
  46. $this->puppet = $puppet;
  47. $this->exploreMode = false;
  48. $this->servicesCache = array();
  49. $this->serviceComponentsCache = array();
  50. $this->logger = new HMCLogger("OrchestratorDB");
  51. }
  52. /**
  53. * Get all the services in the cluster.
  54. * @return array of Service objects
  55. * @return FALSE on error
  56. */
  57. public function getClusterServices() {
  58. $services = $this->db->getAllServicesInfo($this->clusterName);
  59. if ($services === FALSE || $services["result"] != 0) {
  60. $this->logger->log_error("Failed to get service list from DB");
  61. return FALSE;
  62. }
  63. $svcObjs = array();
  64. foreach ($services["services"] as $svc) {
  65. if (!$svc["isEnabled"]) {
  66. continue;
  67. }
  68. $state = STATE::getStateFromString($svc["state"]);
  69. if ($state === FALSE) {
  70. $this->logger->log_error("Found service with invalid state"
  71. . ", service=" . $svc["serviceName"]
  72. . ", state=" . $svc["state"]);
  73. $state = 0; // unknown
  74. }
  75. $svcObj =
  76. $this->getServiceObj($svc["serviceName"], $state, $this, $this->puppet);
  77. array_push($svcObjs, $svcObj);
  78. }
  79. return $svcObjs;
  80. }
  81. /**
  82. * Get the Service object corresponding to the serviceName.
  83. * @param serviceName service name
  84. * @return Service
  85. * @return FALSE on error
  86. */
  87. public function getService($serviceName) {
  88. $serviceInfo = $this->db->getServiceInfo($this->clusterName, $serviceName);
  89. if ($serviceInfo["result"] != 0) {
  90. $this->logger->log_error("Failed to get serviceInfo for $serviceName with "
  91. . $serviceInfo["error"]);
  92. return FALSE;
  93. }
  94. if ($serviceInfo["isEnabled"] != TRUE) {
  95. $this->logger->log_error("Could not find ServiceInfo for legal $serviceName");
  96. return FALSE;
  97. }
  98. $state = STATE::getStateFromString($serviceInfo["state"]);
  99. return $this->getServiceObj($serviceName, $state, $this, $this->puppet);
  100. }
  101. public function getServices($serviceNames) {
  102. $svcObjs = array();
  103. foreach ($serviceNames as $serviceName) {
  104. $svcObj = $this->getService($serviceName);
  105. if ($svcObj === FALSE) {
  106. return FALSE;
  107. }
  108. $svcObjs[$serviceName] = $svcObj;
  109. }
  110. return $svcObjs;
  111. }
  112. /**
  113. * Get service dependencies for the given service.
  114. * @param serviceName service name
  115. * @return array of Service objects
  116. */
  117. public function getServiceDependencies($serviceName) {
  118. $svcDeps = $this->db->getServiceDependencies($serviceName);
  119. if ($svcDeps === FALSE || $svcDeps["result"] != 0) {
  120. $this->logger->log_error("Failed to get service deps from DB");
  121. return FALSE;
  122. }
  123. $services = $this->db->getAllServicesInfo($this->clusterName);
  124. if ($services === FALSE || $services["result"] != 0) {
  125. $this->logger->log_error("Failed to get service list from DB");
  126. return FALSE;
  127. }
  128. $svcObjs = array();
  129. foreach ($svcDeps["serviceDependencies"] as $svcDep) {
  130. if (!isset($services["services"][$svcDep])) {
  131. $this->logger->log_error("Found a service dependency that does not "
  132. . " exist in DB");
  133. return FALSE;
  134. }
  135. $svc = $services["services"][$svcDep];
  136. $state = STATE::getStateFromString($svc["state"]);
  137. if ($state === FALSE) {
  138. $this->logger->log_error("Found service with invalid state"
  139. . ", service=" . $svc["serviceName"]
  140. . ", state=" . $svc["state"]);
  141. $state = 0; // unknown
  142. }
  143. $svcObj =
  144. $this->getServiceObj($svc["serviceName"], $state, $this, $this->puppet);
  145. array_push($svcObjs, $svcObj);
  146. }
  147. return $svcObjs;
  148. }
  149. /**
  150. * Get services which depend on the given service.
  151. * @param serviceName service name
  152. * @return array of Service objects
  153. */
  154. public function getServiceDependents($serviceName) {
  155. $svcDeps = $this->db->getServiceDependents($serviceName);
  156. if ($svcDeps === FALSE || $svcDeps["result"] != 0) {
  157. $this->logger->log_error("Failed to get service deps from DB");
  158. return FALSE;
  159. }
  160. $services = $this->db->getAllServicesInfo($this->clusterName);
  161. if ($services === FALSE || $services["result"] != 0) {
  162. $this->logger->log_error("Failed to get service list from DB");
  163. return FALSE;
  164. }
  165. $svcObjs = array();
  166. foreach ($svcDeps["serviceDependents"] as $svcDep) {
  167. if (!isset($services["services"][$svcDep])) {
  168. $this->logger->log_debug("Found a service dependent that does not "
  169. . " exist in DB");
  170. continue;
  171. }
  172. $svc = $services["services"][$svcDep];
  173. if (!$svc["isEnabled"]) {
  174. continue;
  175. }
  176. $state = STATE::getStateFromString($svc["state"]);
  177. if ($state === FALSE) {
  178. $this->logger->log_warn("Found service with invalid state"
  179. . ", service=" . $svc["serviceName"]
  180. . ", state=" . $svc["state"]);
  181. $state = 0; // unknown
  182. }
  183. $svcObj =
  184. $this->getServiceObj($svc["serviceName"], $state, $this, $this->puppet);
  185. array_push($svcObjs, $svcObj);
  186. }
  187. return $svcObjs;
  188. }
  189. /**
  190. * Get components of a service.
  191. * @param serviceName service name
  192. * @return array of ServiceComponent objects
  193. */
  194. public function getServiceComponents($serviceName) {
  195. $result = $this->db->getAllServiceComponents($serviceName);
  196. if ($result === FALSE || $result["result"] != 0
  197. || !isset($result["components"])
  198. || !is_array($result["components"])) {
  199. $this->logger->log_error("Failed to get component list from DB");
  200. return FALSE;
  201. }
  202. $fullCompList = $result["components"];
  203. $result = $this->db->getAllServiceComponentsInfo($this->clusterName);
  204. if ($result === FALSE || $result["result"] != 0) {
  205. $this->logger->log_error("Failed to get component list from DB");
  206. return FALSE;
  207. }
  208. $compObjs = array();
  209. if (!isset($result["services"][$serviceName])
  210. || !is_array($result["services"][$serviceName])
  211. || !is_array($result["services"][$serviceName]["components"])) {
  212. return array();
  213. }
  214. $comps = $result["services"][$serviceName]["components"];
  215. foreach ($comps as $comp) {
  216. $state = STATE::getStateFromString($comp["state"]);
  217. if ($state === FALSE) {
  218. $this->logger->log_warn("Found component with invalid state"
  219. . ", component=" . $comp["componentName"]
  220. . ", state=" . $comp["state"]);
  221. $state = 0;
  222. }
  223. $isClient = FALSE;
  224. if (isset($fullCompList[$comp["componentName"]])) {
  225. if (isset($fullCompList[$comp["componentName"]]["isClient"])) {
  226. $isClient = $fullCompList[$comp["componentName"]]["isClient"];
  227. }
  228. } else {
  229. $this->logger->log_warn("Found component which doesn't exist in meta list"
  230. . ", component=" . $comp["componentName"]);
  231. }
  232. $compObj =
  233. $this->getServiceComponentObj($comp["componentName"], $serviceName,
  234. $state, $this, $this->puppet, $isClient);
  235. array_push($compObjs, $compObj);
  236. }
  237. return $compObjs;
  238. }
  239. public function getNagiosServerComponent() {
  240. $svc = $this->getService("NAGIOS");
  241. if ($svc === FALSE) {
  242. return $svc;
  243. }
  244. $compObjs = $this->getServiceComponents("NAGIOS");
  245. foreach ($compObjs as $compObj) {
  246. if ($compObj->name == "NAGIOS_SERVER") {
  247. return $compObj;
  248. }
  249. }
  250. return FALSE;
  251. }
  252. public function getDashboardServerComponent() {
  253. $svc = $this->getService("DASHBOARD");
  254. if ($svc === FALSE) {
  255. return $svc;
  256. }
  257. $compObjs = $this->getServiceComponents("DASHBOARD");
  258. foreach ($compObjs as $compObj) {
  259. if ($compObj->name == "DASHBOARD") {
  260. return $compObj;
  261. }
  262. }
  263. return FALSE;
  264. }
  265. /**
  266. * Get component dependencies for a given component of a given
  267. * service.
  268. * @param serviceName service name
  269. * @param componentName component name
  270. * @return array of ServiceComponent objects
  271. */
  272. public function getComponentDependencies($serviceName, $componentName) {
  273. $result = $this->db->getAllServiceComponentsList();
  274. if ($result === FALSE || $result["result"] != 0) {
  275. $this->logger->log_error("Failed to get component list from DB");
  276. return FALSE;
  277. }
  278. $fullCompList = array();
  279. if (isset($result["services"])) {
  280. foreach ($result["services"] as $svc => $svcInfo) {
  281. if (isset($svcInfo["components"])) {
  282. foreach ($svcInfo["components"] as $comp => $compInfo) {
  283. $fullCompList[$comp] = $compInfo["isClient"];
  284. }
  285. }
  286. }
  287. }
  288. $result = $this->db->getAllServiceComponentsInfo($this->clusterName);
  289. if ($result === FALSE || $result["result"] != 0
  290. || !isset($result["services"][$serviceName])
  291. || !is_array($result["services"][$serviceName])
  292. || !is_array($result["services"][$serviceName]["components"])) {
  293. $this->logger->log_error("Failed to get component list from DB");
  294. return FALSE;
  295. }
  296. $compDeps = $this->db->getServiceComponentDependencies($componentName);
  297. if ($result === FALSE || $result["result"] != 0) {
  298. $this->logger->log_error("Failed to get component deps list from DB");
  299. return FALSE;
  300. }
  301. $compObjs = array();
  302. $comps = $result["services"][$serviceName]["components"];
  303. foreach ($comps as $comp) {
  304. if (FALSE === array_search($comp["componentName"], $compDeps["componentDependencies"])) {
  305. $this->logger->log_debug("Skipping component as not in dep list, comp="
  306. . $comp["componentName"]);
  307. continue;
  308. }
  309. $state = STATE::getStateFromString($comp["state"]);
  310. if ($state === FALSE) {
  311. $this->logger->log_error("Found component with invalid state"
  312. . ", component=" . $comp["componentName"]
  313. . ", state=" . $comp["state"]);
  314. $state = 0;
  315. }
  316. $isClient = FALSE;
  317. if (isset($fullCompList[$comp["componentName"]])) {
  318. $isClient = $fullCompList[$comp["componentName"]];
  319. } else {
  320. $this->logger->log_warn("Found component which doesn't exist in meta list"
  321. . ", component=" . $comp["componentName"]);
  322. }
  323. $compObj =
  324. $this->getServiceComponentObj($comp["componentName"], $serviceName,
  325. $state, $this, $this->puppet, $isClient);
  326. array_push($compObjs, $compObj);
  327. }
  328. return $compObjs;
  329. }
  330. /**
  331. * Get component dependents for a given component of a given
  332. * service.
  333. * @param serviceName service name
  334. * @param componentName component name
  335. * @return array of ServiceComponent objects
  336. */
  337. public function getComponentDependents($serviceName, $componentName) {
  338. $result = $this->db->getAllServiceComponentsList();
  339. if ($result === FALSE || $result["result"] != 0) {
  340. $this->logger->log_error("Failed to get component list from DB");
  341. return FALSE;
  342. }
  343. $fullCompList = array();
  344. if (isset($result["services"])) {
  345. foreach ($result["services"] as $svc => $svcInfo) {
  346. if (isset($svcInfo["components"])) {
  347. foreach ($svcInfo["components"] as $comp => $compInfo) {
  348. $fullCompList[$comp] = $compInfo["isClient"];
  349. }
  350. }
  351. }
  352. }
  353. $result = $this->db->getAllServiceComponentsInfo($this->clusterName);
  354. if ($result === FALSE || $result["result"] != 0
  355. || !isset($result["services"][$serviceName])
  356. || !is_array($result["services"][$serviceName])
  357. || !is_array($result["services"][$serviceName]["components"])) {
  358. $this->logger->log_error("Failed to get component list from DB");
  359. return FALSE;
  360. }
  361. $compDeps = $this->db->getServiceComponentDependents($componentName);
  362. if ($result === FALSE || $result["result"] != 0) {
  363. $this->logger->log_error("Failed to get component deps list from DB");
  364. return FALSE;
  365. }
  366. $compObjs = array();
  367. $comps = $result["services"][$serviceName]["components"];
  368. foreach ($comps as $comp) {
  369. if (FALSE === array_search($comp["componentName"], $compDeps["componentDependents"])) {
  370. $this->logger->log_debug("Skipping component as not in dep list, comp="
  371. . $comp["componentName"]);
  372. continue;
  373. }
  374. $state = STATE::getStateFromString($comp["state"]);
  375. if ($state === FALSE) {
  376. $this->logger->log_error("Found component with invalid state"
  377. . ", component=" . $comp["componentName"]
  378. . ", state=" . $comp["state"]);
  379. $state = 0;
  380. }
  381. $isClient = FALSE;
  382. if (isset($fullCompList[$comp["componentName"]])) {
  383. $isClient = $fullCompList[$comp["componentName"]];
  384. } else {
  385. $this->logger->log_warn("Found component which doesn't exist in meta list"
  386. . ", component=" . $comp["componentName"]);
  387. }
  388. $compObj =
  389. $this->getServiceComponentObj($comp["componentName"], $serviceName,
  390. $state, $this, $this->puppet, $isClient);
  391. array_push($compObjs, $compObj);
  392. }
  393. return $compObjs;
  394. }
  395. /**
  396. * Get all nodes in the cluster.
  397. * @return mixed
  398. * array( "result" => 0, "error" => msg, "nodes" => array())
  399. */
  400. public function getAllNodes() {
  401. $result = $this->db->getAllHostsInfo($this->clusterName,
  402. array("=" => array ( "discoveryStatus" => "SUCCESS")), array());
  403. if ($result === FALSE || $result["result"] != 0
  404. || !isset($result["hosts"]) || !is_array($result["hosts"])) {
  405. $this->logger->log_error("Failed to get host list from DB");
  406. return array ("result" => 1,
  407. "error" => "Failed to get host list from DB");
  408. }
  409. $nodes = array();
  410. foreach ($result["hosts"] as $host) {
  411. array_push($nodes, $host["hostName"]);
  412. }
  413. $result = $this->db->getAllHostsByComponent($this->clusterName);
  414. if ($result === FALSE || $result["result"] != 0) {
  415. $this->logger->log_error("Failed to get host component mapping from DB");
  416. return array ("result" => 1,
  417. "error" => "Failed to get host component mapping from DB");
  418. }
  419. $compMapping = array ();
  420. if (isset($result["components"])
  421. && is_array($result["components"])) {
  422. foreach ($result["components"] as $compName => $hostsList) {
  423. if (isset($hostsList["hosts"])
  424. && !empty($hostsList["hosts"])) {
  425. $compMapping[$compName] = array_keys($hostsList["hosts"]);
  426. }
  427. }
  428. }
  429. return array ("result" => 0, "error" => "", "nodes" => $nodes,
  430. "componentMapping" => $compMapping);
  431. }
  432. /**
  433. * Get nodes for the given service-component.
  434. * @param serviceComponent service component
  435. * @return mixed
  436. * array( "result" => 0, "error" => msg, "nodes" => array())
  437. */
  438. public function getComponentNodes($serviceComponent) {
  439. $result = $this->db->getHostsForComponent($this->clusterName,
  440. $serviceComponent->name);
  441. if ($result === FALSE || $result["result"] != 0
  442. || !isset($result["hosts"]) || !is_array($result["hosts"])) {
  443. $this->logger->log_error("Failed to get host list from DB");
  444. return array ("result" => 1,
  445. "error" => "Failed to get host list from DB");
  446. }
  447. $nodes = array_keys($result["hosts"]);
  448. return array ("result" => 0, "error" => "", "nodes" => $nodes);
  449. }
  450. /**
  451. * Set service state.
  452. * @param service service whose state needs to be set
  453. * @param state service state
  454. * @return mixed
  455. * array( "result" => 0, "error" => msg)
  456. */
  457. public function setServiceState($service, $state) {
  458. $this->logger->log_info($service->name . " - ". State::$STATE[$state]);
  459. if (isset(State::$DESIRED_STATE[$state])) {
  460. $result = $this->db->setServiceDesiredState($this->clusterName,
  461. $service->name, State::$DESIRED_STATE[$state]);
  462. }
  463. $result = $this->db->setServiceState($this->clusterName,
  464. $service->name, State::$STATE[$state]);
  465. return $result;
  466. }
  467. /**
  468. * Set service component state.
  469. * @param serviceComponent service-component whose state needs to be set
  470. * @param state service-component state
  471. * @return mixed
  472. * array( "result" => 0, "error" => msg)
  473. */
  474. public function setServiceComponentState($serviceName, $componentName, $state) {
  475. $this->logger->log_info("Update ServiceComponentState " . $serviceName . " - "
  476. . $componentName . " - " . State::$STATE[$state]);
  477. if (isset(State::$DESIRED_STATE[$state])) {
  478. $result = $this->db->setServiceComponentDesiredState($this->clusterName,
  479. $componentName, State::$DESIRED_STATE[$state], TRUE);
  480. }
  481. $result = $this->db->setServiceComponentState($this->clusterName,
  482. $componentName, State::$STATE[$state], TRUE);
  483. return $result;
  484. }
  485. /**
  486. * Persist a single transaction.
  487. * @param transaction transaction to be persisted
  488. * @param state state of the transaction
  489. * @param description description of the transaction
  490. * @param dryRun is this a dry-run?
  491. * @param txnType Type identifier of txn
  492. * @return mixed
  493. * array( "result" => 0, "error" => msg)
  494. */
  495. public function persistTransaction($transaction, $state, $description,
  496. $progress, $txnType, $dryRun) {
  497. if ($transaction == NULL) {
  498. return array ( "result" => 0, "error" => "" );
  499. }
  500. if ($dryRun == TRUE) {
  501. $state = "PENDING";
  502. $progress = "PENDING";
  503. }
  504. $this->logger->log_info("persist: " . $transaction->toString()
  505. . ":" . $state . ":" . $description . ":" . $progress);
  506. $result = $this->db->insertOrUpdateSubTransaction($this->clusterName,
  507. $transaction->txId, $transaction->subTxId, $transaction->parentSubTxId,
  508. $state, $description, $progress, $txnType);
  509. return $result;
  510. }
  511. public function persistTransactionOpStatus($transaction, $opStatus) {
  512. if ($transaction == NULL) {
  513. return array ( "result" => 0, "error" => "" );
  514. }
  515. $result = $this->db->updateSubTransactionOpStatus($this->clusterName,
  516. $transaction->txId, $transaction->subTxId, $opStatus);
  517. return $result;
  518. }
  519. private function getServiceObj($serviceName, $serviceState, $db, $puppet) {
  520. $service = NULL;
  521. if (array_key_exists($serviceName, $this->servicesCache)) {
  522. $service = $this->servicesCache[$serviceName];
  523. $this->logger->log_debug("Got cached service for $serviceName");
  524. } else {
  525. $service = new Service($this->clusterName, $serviceName, $serviceState, $db, $puppet);
  526. $this->servicesCache[$serviceName] = $service;
  527. $this->logger->log_debug("Did not get cached service for $serviceName");
  528. }
  529. return $service;
  530. }
  531. private function getServiceComponentObj($componentName, $serviceName, $componentState,
  532. $db, $puppet, $isClient) {
  533. $serviceComponent = NULL;
  534. if (array_key_exists($componentName, $this->serviceComponentsCache)) {
  535. $serviceComponent = $this->serviceComponentsCache[$componentName];
  536. $this->logger->log_debug("Got cached serviceComponent for $componentName");
  537. } else {
  538. $serviceComponent =
  539. new ServiceComponent($this->clusterName, $componentName, $serviceName,
  540. $componentState, $db, $puppet, $isClient);
  541. $this->logger->log_debug("Did not get cached serviceComponent for $componentName");
  542. $this->serviceComponentsCache[$componentName] = $serviceComponent;
  543. }
  544. return $serviceComponent;
  545. }
  546. public function reset() {
  547. $this->servicesCache = array();
  548. $this->serviceComponentsCache = array();
  549. $this->logger->log_debug("Reset caches.");
  550. }
  551. public function getServiceClientNode($serviceName) {
  552. $this->logger->log_debug("getServiceClientNode called");
  553. $componentName = $serviceName."_CLIENT";
  554. $clients = $this->db->getHostsForComponent($this->clusterName,
  555. $componentName);
  556. if ($clients === FALSE || $clients["result"] != 0) {
  557. return $clients;
  558. }
  559. $nodes = array_keys($clients["hosts"]);
  560. return array("result" => 0, "error" => "", "nodes" => $nodes);
  561. }
  562. public function getNodeServices($node) {
  563. $roles = $this->db->getRolesForHosts($this->clusterName, array($node));
  564. if ($roles === FALSE || $roles["result"] != 0) {
  565. return $roles;
  566. }
  567. $serviceNames = array();
  568. if (isset($roles["hosts"][$node]["services"])) {
  569. $serviceNames = array_keys($roles["hosts"][$node]["services"]);
  570. }
  571. $services = array();
  572. foreach ($serviceNames as $serviceName) {
  573. $service = $this->getService($serviceName);
  574. if (!$service) {
  575. $this->logger->log_warn("Failed to get service object for $serviceName");
  576. return array('result' => -1, 'error' => "Failed to get service object for $serviceName");
  577. }
  578. array_push($services, $service);
  579. }
  580. return array('result' => 0, 'error' => "", 'services' => $services);
  581. }
  582. public function getNodeRolesAndState($nodes) {
  583. $roles = $this->db->getRolesForHosts($this->clusterName, $nodes);
  584. return $roles;
  585. }
  586. public function setHostsState($hostsToUpdate, $state) {
  587. $this->logger->log_debug("Update HostRoleState - " . State::$STATE[$state]
  588. . print_r($hostsToUpdate, true));
  589. if (isset(State::$DESIRED_STATE[$state])) {
  590. $result = $this->db->setHostsDesiredState($this->clusterName,
  591. $hostsToUpdate, State::$DESIRED_STATE[$state]);
  592. }
  593. $result = $this->db->setHostsState($this->clusterName,
  594. $hostsToUpdate, State::$STATE[$state]);
  595. return $result;
  596. }
  597. public function matchHostStateToComponent($hosts) {
  598. $this->db->matchHostDesiredStateToComponent($this->clusterName, $hosts);
  599. $result = $this->db->matchHostStateToComponent($this->clusterName, $hosts);
  600. return $result;
  601. }
  602. public function getRecursiveServiceDependents($serviceName) {
  603. $deps = $this->db->getAllServiceDependencies();
  604. if ($deps["result"] != 0) {
  605. return FALSE;
  606. }
  607. return $this->db->getRecursiveServiceDependents($deps["serviceDependencies"],
  608. $serviceName);
  609. }
  610. };
  611. ?>