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 | |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame^] | 43 | if 'ceph' not in ctx.config.discovery: |
| 44 | logger.debug("Skip ceph discovery due to config setting") |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 45 | return |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 46 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 47 | if 'all_nodes' in ctx.storage: |
| 48 | logger.debug("Skip ceph discovery, use previously discovered nodes") |
| 49 | return |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 50 | |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame^] | 51 | if 'metadata' in ctx.config.discovery: |
| 52 | logger.exception("Ceph metadata discovery is not implemented") |
| 53 | raise StopTestError() |
| 54 | |
| 55 | ignore_errors = 'ignore_errors' in ctx.config.discovery |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 56 | ceph = ctx.config.ceph |
| 57 | root_node_uri = cast(str, ceph.root_node) |
| 58 | cluster = ceph.get("cluster", "ceph") |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame] | 59 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 60 | conf = ceph.get("conf") |
| 61 | key = ceph.get("key") |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 62 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 63 | if conf is None: |
| 64 | conf = "/etc/ceph/{}.conf".format(cluster) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 65 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 66 | if key is None: |
| 67 | key = "/etc/ceph/{}.client.admin.keyring".format(cluster) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 68 | |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame] | 69 | ceph_extra_args = "" |
| 70 | |
| 71 | if conf: |
| 72 | ceph_extra_args += " -c '{}'".format(conf) |
| 73 | |
| 74 | if key: |
| 75 | ceph_extra_args += " -k '{}'".format(key) |
| 76 | |
| 77 | logger.debug("Start discovering ceph nodes from root %s", root_node_uri) |
| 78 | logger.debug("cluster=%s key=%s conf=%s", cluster, conf, key) |
| 79 | |
| 80 | info = NodeInfo(parse_ssh_uri(root_node_uri), set()) |
| 81 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 82 | ceph_params = {"cluster": cluster, "conf": conf, "key": key} |
| 83 | |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame] | 84 | 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] | 85 | log_level=ctx.config.rpc_log_level) as node: |
| 86 | |
| 87 | ssh_key = node.get_file_content("~/.ssh/id_rsa") |
| 88 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 89 | try: |
| 90 | ips = set() |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame] | 91 | 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] | 92 | ips.add(ip) |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame] | 93 | 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] | 94 | info = ctx.merge_node(creds, {'ceph-osd'}) |
| 95 | info.params.setdefault('ceph-osds', []).extend(osds_info) |
| 96 | assert 'ceph' not in info.params or info.params['ceph'] == ceph_params |
| 97 | info.params['ceph'] = ceph_params |
| 98 | |
| 99 | logger.debug("Found %s nodes with ceph-osd role", len(ips)) |
| 100 | except Exception as exc: |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame^] | 101 | if not ignore_errors: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 102 | logger.exception("OSD discovery failed") |
| 103 | raise StopTestError() |
| 104 | else: |
| 105 | logger.warning("OSD discovery failed %s", exc) |
| 106 | |
| 107 | try: |
| 108 | counter = 0 |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame] | 109 | 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] | 110 | 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] | 111 | info = ctx.merge_node(creds, {'ceph-mon'}) |
| 112 | assert 'ceph' not in info.params or info.params['ceph'] == ceph_params |
| 113 | info.params['ceph'] = ceph_params |
| 114 | logger.debug("Found %s nodes with ceph-mon role", counter + 1) |
| 115 | except Exception as exc: |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame^] | 116 | if not ignore_errors: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 117 | logger.exception("MON discovery failed") |
| 118 | raise StopTestError() |
| 119 | else: |
| 120 | logger.warning("MON discovery failed %s", exc) |