From c08a78477f80628ce6bba58818e724a12707baec Mon Sep 17 00:00:00 2001 From: Ionut Balutoiu Date: Sat, 31 Oct 2015 00:11:38 +0200 Subject: [PATCH] Added swift-temp-url-key parameter needed when creating temporary urls from glance --- hooks/glance_relations.py | 10 ++++++++++ hooks/glance_utils.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/hooks/glance_relations.py b/hooks/glance_relations.py index 803e3479..7a388b1c 100755 --- a/hooks/glance_relations.py +++ b/hooks/glance_relations.py @@ -26,6 +26,7 @@ from glance_utils import ( setup_ipv6, REQUIRED_INTERFACES, check_optional_relations, + swift_temp_url_key ) from charmhelpers.core.hookenv import ( config, @@ -222,6 +223,13 @@ def image_service_joined(relation_id=None): juju_log("%s: image-service_joined: To peer glance-api-server=%s" % (CHARM, relation_data['glance-api-server'])) + if ('object-store' in CONFIGS.complete_contexts() and + 'identity-service' in CONFIGS.complete_contexts()): + relation_data.update({ + 'swift-temp-url-key': swift_temp_url_key(), + 'swift-container': 'glance' + }) + relation_set(relation_id=relation_id, **relation_data) @@ -238,6 +246,8 @@ def object_store_joined(): juju_log('swift relation incomplete') return + [image_service_joined(rid) for rid in relation_ids('image-service')] + CONFIGS.write(GLANCE_API_CONF) diff --git a/hooks/glance_utils.py b/hooks/glance_utils.py index d5dff61b..1119f521 100644 --- a/hooks/glance_utils.py +++ b/hooks/glance_utils.py @@ -39,6 +39,7 @@ from charmhelpers.core.host import ( service_restart, lsb_release, write_file, + pwgen ) from charmhelpers.contrib.openstack import ( @@ -460,3 +461,33 @@ def check_optional_relations(configs): return status_get() else: return 'unknown', 'No optional relations' + + +def swift_temp_url_key(): + """Generate a temp URL key, post it to Swift and return its value. + If it is already posted, the current value of the key will be returned. + """ + keystone_ctxt = context.IdentityServiceContext(service='glance', + service_user='glance')() + if not keystone_ctxt: + log('Missing identity-service relation. Skipping generation of ' + 'swift temporary url key.') + return + + auth_url = '%s://%s:%s/v2.0/' % (keystone_ctxt['service_protocol'], + keystone_ctxt['service_host'], + keystone_ctxt['service_port']) + from swiftclient import client + swift_connection = client.Connection( + authurl=auth_url, user='glance', key=keystone_ctxt['admin_password'], + tenant_name=keystone_ctxt['admin_tenant_name'], auth_version='2.0') + + account_stats = swift_connection.head_account() + if 'x-account-meta-temp-url-key' in account_stats: + log("Temp URL key was already posted.") + return account_stats['x-account-meta-temp-url-key'] + + temp_url_key = pwgen(length=64) + swift_connection.post_account(headers={'x-account-meta-temp-url-key': + temp_url_key}) + return temp_url_key