|
| 1 | +import logging |
| 2 | +from textwrap import dedent |
| 3 | +from teuthology.task import Task |
| 4 | +from teuthology import misc |
| 5 | +from teuthology.exceptions import ConfigError |
| 6 | +from tasks.util import get_remote_for_role |
| 7 | +from tasks.cephadm import _shell |
| 8 | + |
| 9 | +log = logging.getLogger(__name__) |
| 10 | + |
| 11 | +conf_file = '/etc/ceph/nvmeof.env' |
| 12 | + |
| 13 | + |
| 14 | +class Nvmeof(Task): |
| 15 | + """ |
| 16 | + Setup nvmeof gateway on client and then share gateway config to target host. |
| 17 | +
|
| 18 | + - nvmeof: |
| 19 | + client: client.0 |
| 20 | + version: default |
| 21 | + rbd: |
| 22 | + pool_name: mypool |
| 23 | + image_name: myimage |
| 24 | + rbd_size: 1024 |
| 25 | + gateway_config: |
| 26 | + source: host.a |
| 27 | + target: client.2 |
| 28 | + vars: |
| 29 | + cli_version: latest |
| 30 | + |
| 31 | + """ |
| 32 | + |
| 33 | + def setup(self): |
| 34 | + super(Nvmeof, self).setup() |
| 35 | + try: |
| 36 | + self.client = self.config['client'] |
| 37 | + except KeyError: |
| 38 | + raise ConfigError('nvmeof requires a client to connect with') |
| 39 | + |
| 40 | + self.cluster_name, type_, self.client_id = misc.split_role(self.client) |
| 41 | + if type_ != 'client': |
| 42 | + msg = 'client role ({0}) must be a client'.format(self.client) |
| 43 | + raise ConfigError(msg) |
| 44 | + self.remote = get_remote_for_role(self.ctx, self.client) |
| 45 | + |
| 46 | + def begin(self): |
| 47 | + super(Nvmeof, self).begin() |
| 48 | + self._set_defaults() |
| 49 | + self.deploy_nvmeof() |
| 50 | + self.set_gateway_cfg() |
| 51 | + |
| 52 | + def _set_defaults(self): |
| 53 | + self.gateway_image = self.config.get('version', 'default') |
| 54 | + |
| 55 | + rbd_config = self.config.get('rbd', {}) |
| 56 | + self.poolname = rbd_config.get('pool_name', 'mypool') |
| 57 | + self.rbd_image_name = rbd_config.get('image_name', 'myimage') |
| 58 | + self.rbd_size = rbd_config.get('rbd_size', 1024*8) |
| 59 | + |
| 60 | + gateway_config = self.config.get('gateway_config', {}) |
| 61 | + conf_vars = gateway_config.get('vars', {}) |
| 62 | + self.cli_image = conf_vars.get('cli_version', 'latest') |
| 63 | + self.bdev = conf_vars.get('bdev', 'mybdev') |
| 64 | + self.serial = conf_vars.get('serial', 'SPDK00000000000001') |
| 65 | + self.nqn = conf_vars.get('nqn', 'nqn.2016-06.io.spdk:cnode1') |
| 66 | + self.port = conf_vars.get('port', '4420') |
| 67 | + self.srport = conf_vars.get('srport', '5500') |
| 68 | + |
| 69 | + def deploy_nvmeof(self): |
| 70 | + """ |
| 71 | + Deploy nvmeof gateway. |
| 72 | + """ |
| 73 | + log.info('[nvmeof]: deploying nvmeof gateway...') |
| 74 | + if not hasattr(self.ctx, 'ceph'): |
| 75 | + self.ctx.ceph = {} |
| 76 | + fsid = self.ctx.ceph[self.cluster_name].fsid |
| 77 | + |
| 78 | + nodes = [] |
| 79 | + daemons = {} |
| 80 | + |
| 81 | + for remote, roles in self.ctx.cluster.remotes.items(): |
| 82 | + for role in [r for r in roles |
| 83 | + if misc.is_type('nvmeof', self.cluster_name)(r)]: |
| 84 | + c_, _, id_ = misc.split_role(role) |
| 85 | + log.info('Adding %s on %s' % (role, remote.shortname)) |
| 86 | + nodes.append(remote.shortname + '=' + id_) |
| 87 | + daemons[role] = (remote, id_) |
| 88 | + |
| 89 | + if nodes: |
| 90 | + image = self.gateway_image |
| 91 | + if (image != "default"): |
| 92 | + log.info(f'[nvmeof]: ceph config set mgr mgr/cephadm/container_image_nvmeof quay.io/ceph/nvmeof:{image}') |
| 93 | + _shell(self.ctx, self.cluster_name, self.remote, [ |
| 94 | + 'ceph', 'config', 'set', 'mgr', |
| 95 | + 'mgr/cephadm/container_image_nvmeof', |
| 96 | + f'quay.io/ceph/nvmeof:{image}' |
| 97 | + ]) |
| 98 | + |
| 99 | + poolname = self.poolname |
| 100 | + imagename = self.rbd_image_name |
| 101 | + |
| 102 | + log.info(f'[nvmeof]: ceph osd pool create {poolname}') |
| 103 | + _shell(self.ctx, self.cluster_name, self.remote, [ |
| 104 | + 'ceph', 'osd', 'pool', 'create', poolname |
| 105 | + ]) |
| 106 | + |
| 107 | + log.info(f'[nvmeof]: rbd pool init {poolname}') |
| 108 | + _shell(self.ctx, self.cluster_name, self.remote, [ |
| 109 | + 'rbd', 'pool', 'init', poolname |
| 110 | + ]) |
| 111 | + |
| 112 | + log.info(f'[nvmeof]: ceph orch apply nvmeof {poolname}') |
| 113 | + _shell(self.ctx, self.cluster_name, self.remote, [ |
| 114 | + 'ceph', 'orch', 'apply', 'nvmeof', poolname, |
| 115 | + '--placement', str(len(nodes)) + ';' + ';'.join(nodes) |
| 116 | + ]) |
| 117 | + |
| 118 | + log.info(f'[nvmeof]: rbd create {poolname}/{imagename} --size {self.rbd_size}') |
| 119 | + _shell(self.ctx, self.cluster_name, self.remote, [ |
| 120 | + 'rbd', 'create', f'{poolname}/{imagename}', '--size', f'{self.rbd_size}' |
| 121 | + ]) |
| 122 | + |
| 123 | + for role, i in daemons.items(): |
| 124 | + remote, id_ = i |
| 125 | + self.ctx.daemons.register_daemon( |
| 126 | + remote, 'nvmeof', id_, |
| 127 | + cluster=self.cluster_name, |
| 128 | + fsid=fsid, |
| 129 | + logger=log.getChild(role), |
| 130 | + wait=False, |
| 131 | + started=True, |
| 132 | + ) |
| 133 | + log.info("[nvmeof]: executed deploy_nvmeof successfully!") |
| 134 | + |
| 135 | + def set_gateway_cfg(self): |
| 136 | + log.info('[nvmeof]: running set_gateway_cfg...') |
| 137 | + gateway_config = self.config.get('gateway_config', {}) |
| 138 | + source_host = gateway_config.get('source') |
| 139 | + target_host = gateway_config.get('target') |
| 140 | + if not (source_host and target_host): |
| 141 | + raise ConfigError('gateway_config requires "source" and "target"') |
| 142 | + remote = list(self.ctx.cluster.only(source_host).remotes.keys())[0] |
| 143 | + ip_address = remote.ip_address |
| 144 | + gateway_name = "" |
| 145 | + nvmeof_daemons = self.ctx.daemons.iter_daemons_of_role('nvmeof', cluster=self.cluster_name) |
| 146 | + for daemon in nvmeof_daemons: |
| 147 | + if ip_address == daemon.remote.ip_address: |
| 148 | + gateway_name = daemon.name() |
| 149 | + conf_data = dedent(f""" |
| 150 | + NVMEOF_GATEWAY_IP_ADDRESS={ip_address} |
| 151 | + NVMEOF_GATEWAY_NAME={gateway_name} |
| 152 | + NVMEOF_CLI_IMAGE="quay.io/ceph/nvmeof-cli:{self.cli_image}" |
| 153 | + NVMEOF_BDEV={self.bdev} |
| 154 | + NVMEOF_SERIAL={self.serial} |
| 155 | + NVMEOF_NQN={self.nqn} |
| 156 | + NVMEOF_PORT={self.port} |
| 157 | + NVMEOF_SRPORT={self.srport} |
| 158 | + """) |
| 159 | + target_remote = list(self.ctx.cluster.only(target_host).remotes.keys())[0] |
| 160 | + target_remote.write_file( |
| 161 | + path=conf_file, |
| 162 | + data=conf_data, |
| 163 | + sudo=True |
| 164 | + ) |
| 165 | + log.info("[nvmeof]: executed set_gateway_cfg successfully!") |
| 166 | + |
| 167 | + |
| 168 | +task = Nvmeof |
0 commit comments