2014-02-03 21:50:07 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import contextlib
|
|
|
|
import logging
|
2014-02-06 18:29:15 +00:00
|
|
|
import textwrap
|
2014-02-19 21:54:15 +00:00
|
|
|
import time
|
2020-07-19 09:35:16 +00:00
|
|
|
from configparser import ConfigParser
|
|
|
|
from io import BytesIO, StringIO
|
2014-02-03 21:50:07 +00:00
|
|
|
|
2014-08-07 14:24:59 +00:00
|
|
|
from teuthology.orchestra import run
|
|
|
|
from teuthology import misc
|
|
|
|
from teuthology.contextutil import nested
|
2014-02-03 21:50:07 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEVSTACK_GIT_REPO = 'https://github.com/openstack-dev/devstack.git'
|
2014-02-14 17:32:37 +00:00
|
|
|
DS_STABLE_BRANCHES = ("havana", "grizzly")
|
2014-02-03 21:50:07 +00:00
|
|
|
|
2014-02-13 16:54:19 +00:00
|
|
|
is_devstack_node = lambda role: role.startswith('devstack')
|
|
|
|
is_osd_node = lambda role: role.startswith('osd')
|
|
|
|
|
2014-02-03 21:50:07 +00:00
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def task(ctx, config):
|
2014-02-13 16:54:19 +00:00
|
|
|
if config is None:
|
|
|
|
config = {}
|
|
|
|
if not isinstance(config, dict):
|
|
|
|
raise TypeError("config must be a dict")
|
|
|
|
with nested(lambda: install(ctx=ctx, config=config),
|
2014-02-14 20:54:10 +00:00
|
|
|
lambda: smoke(ctx=ctx, config=config),
|
2014-02-13 16:54:19 +00:00
|
|
|
):
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def install(ctx, config):
|
2014-02-03 21:50:07 +00:00
|
|
|
"""
|
|
|
|
Install OpenStack DevStack and configure it to use a Ceph cluster for
|
|
|
|
Glance and Cinder.
|
|
|
|
|
|
|
|
Requires one node with a role 'devstack'
|
2014-02-12 16:48:11 +00:00
|
|
|
|
|
|
|
Since devstack runs rampant on the system it's used on, typically you will
|
|
|
|
want to reprovision that machine after using devstack on it.
|
|
|
|
|
|
|
|
Also, the default 2GB of RAM that is given to vps nodes is insufficient. I
|
|
|
|
recommend 4GB. Downburst can be instructed to give 4GB to a vps node by
|
|
|
|
adding this to the yaml:
|
|
|
|
|
|
|
|
downburst:
|
|
|
|
ram: 4G
|
2014-02-20 17:01:58 +00:00
|
|
|
|
|
|
|
This was created using documentation found here:
|
|
|
|
https://github.com/openstack-dev/devstack/blob/master/README.md
|
2020-11-23 12:36:32 +00:00
|
|
|
http://docs.ceph.com/en/latest/rbd/rbd-openstack/
|
2014-02-03 21:50:07 +00:00
|
|
|
"""
|
|
|
|
if config is None:
|
|
|
|
config = {}
|
|
|
|
if not isinstance(config, dict):
|
|
|
|
raise TypeError("config must be a dict")
|
|
|
|
|
qa/tasks: use next(iter(..)) for accessing first element in a view
in python2, dict.values() and dict.keys() return lists. but in python3,
they return views, which cannot be indexed directly using an integer index.
there are three use cases when we access these views in python3:
1. get the first element
2. get all the elements and then *might* want to access them by index
3. get the first element assuming there is only a single element in
the view
4. iterate thru the view
in the 1st case, we cannot assume the number of elements, so to be
python3 compatible, we should use `next(iter(a_dict))` instead.
in the 2nd case, in this change, the view is materialized using
`list(a_dict)`.
in the 3rd case, we can just continue using the short hand of
```py
(first_element,) = a_dict.keys()
```
to unpack the view. this works in both python2 and python3.
in the 4th case, the existing code works in both python2 and python3, as
both list and view can be iterated using `iter`, and `len` works as
well.
Signed-off-by: Kefu Chai <kchai@redhat.com>
2020-03-31 02:16:40 +00:00
|
|
|
devstack_node = next(iter(ctx.cluster.only(is_devstack_node).remotes.keys()))
|
|
|
|
an_osd_node = next(iter(ctx.cluster.only(is_osd_node).remotes.keys()))
|
2014-02-13 16:54:19 +00:00
|
|
|
|
2014-02-14 17:32:37 +00:00
|
|
|
devstack_branch = config.get("branch", "master")
|
|
|
|
install_devstack(devstack_node, devstack_branch)
|
2014-02-03 21:50:07 +00:00
|
|
|
try:
|
2014-02-06 19:20:35 +00:00
|
|
|
configure_devstack_and_ceph(ctx, config, devstack_node, an_osd_node)
|
2014-02-03 21:50:07 +00:00
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-02-14 17:32:37 +00:00
|
|
|
def install_devstack(devstack_node, branch="master"):
|
2014-02-10 20:16:53 +00:00
|
|
|
log.info("Cloning DevStack repo...")
|
|
|
|
|
2014-02-03 21:50:07 +00:00
|
|
|
args = ['git', 'clone', DEVSTACK_GIT_REPO]
|
|
|
|
devstack_node.run(args=args)
|
|
|
|
|
2014-02-14 17:32:37 +00:00
|
|
|
if branch != "master":
|
|
|
|
if branch in DS_STABLE_BRANCHES and not branch.startswith("stable"):
|
|
|
|
branch = "stable/" + branch
|
|
|
|
log.info("Checking out {branch} branch...".format(branch=branch))
|
|
|
|
cmd = "cd devstack && git checkout " + branch
|
|
|
|
devstack_node.run(args=cmd)
|
|
|
|
|
2014-02-10 20:16:53 +00:00
|
|
|
log.info("Installing DevStack...")
|
2014-02-03 21:50:07 +00:00
|
|
|
args = ['cd', 'devstack', run.Raw('&&'), './stack.sh']
|
|
|
|
devstack_node.run(args=args)
|
2014-02-06 02:48:05 +00:00
|
|
|
|
|
|
|
|
2014-02-06 19:20:35 +00:00
|
|
|
def configure_devstack_and_ceph(ctx, config, devstack_node, ceph_node):
|
|
|
|
pool_size = config.get('pool_size', '128')
|
2014-02-06 18:29:15 +00:00
|
|
|
create_pools(ceph_node, pool_size)
|
|
|
|
distribute_ceph_conf(devstack_node, ceph_node)
|
2014-02-06 22:39:10 +00:00
|
|
|
# This is where we would install python-ceph and ceph-common but it appears
|
|
|
|
# the ceph task does that for us.
|
|
|
|
generate_ceph_keys(ceph_node)
|
2014-02-06 18:29:15 +00:00
|
|
|
distribute_ceph_keys(devstack_node, ceph_node)
|
2014-02-06 19:20:35 +00:00
|
|
|
secret_uuid = set_libvirt_secret(devstack_node, ceph_node)
|
|
|
|
update_devstack_config_files(devstack_node, secret_uuid)
|
2014-02-10 20:12:06 +00:00
|
|
|
set_apache_servername(devstack_node)
|
|
|
|
# Rebooting is the most-often-used method of restarting devstack services
|
2014-02-12 17:01:37 +00:00
|
|
|
misc.reboot(devstack_node)
|
2014-02-10 20:12:06 +00:00
|
|
|
start_devstack(devstack_node)
|
2014-02-11 03:04:53 +00:00
|
|
|
restart_apache(devstack_node)
|
2014-02-06 18:29:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_pools(ceph_node, pool_size):
|
2014-02-10 20:16:53 +00:00
|
|
|
log.info("Creating pools on Ceph cluster...")
|
|
|
|
|
2014-02-06 02:48:05 +00:00
|
|
|
for pool_name in ['volumes', 'images', 'backups']:
|
2015-09-11 16:11:31 +00:00
|
|
|
args = ['sudo', 'ceph', 'osd', 'pool', 'create', pool_name, pool_size]
|
2014-02-06 02:48:05 +00:00
|
|
|
ceph_node.run(args=args)
|
|
|
|
|
2014-02-06 18:29:15 +00:00
|
|
|
|
|
|
|
def distribute_ceph_conf(devstack_node, ceph_node):
|
2014-02-10 20:16:53 +00:00
|
|
|
log.info("Copying ceph.conf to DevStack node...")
|
|
|
|
|
2014-02-06 22:42:35 +00:00
|
|
|
ceph_conf_path = '/etc/ceph/ceph.conf'
|
2020-08-13 16:11:44 +00:00
|
|
|
ceph_conf = ceph_node.read_file(ceph_conf_path, sudo=True)
|
2020-07-20 09:24:12 +00:00
|
|
|
devstack_node.write_file(ceph_conf_path, ceph_conf, sudo=True)
|
2014-02-06 22:39:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def generate_ceph_keys(ceph_node):
|
2014-02-10 20:16:53 +00:00
|
|
|
log.info("Generating Ceph keys...")
|
|
|
|
|
2014-02-06 02:48:05 +00:00
|
|
|
ceph_auth_cmds = [
|
2015-09-11 16:11:31 +00:00
|
|
|
['sudo', 'ceph', 'auth', 'get-or-create', 'client.cinder', 'mon',
|
2014-02-06 02:48:05 +00:00
|
|
|
'allow r', 'osd', 'allow class-read object_prefix rbd_children, allow rwx pool=volumes, allow rx pool=images'], # noqa
|
2015-09-11 16:11:31 +00:00
|
|
|
['sudo', 'ceph', 'auth', 'get-or-create', 'client.glance', 'mon',
|
2014-02-06 02:48:05 +00:00
|
|
|
'allow r', 'osd', 'allow class-read object_prefix rbd_children, allow rwx pool=images'], # noqa
|
2015-09-11 16:11:31 +00:00
|
|
|
['sudo', 'ceph', 'auth', 'get-or-create', 'client.cinder-backup', 'mon',
|
2014-02-06 02:48:05 +00:00
|
|
|
'allow r', 'osd', 'allow class-read object_prefix rbd_children, allow rwx pool=backups'], # noqa
|
|
|
|
]
|
|
|
|
for cmd in ceph_auth_cmds:
|
|
|
|
ceph_node.run(args=cmd)
|
|
|
|
|
2014-02-06 18:29:15 +00:00
|
|
|
|
|
|
|
def distribute_ceph_keys(devstack_node, ceph_node):
|
2014-02-10 20:16:53 +00:00
|
|
|
log.info("Copying Ceph keys to DevStack node...")
|
|
|
|
|
2014-02-06 02:48:05 +00:00
|
|
|
def copy_key(from_remote, key_name, to_remote, dest_path, owner):
|
2019-12-16 02:47:38 +00:00
|
|
|
key_stringio = BytesIO()
|
2014-02-06 02:48:05 +00:00
|
|
|
from_remote.run(
|
2015-09-11 16:11:31 +00:00
|
|
|
args=['sudo', 'ceph', 'auth', 'get-or-create', key_name],
|
2014-02-06 02:48:05 +00:00
|
|
|
stdout=key_stringio)
|
2014-02-07 17:05:13 +00:00
|
|
|
key_stringio.seek(0)
|
2020-07-20 09:24:12 +00:00
|
|
|
to_remote.write_file(dest_path, key_stringio, owner=owner, sudo=True)
|
2014-02-06 02:48:05 +00:00
|
|
|
keys = [
|
|
|
|
dict(name='client.glance',
|
|
|
|
path='/etc/ceph/ceph.client.glance.keyring',
|
2014-02-07 00:17:17 +00:00
|
|
|
# devstack appears to just want root:root
|
|
|
|
#owner='glance:glance',
|
|
|
|
),
|
2014-02-06 02:48:05 +00:00
|
|
|
dict(name='client.cinder',
|
|
|
|
path='/etc/ceph/ceph.client.cinder.keyring',
|
2014-02-07 00:17:17 +00:00
|
|
|
# devstack appears to just want root:root
|
|
|
|
#owner='cinder:cinder',
|
|
|
|
),
|
2014-02-06 02:48:05 +00:00
|
|
|
dict(name='client.cinder-backup',
|
|
|
|
path='/etc/ceph/ceph.client.cinder-backup.keyring',
|
2014-02-07 00:17:17 +00:00
|
|
|
# devstack appears to just want root:root
|
|
|
|
#owner='cinder:cinder',
|
|
|
|
),
|
2014-02-06 02:48:05 +00:00
|
|
|
]
|
|
|
|
for key_dict in keys:
|
|
|
|
copy_key(ceph_node, key_dict['name'], devstack_node,
|
2014-02-07 00:17:17 +00:00
|
|
|
key_dict['path'], key_dict.get('owner'))
|
2014-02-06 18:29:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def set_libvirt_secret(devstack_node, ceph_node):
|
2014-02-10 20:16:53 +00:00
|
|
|
log.info("Setting libvirt secret...")
|
|
|
|
|
2019-12-16 02:47:38 +00:00
|
|
|
cinder_key = ceph_node.sh('sudo ceph auth get-key client.cinder').strip()
|
|
|
|
uuid = devstack_node.sh('uuidgen').strip()
|
2014-02-06 18:29:15 +00:00
|
|
|
|
|
|
|
secret_path = '/tmp/secret.xml'
|
|
|
|
secret_template = textwrap.dedent("""
|
|
|
|
<secret ephemeral='no' private='no'>
|
|
|
|
<uuid>{uuid}</uuid>
|
|
|
|
<usage type='ceph'>
|
|
|
|
<name>client.cinder secret</name>
|
|
|
|
</usage>
|
|
|
|
</secret>""")
|
2020-07-20 09:24:12 +00:00
|
|
|
secret_data = secret_template.format(uuid=uuid)
|
|
|
|
devstack_node.write_file(secret_path, secret_data)
|
2014-02-06 18:29:15 +00:00
|
|
|
devstack_node.run(args=['sudo', 'virsh', 'secret-define', '--file',
|
|
|
|
secret_path])
|
2014-02-07 16:34:47 +00:00
|
|
|
devstack_node.run(args=['sudo', 'virsh', 'secret-set-value', '--secret',
|
2014-02-06 18:29:15 +00:00
|
|
|
uuid, '--base64', cinder_key])
|
2014-02-06 19:20:35 +00:00
|
|
|
return uuid
|
|
|
|
|
|
|
|
|
|
|
|
def update_devstack_config_files(devstack_node, secret_uuid):
|
2014-02-10 20:16:53 +00:00
|
|
|
log.info("Updating DevStack config files to use Ceph...")
|
|
|
|
|
2014-02-06 19:20:35 +00:00
|
|
|
def backup_config(node, file_name, backup_ext='.orig.teuth'):
|
|
|
|
node.run(args=['cp', '-f', file_name, file_name + backup_ext])
|
|
|
|
|
2014-02-07 18:49:45 +00:00
|
|
|
def update_config(config_name, config_stream, update_dict,
|
|
|
|
section='DEFAULT'):
|
2014-02-06 19:20:35 +00:00
|
|
|
parser = ConfigParser()
|
2014-02-07 17:37:15 +00:00
|
|
|
parser.read_file(config_stream)
|
2014-02-07 18:49:45 +00:00
|
|
|
for (key, value) in update_dict.items():
|
|
|
|
parser.set(section, key, value)
|
2020-07-19 09:35:16 +00:00
|
|
|
out_stream = StringIO()
|
2014-02-06 19:20:35 +00:00
|
|
|
parser.write(out_stream)
|
2014-02-07 18:49:45 +00:00
|
|
|
out_stream.seek(0)
|
2014-02-06 19:20:35 +00:00
|
|
|
return out_stream
|
|
|
|
|
|
|
|
updates = [
|
|
|
|
dict(name='/etc/glance/glance-api.conf', options=dict(
|
|
|
|
default_store='rbd',
|
|
|
|
rbd_store_user='glance',
|
|
|
|
rbd_store_pool='images',
|
|
|
|
show_image_direct_url='True',)),
|
|
|
|
dict(name='/etc/cinder/cinder.conf', options=dict(
|
|
|
|
volume_driver='cinder.volume.drivers.rbd.RBDDriver',
|
|
|
|
rbd_pool='volumes',
|
|
|
|
rbd_ceph_conf='/etc/ceph/ceph.conf',
|
|
|
|
rbd_flatten_volume_from_snapshot='false',
|
|
|
|
rbd_max_clone_depth='5',
|
|
|
|
glance_api_version='2',
|
|
|
|
rbd_user='cinder',
|
|
|
|
rbd_secret_uuid=secret_uuid,
|
|
|
|
backup_driver='cinder.backup.drivers.ceph',
|
|
|
|
backup_ceph_conf='/etc/ceph/ceph.conf',
|
|
|
|
backup_ceph_user='cinder-backup',
|
|
|
|
backup_ceph_chunk_size='134217728',
|
|
|
|
backup_ceph_pool='backups',
|
|
|
|
backup_ceph_stripe_unit='0',
|
|
|
|
backup_ceph_stripe_count='0',
|
|
|
|
restore_discard_excess_bytes='true',
|
|
|
|
)),
|
|
|
|
dict(name='/etc/nova/nova.conf', options=dict(
|
|
|
|
libvirt_images_type='rbd',
|
|
|
|
libvirt_images_rbd_pool='volumes',
|
|
|
|
libvirt_images_rbd_ceph_conf='/etc/ceph/ceph.conf',
|
|
|
|
rbd_user='cinder',
|
|
|
|
rbd_secret_uuid=secret_uuid,
|
|
|
|
libvirt_inject_password='false',
|
|
|
|
libvirt_inject_key='false',
|
|
|
|
libvirt_inject_partition='-2',
|
|
|
|
)),
|
|
|
|
]
|
2014-02-06 18:29:15 +00:00
|
|
|
|
2014-02-06 19:20:35 +00:00
|
|
|
for update in updates:
|
|
|
|
file_name = update['name']
|
|
|
|
options = update['options']
|
2020-08-13 16:11:44 +00:00
|
|
|
config_data = devstack_node.read_file(file_name, sudo=True)
|
2020-07-19 09:35:16 +00:00
|
|
|
config_stream = StringIO(config_data)
|
2014-02-06 19:20:35 +00:00
|
|
|
backup_config(devstack_node, file_name)
|
|
|
|
new_config_stream = update_config(file_name, config_stream, options)
|
2020-07-20 09:24:12 +00:00
|
|
|
devstack_node.write_file(file_name, new_config_stream, sudo=True)
|
2014-02-10 20:12:06 +00:00
|
|
|
|
|
|
|
|
2014-02-11 03:04:53 +00:00
|
|
|
def set_apache_servername(node):
|
2014-02-10 20:12:06 +00:00
|
|
|
# Apache complains: "Could not reliably determine the server's fully
|
|
|
|
# qualified domain name, using 127.0.0.1 for ServerName"
|
|
|
|
# So, let's make sure it knows its name.
|
2014-02-10 20:16:53 +00:00
|
|
|
log.info("Setting Apache ServerName...")
|
|
|
|
|
2014-02-11 03:04:53 +00:00
|
|
|
hostname = node.hostname
|
2014-02-10 20:12:06 +00:00
|
|
|
config_file = '/etc/apache2/conf.d/servername'
|
2020-07-20 09:24:12 +00:00
|
|
|
config_data = "ServerName {name}".format(name=hostname)
|
|
|
|
node.write_file(config_file, config_data, sudo=True)
|
2014-02-10 20:12:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def start_devstack(devstack_node):
|
2014-02-11 01:22:57 +00:00
|
|
|
log.info("Patching devstack start script...")
|
|
|
|
# This causes screen to start headless - otherwise rejoin-stack.sh fails
|
|
|
|
# because there is no terminal attached.
|
|
|
|
cmd = "cd devstack && sed -ie 's/screen -c/screen -dm -c/' rejoin-stack.sh"
|
|
|
|
devstack_node.run(args=cmd)
|
2014-02-10 20:16:53 +00:00
|
|
|
|
2014-02-11 01:22:57 +00:00
|
|
|
log.info("Starting devstack...")
|
|
|
|
cmd = "cd devstack && ./rejoin-stack.sh"
|
|
|
|
devstack_node.run(args=cmd)
|
2014-02-11 03:04:53 +00:00
|
|
|
|
2014-02-19 21:54:15 +00:00
|
|
|
# This was added because I was getting timeouts on Cinder requests - which
|
|
|
|
# were trying to access Keystone on port 5000. A more robust way to handle
|
|
|
|
# this would be to introduce a wait-loop on devstack_node that checks to
|
|
|
|
# see if a service is listening on port 5000.
|
|
|
|
log.info("Waiting 30s for devstack to start...")
|
|
|
|
time.sleep(30)
|
|
|
|
|
2014-02-11 03:04:53 +00:00
|
|
|
|
|
|
|
def restart_apache(node):
|
|
|
|
node.run(args=['sudo', '/etc/init.d/apache2', 'restart'], wait=True)
|
2014-02-13 16:54:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def exercise(ctx, config):
|
|
|
|
log.info("Running devstack exercises...")
|
|
|
|
|
|
|
|
if config is None:
|
|
|
|
config = {}
|
|
|
|
if not isinstance(config, dict):
|
|
|
|
raise TypeError("config must be a dict")
|
|
|
|
|
qa/tasks: use next(iter(..)) for accessing first element in a view
in python2, dict.values() and dict.keys() return lists. but in python3,
they return views, which cannot be indexed directly using an integer index.
there are three use cases when we access these views in python3:
1. get the first element
2. get all the elements and then *might* want to access them by index
3. get the first element assuming there is only a single element in
the view
4. iterate thru the view
in the 1st case, we cannot assume the number of elements, so to be
python3 compatible, we should use `next(iter(a_dict))` instead.
in the 2nd case, in this change, the view is materialized using
`list(a_dict)`.
in the 3rd case, we can just continue using the short hand of
```py
(first_element,) = a_dict.keys()
```
to unpack the view. this works in both python2 and python3.
in the 4th case, the existing code works in both python2 and python3, as
both list and view can be iterated using `iter`, and `len` works as
well.
Signed-off-by: Kefu Chai <kchai@redhat.com>
2020-03-31 02:16:40 +00:00
|
|
|
devstack_node = next(iter(ctx.cluster.only(is_devstack_node).remotes.keys()))
|
2014-02-13 16:54:19 +00:00
|
|
|
|
2014-02-14 17:36:20 +00:00
|
|
|
# TODO: save the log *and* preserve failures
|
|
|
|
#devstack_archive_dir = create_devstack_archive(ctx, devstack_node)
|
2014-02-13 16:54:19 +00:00
|
|
|
|
|
|
|
try:
|
2014-02-14 17:36:20 +00:00
|
|
|
#cmd = "cd devstack && ./exercise.sh 2>&1 | tee {dir}/exercise.log".format( # noqa
|
|
|
|
# dir=devstack_archive_dir)
|
|
|
|
cmd = "cd devstack && ./exercise.sh"
|
2014-02-13 16:54:19 +00:00
|
|
|
devstack_node.run(args=cmd, wait=True)
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def create_devstack_archive(ctx, devstack_node):
|
|
|
|
test_dir = misc.get_testdir(ctx)
|
|
|
|
devstack_archive_dir = "{test_dir}/archive/devstack".format(
|
|
|
|
test_dir=test_dir)
|
|
|
|
devstack_node.run(args="mkdir -p " + devstack_archive_dir)
|
|
|
|
return devstack_archive_dir
|
2014-02-14 20:54:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def smoke(ctx, config):
|
|
|
|
log.info("Running a basic smoketest...")
|
|
|
|
|
qa/tasks: use next(iter(..)) for accessing first element in a view
in python2, dict.values() and dict.keys() return lists. but in python3,
they return views, which cannot be indexed directly using an integer index.
there are three use cases when we access these views in python3:
1. get the first element
2. get all the elements and then *might* want to access them by index
3. get the first element assuming there is only a single element in
the view
4. iterate thru the view
in the 1st case, we cannot assume the number of elements, so to be
python3 compatible, we should use `next(iter(a_dict))` instead.
in the 2nd case, in this change, the view is materialized using
`list(a_dict)`.
in the 3rd case, we can just continue using the short hand of
```py
(first_element,) = a_dict.keys()
```
to unpack the view. this works in both python2 and python3.
in the 4th case, the existing code works in both python2 and python3, as
both list and view can be iterated using `iter`, and `len` works as
well.
Signed-off-by: Kefu Chai <kchai@redhat.com>
2020-03-31 02:16:40 +00:00
|
|
|
devstack_node = next(iter(ctx.cluster.only(is_devstack_node).remotes.keys()))
|
|
|
|
an_osd_node = next(iter(ctx.cluster.only(is_osd_node).remotes.keys()))
|
2014-02-14 20:54:10 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
create_volume(devstack_node, an_osd_node, 'smoke0', 1)
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def create_volume(devstack_node, ceph_node, vol_name, size):
|
|
|
|
"""
|
|
|
|
:param size: The size of the volume, in GB
|
|
|
|
"""
|
|
|
|
size = str(size)
|
|
|
|
log.info("Creating a {size}GB volume named {name}...".format(
|
|
|
|
name=vol_name,
|
|
|
|
size=size))
|
2014-02-18 15:53:57 +00:00
|
|
|
args = ['source', 'devstack/openrc', run.Raw('&&'), 'cinder', 'create',
|
|
|
|
'--display-name', vol_name, size]
|
2020-03-05 23:27:59 +00:00
|
|
|
cinder_create = devstack_node.sh(args, wait=True)
|
|
|
|
vol_info = parse_os_table(cinder_create)
|
2014-02-20 17:14:55 +00:00
|
|
|
log.debug("Volume info: %s", str(vol_info))
|
2014-02-14 20:54:10 +00:00
|
|
|
|
2014-02-20 17:14:55 +00:00
|
|
|
try:
|
2020-03-05 23:27:59 +00:00
|
|
|
rbd_output = ceph_node.sh("rbd --id cinder ls -l volumes", wait=True)
|
2014-02-20 17:14:55 +00:00
|
|
|
except run.CommandFailedError:
|
|
|
|
log.debug("Original rbd call failed; retrying without '--id cinder'")
|
2020-03-05 23:27:59 +00:00
|
|
|
rbd_output = ceph_node.sh("rbd ls -l volumes", wait=True)
|
2014-02-20 17:14:55 +00:00
|
|
|
|
2020-03-05 23:27:59 +00:00
|
|
|
assert vol_info['id'] in rbd_output, \
|
2014-02-21 15:57:29 +00:00
|
|
|
"Volume not found on Ceph cluster"
|
|
|
|
assert vol_info['size'] == size, \
|
|
|
|
"Volume size on Ceph cluster is different than specified"
|
2014-02-14 20:54:10 +00:00
|
|
|
return vol_info['id']
|
|
|
|
|
|
|
|
|
|
|
|
def parse_os_table(table_str):
|
|
|
|
out_dict = dict()
|
|
|
|
for line in table_str.split('\n'):
|
|
|
|
if line.startswith('|'):
|
|
|
|
items = line.split()
|
|
|
|
out_dict[items[1]] = items[3]
|
|
|
|
return out_dict
|