summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBilal Baqar <bbaqar@plumgrid.com>2016-03-28 11:18:41 -0700
committerBilal Baqar <bbaqar@plumgrid.com>2016-03-28 11:18:41 -0700
commit89817b2437c390f55672256d5e5318598bebdaa6 (patch)
treea7e9921fa34cc047cdc619fb70d2e0bd3b2ff38a
parenta8f68eae1ebfa9bf0d5e6fb29355094acb566ca7 (diff)
parente74a4757fbfea9ef05f6fca2313214311b9ab7e8 (diff)
OPSVM Changes - Ticket: [SOL-830]
- Getting OPSVM IP from director relation - Making OPSVM specific changes - Cleaned code in various functions - Added restart_on_change decorater function that restarts plumgrid service only when there has been any change in the configuration files - Removed restart of plumgrid service when only two directors are available - Fixed unit tests accordingly
-rw-r--r--hooks/pg_gw_context.py36
-rwxr-xr-xhooks/pg_gw_hooks.py24
-rw-r--r--hooks/pg_gw_utils.py33
l---------hooks/plumgrid-relation-joined1
-rw-r--r--templates/kilo/00-pg.conf1
-rw-r--r--templates/kilo/plumgrid.conf2
-rw-r--r--unit_tests/test_pg_gw_context.py17
-rw-r--r--unit_tests/test_pg_gw_hooks.py15
-rw-r--r--unit_tests/test_pg_gw_utils.py4
9 files changed, 81 insertions, 52 deletions
diff --git a/hooks/pg_gw_context.py b/hooks/pg_gw_context.py
index 91149bf..de1b0a3 100644
--- a/hooks/pg_gw_context.py
+++ b/hooks/pg_gw_context.py
@@ -16,16 +16,23 @@ from socket import (
16) 16)
17 17
18 18
19def _pg_dir_settings(): 19def _pg_dir_context():
20 ''' 20 '''
21 Inspects relation with PLUMgrid director. 21 Inspects relation with PLUMgrid director.
22 ''' 22 '''
23 director_ips = [] 23 ctxt = {
24 'opsvm_ip': '127.0.0.1',
25 'director_ips': [],
26 }
24 for rid in relation_ids('plumgrid'): 27 for rid in relation_ids('plumgrid'):
25 for unit in related_units(rid): 28 for unit in related_units(rid):
26 rdata = relation_get(rid=rid, unit=unit) 29 rdata = relation_get(rid=rid, unit=unit)
27 director_ips.append(str(get_host_ip(rdata['private-address']))) 30 ctxt['director_ips'
28 return director_ips 31 ].append(str(get_host_ip(rdata['private-address'])))
32 if "opsvm_ip" in rdata:
33 ctxt['opsvm_ip'] = \
34 rdata['opsvm_ip']
35 return ctxt
29 36
30 37
31class PGGwContext(context.NeutronContext): 38class PGGwContext(context.NeutronContext):
@@ -62,16 +69,16 @@ class PGGwContext(context.NeutronContext):
62 if not pg_ctxt: 69 if not pg_ctxt:
63 return {} 70 return {}
64 71
65 pg_dir_ips = '' 72 pg_dir_context = _pg_dir_context()
66 pg_dir_settings = sorted(_pg_dir_settings()) 73 pg_dir_ips = sorted(pg_dir_context['director_ips'])
67 single_ip = True 74 dir_count = len(pg_dir_ips)
68 for ip in pg_dir_settings: 75 pg_ctxt['director_ips_string'] = (str(pg_dir_ips[0]) + ',' +
69 if single_ip: 76 str(pg_dir_ips[1]) + ',' +
70 pg_dir_ips = str(ip) 77 str(pg_dir_ips[2])
71 single_ip = False 78 if dir_count == 3 else
72 else: 79 str(pg_dir_ips[0])
73 pg_dir_ips = pg_dir_ips + ',' + str(ip) 80 if dir_count == 1 else
74 pg_ctxt['local_ip'] = pg_dir_ips 81 '')
75 unit_hostname = gethostname() 82 unit_hostname = gethostname()
76 pg_ctxt['pg_hostname'] = unit_hostname 83 pg_ctxt['pg_hostname'] = unit_hostname
77 pg_ctxt['pg_fqdn'] = getfqdn() 84 pg_ctxt['pg_fqdn'] = getfqdn()
@@ -85,5 +92,6 @@ class PGGwContext(context.NeutronContext):
85 pg_ctxt['label'] = unit_hostname 92 pg_ctxt['label'] = unit_hostname
86 pg_ctxt['fabric_mode'] = 'host' 93 pg_ctxt['fabric_mode'] = 'host'
87 pg_ctxt['ext_interfaces'] = get_gw_interfaces() 94 pg_ctxt['ext_interfaces'] = get_gw_interfaces()
95 pg_ctxt['opsvm_ip'] = pg_dir_context['opsvm_ip']
88 96
89 return pg_ctxt 97 return pg_ctxt
diff --git a/hooks/pg_gw_hooks.py b/hooks/pg_gw_hooks.py
index b302e4a..ad020ed 100755
--- a/hooks/pg_gw_hooks.py
+++ b/hooks/pg_gw_hooks.py
@@ -18,7 +18,6 @@ from charmhelpers.core.host import service_running
18 18
19from charmhelpers.fetch import ( 19from charmhelpers.fetch import (
20 apt_install, 20 apt_install,
21 apt_purge,
22 configure_sources, 21 configure_sources,
23) 22)
24 23
@@ -26,6 +25,7 @@ from pg_gw_utils import (
26 register_configs, 25 register_configs,
27 ensure_files, 26 ensure_files,
28 restart_pg, 27 restart_pg,
28 restart_map,
29 stop_pg, 29 stop_pg,
30 determine_packages, 30 determine_packages,
31 load_iovisor, 31 load_iovisor,
@@ -34,6 +34,8 @@ from pg_gw_utils import (
34 add_lcm_key, 34 add_lcm_key,
35 fabric_interface_changed, 35 fabric_interface_changed,
36 load_iptables, 36 load_iptables,
37 restart_on_change,
38 director_cluster_ready
37) 39)
38 40
39hooks = Hooks() 41hooks = Hooks()
@@ -56,18 +58,16 @@ def install():
56 add_lcm_key() 58 add_lcm_key()
57 59
58 60
59@hooks.hook('plumgrid-relation-joined')
60@hooks.hook('plumgrid-relation-changed') 61@hooks.hook('plumgrid-relation-changed')
61def plumgrid_joined(): 62@restart_on_change(restart_map())
63def plumgrid_changed():
62 ''' 64 '''
63 This hook is run when relation between plumgrid-gateway and 65 This hook is run when relation between plumgrid-gateway and
64 plumgrid-director is made. 66 plumgrid-director is made.
65 ''' 67 '''
66 ensure_mtu() 68 if director_cluster_ready():
67 ensure_files() 69 ensure_mtu()
68 add_lcm_key() 70 CONFIGS.write_all()
69 CONFIGS.write_all()
70 restart_pg()
71 71
72 72
73@hooks.hook('config-changed') 73@hooks.hook('config-changed')
@@ -105,12 +105,10 @@ def config_changed():
105 105
106 106
107@hooks.hook('upgrade-charm') 107@hooks.hook('upgrade-charm')
108@restart_on_change(restart_map())
108def upgrade_charm(): 109def upgrade_charm():
109 load_iptables()
110 ensure_mtu() 110 ensure_mtu()
111 ensure_files()
112 CONFIGS.write_all() 111 CONFIGS.write_all()
113 restart_pg()
114 112
115 113
116@hooks.hook('stop') 114@hooks.hook('stop')
@@ -119,10 +117,6 @@ def stop():
119 This hook is run when the charm is destroyed. 117 This hook is run when the charm is destroyed.
120 ''' 118 '''
121 stop_pg() 119 stop_pg()
122 remove_iovisor()
123 pkgs = determine_packages()
124 for pkg in pkgs:
125 apt_purge(pkg, fatal=False)
126 120
127 121
128def main(): 122def main():
diff --git a/hooks/pg_gw_utils.py b/hooks/pg_gw_utils.py
index 1830f12..5bf695c 100644
--- a/hooks/pg_gw_utils.py
+++ b/hooks/pg_gw_utils.py
@@ -12,7 +12,6 @@ from socket import gethostname as get_unit_hostname
12from copy import deepcopy 12from copy import deepcopy
13from charmhelpers.contrib.openstack.neutron import neutron_plugin_attribute 13from charmhelpers.contrib.openstack.neutron import neutron_plugin_attribute
14from charmhelpers.contrib.storage.linux.ceph import modprobe 14from charmhelpers.contrib.storage.linux.ceph import modprobe
15from charmhelpers.core.host import set_nic_mtu
16from charmhelpers.contrib.openstack import templating 15from charmhelpers.contrib.openstack import templating
17from charmhelpers.core.hookenv import ( 16from charmhelpers.core.hookenv import (
18 log, 17 log,
@@ -30,7 +29,9 @@ from charmhelpers.core.host import (
30 write_file, 29 write_file,
31 service_start, 30 service_start,
32 service_stop, 31 service_stop,
33 service_running 32 service_running,
33 path_hash,
34 set_nic_mtu
34) 35)
35from charmhelpers.fetch import ( 36from charmhelpers.fetch import (
36 apt_cache, 37 apt_cache,
@@ -47,6 +48,7 @@ PG_CONF = '%s/conf/pg/plumgrid.conf' % PG_LXC_DATA_PATH
47PG_HN_CONF = '%s/conf/etc/hostname' % PG_LXC_DATA_PATH 48PG_HN_CONF = '%s/conf/etc/hostname' % PG_LXC_DATA_PATH
48PG_HS_CONF = '%s/conf/etc/hosts' % PG_LXC_DATA_PATH 49PG_HS_CONF = '%s/conf/etc/hosts' % PG_LXC_DATA_PATH
49PG_IFCS_CONF = '%s/conf/pg/ifcs.conf' % PG_LXC_DATA_PATH 50PG_IFCS_CONF = '%s/conf/pg/ifcs.conf' % PG_LXC_DATA_PATH
51OPS_CONF = '%s/conf/etc/00-pg.conf' % PG_LXC_DATA_PATH
50AUTH_KEY_PATH = '%s/root/.ssh/authorized_keys' % PG_LXC_DATA_PATH 52AUTH_KEY_PATH = '%s/root/.ssh/authorized_keys' % PG_LXC_DATA_PATH
51IFC_LIST_GW = '/var/run/plumgrid/lxc/ifc_list_gateway' 53IFC_LIST_GW = '/var/run/plumgrid/lxc/ifc_list_gateway'
52SUDOERS_CONF = '/etc/sudoers.d/ifc_ctl_sudoers' 54SUDOERS_CONF = '/etc/sudoers.d/ifc_ctl_sudoers'
@@ -64,6 +66,10 @@ BASE_RESOURCE_MAP = OrderedDict([
64 'services': ['plumgrid'], 66 'services': ['plumgrid'],
65 'contexts': [pg_gw_context.PGGwContext()], 67 'contexts': [pg_gw_context.PGGwContext()],
66 }), 68 }),
69 (OPS_CONF, {
70 'services': ['plumgrid'],
71 'contexts': [pg_gw_context.PGGwContext()],
72 }),
67 (PG_IFCS_CONF, { 73 (PG_IFCS_CONF, {
68 'services': [], 74 'services': [],
69 'contexts': [pg_gw_context.PGGwContext()], 75 'contexts': [pg_gw_context.PGGwContext()],
@@ -161,7 +167,7 @@ def stop_pg():
161 Stops PLUMgrid service. 167 Stops PLUMgrid service.
162 ''' 168 '''
163 service_stop('plumgrid') 169 service_stop('plumgrid')
164 time.sleep(30) 170 time.sleep(2)
165 171
166 172
167def load_iovisor(): 173def load_iovisor():
@@ -380,3 +386,24 @@ def get_cidr_from_iface(interface):
380 return None 386 return None
381 else: 387 else:
382 return None 388 return None
389
390
391def director_cluster_ready():
392 dirs_count = len(pg_gw_context._pg_dir_context()['director_ips'])
393 return True if dirs_count == 1 or dirs_count == 3 else False
394
395
396def restart_on_change(restart_map):
397 """
398 Restart services based on configuration files changing
399 """
400 def wrap(f):
401 def wrapped_f(*args, **kwargs):
402 checksums = {path: path_hash(path) for path in restart_map}
403 f(*args, **kwargs)
404 for path in restart_map:
405 if path_hash(path) != checksums[path]:
406 restart_pg()
407 break
408 return wrapped_f
409 return wrap
diff --git a/hooks/plumgrid-relation-joined b/hooks/plumgrid-relation-joined
deleted file mode 120000
index 3aec9ba..0000000
--- a/hooks/plumgrid-relation-joined
+++ /dev/null
@@ -1 +0,0 @@
1pg_gw_hooks.py \ No newline at end of file
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/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 @@
1plumgrid_ip={{ local_ip }} 1plumgrid_ip={{ director_ips_string }}
2plumgrid_port=8001 2plumgrid_port=8001
3mgmt_dev={{ interface }} 3mgmt_dev={{ interface }}
4label={{ label}} 4label={{ label}}
diff --git a/unit_tests/test_pg_gw_context.py b/unit_tests/test_pg_gw_context.py
index 7fa2c16..b39cfb7 100644
--- a/unit_tests/test_pg_gw_context.py
+++ b/unit_tests/test_pg_gw_context.py
@@ -35,14 +35,14 @@ class PGGwContextTest(CharmTestCase):
35 @patch.object(charmhelpers.contrib.openstack.context, 35 @patch.object(charmhelpers.contrib.openstack.context,
36 'config_flags_parser') 36 'config_flags_parser')
37 @patch.object(context.PGGwContext, '_save_flag_file') 37 @patch.object(context.PGGwContext, '_save_flag_file')
38 @patch.object(context, '_pg_dir_settings') 38 @patch.object(context, '_pg_dir_context')
39 @patch.object(charmhelpers.contrib.openstack.context, 39 @patch.object(charmhelpers.contrib.openstack.context,
40 'neutron_plugin_attribute') 40 'neutron_plugin_attribute')
41 @patch.object(utils, 'get_mgmt_interface') 41 @patch.object(utils, 'get_mgmt_interface')
42 @patch.object(utils, 'get_fabric_interface') 42 @patch.object(utils, 'get_fabric_interface')
43 @patch.object(utils, 'get_gw_interfaces') 43 @patch.object(utils, 'get_gw_interfaces')
44 def test_neutroncc_context_api_rel(self, _gw_int, _fabric_int, 44 def test_neutroncc_context_api_rel(self, _gw_int, _fabric_int,
45 _mgmt_int, _npa, _pg_dir_settings, 45 _mgmt_int, _npa, _pg_dir_context,
46 _save_flag_file, _config_flag, 46 _save_flag_file, _config_flag,
47 _unit_get, _unit_priv_ip, _config, 47 _unit_get, _unit_priv_ip, _config,
48 _is_clus, _https, _ens_pkgs): 48 _is_clus, _https, _ens_pkgs):
@@ -54,13 +54,14 @@ class PGGwContextTest(CharmTestCase):
54 54
55 self.maxDiff = None 55 self.maxDiff = None
56 _npa.side_effect = mock_npa 56 _npa.side_effect = mock_npa
57 _unit_get.return_value = '192.168.100.201' 57 _unit_get.return_value = '192.168.100.203'
58 _unit_priv_ip.return_value = '192.168.100.201' 58 _unit_priv_ip.return_value = '192.168.100.203'
59 self.gethostname.return_value = 'node0' 59 self.gethostname.return_value = 'node0'
60 self.getfqdn.return_value = 'node0' 60 self.getfqdn.return_value = 'node0'
61 _is_clus.return_value = False 61 _is_clus.return_value = False
62 _config_flag.return_value = False 62 _config_flag.return_value = False
63 _pg_dir_settings.return_value = {'pg_dir_ip': '192.168.100.201'} 63 _pg_dir_context.return_value = {'director_ips': ['192.168.100.201'],
64 'opsvm_ip': '127.0.0.1'}
64 _mgmt_int.return_value = 'juju-br0' 65 _mgmt_int.return_value = 'juju-br0'
65 _fabric_int.return_value = 'juju-br0' 66 _fabric_int.return_value = 'juju-br0'
66 _gw_int.return_value = ['eth1'] 67 _gw_int.return_value = ['eth1']
@@ -69,11 +70,12 @@ class PGGwContextTest(CharmTestCase):
69 'ext_interfaces': ['eth1'], 70 'ext_interfaces': ['eth1'],
70 'config': 'neutron.randomconfig', 71 'config': 'neutron.randomconfig',
71 'core_plugin': 'neutron.randomdriver', 72 'core_plugin': 'neutron.randomdriver',
72 'local_ip': 'pg_dir_ip', 73 'local_ip': '192.168.100.203',
74 'director_ips_string': '192.168.100.201',
73 'network_manager': 'neutron', 75 'network_manager': 'neutron',
74 'neutron_plugin': 'plumgrid', 76 'neutron_plugin': 'plumgrid',
75 'neutron_security_groups': None, 77 'neutron_security_groups': None,
76 'neutron_url': 'https://192.168.100.201:9696', 78 'neutron_url': 'https://192.168.100.203:9696',
77 'pg_hostname': 'node0', 79 'pg_hostname': 'node0',
78 'pg_fqdn': 'node0', 80 'pg_fqdn': 'node0',
79 'interface': 'juju-br0', 81 'interface': 'juju-br0',
@@ -81,5 +83,6 @@ class PGGwContextTest(CharmTestCase):
81 'label': 'node0', 83 'label': 'node0',
82 'fabric_mode': 'host', 84 'fabric_mode': 'host',
83 'neutron_alchemy_flags': False, 85 'neutron_alchemy_flags': False,
86 'opsvm_ip': '127.0.0.1',
84 } 87 }
85 self.assertEquals(expect, napi_ctxt()) 88 self.assertEquals(expect, napi_ctxt())
diff --git a/unit_tests/test_pg_gw_hooks.py b/unit_tests/test_pg_gw_hooks.py
index c132dd3..b218be3 100644
--- a/unit_tests/test_pg_gw_hooks.py
+++ b/unit_tests/test_pg_gw_hooks.py
@@ -19,7 +19,6 @@ utils.restart_map = _map
19TO_PATCH = [ 19TO_PATCH = [
20 'remove_iovisor', 20 'remove_iovisor',
21 'apt_install', 21 'apt_install',
22 'apt_purge',
23 'CONFIGS', 22 'CONFIGS',
24 'log', 23 'log',
25 'configure_sources', 24 'configure_sources',
@@ -30,7 +29,8 @@ TO_PATCH = [
30 'ensure_mtu', 29 'ensure_mtu',
31 'add_lcm_key', 30 'add_lcm_key',
32 'determine_packages', 31 'determine_packages',
33 'load_iptables' 32 'load_iptables',
33 'director_cluster_ready'
34] 34]
35NEUTRON_CONF_DIR = "/etc/neutron" 35NEUTRON_CONF_DIR = "/etc/neutron"
36 36
@@ -62,17 +62,12 @@ class PGGwHooksTests(CharmTestCase):
62 self.ensure_files.assert_called_with() 62 self.ensure_files.assert_called_with()
63 self.add_lcm_key.assert_called_with() 63 self.add_lcm_key.assert_called_with()
64 64
65 def test_plumgrid_joined(self): 65 def test_plumgrid_changed(self):
66 self._call_hook('plumgrid-relation-joined') 66 self._call_hook('plumgrid-relation-changed')
67 self.director_cluster_ready.return_value = True
67 self.ensure_mtu.assert_called_with() 68 self.ensure_mtu.assert_called_with()
68 self.ensure_files.assert_called_with()
69 self.add_lcm_key.assert_called_with()
70 self.CONFIGS.write_all.assert_called_with() 69 self.CONFIGS.write_all.assert_called_with()
71 self.restart_pg.assert_called_with()
72 70
73 def test_stop(self): 71 def test_stop(self):
74 _pkgs = ['plumgrid-lxc', 'iovisor-dkms']
75 self._call_hook('stop') 72 self._call_hook('stop')
76 self.stop_pg.assert_called_with() 73 self.stop_pg.assert_called_with()
77 self.remove_iovisor.assert_called_with()
78 self.determine_packages.return_value = _pkgs
diff --git a/unit_tests/test_pg_gw_utils.py b/unit_tests/test_pg_gw_utils.py
index 366e44e..113e15a 100644
--- a/unit_tests/test_pg_gw_utils.py
+++ b/unit_tests/test_pg_gw_utils.py
@@ -53,7 +53,8 @@ class TestPGGwUtils(CharmTestCase):
53 confs = [nutils.PG_CONF, 53 confs = [nutils.PG_CONF,
54 nutils.PG_HN_CONF, 54 nutils.PG_HN_CONF,
55 nutils.PG_HS_CONF, 55 nutils.PG_HS_CONF,
56 nutils.PG_IFCS_CONF] 56 nutils.PG_IFCS_CONF,
57 nutils.OPS_CONF]
57 self.assertItemsEqual(_regconfs.configs, confs) 58 self.assertItemsEqual(_regconfs.configs, confs)
58 59
59 def test_resource_map(self): 60 def test_resource_map(self):
@@ -69,6 +70,7 @@ class TestPGGwUtils(CharmTestCase):
69 (nutils.PG_CONF, ['plumgrid']), 70 (nutils.PG_CONF, ['plumgrid']),
70 (nutils.PG_HN_CONF, ['plumgrid']), 71 (nutils.PG_HN_CONF, ['plumgrid']),
71 (nutils.PG_HS_CONF, ['plumgrid']), 72 (nutils.PG_HS_CONF, ['plumgrid']),
73 (nutils.OPS_CONF, ['plumgrid']),
72 (nutils.PG_IFCS_CONF, []), 74 (nutils.PG_IFCS_CONF, []),
73 ]) 75 ])
74 self.assertEqual(expect, _restart_map) 76 self.assertEqual(expect, _restart_map)