From 356d6e2d89817553ea38dac6a5358e06468bfbde Mon Sep 17 00:00:00 2001 From: pporecha Date: Tue, 8 Jul 2014 14:15:04 +0200 Subject: [PATCH] OpenStack plugin - Add support for reading credentials from openrc.sh --- imagefactory_plugins/OpenStack/OpenStack.py | 22 ++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/imagefactory_plugins/OpenStack/OpenStack.py b/imagefactory_plugins/OpenStack/OpenStack.py index c370426c..bf0ff139 100644 --- a/imagefactory_plugins/OpenStack/OpenStack.py +++ b/imagefactory_plugins/OpenStack/OpenStack.py @@ -20,6 +20,7 @@ import json import os import struct +import getpass from xml.etree.ElementTree import fromstring from imgfac.Template import Template from imgfac.ApplicationConfiguration import ApplicationConfiguration @@ -51,6 +52,9 @@ def __init__(self): self.version = GLANCE_VERSION if self.version == 2: self.credentials_attrs = [ 'auth_url', 'password', 'tenant', 'username'] + self.openrc_credentials_attrs = { 'OS_AUTH_URL': 'auth_url', + 'OS_TENANT_NAME': 'tenant', + 'OS_USERNAME': 'username'} else: self.credentials_attrs = [ 'auth_url', 'password', 'strategy', 'tenant', 'username'] @@ -113,7 +117,11 @@ def openstack_decode_credentials(self, credentials): self.activity("Preparing OpenStack credentials") # TODO: Validate these - in particular, ensure that if some nodes are missing at least # a minimal acceptable set of auth is present - doc = libxml2.parseDoc(credentials) + try: + doc = libxml2.parseDoc(credentials) + except libxml2.parserError: + self._populate_credentials_dict_from_openrc(credentials) + return self.credentials_dict = { } for authprop in self.credentials_attrs: @@ -130,6 +138,18 @@ def _get_xml_node(self, doc, credtype): return nodes[0].content + def _populate_credentials_dict_from_openrc(self, credentials_str): + credentials_list = credentials_str.split('\n') + self.credentials_dict = {} + for authprop in self.openrc_credentials_attrs.keys(): + corresponding_authprop = self.openrc_credentials_attrs[authprop] + matching = [line for line in credentials_list if authprop in line] + if matching == []: + raise ImageFactoryException("Credentials file does not have required field - " + authprop) + self.credentials_dict[corresponding_authprop] = matching[0][matching[0].index('=') + 1 : ].strip('"') + self.credentials_dict['password'] = getpass.getpass(prompt='Please enter your OpenStack Password:\n') + self.credentials_token = None + def snapshot_image_on_provider(self, builder, provider, credentials, template, parameters): # TODO: Implement snapshot builds raise ImageFactoryException("Snapshot builds not currently supported on OpenStack KVM")