From b9b3c0ded6bc87420c8891ed28fb175f66d273f9 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Mon, 3 Nov 2014 10:34:59 -0600 Subject: [PATCH] Support boot from volume discovery --- plugins/inventory/rax.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/plugins/inventory/rax.py b/plugins/inventory/rax.py index 778f903216..ef45148c5b 100644 --- a/plugins/inventory/rax.py +++ b/plugins/inventory/rax.py @@ -160,6 +160,7 @@ except ImportError: try: import pyrax + from pyrax.utils import slugify except ImportError: print('pyrax is required for this module') sys.exit(1) @@ -215,6 +216,8 @@ def _list(regions): groups = collections.defaultdict(list) hostvars = collections.defaultdict(dict) images = {} + cbs_attachments = collections.defaultdict(dict) + prefix = get_config(p, 'rax', 'meta_prefix', 'RAX_META_PREFIX', 'meta') network = get_config(p, 'rax', 'access_network', 'RAX_ACCESS_NETWORK', @@ -258,11 +261,33 @@ def _list(regions): hostvars[server.name]['rax_region'] = region for key, value in server.metadata.iteritems(): - prefix = os.getenv('RAX_META_PREFIX', 'meta') groups['%s_%s_%s' % (prefix, key, value)].append(server.name) groups['instance-%s' % server.id].append(server.name) groups['flavor-%s' % server.flavor['id']].append(server.name) + + # Handle boot from volume + if not server.image: + if not cbs_attachments[region]: + cbs = pyrax.connect_to_cloud_blockstorage(region) + for vol in cbs.list(): + if mk_boolean(vol.bootable): + for attachment in vol.attachments: + metadata = vol.volume_image_metadata + server_id = attachment['server_id'] + cbs_attachments[region][server_id] = { + 'id': metadata['image_id'], + 'name': slugify(metadata['image_name']) + } + image = cbs_attachments[region].get(server.id) + if image: + server.image = {'id': image['id']} + hostvars[server.name]['rax_image'] = server.image + hostvars[server.name]['rax_boot_source'] = 'volume' + images[image['id']] = image['name'] + else: + hostvars[server.name]['rax_boot_source'] = 'local' + try: imagegroup = 'image-%s' % images[server.image['id']] groups[imagegroup].append(server.name)