From 8ecd96dfc4ed4a130190c24e0a84b54b71de019c Mon Sep 17 00:00:00 2001 From: Hassaan Pasha Date: Mon, 10 Aug 2015 15:06:33 +0500 Subject: [PATCH] Added HA coverage --- README.ex | 1 + hooks/director-relation-joined | 1 + hooks/pg_dir_context.py | 35 ++++++++++++++++++++++++++++++---- hooks/pg_dir_hooks.py | 18 +++++++++++++++++ hooks/pg_dir_utils.py | 29 ++++++++++++++++++++++------ metadata.yaml | 4 ++++ templates/kilo/nginx.conf | 4 +++- templates/kilo/plumgrid.conf | 2 +- 8 files changed, 82 insertions(+), 12 deletions(-) create mode 120000 hooks/director-relation-joined diff --git a/README.ex b/README.ex index fc08251..a10f9a0 100644 --- a/README.ex +++ b/README.ex @@ -11,6 +11,7 @@ Step by step instructions on using the charm: juju deploy plumgrid-director juju add-relation neutron-api neutron-api-plumgrid + juju add-relation neutron-api-plumgrid plumgrid-director For plumgrid-director to work make the configuration in the neutron-api and neutron-api-plumgrid charms as specified in the configuration section below. diff --git a/hooks/director-relation-joined b/hooks/director-relation-joined new file mode 120000 index 0000000..6530c5a --- /dev/null +++ b/hooks/director-relation-joined @@ -0,0 +1 @@ +pg_dir_hooks.py \ No newline at end of file diff --git a/hooks/pg_dir_context.py b/hooks/pg_dir_context.py index 9be0b9f..d7f3109 100644 --- a/hooks/pg_dir_context.py +++ b/hooks/pg_dir_context.py @@ -6,6 +6,11 @@ from charmhelpers.core.hookenv import ( config, unit_get, ) +from charmhelpers.core.hookenv import ( + relation_ids, + related_units, + relation_get, +) from charmhelpers.contrib.openstack import context from charmhelpers.contrib.openstack.utils import get_host_ip from charmhelpers.contrib.network.ip import get_address_in_network @@ -13,6 +18,16 @@ from charmhelpers.contrib.network.ip import get_address_in_network import re from socket import gethostname as get_unit_hostname +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(rdata['private-address']) + return pg_dir_ips class PGDirContext(context.NeutronContext): @@ -48,12 +63,24 @@ class PGDirContext(context.NeutronContext): return {} conf = config() - pg_ctxt['local_ip'] = \ - get_address_in_network(network=None, - fallback=get_host_ip(unit_get('private-address'))) + pg_dir_ips =_pg_dir_ips() + pg_dir_ips.append(str(get_address_in_network(network=None, + fallback=get_host_ip(unit_get('private-address'))))) + 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_ctxt['virtual_ip'] = conf['plumgrid-virtual-ip'] pg_ctxt['pg_hostname'] = "pg-director" - pg_ctxt['interface'] = "juju-br0" + from pg_dir_utils import check_interface_type + interface_type = check_interface_type() + pg_ctxt['interface'] = interface_type pg_ctxt['label'] = get_unit_hostname() pg_ctxt['fabric_mode'] = 'host' virtual_ip_array = re.split('\.', conf['plumgrid-virtual-ip']) diff --git a/hooks/pg_dir_hooks.py b/hooks/pg_dir_hooks.py index 53fc93d..9ceff67 100755 --- a/hooks/pg_dir_hooks.py +++ b/hooks/pg_dir_hooks.py @@ -50,6 +50,24 @@ def install(): add_lcm_key() +@hooks.hook('plumgrid-plugin-relation-joined') +def plumgrid_dir(): + ''' + This hook is run when relation between neutron-api-plumgrid + and plumgrid-director is made. + ''' + ensure_mtu() + ensure_files() + add_lcm_key() + CONFIGS.write_all() + restart_pg() + +@hooks.hook('director-relation-joined') +def dir_joined(): + ensure_files() + CONFIGS.write_all() + restart_pg() + @hooks.hook('config-changed') def config_changed(): ''' diff --git a/hooks/pg_dir_utils.py b/hooks/pg_dir_utils.py index f2cbf3d..4f3f6f3 100644 --- a/hooks/pg_dir_utils.py +++ b/hooks/pg_dir_utils.py @@ -23,6 +23,7 @@ import pg_dir_context import subprocess import time import os +import re LXC_CONF = '/etc/libvirt/lxc.conf' TEMPLATES = 'templates/' @@ -143,6 +144,20 @@ def remove_iovisor(): ''' _exec_cmd(cmd=['rmmod', 'iovisor'], error_msg='Error Loading IOVisor Kernel Module') +def check_interface_type(): + ''' + Checks the interface. Support added for AWS deployments. There are 2 + possible interfaces "juju-br0" and "eth0". The default being juju-br0 + ''' + log("Checking Interface Type") + default_interface = "juju-br0" + AWS_interface = "eth0" + shell_output = subprocess.check_output(['brctl','show','juju-br0']) + output = re.split(' |\n|\t',shell_output) + if output[10] == '': + return AWS_interface + else: + return default_interface def ensure_mtu(): ''' @@ -150,12 +165,14 @@ def ensure_mtu(): ''' log("Changing MTU of juju-br0 and all attached interfaces") interface_mtu = config('network-device-mtu') - cmd = subprocess.check_output(["brctl", "show", "juju-br0"]) - words = cmd.split() - for word in words: - if 'eth' in word: - set_nic_mtu(word, interface_mtu) - set_nic_mtu('juju-br0', interface_mtu) + interface_type = check_interface_type() + if interface_type == "juju-br0": + cmd = subprocess.check_output(["brctl", "show", interface_type]) + words = cmd.split() + for word in words: + if 'eth' in word: + set_nic_mtu(word, interface_mtu) + set_nic_mtu(interface_type, interface_mtu) def _exec_cmd(cmd=None, error_msg='Command exited with ERRORs', fatal=False): diff --git a/metadata.yaml b/metadata.yaml index 820d422..3a00f13 100644 --- a/metadata.yaml +++ b/metadata.yaml @@ -15,3 +15,7 @@ requires: provides: plumgrid: interface: plumgrid +peers: + director: + interface: director + diff --git a/templates/kilo/nginx.conf b/templates/kilo/nginx.conf index 3213c30..179bb67 100644 --- a/templates/kilo/nginx.conf +++ b/templates/kilo/nginx.conf @@ -24,7 +24,9 @@ lua_shared_dict apache_servers 16K; lua_shared_dict tc_servers 16K; init_by_lua 'lb = require "lb" init_servers = { - ["{{ local_ip }}"] = true, +{% for ip in director_ips -%} + ["{{ ip }}"] = true, +{% endfor -%} }'; # Redirect http to https diff --git a/templates/kilo/plumgrid.conf b/templates/kilo/plumgrid.conf index 52a9dc8..acb8e9b 100644 --- a/templates/kilo/plumgrid.conf +++ b/templates/kilo/plumgrid.conf @@ -1,4 +1,4 @@ -plumgrid_ip={{ local_ip }} +plumgrid_ip={{ director_ips_string }} plumgrid_port=8001 mgmt_dev={{ interface }} label={{ label}}