koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 1 | """ Collect data about ceph nodes""" |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 2 | import os |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 3 | import logging |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 4 | from typing import Dict, cast, List, Set |
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 | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 24 | def get_osds_info(node: IRPCNode, ceph_extra_args: str = "", thcount: int = 8) -> 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 |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 28 | for ip, osd_info_list in discover.get_osds_nodes(node.run, ceph_extra_args, thcount=thcount).items()} |
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 | cdfcdaf | 2017-04-29 10:03:39 +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 | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 43 | if 'ceph' not in ctx.config.discover: |
| 44 | print(ctx.config.discover) |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame] | 45 | logger.debug("Skip ceph discovery due to config setting") |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 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 | |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 52 | if 'metadata' in ctx.config.discover: |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame] | 53 | logger.exception("Ceph metadata discovery is not implemented") |
| 54 | raise StopTestError() |
| 55 | |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 56 | ignore_errors = 'ignore_errors' in ctx.config.discover |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 57 | ceph = ctx.config.ceph |
| 58 | root_node_uri = cast(str, ceph.root_node) |
| 59 | cluster = ceph.get("cluster", "ceph") |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame] | 60 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 61 | conf = ceph.get("conf") |
| 62 | key = ceph.get("key") |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 63 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 64 | if conf is None: |
| 65 | conf = "/etc/ceph/{}.conf".format(cluster) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 66 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 67 | if key is None: |
| 68 | key = "/etc/ceph/{}.client.admin.keyring".format(cluster) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 69 | |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame] | 70 | ceph_extra_args = "" |
| 71 | |
| 72 | if conf: |
| 73 | ceph_extra_args += " -c '{}'".format(conf) |
| 74 | |
| 75 | if key: |
| 76 | ceph_extra_args += " -k '{}'".format(key) |
| 77 | |
| 78 | logger.debug("Start discovering ceph nodes from root %s", root_node_uri) |
| 79 | logger.debug("cluster=%s key=%s conf=%s", cluster, conf, key) |
| 80 | |
| 81 | info = NodeInfo(parse_ssh_uri(root_node_uri), set()) |
| 82 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 83 | ceph_params = {"cluster": cluster, "conf": conf, "key": key} |
| 84 | |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 85 | ip_remap = { |
| 86 | '10.8.0.4': '172.16.164.71', |
| 87 | '10.8.0.3': '172.16.164.72', |
| 88 | '10.8.0.2': '172.16.164.73', |
| 89 | '10.8.0.5': '172.16.164.74', |
| 90 | '10.8.0.6': '172.16.164.75', |
| 91 | '10.8.0.7': '172.16.164.76', |
| 92 | '10.8.0.8': '172.16.164.77', |
| 93 | '10.8.0.9': '172.16.164.78', |
| 94 | } |
| 95 | |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame] | 96 | 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] | 97 | log_level=ctx.config.rpc_log_level) as node: |
| 98 | |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 99 | # ssh_key = node.get_file_content("~/.ssh/id_rsa", expanduser=True) |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 100 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 101 | try: |
| 102 | ips = set() |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 103 | for ip, osds_info in get_osds_info(node, ceph_extra_args, thcount=16).items(): |
| 104 | ip = ip_remap[ip] |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 105 | ips.add(ip) |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 106 | # creds = ConnCreds(to_ip(cast(str, ip)), user="root", key=ssh_key) |
| 107 | creds = ConnCreds(to_ip(cast(str, ip)), user="root") |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 108 | info = ctx.merge_node(creds, {'ceph-osd'}) |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 109 | info.params.setdefault('ceph-osds', []).extend(info.__dict__.copy() for info in osds_info) |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 110 | assert 'ceph' not in info.params or info.params['ceph'] == ceph_params |
| 111 | info.params['ceph'] = ceph_params |
| 112 | |
| 113 | logger.debug("Found %s nodes with ceph-osd role", len(ips)) |
| 114 | except Exception as exc: |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame] | 115 | if not ignore_errors: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 116 | logger.exception("OSD discovery failed") |
| 117 | raise StopTestError() |
| 118 | else: |
| 119 | logger.warning("OSD discovery failed %s", exc) |
| 120 | |
| 121 | try: |
| 122 | counter = 0 |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame] | 123 | for counter, ip in enumerate(get_mons_ips(node, ceph_extra_args)): |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 124 | ip = ip_remap[ip] |
| 125 | # creds = ConnCreds(to_ip(cast(str, ip)), user="root", key=ssh_key) |
| 126 | creds = ConnCreds(to_ip(cast(str, ip)), user="root") |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 127 | info = ctx.merge_node(creds, {'ceph-mon'}) |
| 128 | assert 'ceph' not in info.params or info.params['ceph'] == ceph_params |
| 129 | info.params['ceph'] = ceph_params |
| 130 | logger.debug("Found %s nodes with ceph-mon role", counter + 1) |
| 131 | except Exception as exc: |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame] | 132 | if not ignore_errors: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 133 | logger.exception("MON discovery failed") |
| 134 | raise StopTestError() |
| 135 | else: |
| 136 | logger.warning("MON discovery failed %s", exc) |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 137 | |
| 138 | |
| 139 | def raw_dev_name(path): |
| 140 | if path.startswith("/dev/"): |
| 141 | path = path[5:] |
| 142 | while path and path[-1].isdigit(): |
| 143 | path = path[:-1] |
| 144 | return path |
| 145 | |
| 146 | |
| 147 | class FillCephInfoStage(Stage): |
| 148 | config_block = 'ceph' |
| 149 | priority = StepOrder.UPDATE_NODES_INFO |
| 150 | |
| 151 | def run(self, ctx: TestRun) -> None: |
| 152 | for node in ctx.nodes: |
| 153 | if 'ceph_storage_devs' not in node.info.params: |
| 154 | if 'ceph-osd' in node.info.roles: |
| 155 | jdevs = set() |
| 156 | sdevs = set() |
| 157 | for osd_info in node.info.params['ceph-osds']: |
| 158 | for key, sset in [('journal', jdevs), ('storage', sdevs)]: |
| 159 | path = osd_info.get(key) |
| 160 | if path: |
| 161 | dpath = node.conn.fs.get_dev_for_file(path) |
| 162 | if isinstance(dpath, bytes): |
| 163 | dpath = dpath.decode('utf8') |
| 164 | sset.add(raw_dev_name(dpath)) |
| 165 | node.info.params['ceph_storage_devs'] = list(sdevs) |
| 166 | node.info.params['ceph_journal_devs'] = list(jdevs) |