OrchestratorDB.php 25 KB

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