123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693 |
- <?php
- /*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- include_once "../orchestrator/State.php";
- include_once "../orchestrator/Service.php";
- include_once "../orchestrator/ServiceComponent.php";
- include_once "../orchestrator/Cluster.php";
- include_once '../util/Logger.php';
- include_once '../conf/Config.inc';
- include_once "../util/lock.php";
- include_once '../db/HMCDBAccessor.php';
- include_once "Transaction.php";
- class OrchestratorDB {
- // Cluster name
- public $clusterName;
- private $db;
- private $logger;
- private $puppet;
- private $exploreMode;
- private $servicesCache;
- private $serviceComponentsCache;
- /**
- * Initialize ODB
- * @param string $dbPath
- * @param string $clusterName
- * @param object $puppet PuppetInvoker
- */
- function __construct($dbPath, $clusterName, $puppet) {
- $this->clusterName = $clusterName;
- $this->db = new HMCDBAccessor($dbPath);
- $this->puppet = $puppet;
- $this->exploreMode = false;
- $this->servicesCache = array();
- $this->serviceComponentsCache = array();
- $this->logger = new HMCLogger("OrchestratorDB");
- }
- /**
- * Get all the services in the cluster.
- * @return array of Service objects
- * @return FALSE on error
- */
- public function getClusterServices() {
- $services = $this->db->getAllServicesInfo($this->clusterName);
- if ($services === FALSE || $services["result"] != 0) {
- $this->logger->log_error("Failed to get service list from DB");
- return FALSE;
- }
- $svcObjs = array();
- foreach ($services["services"] as $svc) {
- if (!$svc["isEnabled"]) {
- continue;
- }
- $state = STATE::getStateFromString($svc["state"]);
- if ($state === FALSE) {
- $this->logger->log_error("Found service with invalid state"
- . ", service=" . $svc["serviceName"]
- . ", state=" . $svc["state"]);
- $state = 0; // unknown
- }
- $svcObj =
- $this->getServiceObj($svc["serviceName"], $state, $this, $this->puppet);
- array_push($svcObjs, $svcObj);
- }
- return $svcObjs;
- }
- /**
- * Get the Service object corresponding to the serviceName.
- * @param serviceName service name
- * @return Service
- * @return FALSE on error
- */
- public function getService($serviceName) {
- $serviceInfo = $this->db->getServiceInfo($this->clusterName, $serviceName);
- if ($serviceInfo["result"] != 0) {
- $this->logger->log_error("Failed to get serviceInfo for $serviceName with "
- . $serviceInfo["error"]);
- return FALSE;
- }
- if ($serviceInfo["isEnabled"] != TRUE) {
- $this->logger->log_error("Could not find ServiceInfo for legal $serviceName");
- return FALSE;
- }
- $state = STATE::getStateFromString($serviceInfo["state"]);
- return $this->getServiceObj($serviceName, $state, $this, $this->puppet);
- }
- public function getServices($serviceNames) {
- $svcObjs = array();
- foreach ($serviceNames as $serviceName) {
- $svcObj = $this->getService($serviceName);
- if ($svcObj === FALSE) {
- return FALSE;
- }
- $svcObjs[$serviceName] = $svcObj;
- }
- return $svcObjs;
- }
- /**
- * Get service dependencies for the given service.
- * @param serviceName service name
- * @return array of Service objects
- */
- public function getServiceDependencies($serviceName) {
- $svcDeps = $this->db->getServiceDependencies($serviceName);
- if ($svcDeps === FALSE || $svcDeps["result"] != 0) {
- $this->logger->log_error("Failed to get service deps from DB");
- return FALSE;
- }
- $services = $this->db->getAllServicesInfo($this->clusterName);
- if ($services === FALSE || $services["result"] != 0) {
- $this->logger->log_error("Failed to get service list from DB");
- return FALSE;
- }
- $svcObjs = array();
- foreach ($svcDeps["serviceDependencies"] as $svcDep) {
- if (!isset($services["services"][$svcDep])) {
- $this->logger->log_error("Found a service dependency that does not "
- . " exist in DB");
- return FALSE;
- }
- $svc = $services["services"][$svcDep];
- $state = STATE::getStateFromString($svc["state"]);
- if ($state === FALSE) {
- $this->logger->log_error("Found service with invalid state"
- . ", service=" . $svc["serviceName"]
- . ", state=" . $svc["state"]);
- $state = 0; // unknown
- }
- $svcObj =
- $this->getServiceObj($svc["serviceName"], $state, $this, $this->puppet);
- array_push($svcObjs, $svcObj);
- }
- return $svcObjs;
- }
- /**
- * Get services which depend on the given service.
- * @param serviceName service name
- * @return array of Service objects
- */
- public function getServiceDependents($serviceName) {
- $svcDeps = $this->db->getServiceDependents($serviceName);
- if ($svcDeps === FALSE || $svcDeps["result"] != 0) {
- $this->logger->log_error("Failed to get service deps from DB");
- return FALSE;
- }
- $services = $this->db->getAllServicesInfo($this->clusterName);
- if ($services === FALSE || $services["result"] != 0) {
- $this->logger->log_error("Failed to get service list from DB");
- return FALSE;
- }
- $svcObjs = array();
- foreach ($svcDeps["serviceDependents"] as $svcDep) {
- if (!isset($services["services"][$svcDep])) {
- $this->logger->log_debug("Found a service dependent that does not "
- . " exist in DB");
- continue;
- }
- $svc = $services["services"][$svcDep];
- if (!$svc["isEnabled"]) {
- continue;
- }
- $state = STATE::getStateFromString($svc["state"]);
- if ($state === FALSE) {
- $this->logger->log_warn("Found service with invalid state"
- . ", service=" . $svc["serviceName"]
- . ", state=" . $svc["state"]);
- $state = 0; // unknown
- }
- $svcObj =
- $this->getServiceObj($svc["serviceName"], $state, $this, $this->puppet);
- array_push($svcObjs, $svcObj);
- }
- return $svcObjs;
- }
- /**
- * Get components of a service.
- * @param serviceName service name
- * @return array of ServiceComponent objects
- */
- public function getServiceComponents($serviceName) {
- $result = $this->db->getAllServiceComponents($serviceName);
- if ($result === FALSE || $result["result"] != 0
- || !isset($result["components"])
- || !is_array($result["components"])) {
- $this->logger->log_error("Failed to get component list from DB");
- return FALSE;
- }
- $fullCompList = $result["components"];
- $result = $this->db->getAllServiceComponentsInfo($this->clusterName);
- if ($result === FALSE || $result["result"] != 0) {
- $this->logger->log_error("Failed to get component list from DB");
- return FALSE;
- }
- $compObjs = array();
- if (!isset($result["services"][$serviceName])
- || !is_array($result["services"][$serviceName])
- || !is_array($result["services"][$serviceName]["components"])) {
- return array();
- }
- $comps = $result["services"][$serviceName]["components"];
- foreach ($comps as $comp) {
- $state = STATE::getStateFromString($comp["state"]);
- if ($state === FALSE) {
- $this->logger->log_warn("Found component with invalid state"
- . ", component=" . $comp["componentName"]
- . ", state=" . $comp["state"]);
- $state = 0;
- }
- $isClient = FALSE;
- if (isset($fullCompList[$comp["componentName"]])) {
- if (isset($fullCompList[$comp["componentName"]]["isClient"])) {
- $isClient = $fullCompList[$comp["componentName"]]["isClient"];
- }
- } else {
- $this->logger->log_warn("Found component which doesn't exist in meta list"
- . ", component=" . $comp["componentName"]);
- }
- $compObj =
- $this->getServiceComponentObj($comp["componentName"], $serviceName,
- $state, $this, $this->puppet, $isClient);
- array_push($compObjs, $compObj);
- }
- return $compObjs;
- }
- public function getNagiosServerComponent() {
- $svc = $this->getService("NAGIOS");
- if ($svc === FALSE) {
- return $svc;
- }
- $compObjs = $this->getServiceComponents("NAGIOS");
- foreach ($compObjs as $compObj) {
- if ($compObj->name == "NAGIOS_SERVER") {
- return $compObj;
- }
- }
- return FALSE;
- }
- public function getDashboardServerComponent() {
- $svc = $this->getService("DASHBOARD");
- if ($svc === FALSE) {
- return $svc;
- }
- $compObjs = $this->getServiceComponents("DASHBOARD");
- foreach ($compObjs as $compObj) {
- if ($compObj->name == "DASHBOARD") {
- return $compObj;
- }
- }
- return FALSE;
- }
- /**
- * Get component dependencies for a given component of a given
- * service.
- * @param serviceName service name
- * @param componentName component name
- * @return array of ServiceComponent objects
- */
- public function getComponentDependencies($serviceName, $componentName) {
- $result = $this->db->getAllServiceComponentsList();
- if ($result === FALSE || $result["result"] != 0) {
- $this->logger->log_error("Failed to get component list from DB");
- return FALSE;
- }
- $fullCompList = array();
- if (isset($result["services"])) {
- foreach ($result["services"] as $svc => $svcInfo) {
- if (isset($svcInfo["components"])) {
- foreach ($svcInfo["components"] as $comp => $compInfo) {
- $fullCompList[$comp] = $compInfo["isClient"];
- }
- }
- }
- }
- $result = $this->db->getAllServiceComponentsInfo($this->clusterName);
- if ($result === FALSE || $result["result"] != 0
- || !isset($result["services"][$serviceName])
- || !is_array($result["services"][$serviceName])
- || !is_array($result["services"][$serviceName]["components"])) {
- $this->logger->log_error("Failed to get component list from DB");
- return FALSE;
- }
- $compDeps = $this->db->getServiceComponentDependencies($componentName);
- if ($result === FALSE || $result["result"] != 0) {
- $this->logger->log_error("Failed to get component deps list from DB");
- return FALSE;
- }
- $compObjs = array();
- $comps = $result["services"][$serviceName]["components"];
- foreach ($comps as $comp) {
- if (FALSE === array_search($comp["componentName"], $compDeps["componentDependencies"])) {
- $this->logger->log_debug("Skipping component as not in dep list, comp="
- . $comp["componentName"]);
- continue;
- }
- $state = STATE::getStateFromString($comp["state"]);
- if ($state === FALSE) {
- $this->logger->log_error("Found component with invalid state"
- . ", component=" . $comp["componentName"]
- . ", state=" . $comp["state"]);
- $state = 0;
- }
- $isClient = FALSE;
- if (isset($fullCompList[$comp["componentName"]])) {
- $isClient = $fullCompList[$comp["componentName"]];
- } else {
- $this->logger->log_warn("Found component which doesn't exist in meta list"
- . ", component=" . $comp["componentName"]);
- }
- $compObj =
- $this->getServiceComponentObj($comp["componentName"], $serviceName,
- $state, $this, $this->puppet, $isClient);
- array_push($compObjs, $compObj);
- }
- return $compObjs;
- }
- /**
- * Get component dependents for a given component of a given
- * service.
- * @param serviceName service name
- * @param componentName component name
- * @return array of ServiceComponent objects
- */
- public function getComponentDependents($serviceName, $componentName) {
- $result = $this->db->getAllServiceComponentsList();
- if ($result === FALSE || $result["result"] != 0) {
- $this->logger->log_error("Failed to get component list from DB");
- return FALSE;
- }
- $fullCompList = array();
- if (isset($result["services"])) {
- foreach ($result["services"] as $svc => $svcInfo) {
- if (isset($svcInfo["components"])) {
- foreach ($svcInfo["components"] as $comp => $compInfo) {
- $fullCompList[$comp] = $compInfo["isClient"];
- }
- }
- }
- }
- $result = $this->db->getAllServiceComponentsInfo($this->clusterName);
- if ($result === FALSE || $result["result"] != 0
- || !isset($result["services"][$serviceName])
- || !is_array($result["services"][$serviceName])
- || !is_array($result["services"][$serviceName]["components"])) {
- $this->logger->log_error("Failed to get component list from DB");
- return FALSE;
- }
- $compDeps = $this->db->getServiceComponentDependents($componentName);
- if ($result === FALSE || $result["result"] != 0) {
- $this->logger->log_error("Failed to get component deps list from DB");
- return FALSE;
- }
- $compObjs = array();
- $comps = $result["services"][$serviceName]["components"];
- foreach ($comps as $comp) {
- if (FALSE === array_search($comp["componentName"], $compDeps["componentDependents"])) {
- $this->logger->log_debug("Skipping component as not in dep list, comp="
- . $comp["componentName"]);
- continue;
- }
- $state = STATE::getStateFromString($comp["state"]);
- if ($state === FALSE) {
- $this->logger->log_error("Found component with invalid state"
- . ", component=" . $comp["componentName"]
- . ", state=" . $comp["state"]);
- $state = 0;
- }
- $isClient = FALSE;
- if (isset($fullCompList[$comp["componentName"]])) {
- $isClient = $fullCompList[$comp["componentName"]];
- } else {
- $this->logger->log_warn("Found component which doesn't exist in meta list"
- . ", component=" . $comp["componentName"]);
- }
- $compObj =
- $this->getServiceComponentObj($comp["componentName"], $serviceName,
- $state, $this, $this->puppet, $isClient);
- array_push($compObjs, $compObj);
- }
- return $compObjs;
- }
- /**
- * Get all nodes in the cluster.
- * @return mixed
- * array( "result" => 0, "error" => msg, "nodes" => array())
- */
- public function getAllNodes() {
- $result = $this->db->getAllHostsInfo($this->clusterName,
- array("=" => array ( "discoveryStatus" => "SUCCESS")), array());
- if ($result === FALSE || $result["result"] != 0
- || !isset($result["hosts"]) || !is_array($result["hosts"])) {
- $this->logger->log_error("Failed to get host list from DB");
- return array ("result" => 1,
- "error" => "Failed to get host list from DB");
- }
- $nodes = array();
- foreach ($result["hosts"] as $host) {
- array_push($nodes, $host["hostName"]);
- }
- $result = $this->db->getAllHostsByComponent($this->clusterName);
- if ($result === FALSE || $result["result"] != 0) {
- $this->logger->log_error("Failed to get host component mapping from DB");
- return array ("result" => 1,
- "error" => "Failed to get host component mapping from DB");
- }
- $compMapping = array ();
- if (isset($result["components"])
- && is_array($result["components"])) {
- foreach ($result["components"] as $compName => $hostsList) {
- if (isset($hostsList["hosts"])
- && !empty($hostsList["hosts"])) {
- $compMapping[$compName] = array_keys($hostsList["hosts"]);
- }
- }
- }
- return array ("result" => 0, "error" => "", "nodes" => $nodes,
- "componentMapping" => $compMapping);
- }
- /**
- * Get nodes for the given service-component.
- * @param serviceComponent service component
- * @return mixed
- * array( "result" => 0, "error" => msg, "nodes" => array())
- */
- public function getComponentNodes($serviceComponent) {
- $result = $this->db->getHostsForComponent($this->clusterName,
- $serviceComponent->name);
- if ($result === FALSE || $result["result"] != 0
- || !isset($result["hosts"]) || !is_array($result["hosts"])) {
- $this->logger->log_error("Failed to get host list from DB");
- return array ("result" => 1,
- "error" => "Failed to get host list from DB");
- }
- $nodes = array_keys($result["hosts"]);
- return array ("result" => 0, "error" => "", "nodes" => $nodes);
- }
- /**
- * Set service state.
- * @param service service whose state needs to be set
- * @param state service state
- * @return mixed
- * array( "result" => 0, "error" => msg)
- */
- public function setServiceState($service, $state) {
- $this->logger->log_info($service->name . " - ". State::$STATE[$state]);
- if (isset(State::$DESIRED_STATE[$state])) {
- $result = $this->db->setServiceDesiredState($this->clusterName,
- $service->name, State::$DESIRED_STATE[$state]);
- }
- $result = $this->db->setServiceState($this->clusterName,
- $service->name, State::$STATE[$state]);
- return $result;
- }
- /**
- * Set service component state.
- * @param serviceComponent service-component whose state needs to be set
- * @param state service-component state
- * @return mixed
- * array( "result" => 0, "error" => msg)
- */
- public function setServiceComponentState($serviceName, $componentName, $state) {
- $this->logger->log_info("Update ServiceComponentState " . $serviceName . " - "
- . $componentName . " - " . State::$STATE[$state]);
- if (isset(State::$DESIRED_STATE[$state])) {
- $result = $this->db->setServiceComponentDesiredState($this->clusterName,
- $componentName, State::$DESIRED_STATE[$state], TRUE);
- }
- $result = $this->db->setServiceComponentState($this->clusterName,
- $componentName, State::$STATE[$state], TRUE);
- return $result;
- }
- /**
- * Persist a single transaction.
- * @param transaction transaction to be persisted
- * @param state state of the transaction
- * @param description description of the transaction
- * @param dryRun is this a dry-run?
- * @param txnType Type identifier of txn
- * @return mixed
- * array( "result" => 0, "error" => msg)
- */
- public function persistTransaction($transaction, $state, $description,
- $progress, $txnType, $dryRun) {
- if ($transaction == NULL) {
- return array ( "result" => 0, "error" => "" );
- }
- if ($dryRun == TRUE) {
- $state = "PENDING";
- $progress = "PENDING";
- }
- $this->logger->log_info("persist: " . $transaction->toString()
- . ":" . $state . ":" . $description . ":" . $progress);
- $result = $this->db->insertOrUpdateSubTransaction($this->clusterName,
- $transaction->txId, $transaction->subTxId, $transaction->parentSubTxId,
- $state, $description, $progress, $txnType);
- return $result;
- }
- public function persistTransactionOpStatus($transaction, $opStatus) {
- if ($transaction == NULL) {
- return array ( "result" => 0, "error" => "" );
- }
- $result = $this->db->updateSubTransactionOpStatus($this->clusterName,
- $transaction->txId, $transaction->subTxId, $opStatus);
- return $result;
- }
- private function getServiceObj($serviceName, $serviceState, $db, $puppet) {
- $service = NULL;
- if (array_key_exists($serviceName, $this->servicesCache)) {
- $service = $this->servicesCache[$serviceName];
- $this->logger->log_debug("Got cached service for $serviceName");
- } else {
- $service = new Service($this->clusterName, $serviceName, $serviceState, $db, $puppet);
- $this->servicesCache[$serviceName] = $service;
- $this->logger->log_debug("Did not get cached service for $serviceName");
- }
- return $service;
- }
- private function getServiceComponentObj($componentName, $serviceName, $componentState,
- $db, $puppet, $isClient) {
- $serviceComponent = NULL;
- if (array_key_exists($componentName, $this->serviceComponentsCache)) {
- $serviceComponent = $this->serviceComponentsCache[$componentName];
- $this->logger->log_debug("Got cached serviceComponent for $componentName");
- } else {
- $serviceComponent =
- new ServiceComponent($this->clusterName, $componentName, $serviceName,
- $componentState, $db, $puppet, $isClient);
- $this->logger->log_debug("Did not get cached serviceComponent for $componentName");
- $this->serviceComponentsCache[$componentName] = $serviceComponent;
- }
- return $serviceComponent;
- }
- public function reset() {
- $this->servicesCache = array();
- $this->serviceComponentsCache = array();
- $this->logger->log_debug("Reset caches.");
- }
- public function getServiceClientNode($serviceName) {
- $this->logger->log_debug("getServiceClientNode called");
- $componentName = $serviceName."_CLIENT";
- $clients = $this->db->getHostsForComponent($this->clusterName,
- $componentName);
- if ($clients === FALSE || $clients["result"] != 0) {
- return $clients;
- }
- $nodes = array_keys($clients["hosts"]);
- return array("result" => 0, "error" => "", "nodes" => $nodes);
- }
- public function getNodeServices($node) {
- $roles = $this->db->getRolesForHosts($this->clusterName, array($node));
- if ($roles === FALSE || $roles["result"] != 0) {
- return $roles;
- }
- $serviceNames = array();
- if (isset($roles["hosts"][$node]["services"])) {
- $serviceNames = array_keys($roles["hosts"][$node]["services"]);
- }
- $services = array();
- foreach ($serviceNames as $serviceName) {
- $service = $this->getService($serviceName);
- if (!$service) {
- $this->logger->log_warn("Failed to get service object for $serviceName");
- return array('result' => -1, 'error' => "Failed to get service object for $serviceName");
- }
- array_push($services, $service);
- }
- return array('result' => 0, 'error' => "", 'services' => $services);
- }
- public function getNodeRolesAndState($nodes) {
- $roles = $this->db->getRolesForHosts($this->clusterName, $nodes);
- return $roles;
- }
- public function setHostsState($hostsToUpdate, $state) {
- $this->logger->log_debug("Update HostRoleState - " . State::$STATE[$state]
- . print_r($hostsToUpdate, true));
- if (isset(State::$DESIRED_STATE[$state])) {
- $result = $this->db->setHostsDesiredState($this->clusterName,
- $hostsToUpdate, State::$DESIRED_STATE[$state]);
- }
- $result = $this->db->setHostsState($this->clusterName,
- $hostsToUpdate, State::$STATE[$state]);
- return $result;
- }
- public function matchHostStateToComponent($hosts) {
- $this->db->matchHostDesiredStateToComponent($this->clusterName, $hosts);
- $result = $this->db->matchHostStateToComponent($this->clusterName, $hosts);
- return $result;
- }
- public function getRecursiveServiceDependents($serviceName) {
- $deps = $this->db->getAllServiceDependencies();
- if ($deps["result"] != 0) {
- return FALSE;
- }
- return $this->db->getRecursiveServiceDependents($deps["serviceDependencies"],
- $serviceName);
- }
- };
- ?>
|