From d82ab896f26c6389b1aaa0fac374713ba046cb95 Mon Sep 17 00:00:00 2001 From: Bilal Baqar Date: Sun, 9 Aug 2015 08:32:44 -0700 Subject: [PATCH] Support added for metadata --- hooks/neutron_plumgrid_context.py | 25 ++++- hooks/neutron_plumgrid_hooks.py | 24 +---- hooks/plumgrid-plugin-relation-changed | 94 +++++++++++++++++++ hooks/start | 1 - templates/kilo/plumlib.ini | 6 +- .../test_neutron_plumgrid_plugin_context.py | 4 + .../test_neutron_plumgrid_plugin_hooks.py | 6 +- 7 files changed, 130 insertions(+), 30 deletions(-) create mode 100755 hooks/plumgrid-plugin-relation-changed delete mode 120000 hooks/start diff --git a/hooks/neutron_plumgrid_context.py b/hooks/neutron_plumgrid_context.py index ccd3959..3fdfc44 100644 --- a/hooks/neutron_plumgrid_context.py +++ b/hooks/neutron_plumgrid_context.py @@ -11,6 +11,22 @@ from charmhelpers.core.hookenv import ( from charmhelpers.contrib.openstack import context +def _edge_settings(): + ''' + Inspects plumgrid-edge relation to get metadata shared secret. + ''' + ctxt = { + 'metadata_shared_secret': 'plumgrid', + } + for rid in relation_ids('plumgrid-plugin'): + for unit in related_units(rid): + rdata = relation_get(rid=rid, unit=unit) + if 'metadata-shared-secret' in rdata: + ctxt['metadata_shared_secret'] = \ + rdata['metadata-shared-secret'] + return ctxt + + def _container_settings(): ''' Inspects current container relation to get keystone context. @@ -87,11 +103,16 @@ class NeutronPGPluginContext(context.NeutronContext): return {} conf = config() - pg_ctxt['enable_metadata'] = conf['enable-metadata'] + enable_metadata = conf['enable-metadata'] + pg_ctxt['enable_metadata'] = enable_metadata pg_ctxt['pg_metadata_ip'] = '169.254.169.254' pg_ctxt['pg_metadata_port'] = '8775' - pg_ctxt['nova_metadata_proxy_secret'] = 'plumgrid' pg_ctxt['metadata_mode'] = 'tunnel' + if enable_metadata: + plumgrid_edge_settings = _edge_settings() + pg_ctxt['nova_metadata_proxy_secret'] = plumgrid_edge_settings['metadata_shared_secret'] + else: + pg_ctxt['nova_metadata_proxy_secret'] = 'plumgrid' neutron_api_settings = _container_settings() pg_ctxt['admin_user'] = neutron_api_settings['service_username'] diff --git a/hooks/neutron_plumgrid_hooks.py b/hooks/neutron_plumgrid_hooks.py index c367418..57a7c2f 100755 --- a/hooks/neutron_plumgrid_hooks.py +++ b/hooks/neutron_plumgrid_hooks.py @@ -61,21 +61,13 @@ def config_changed(): @hooks.hook('neutron-plugin-api-relation-joined') -def neutron_plugin_api_joined(): - ''' - This hook is run when relation between neutron-api and - neutron-api-plumgrid is made. - ''' - ensure_files() - CONFIGS.write_all() - - +@hooks.hook('plumgrid-plugin-relation-changed') @hooks.hook('container-relation-changed') @restart_on_change(restart_map()) -def container_changed(): +def relation_changed(): ''' - This hook is run when relation between neutron-api and - neutron-api-plumgrid is changed. + This hook is run when relation between neutron-api-plumgrid and + neutron-api or plumgrid-edge is made. ''' ensure_files() CONFIGS.write_all() @@ -91,14 +83,6 @@ def stop(): apt_purge(pkg, fatal=False) -@hooks.hook('start') -def start(): - ''' - This hook is run after all relations are joined. - ''' - ensure_files() - - def main(): try: hooks.execute(sys.argv) diff --git a/hooks/plumgrid-plugin-relation-changed b/hooks/plumgrid-plugin-relation-changed new file mode 100755 index 0000000..57a7c2f --- /dev/null +++ b/hooks/plumgrid-plugin-relation-changed @@ -0,0 +1,94 @@ +#!/usr/bin/python + +# Copyright (c) 2015, PLUMgrid Inc, http://plumgrid.com + +# The hooks of this charm have been symlinked to functions +# in this file. + +import sys + +from charmhelpers.core.hookenv import ( + Hooks, + UnregisteredHookError, + log, +) + +from charmhelpers.core.host import ( + restart_on_change, +) + +from charmhelpers.fetch import ( + apt_install, + apt_update, + configure_sources, + apt_purge, +) + +from neutron_plumgrid_utils import ( + determine_packages, + register_configs, + restart_map, + ensure_files, +) + +hooks = Hooks() +CONFIGS = register_configs() + + +@hooks.hook() +def install(): + ''' + Install hook is run when the charm is first deployed on a node. + ''' + configure_sources() + apt_update() + apt_install(determine_packages(), options=['--force-yes'], fatal=True) + ensure_files() + + +@hooks.hook('config-changed') +def config_changed(): + ''' + This hook is run when a config parameter is changed. + It also runs on node reboot. + ''' + stop() + configure_sources() + apt_update() + apt_install(determine_packages(), options=['--force-yes'], fatal=True) + ensure_files() + CONFIGS.write_all() + + +@hooks.hook('neutron-plugin-api-relation-joined') +@hooks.hook('plumgrid-plugin-relation-changed') +@hooks.hook('container-relation-changed') +@restart_on_change(restart_map()) +def relation_changed(): + ''' + This hook is run when relation between neutron-api-plumgrid and + neutron-api or plumgrid-edge is made. + ''' + ensure_files() + CONFIGS.write_all() + + +@hooks.hook('stop') +def stop(): + ''' + This hook is run when the charm is destroyed. + ''' + pkgs = determine_packages() + for pkg in pkgs: + apt_purge(pkg, fatal=False) + + +def main(): + try: + hooks.execute(sys.argv) + except UnregisteredHookError as e: + log('Unknown hook {} - skipping.'.format(e)) + + +if __name__ == '__main__': + main() diff --git a/hooks/start b/hooks/start deleted file mode 120000 index 4dc4326..0000000 --- a/hooks/start +++ /dev/null @@ -1 +0,0 @@ -neutron_plumgrid_hooks.py \ No newline at end of file diff --git a/templates/kilo/plumlib.ini b/templates/kilo/plumlib.ini index beba41f..4ceecf8 100644 --- a/templates/kilo/plumlib.ini +++ b/templates/kilo/plumlib.ini @@ -18,9 +18,9 @@ vapp_flag = False # namespace for metadata won't be created on this node [PLUMgridMetadata] enable_pg_metadata = {{ enable_metadata }} -metadata_ip = {{ pg_metadata_ip }} -metadata_port = {{ pg_metadata_port }} -metadata_key = {{ nova_metadata_proxy_secret }} +nova_metadata_ip = {{ pg_metadata_ip }} +nova_metadata_port = {{ pg_metadata_port }} +metadata_proxy_shared_secret = {{ nova_metadata_proxy_secret }} metadata_ns = True diff --git a/unit_tests/test_neutron_plumgrid_plugin_context.py b/unit_tests/test_neutron_plumgrid_plugin_context.py index 0fb6f21..8f20572 100644 --- a/unit_tests/test_neutron_plumgrid_plugin_context.py +++ b/unit_tests/test_neutron_plumgrid_plugin_context.py @@ -87,5 +87,9 @@ class NeutronPGContextTest(CharmTestCase): 'service_protocol': 'http', 'auth_port': '35357', 'auth_host': '10.0.0.1', + 'metadata_mode': 'tunnel', + 'nova_metadata_proxy_secret': 'plumgrid', + 'pg_metadata_ip': '169.254.169.254', + 'pg_metadata_port': '8775', } self.assertEquals(expect, napi_ctxt()) diff --git a/unit_tests/test_neutron_plumgrid_plugin_hooks.py b/unit_tests/test_neutron_plumgrid_plugin_hooks.py index 10cc99c..6c5036e 100644 --- a/unit_tests/test_neutron_plumgrid_plugin_hooks.py +++ b/unit_tests/test_neutron_plumgrid_plugin_hooks.py @@ -47,8 +47,7 @@ class NeutronPGHooksTests(CharmTestCase): self.apt_update.assert_called_with() self.apt_install.assert_has_calls([ call(_pkgs, fatal=True, - options=['--force-yes', - '--option=Dpkg::Options::=--force-confold']), + options=['--force-yes']), ]) self.ensure_files.assert_called_with() @@ -60,8 +59,7 @@ class NeutronPGHooksTests(CharmTestCase): self.apt_update.assert_called_with() self.apt_install.assert_has_calls([ call(_pkgs, fatal=True, - options=['--force-yes', - '--option=Dpkg::Options::=--force-confold']), + options=['--force-yes']), ]) self.ensure_files.assert_called_with() self.CONFIGS.write_all.assert_called_with()