From 2ba52039fafac9a0cb804c2ac235bab4e9d75a7e Mon Sep 17 00:00:00 2001 From: Bilal Baqar Date: Thu, 24 Mar 2016 05:33:25 -0700 Subject: [PATCH 1/6] First commit-mw --- config.yaml | 6 +++++- hooks/pg_dir_context.py | 1 + hooks/pg_dir_hooks.py | 20 ++++++++++++++++++++ hooks/pg_dir_utils.py | 5 +++++ templates/kilo/00-pg.conf | 1 + templates/kilo/nginx.conf | 2 +- 6 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 templates/kilo/00-pg.conf diff --git a/config.yaml b/config.yaml index c450253..7782791 100644 --- a/config.yaml +++ b/config.yaml @@ -25,7 +25,7 @@ options: type: string description: | Interfaces that will provide fabric connectivity on the director nodes. - Provided in form of json in a string. These interfaces have to be connected + Provided in form of json in a string. These interfaces have to be connected to the os-data-network specified in the config. Default value is MANAGEMENT which will configure the management interface as the fabric interface on each director. @@ -55,3 +55,7 @@ options: default: null type: string description: Provide the PLUMgrid ONS License key. + opsvm-ip: + default: 127.0.0.1 + type: string + description: IP address of the PLUMgrid Operations VM Management interface. diff --git a/hooks/pg_dir_context.py b/hooks/pg_dir_context.py index 3ca7b46..f509684 100644 --- a/hooks/pg_dir_context.py +++ b/hooks/pg_dir_context.py @@ -95,5 +95,6 @@ class PGDirContext(context.NeutronContext): pg_ctxt['fabric_mode'] = 'host' virtual_ip_array = re.split('\.', conf['plumgrid-virtual-ip']) pg_ctxt['virtual_router_id'] = virtual_ip_array[3] + pg_ctxt['opsvm_ip'] = conf['opsvm-ip'] return pg_ctxt diff --git a/hooks/pg_dir_hooks.py b/hooks/pg_dir_hooks.py index 51ec5ca..b59941c 100755 --- a/hooks/pg_dir_hooks.py +++ b/hooks/pg_dir_hooks.py @@ -7,6 +7,7 @@ import sys import time +from charmhelpers.contrib.network.ip import is_ip from charmhelpers.core.hookenv import ( Hooks, @@ -63,6 +64,23 @@ def dir_joined(): restart_pg() +@hooks.hook('plumgrid-relation-joined') +def plumgrid_joined(relation_id=None): + ''' + This hook is run when relation with edge or gateway is created. + ''' + opsvm_ip = onfig('opsvm-ip') + if opsvm_ip == '127.0.0.1': + return 1 + elif not is_ip(opsvm-ip): + raise ValueError('Incorrect IP specified') + else: + relation_set(relation_id=relation_id, opsvm_ip=opsvm_ip}) + #rel_data = { + # 'opsvm-ip': opsvm-ip, + #} + + @hooks.hook('config-changed') def config_changed(): ''' @@ -92,6 +110,8 @@ def config_changed(): apt_install(pkg, options=['--force-yes'], fatal=True) remove_iovisor() load_iovisor() + for rid in relation_ids('plumgrid'): + neutron_plugin_joined(rid) ensure_mtu() add_lcm_key() CONFIGS.write_all() diff --git a/hooks/pg_dir_utils.py b/hooks/pg_dir_utils.py index c580d85..304a08c 100644 --- a/hooks/pg_dir_utils.py +++ b/hooks/pg_dir_utils.py @@ -49,6 +49,7 @@ PG_DEF_CONF = '%s/conf/pg/nginx.conf' % PG_LXC_DATA_PATH PG_HN_CONF = '%s/conf/etc/hostname' % PG_LXC_DATA_PATH PG_HS_CONF = '%s/conf/etc/hosts' % PG_LXC_DATA_PATH PG_IFCS_CONF = '%s/conf/pg/ifcs.conf' % PG_LXC_DATA_PATH +OPS_CONF = '%s/conf/etc/00-pg.conf' % PG_LXC_DATA_PATH AUTH_KEY_PATH = '%s/root/.ssh/authorized_keys' % PG_LXC_DATA_PATH TEMP_LICENSE_FILE = '/tmp/license' @@ -73,6 +74,10 @@ BASE_RESOURCE_MAP = OrderedDict([ 'services': ['plumgrid'], 'contexts': [pg_dir_context.PGDirContext()], }), + (OPS_CONF, { + 'services': ['plumgrid'], + 'contexts': [pg_dir_context.PGDirContext()], + }), (PG_IFCS_CONF, { 'services': [], 'contexts': [pg_dir_context.PGDirContext()], diff --git a/templates/kilo/00-pg.conf b/templates/kilo/00-pg.conf new file mode 100644 index 0000000..c5a6016 --- /dev/null +++ b/templates/kilo/00-pg.conf @@ -0,0 +1 @@ +$template ls_json,"{{'{'}}{{'%'}}timestamp:::date-rfc3339,jsonf:@timestamp%,%source:::jsonf:@source_host%,%msg:::json%}":syslogtag,isequal,"pg:" @{{ opsvm_ip }}:6000;ls_json diff --git a/templates/kilo/nginx.conf b/templates/kilo/nginx.conf index 00f58f1..7f5c51b 100644 --- a/templates/kilo/nginx.conf +++ b/templates/kilo/nginx.conf @@ -13,7 +13,7 @@ upstream pgCli { } upstream pgMW { - server 127.0.0.1:4000; + server {{ opsvm_ip }}:4000; } map $http_upgrade $connection_upgrade { From fa4588b0e385909eb80df936031470e07d9f45fb Mon Sep 17 00:00:00 2001 From: Bilal Baqar Date: Sat, 26 Mar 2016 02:41:01 +0100 Subject: [PATCH 2/6] restart_on_change --- hooks/pg_dir_hooks.py | 7 ++++--- hooks/pg_dir_utils.py | 21 +++++++++++++++++++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/hooks/pg_dir_hooks.py b/hooks/pg_dir_hooks.py index 339e5b2..5e1f772 100755 --- a/hooks/pg_dir_hooks.py +++ b/hooks/pg_dir_hooks.py @@ -36,7 +36,8 @@ from pg_dir_utils import ( add_lcm_key, post_pg_license, fabric_interface_changed, - load_iptables + load_iptables, + restart_on_change ) hooks = Hooks() @@ -74,7 +75,7 @@ def plumgrid_joined(relation_id=None): ''' opsvm_ip = config('opsvm-ip') if opsvm_ip == '127.0.0.1': - return 1 + pass elif not is_ip(opsvm_ip): raise ValueError('Incorrect IP specified') else: @@ -138,13 +139,13 @@ def start(): @hooks.hook('upgrade-charm') +@restart_on_change(restart_map()) def upgrade_charm(): ''' This hook is run when the charm is upgraded ''' ensure_mtu() CONFIGS.write_all() - restart_pg() @hooks.hook('stop') diff --git a/hooks/pg_dir_utils.py b/hooks/pg_dir_utils.py index c5f92d9..be1dc50 100644 --- a/hooks/pg_dir_utils.py +++ b/hooks/pg_dir_utils.py @@ -12,7 +12,6 @@ from socket import gethostname as get_unit_hostname from copy import deepcopy from charmhelpers.contrib.openstack.neutron import neutron_plugin_attribute from charmhelpers.contrib.openstack import templating -from charmhelpers.core.host import set_nic_mtu from charmhelpers.contrib.storage.linux.ceph import modprobe from charmhelpers.core.hookenv import ( log, @@ -30,7 +29,9 @@ from charmhelpers.contrib.network.ip import ( from charmhelpers.core.host import ( service_start, service_stop, - service_running + service_running, + path_hash, + set_nic_mtu ) from charmhelpers.fetch import ( apt_cache, @@ -406,3 +407,19 @@ def get_cidr_from_iface(interface): return None else: return None + + +def restart_on_change(restart_map): + """ + Restart services based on configuration files changing + """ + def wrap(f): + def wrapped_f(*args, **kwargs): + checksums = {path: path_hash(path) for path in restart_map} + f(*args, **kwargs) + for path in restart_map: + if path_hash(path) != checksums[path]: + restart_pg() + break + return wrapped_f + return wrap From f7ff2f8dad4155bbee42c106d68ffe961a94b8a9 Mon Sep 17 00:00:00 2001 From: Bilal Baqar Date: Sat, 26 Mar 2016 19:43:53 +0100 Subject: [PATCH 3/6] Removed package removal in stop hook --- hooks/pg_dir_hooks.py | 15 +++++++-------- hooks/pg_dir_utils.py | 9 +++++++++ unit_tests/test_pg_dir_context.py | 4 +++- unit_tests/test_pg_dir_hooks.py | 4 ---- unit_tests/test_pg_dir_utils.py | 4 +++- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/hooks/pg_dir_hooks.py b/hooks/pg_dir_hooks.py index 5e1f772..e25770d 100755 --- a/hooks/pg_dir_hooks.py +++ b/hooks/pg_dir_hooks.py @@ -21,13 +21,13 @@ from charmhelpers.core.hookenv import ( from charmhelpers.fetch import ( apt_install, - apt_purge, configure_sources, ) from pg_dir_utils import ( register_configs, restart_pg, + restart_map, stop_pg, determine_packages, load_iovisor, @@ -37,7 +37,8 @@ from pg_dir_utils import ( post_pg_license, fabric_interface_changed, load_iptables, - restart_on_change + restart_on_change, + director_cluster_ready ) hooks = Hooks() @@ -60,12 +61,14 @@ def install(): @hooks.hook('director-relation-joined') +@restart_on_change(restart_map()) def dir_joined(): ''' This hook is run when a unit of director is added. ''' - CONFIGS.write_all() - restart_pg() + if director_cluster_ready(): + ensure_mtu() + CONFIGS.write_all() @hooks.hook('plumgrid-relation-joined') @@ -154,10 +157,6 @@ def stop(): This hook is run when the charm is destroyed. ''' stop_pg() - remove_iovisor() - pkgs = determine_packages() - for pkg in pkgs: - apt_purge(pkg, fatal=False) def main(): diff --git a/hooks/pg_dir_utils.py b/hooks/pg_dir_utils.py index be1dc50..6a4324d 100644 --- a/hooks/pg_dir_utils.py +++ b/hooks/pg_dir_utils.py @@ -409,6 +409,15 @@ def get_cidr_from_iface(interface): return None +def director_cluster_ready(): + dirs_count = len(pg_dir_context._pg_dir_ips()) + log('DIR COUNT: %s' % dirs_count) + if dirs_count == 2: + return True + else: + return False + + def restart_on_change(restart_map): """ Restart services based on configuration files changing diff --git a/unit_tests/test_pg_dir_context.py b/unit_tests/test_pg_dir_context.py index 9d32722..baac434 100644 --- a/unit_tests/test_pg_dir_context.py +++ b/unit_tests/test_pg_dir_context.py @@ -59,7 +59,8 @@ class PGDirContextTest(CharmTestCase): if section == "config": return "neutron.randomconfig" - config = {'plumgrid-virtual-ip': "192.168.100.250"} + config = {'plumgrid-virtual-ip': "192.168.100.250", + 'opsvm-ip':'127.0.0.1'} def mock_config(key=None): if key: @@ -99,5 +100,6 @@ class PGDirContextTest(CharmTestCase): '192.168.100.203'], 'director_ips_string': '192.168.100.201,192.168.100.202,192.168.100.203', + 'opsvm_ip': '127.0.0.1', } self.assertEquals(expect, napi_ctxt()) diff --git a/unit_tests/test_pg_dir_hooks.py b/unit_tests/test_pg_dir_hooks.py index c453ca8..1b8e5e8 100644 --- a/unit_tests/test_pg_dir_hooks.py +++ b/unit_tests/test_pg_dir_hooks.py @@ -20,7 +20,6 @@ utils.restart_map = _map TO_PATCH = [ 'remove_iovisor', 'apt_install', - 'apt_purge', 'CONFIGS', 'log', 'configure_sources', @@ -67,8 +66,5 @@ class PGDirHooksTests(CharmTestCase): self.test_config.set('plumgrid-license-key', None) def test_stop(self): - _pkgs = ['plumgrid-lxc', 'iovisor-dkms'] self._call_hook('stop') self.stop_pg.assert_called_with() - self.remove_iovisor.assert_called_with() - self.determine_packages.return_value = _pkgs diff --git a/unit_tests/test_pg_dir_utils.py b/unit_tests/test_pg_dir_utils.py index d4fe0b1..4e0741e 100644 --- a/unit_tests/test_pg_dir_utils.py +++ b/unit_tests/test_pg_dir_utils.py @@ -55,7 +55,8 @@ class TestPGDirUtils(CharmTestCase): nutils.PG_DEF_CONF, nutils.PG_HN_CONF, nutils.PG_HS_CONF, - nutils.PG_IFCS_CONF] + nutils.PG_IFCS_CONF, + nutils.OPS_CONF] self.assertItemsEqual(_regconfs.configs, confs) def test_resource_map(self): @@ -73,6 +74,7 @@ class TestPGDirUtils(CharmTestCase): (nutils.PG_DEF_CONF, ['plumgrid']), (nutils.PG_HN_CONF, ['plumgrid']), (nutils.PG_HS_CONF, ['plumgrid']), + (nutils.OPS_CONF, ['plumgrid']), (nutils.PG_IFCS_CONF, []), ]) self.assertEqual(expect, _restart_map) From 382c0c71fa6415ce9aa21a02c8c275e592c44262 Mon Sep 17 00:00:00 2001 From: Bilal Baqar Date: Sat, 26 Mar 2016 20:07:37 +0100 Subject: [PATCH 4/6] Removed package removal in stop hook --- hooks/pg_dir_hooks.py | 8 +++----- hooks/pg_dir_utils.py | 5 +---- unit_tests/test_pg_dir_context.py | 2 +- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/hooks/pg_dir_hooks.py b/hooks/pg_dir_hooks.py index e25770d..2476136 100755 --- a/hooks/pg_dir_hooks.py +++ b/hooks/pg_dir_hooks.py @@ -77,10 +77,8 @@ def plumgrid_joined(relation_id=None): This hook is run when relation with edge or gateway is created. ''' opsvm_ip = config('opsvm-ip') - if opsvm_ip == '127.0.0.1': - pass - elif not is_ip(opsvm_ip): - raise ValueError('Incorrect IP specified') + if not is_ip(opsvm_ip): + raise ValueError('Incorrect OPSVM IP specified') else: relation_set(relation_id=relation_id, opsvm_ip=opsvm_ip) @@ -137,7 +135,7 @@ def start(): while (count < 10): if post_pg_license(): break - count = count + 1 + count += 1 time.sleep(15) diff --git a/hooks/pg_dir_utils.py b/hooks/pg_dir_utils.py index 6a4324d..7f7d988 100644 --- a/hooks/pg_dir_utils.py +++ b/hooks/pg_dir_utils.py @@ -412,10 +412,7 @@ def get_cidr_from_iface(interface): def director_cluster_ready(): dirs_count = len(pg_dir_context._pg_dir_ips()) log('DIR COUNT: %s' % dirs_count) - if dirs_count == 2: - return True - else: - return False + return True if dirs_count == 2 else False def restart_on_change(restart_map): diff --git a/unit_tests/test_pg_dir_context.py b/unit_tests/test_pg_dir_context.py index baac434..84a29b2 100644 --- a/unit_tests/test_pg_dir_context.py +++ b/unit_tests/test_pg_dir_context.py @@ -60,7 +60,7 @@ class PGDirContextTest(CharmTestCase): return "neutron.randomconfig" config = {'plumgrid-virtual-ip': "192.168.100.250", - 'opsvm-ip':'127.0.0.1'} + 'opsvm-ip': '127.0.0.1'} def mock_config(key=None): if key: From c294e408dd2cb1d773d584bb2bf016fd1115c7ce Mon Sep 17 00:00:00 2001 From: Bilal Baqar Date: Sat, 26 Mar 2016 21:30:59 +0100 Subject: [PATCH 5/6] Changed syntax of pg_dir_ips() --- hooks/pg_dir_context.py | 11 +++++------ hooks/pg_dir_utils.py | 1 - 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/hooks/pg_dir_context.py b/hooks/pg_dir_context.py index 476dc1e..d84025b 100644 --- a/hooks/pg_dir_context.py +++ b/hooks/pg_dir_context.py @@ -31,12 +31,11 @@ def _pg_dir_ips(): Inspects plumgrid-director peer relation and returns the ips of the peer directors ''' - pg_dir_ips = [] - for rid in relation_ids('director'): - for unit in related_units(rid): - rdata = relation_get(rid=rid, unit=unit) - pg_dir_ips.append(get_host_ip(rdata['private-address'])) - return pg_dir_ips + return [get_host_ip(rdata['private-address']) + for rid in relation_ids("director") + for rdata in + (relation_get(rid=rid, unit=unit) for unit in related_units(rid)) + if rdata] class PGDirContext(context.NeutronContext): diff --git a/hooks/pg_dir_utils.py b/hooks/pg_dir_utils.py index 7f7d988..cc9c5bc 100644 --- a/hooks/pg_dir_utils.py +++ b/hooks/pg_dir_utils.py @@ -411,7 +411,6 @@ def get_cidr_from_iface(interface): def director_cluster_ready(): dirs_count = len(pg_dir_context._pg_dir_ips()) - log('DIR COUNT: %s' % dirs_count) return True if dirs_count == 2 else False From c3c8e37fcd4624c07c0321e4030122a889a58321 Mon Sep 17 00:00:00 2001 From: Bilal Baqar Date: Sat, 26 Mar 2016 23:04:58 +0100 Subject: [PATCH 6/6] Improved dir ips string --- hooks/pg_dir_context.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/hooks/pg_dir_context.py b/hooks/pg_dir_context.py index d84025b..1a03fe9 100644 --- a/hooks/pg_dir_context.py +++ b/hooks/pg_dir_context.py @@ -78,18 +78,15 @@ class PGDirContext(context.NeutronContext): fallback=get_host_ip(unit_get('private-address'))))) pg_dir_ips = sorted(pg_dir_ips) pg_ctxt['director_ips'] = pg_dir_ips - pg_dir_ips_string = '' - single_ip = True - for ip in pg_dir_ips: - if single_ip: - pg_dir_ips_string = str(ip) - single_ip = False - else: - pg_dir_ips_string = pg_dir_ips_string + ',' + str(ip) - pg_ctxt['director_ips_string'] = pg_dir_ips_string - PG_VIP = config('plumgrid-virtual-ip') + dir_count = len(pg_dir_ips) + pg_ctxt['director_ips_string'] = (str(pg_dir_ips[0]) + ',' + + str(pg_dir_ips[1]) + ',' + + str(pg_dir_ips[2]) + if dir_count == 3 else + str(pg_dir_ips[0])) + PG_VIP = conf['plumgrid-virtual-ip'] if is_ip(PG_VIP): - pg_ctxt['virtual_ip'] = conf['plumgrid-virtual-ip'] + pg_ctxt['virtual_ip'] = PG_VIP else: raise ValueError('Invalid PLUMgrid Virtual IP Provided') unit_hostname = gethostname()