koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 1 | """ Collect data about ceph nodes""" |
| 2 | import json |
| 3 | import logging |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 4 | from typing import Dict, cast, List, Set, Optional |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 5 | |
| 6 | |
| 7 | from .node_interfaces import NodeInfo, IRPCNode |
| 8 | from .ssh_utils import ConnCreds |
| 9 | from .common_types import IP |
| 10 | from .stage import Stage, StepOrder |
| 11 | from .test_run_class import TestRun |
| 12 | from .ssh_utils import parse_ssh_uri |
| 13 | from .node import connect, setup_rpc |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame] | 14 | from .utils import StopTestError, to_ip |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 15 | |
| 16 | |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame] | 17 | from cephlib import discover |
| 18 | from cephlib.discover import OSDInfo |
| 19 | |
| 20 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 21 | logger = logging.getLogger("wally") |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 22 | |
| 23 | |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame] | 24 | def get_osds_info(node: IRPCNode, ceph_extra_args: str = "") -> Dict[IP, List[OSDInfo]]: |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 25 | """Get set of osd's ip""" |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame] | 26 | res = {} # type: Dict[IP, List[OSDInfo]] |
| 27 | return {IP(ip): osd_info_list |
| 28 | for ip, osd_info_list in discover.get_osds_nodes(node.run, ceph_extra_args)} |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 29 | |
| 30 | |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame] | 31 | def get_mons_ips(node: IRPCNode, ceph_extra_args: str = "") -> Set[IP]: |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 32 | """Return mon ip set""" |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame] | 33 | return {IP(ip) for ip in discover.get_mons_nodes(node.run, ceph_extra_args).values()} |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 34 | |
| 35 | |
| 36 | class DiscoverCephStage(Stage): |
| 37 | config_block = 'ceph' |
| 38 | priority = StepOrder.DISCOVER |
| 39 | |
| 40 | def run(self, ctx: TestRun) -> None: |
| 41 | """Return list of ceph's nodes NodeInfo""" |
| 42 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 43 | discovery = ctx.config.get("discovery") |
| 44 | if discovery == 'disable' or discovery == 'metadata': |
| 45 | logger.info("Skip ceph discovery due to config setting") |
| 46 | return |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 47 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 48 | if 'all_nodes' in ctx.storage: |
| 49 | logger.debug("Skip ceph discovery, use previously discovered nodes") |
| 50 | return |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 51 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 52 | ceph = ctx.config.ceph |
| 53 | root_node_uri = cast(str, ceph.root_node) |
| 54 | cluster = ceph.get("cluster", "ceph") |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame] | 55 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 56 | conf = ceph.get("conf") |
| 57 | key = ceph.get("key") |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 58 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 59 | if conf is None: |
| 60 | conf = "/etc/ceph/{}.conf".format(cluster) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 61 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 62 | if key is None: |
| 63 | key = "/etc/ceph/{}.client.admin.keyring".format(cluster) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 64 | |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame] | 65 | ceph_extra_args = "" |
| 66 | |
| 67 | if conf: |
| 68 | ceph_extra_args += " -c '{}'".format(conf) |
| 69 | |
| 70 | if key: |
| 71 | ceph_extra_args += " -k '{}'".format(key) |
| 72 | |
| 73 | logger.debug("Start discovering ceph nodes from root %s", root_node_uri) |
| 74 | logger.debug("cluster=%s key=%s conf=%s", cluster, conf, key) |
| 75 | |
| 76 | info = NodeInfo(parse_ssh_uri(root_node_uri), set()) |
| 77 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 78 | ceph_params = {"cluster": cluster, "conf": conf, "key": key} |
| 79 | |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame] | 80 | with setup_rpc(connect(info), ctx.rpc_code, ctx.default_rpc_plugins, |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 81 | log_level=ctx.config.rpc_log_level) as node: |
| 82 | |
| 83 | ssh_key = node.get_file_content("~/.ssh/id_rsa") |
| 84 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 85 | try: |
| 86 | ips = set() |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame] | 87 | for ip, osds_info in get_osds_info(node, ceph_extra_args).items(): |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 88 | ips.add(ip) |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame] | 89 | creds = ConnCreds(to_ip(cast(str, ip)), user="root", key=ssh_key) |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 90 | info = ctx.merge_node(creds, {'ceph-osd'}) |
| 91 | info.params.setdefault('ceph-osds', []).extend(osds_info) |
| 92 | assert 'ceph' not in info.params or info.params['ceph'] == ceph_params |
| 93 | info.params['ceph'] = ceph_params |
| 94 | |
| 95 | logger.debug("Found %s nodes with ceph-osd role", len(ips)) |
| 96 | except Exception as exc: |
| 97 | if discovery != 'ignore_errors': |
| 98 | logger.exception("OSD discovery failed") |
| 99 | raise StopTestError() |
| 100 | else: |
| 101 | logger.warning("OSD discovery failed %s", exc) |
| 102 | |
| 103 | try: |
| 104 | counter = 0 |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame] | 105 | for counter, ip in enumerate(get_mons_ips(node, ceph_extra_args)): |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame] | 106 | creds = ConnCreds(to_ip(cast(str, ip)), user="root", key=ssh_key) |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 107 | info = ctx.merge_node(creds, {'ceph-mon'}) |
| 108 | assert 'ceph' not in info.params or info.params['ceph'] == ceph_params |
| 109 | info.params['ceph'] = ceph_params |
| 110 | logger.debug("Found %s nodes with ceph-mon role", counter + 1) |
| 111 | except Exception as exc: |
| 112 | if discovery != 'ignore_errors': |
| 113 | logger.exception("MON discovery failed") |
| 114 | raise StopTestError() |
| 115 | else: |
| 116 | logger.warning("MON discovery failed %s", exc) |