diff options
Diffstat (limited to 'app/discover/fetchers/db')
-rw-r--r-- | app/discover/fetchers/db/db_access.py | 43 | ||||
-rw-r--r-- | app/discover/fetchers/db/db_fetch_host_network_agents.py | 3 | ||||
-rw-r--r-- | app/discover/fetchers/db/db_fetch_oteps.py | 4 | ||||
-rw-r--r-- | app/discover/fetchers/db/db_fetch_vedges_ovs.py | 63 |
4 files changed, 75 insertions, 38 deletions
diff --git a/app/discover/fetchers/db/db_access.py b/app/discover/fetchers/db/db_access.py index 49fdb5e..64d7372 100644 --- a/app/discover/fetchers/db/db_access.py +++ b/app/discover/fetchers/db/db_access.py @@ -7,6 +7,7 @@ # which accompanies this distribution, and is available at # # http://www.apache.org/licenses/LICENSE-2.0 # ############################################################################### +import functools import mysql.connector from discover.configuration import Configuration @@ -15,6 +16,24 @@ from discover.scan_error import ScanError from utils.string_utils import jsonify +def with_cursor(method): + @functools.wraps(method) + def wrap(self, *args, **kwargs): + self.connect_to_db(DbAccess.query_count_per_con >= 25) + DbAccess.query_count_per_con += 1 + cursor = DbAccess.conn.cursor(dictionary=True) + try: + res = method(self, *args, cursor=cursor, **kwargs) + DbAccess.conn.commit() + return res + except: + DbAccess.conn.rollback() + raise + finally: + cursor.close() + return wrap + + class DbAccess(Fetcher): conn = None query_count_per_con = 0 @@ -47,10 +66,9 @@ class DbAccess(Fetcher): return DbAccess.query_count_per_con = 0 - @staticmethod - def get_neutron_db_name(): + @with_cursor + def get_neutron_db_name(self, cursor=None): # check if DB schema 'neutron' exists - cursor = DbAccess.conn.cursor(dictionary=True) cursor.execute('SHOW DATABASES') matches = [row.get('Database', '') for row in cursor if 'neutron' in row.get('Database', '')] @@ -68,6 +86,8 @@ class DbAccess(Fetcher): self.log.info("DbAccess: ****** forcing reconnect, " + "query count: %s ******", DbAccess.query_count_per_con) + DbAccess.conn.commit() + DbAccess.conn.close() DbAccess.conn = None self.conf = self.config.get("mysql") cnf = self.conf @@ -76,16 +96,15 @@ class DbAccess(Fetcher): cnf["user"], cnf["pwd"], cnf["schema"]) - def get_objects_list_for_id(self, query, object_type, id): - self.connect_to_db(DbAccess.query_count_per_con >= 25) - DbAccess.query_count_per_con += 1 + @with_cursor + def get_objects_list_for_id(self, query, object_type, object_id, + cursor=None): self.log.debug("query count: %s, running query:\n%s\n", str(DbAccess.query_count_per_con), query) - cursor = DbAccess.conn.cursor(dictionary=True) try: - if id: - cursor.execute(query, [str(id)]) + if object_id: + cursor.execute(query, [str(object_id)]) else: cursor.execute(query) except (AttributeError, mysql.connector.errors.OperationalError) as e: @@ -93,13 +112,13 @@ class DbAccess(Fetcher): self.connect_to_db(True) # try again to run the query cursor = DbAccess.conn.cursor(dictionary=True) - if id: - cursor.execute(query, [str(id)]) + if object_id: + cursor.execute(query, [str(object_id)]) else: cursor.execute(query) rows = [] - for row in cursor: + for row in cursor.fetchall(): rows.append(row) return rows diff --git a/app/discover/fetchers/db/db_fetch_host_network_agents.py b/app/discover/fetchers/db/db_fetch_host_network_agents.py index c323573..7d415f2 100644 --- a/app/discover/fetchers/db/db_fetch_host_network_agents.py +++ b/app/discover/fetchers/db/db_fetch_host_network_agents.py @@ -27,9 +27,8 @@ class DbFetchHostNetworkAgents(DbAccess): host_id = id[:-1 * len("-network_agents")] results = self.get_objects_list_for_id(query, "network_agent", host_id) mechanism_drivers = self.env_config['mechanism_drivers'] - id_prefix = mechanism_drivers[0] if mechanism_drivers else 'network_agent' for o in results: o["configurations"] = json.loads(o["configurations"]) o["name"] = o["binary"] - o['id'] = id_prefix + '-' + o['id'] + o['id'] = o['name'] + '-' + o['id'] return results diff --git a/app/discover/fetchers/db/db_fetch_oteps.py b/app/discover/fetchers/db/db_fetch_oteps.py index 3e3f4e1..f7eb8bd 100644 --- a/app/discover/fetchers/db/db_fetch_oteps.py +++ b/app/discover/fetchers/db/db_fetch_oteps.py @@ -35,7 +35,9 @@ class DbFetchOteps(DbAccess, CliAccess, metaclass=Singleton): table_name = "{}.ml2_{}_endpoints".format(self.neutron_db, tunnel_type) env_config = self.config.get_env_config() distribution = env_config["distribution"] - if distribution == "Canonical-icehouse": + distribution_version = env_config["distribution_version"] + dist_ver = "{}-{}".format(distribution, distribution_version) + if dist_ver == "Canonical-icehouse": # for Icehouse, we only get IP address from the DB, so take the # host IP address and from the host data in Mongo host = self.inv.get_by_id(self.get_env(), host_id) diff --git a/app/discover/fetchers/db/db_fetch_vedges_ovs.py b/app/discover/fetchers/db/db_fetch_vedges_ovs.py index 838ccb9..f516d10 100644 --- a/app/discover/fetchers/db/db_fetch_vedges_ovs.py +++ b/app/discover/fetchers/db/db_fetch_vedges_ovs.py @@ -24,8 +24,8 @@ class DbFetchVedgesOvs(DbAccess, CliAccess, metaclass=Singleton): self.port_re = re.compile("^\s*port (\d+): ([^(]+)( \(internal\))?$") self.port_line_header_prefix = " " * 8 + "Port " - def get(self, id): - host_id = id[:id.rindex('-')] + def get(self, parent_id): + host_id = parent_id[:parent_id.rindex('-')] results = self.get_objects_list_for_id( """ SELECT * @@ -66,11 +66,11 @@ class DbFetchVedgesOvs(DbAccess, CliAccess, metaclass=Singleton): if not port_matches: continue port = {} - id = port_matches.group(1) + port_id = port_matches.group(1) name = port_matches.group(2) is_internal = port_matches.group(3) == " (internal)" port["internal"] = is_internal - port["id"] = id + port["id"] = port_id port["name"] = name ports[name] = port return ports @@ -106,7 +106,7 @@ class DbFetchVedgesOvs(DbAccess, CliAccess, metaclass=Singleton): if "tunneling_ip" not in doc["configurations"]: return {} if not doc["configurations"]["tunneling_ip"]: - self.get_bridge_pnic(doc) + self.get_pnics(doc) return {} # read the 'br-tun' interface ports @@ -148,31 +148,48 @@ class DbFetchVedgesOvs(DbAccess, CliAccess, metaclass=Singleton): tunnel_ports[port["name"]] = port return tunnel_ports - def get_bridge_pnic(self, doc): - conf = doc["configurations"] - if "bridge_mappings" not in conf or not conf["bridge_mappings"]: - return - for v in conf["bridge_mappings"].values(): br = v - ifaces_list_lines = self.run_fetch_lines("ovs-vsctl list-ifaces " + br, - doc["host"]) - br_pnic_postfix = br + "--br-" - interface = "" + def get_pnics(self, vedge) -> dict: + bridges = vedge["configurations"].get("bridge_mappings", {}) + pnics = {} + for bridge in bridges.values(): + self.get_bridge_pnic(pnics, vedge, bridge) + return pnics + + MIRANTIS_DIST = "Mirantis" + + def get_bridge_pnic(self, pnics: dict, vedge: dict, bridge: dict): + cmd = "ovs-vsctl list-ifaces {}".format(bridge) + ifaces_list_lines = self.run_fetch_lines(cmd, vedge["host"]) + env_config = self.configuration.get_env_config() + distribution = env_config.get("distribution") + dist_version = env_config.get("distribution_version") + use_br_postfix = distribution == self.MIRANTIS_DIST and \ + dist_version in ["6.0", "7.0", "8.0"] for l in ifaces_list_lines: - if l.startswith(br_pnic_postfix): - interface = l[len(br_pnic_postfix):] - break - if not interface: - return - doc["pnic"] = interface + if use_br_postfix: + br_pnic_postfix = "{}--br-".format(bridge) + interface = l[len(br_pnic_postfix):] \ + if l.startswith(br_pnic_postfix) \ + else "" + else: + interface = l + if interface: + pnic = self.find_pnic_for_interface(vedge, interface) + if pnic: + pnics[pnic["name"]] = pnic + + def find_pnic_for_interface(self, vedge, interface): # add port ID to pNIC pnic = self.inv.find_items({ "environment": self.get_env(), "type": "host_pnic", - "host": doc["host"], + "host": vedge["host"], "name": interface }, get_single=True) if not pnic: return - port = doc["ports"][interface] - pnic["port_id"] = port["id"] + vedge["pnic"] = interface + port = vedge["ports"].get(interface, {}) + pnic["port_id"] = port.get("id", "") self.inv.set(pnic) + return pnic |