summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBilal Baqar <bbaqar@plumgrid.com>2016-03-13 23:43:21 -0700
committerBilal Baqar <bbaqar@plumgrid.com>2016-03-13 23:43:21 -0700
commitc4868ea75125efa2fd65fe4e840fe8a902d19a6d (patch)
tree636ee15974273d8cf0358e542883a3459b498985
parent806d2da3e7d673eca9504e4ee77bf953cefd9c69 (diff)
parentaeafa927221f63b935a9152d155fe075c7ec218e (diff)
Made the following changes:
1. Reordered file and module imports 2. Sorted director IPs 3. Added unit fqdn in /etc/hosts of plumgrid-lxc 4. Loading plumgrid specific iptables on install 5. Added temporary upgrade hook to load iptables 6. stop_pg() is being used in restart_pg() 7. persistant iptables
-rw-r--r--hooks/pg_gw_context.py14
-rwxr-xr-xhooks/pg_gw_hooks.py13
-rw-r--r--hooks/pg_gw_utils.py80
l---------hooks/upgrade-charm1
-rw-r--r--templates/kilo/hosts2
-rw-r--r--unit_tests/test_pg_gw_context.py7
-rw-r--r--unit_tests/test_pg_gw_hooks.py1
7 files changed, 90 insertions, 28 deletions
diff --git a/hooks/pg_gw_context.py b/hooks/pg_gw_context.py
index f3dada7..91149bf 100644
--- a/hooks/pg_gw_context.py
+++ b/hooks/pg_gw_context.py
@@ -3,14 +3,17 @@
3# This file contains the class that generates context for 3# This file contains the class that generates context for
4# PLUMgrid template files. 4# PLUMgrid template files.
5 5
6from charmhelpers.contrib.openstack import context
7from charmhelpers.contrib.openstack.utils import get_host_ip
6from charmhelpers.core.hookenv import ( 8from charmhelpers.core.hookenv import (
7 relation_ids, 9 relation_ids,
8 related_units, 10 related_units,
9 relation_get, 11 relation_get,
10) 12)
11from charmhelpers.contrib.openstack import context 13from socket import (
12from charmhelpers.contrib.openstack.utils import get_host_ip 14 gethostname,
13from socket import gethostname as get_unit_hostname 15 getfqdn
16)
14 17
15 18
16def _pg_dir_settings(): 19def _pg_dir_settings():
@@ -60,7 +63,7 @@ class PGGwContext(context.NeutronContext):
60 return {} 63 return {}
61 64
62 pg_dir_ips = '' 65 pg_dir_ips = ''
63 pg_dir_settings = _pg_dir_settings() 66 pg_dir_settings = sorted(_pg_dir_settings())
64 single_ip = True 67 single_ip = True
65 for ip in pg_dir_settings: 68 for ip in pg_dir_settings:
66 if single_ip: 69 if single_ip:
@@ -69,8 +72,9 @@ class PGGwContext(context.NeutronContext):
69 else: 72 else:
70 pg_dir_ips = pg_dir_ips + ',' + str(ip) 73 pg_dir_ips = pg_dir_ips + ',' + str(ip)
71 pg_ctxt['local_ip'] = pg_dir_ips 74 pg_ctxt['local_ip'] = pg_dir_ips
72 unit_hostname = get_unit_hostname() 75 unit_hostname = gethostname()
73 pg_ctxt['pg_hostname'] = unit_hostname 76 pg_ctxt['pg_hostname'] = unit_hostname
77 pg_ctxt['pg_fqdn'] = getfqdn()
74 from pg_gw_utils import ( 78 from pg_gw_utils import (
75 get_mgmt_interface, 79 get_mgmt_interface,
76 get_gw_interfaces, 80 get_gw_interfaces,
diff --git a/hooks/pg_gw_hooks.py b/hooks/pg_gw_hooks.py
index d976b7e..97ea9c6 100755
--- a/hooks/pg_gw_hooks.py
+++ b/hooks/pg_gw_hooks.py
@@ -30,7 +30,8 @@ from pg_gw_utils import (
30 remove_iovisor, 30 remove_iovisor,
31 ensure_mtu, 31 ensure_mtu,
32 add_lcm_key, 32 add_lcm_key,
33 fabric_interface_changed 33 fabric_interface_changed,
34 load_iptables,
34) 35)
35 36
36hooks = Hooks() 37hooks = Hooks()
@@ -42,6 +43,7 @@ def install():
42 ''' 43 '''
43 Install hook is run when the charm is first deployed on a node. 44 Install hook is run when the charm is first deployed on a node.
44 ''' 45 '''
46 load_iptables()
45 configure_sources(update=True) 47 configure_sources(update=True)
46 pkgs = determine_packages() 48 pkgs = determine_packages()
47 for pkg in pkgs: 49 for pkg in pkgs:
@@ -98,6 +100,15 @@ def config_changed():
98 restart_pg() 100 restart_pg()
99 101
100 102
103@hooks.hook('upgrade-charm')
104def upgrade_charm():
105 load_iptables()
106 ensure_mtu()
107 ensure_files()
108 CONFIGS.write_all()
109 restart_pg()
110
111
101@hooks.hook('stop') 112@hooks.hook('stop')
102def stop(): 113def stop():
103 ''' 114 '''
diff --git a/hooks/pg_gw_utils.py b/hooks/pg_gw_utils.py
index e3586d0..186870f 100644
--- a/hooks/pg_gw_utils.py
+++ b/hooks/pg_gw_utils.py
@@ -2,8 +2,18 @@
2 2
3# This file contains functions used by the hooks to deploy PLUMgrid Gateway. 3# This file contains functions used by the hooks to deploy PLUMgrid Gateway.
4 4
5from charmhelpers.contrib.openstack.neutron import neutron_plugin_attribute 5import pg_gw_context
6import subprocess
7import time
8import os
9import json
10from collections import OrderedDict
11from socket import gethostname as get_unit_hostname
6from copy import deepcopy 12from copy import deepcopy
13from charmhelpers.contrib.openstack.neutron import neutron_plugin_attribute
14from charmhelpers.contrib.storage.linux.ceph import modprobe
15from charmhelpers.core.host import set_nic_mtu
16from charmhelpers.contrib.openstack import templating
7from charmhelpers.core.hookenv import ( 17from charmhelpers.core.hookenv import (
8 log, 18 log,
9 config, 19 config,
@@ -22,33 +32,22 @@ from charmhelpers.core.host import (
22 service_stop, 32 service_stop,
23) 33)
24from charmhelpers.fetch import ( 34from charmhelpers.fetch import (
25 apt_cache 35 apt_cache,
36 apt_install
26) 37)
27from charmhelpers.contrib.storage.linux.ceph import modprobe
28from charmhelpers.core.host import set_nic_mtu
29from charmhelpers.contrib.openstack import templating
30from collections import OrderedDict
31from charmhelpers.contrib.openstack.utils import ( 38from charmhelpers.contrib.openstack.utils import (
32 os_release, 39 os_release,
33) 40)
34from socket import gethostname as get_unit_hostname
35import pg_gw_context
36import subprocess
37import time
38import os
39import json
40 41
41LXC_CONF = "/etc/libvirt/lxc.conf" 42LXC_CONF = "/etc/libvirt/lxc.conf"
42TEMPLATES = 'templates/' 43TEMPLATES = 'templates/'
43PG_LXC_DATA_PATH = '/var/lib/libvirt/filesystems/plumgrid-data' 44PG_LXC_DATA_PATH = '/var/lib/libvirt/filesystems/plumgrid-data'
44
45PG_CONF = '%s/conf/pg/plumgrid.conf' % PG_LXC_DATA_PATH 45PG_CONF = '%s/conf/pg/plumgrid.conf' % PG_LXC_DATA_PATH
46PG_HN_CONF = '%s/conf/etc/hostname' % PG_LXC_DATA_PATH 46PG_HN_CONF = '%s/conf/etc/hostname' % PG_LXC_DATA_PATH
47PG_HS_CONF = '%s/conf/etc/hosts' % PG_LXC_DATA_PATH 47PG_HS_CONF = '%s/conf/etc/hosts' % PG_LXC_DATA_PATH
48PG_IFCS_CONF = '%s/conf/pg/ifcs.conf' % PG_LXC_DATA_PATH 48PG_IFCS_CONF = '%s/conf/pg/ifcs.conf' % PG_LXC_DATA_PATH
49AUTH_KEY_PATH = '%s/root/.ssh/authorized_keys' % PG_LXC_DATA_PATH 49AUTH_KEY_PATH = '%s/root/.ssh/authorized_keys' % PG_LXC_DATA_PATH
50IFC_LIST_GW = '/var/run/plumgrid/lxc/ifc_list_gateway' 50IFC_LIST_GW = '/var/run/plumgrid/lxc/ifc_list_gateway'
51
52SUDOERS_CONF = '/etc/sudoers.d/ifc_ctl_sudoers' 51SUDOERS_CONF = '/etc/sudoers.d/ifc_ctl_sudoers'
53 52
54BASE_RESOURCE_MAP = OrderedDict([ 53BASE_RESOURCE_MAP = OrderedDict([
@@ -141,9 +140,7 @@ def restart_pg():
141 ''' 140 '''
142 Stops and Starts PLUMgrid service after flushing iptables. 141 Stops and Starts PLUMgrid service after flushing iptables.
143 ''' 142 '''
144 service_stop('plumgrid') 143 stop_pg()
145 time.sleep(30)
146 _exec_cmd(cmd=['iptables', '-F'])
147 service_start('plumgrid') 144 service_start('plumgrid')
148 time.sleep(30) 145 time.sleep(30)
149 146
@@ -153,7 +150,7 @@ def stop_pg():
153 Stops PLUMgrid service. 150 Stops PLUMgrid service.
154 ''' 151 '''
155 service_stop('plumgrid') 152 service_stop('plumgrid')
156 time.sleep(2) 153 time.sleep(30)
157 154
158 155
159def load_iovisor(): 156def load_iovisor():
@@ -168,7 +165,7 @@ def remove_iovisor():
168 Removes iovisor kernel module. 165 Removes iovisor kernel module.
169 ''' 166 '''
170 _exec_cmd(cmd=['rmmod', 'iovisor'], 167 _exec_cmd(cmd=['rmmod', 'iovisor'],
171 error_msg='Error Loading Iovisor Kernel Module') 168 error_msg='Error Removing IOVisor Kernel Module')
172 time.sleep(1) 169 time.sleep(1)
173 170
174 171
@@ -327,3 +324,48 @@ def add_lcm_key():
327 fa.write('\n') 324 fa.write('\n')
328 fa.close() 325 fa.close()
329 return 1 326 return 1
327
328
329def load_iptables():
330 '''
331 Loads iptables rules to allow all PLUMgrid communication.
332 '''
333 network = get_cidr_from_iface(get_mgmt_interface())
334 if network:
335 _exec_cmd(['sudo', 'iptables', '-A', 'INPUT', '-p', 'tcp',
336 '-j', 'ACCEPT', '-s', network, '-d',
337 network, '-m', 'state', '--state', 'NEW'])
338 _exec_cmd(['sudo', 'iptables', '-A', 'INPUT', '-p', 'udp', '-j',
339 'ACCEPT', '-s', network, '-d', network,
340 '-m', 'state', '--state', 'NEW'])
341 apt_install('iptables-persistent')
342
343
344def get_cidr_from_iface(interface):
345 '''
346 Determines Network CIDR from interface.
347 '''
348 if not interface:
349 return None
350 apt_install('ohai')
351 try:
352 os_info = subprocess.check_output(['ohai', '-l', 'fatal'])
353 except OSError:
354 log('Unable to get operating system information')
355 return None
356 try:
357 os_info_json = json.loads(os_info)
358 except ValueError:
359 log('Unable to determine network')
360 return None
361 device = os_info_json['network']['interfaces'].get(interface)
362 if device is not None:
363 if device.get('routes'):
364 routes = device['routes']
365 for net in routes:
366 if 'scope' in net:
367 return net.get('destination')
368 else:
369 return None
370 else:
371 return None
diff --git a/hooks/upgrade-charm b/hooks/upgrade-charm
new file mode 120000
index 0000000..3aec9ba
--- /dev/null
+++ b/hooks/upgrade-charm
@@ -0,0 +1 @@
pg_gw_hooks.py \ No newline at end of file
diff --git a/templates/kilo/hosts b/templates/kilo/hosts
index 99e3be5..e19e49d 100644
--- a/templates/kilo/hosts
+++ b/templates/kilo/hosts
@@ -1,5 +1,5 @@
1127.0.0.1 localhost 1127.0.0.1 localhost
2127.0.1.1 {{ pg_hostname }} 2127.0.1.1 {{ pg_fqdn }} {{ pg_hostname }}
3 3
4# The following lines are desirable for IPv6 capable hosts 4# The following lines are desirable for IPv6 capable hosts
5::1 ip6-localhost ip6-loopback 5::1 ip6-localhost ip6-loopback
diff --git a/unit_tests/test_pg_gw_context.py b/unit_tests/test_pg_gw_context.py
index 18d9f98..7fa2c16 100644
--- a/unit_tests/test_pg_gw_context.py
+++ b/unit_tests/test_pg_gw_context.py
@@ -5,7 +5,8 @@ import pg_gw_utils as utils
5import charmhelpers 5import charmhelpers
6 6
7TO_PATCH = [ 7TO_PATCH = [
8 'get_unit_hostname', 8 'gethostname',
9 'getfqdn'
9] 10]
10 11
11 12
@@ -55,7 +56,8 @@ class PGGwContextTest(CharmTestCase):
55 _npa.side_effect = mock_npa 56 _npa.side_effect = mock_npa
56 _unit_get.return_value = '192.168.100.201' 57 _unit_get.return_value = '192.168.100.201'
57 _unit_priv_ip.return_value = '192.168.100.201' 58 _unit_priv_ip.return_value = '192.168.100.201'
58 self.get_unit_hostname.return_value = 'node0' 59 self.gethostname.return_value = 'node0'
60 self.getfqdn.return_value = 'node0'
59 _is_clus.return_value = False 61 _is_clus.return_value = False
60 _config_flag.return_value = False 62 _config_flag.return_value = False
61 _pg_dir_settings.return_value = {'pg_dir_ip': '192.168.100.201'} 63 _pg_dir_settings.return_value = {'pg_dir_ip': '192.168.100.201'}
@@ -73,6 +75,7 @@ class PGGwContextTest(CharmTestCase):
73 'neutron_security_groups': None, 75 'neutron_security_groups': None,
74 'neutron_url': 'https://192.168.100.201:9696', 76 'neutron_url': 'https://192.168.100.201:9696',
75 'pg_hostname': 'node0', 77 'pg_hostname': 'node0',
78 'pg_fqdn': 'node0',
76 'interface': 'juju-br0', 79 'interface': 'juju-br0',
77 'fabric_interface': 'juju-br0', 80 'fabric_interface': 'juju-br0',
78 'label': 'node0', 81 'label': 'node0',
diff --git a/unit_tests/test_pg_gw_hooks.py b/unit_tests/test_pg_gw_hooks.py
index 8e87dc8..a1b5779 100644
--- a/unit_tests/test_pg_gw_hooks.py
+++ b/unit_tests/test_pg_gw_hooks.py
@@ -30,6 +30,7 @@ TO_PATCH = [
30 'ensure_mtu', 30 'ensure_mtu',
31 'add_lcm_key', 31 'add_lcm_key',
32 'determine_packages', 32 'determine_packages',
33 'load_iptables'
33] 34]
34NEUTRON_CONF_DIR = "/etc/neutron" 35NEUTRON_CONF_DIR = "/etc/neutron"
35 36