Merge lp:~bbaqar/charms/trusty/plumgrid-gateway/ip-sort into lp:~plumgrid-team/charms/trusty/plumgrid-gateway/trunk

Proposed by Bilal Baqar on 2016-03-10
Status: Merged
Merged at revision: 22
Proposed branch: lp:~bbaqar/charms/trusty/plumgrid-gateway/ip-sort
Merge into: lp:~plumgrid-team/charms/trusty/plumgrid-gateway/trunk
Diff against target: 276 lines (+89/-28)
6 files modified
hooks/pg_gw_context.py (+9/-5)
hooks/pg_gw_hooks.py (+12/-1)
hooks/pg_gw_utils.py (+61/-19)
templates/kilo/hosts (+1/-1)
unit_tests/test_pg_gw_context.py (+5/-2)
unit_tests/test_pg_gw_hooks.py (+1/-0)
To merge this branch: bzr merge lp:~bbaqar/charms/trusty/plumgrid-gateway/ip-sort
Reviewer Review Type Date Requested Status
Abdullah Khan Wazir 2016-03-10 Approve on 2016-03-14
Review via email: mp+288719@code.launchpad.net

Description of the change

Sorted the director IPs and setting required iptables rules
Ticket: [CFB-829]

To post a comment you must log in.
19. By Bilal Baqar on 2016-03-14

Local commit

20. By Bilal Baqar on 2016-03-14

Adding restart in upgrade hook

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hooks/pg_gw_context.py'
2--- hooks/pg_gw_context.py 2015-11-22 03:26:23 +0000
3+++ hooks/pg_gw_context.py 2016-03-14 04:18:28 +0000
4@@ -3,14 +3,17 @@
5 # This file contains the class that generates context for
6 # PLUMgrid template files.
7
8+from charmhelpers.contrib.openstack import context
9+from charmhelpers.contrib.openstack.utils import get_host_ip
10 from charmhelpers.core.hookenv import (
11 relation_ids,
12 related_units,
13 relation_get,
14 )
15-from charmhelpers.contrib.openstack import context
16-from charmhelpers.contrib.openstack.utils import get_host_ip
17-from socket import gethostname as get_unit_hostname
18+from socket import (
19+ gethostname,
20+ getfqdn
21+)
22
23
24 def _pg_dir_settings():
25@@ -60,7 +63,7 @@
26 return {}
27
28 pg_dir_ips = ''
29- pg_dir_settings = _pg_dir_settings()
30+ pg_dir_settings = sorted(_pg_dir_settings())
31 single_ip = True
32 for ip in pg_dir_settings:
33 if single_ip:
34@@ -69,8 +72,9 @@
35 else:
36 pg_dir_ips = pg_dir_ips + ',' + str(ip)
37 pg_ctxt['local_ip'] = pg_dir_ips
38- unit_hostname = get_unit_hostname()
39+ unit_hostname = gethostname()
40 pg_ctxt['pg_hostname'] = unit_hostname
41+ pg_ctxt['pg_fqdn'] = getfqdn()
42 from pg_gw_utils import (
43 get_mgmt_interface,
44 get_gw_interfaces,
45
46=== modified file 'hooks/pg_gw_hooks.py'
47--- hooks/pg_gw_hooks.py 2015-11-22 03:26:23 +0000
48+++ hooks/pg_gw_hooks.py 2016-03-14 04:18:28 +0000
49@@ -30,7 +30,8 @@
50 remove_iovisor,
51 ensure_mtu,
52 add_lcm_key,
53- fabric_interface_changed
54+ fabric_interface_changed,
55+ load_iptables,
56 )
57
58 hooks = Hooks()
59@@ -42,6 +43,7 @@
60 '''
61 Install hook is run when the charm is first deployed on a node.
62 '''
63+ load_iptables()
64 configure_sources(update=True)
65 pkgs = determine_packages()
66 for pkg in pkgs:
67@@ -98,6 +100,15 @@
68 restart_pg()
69
70
71+@hooks.hook('upgrade-charm')
72+def upgrade_charm():
73+ load_iptables()
74+ ensure_mtu()
75+ ensure_files()
76+ CONFIGS.write_all()
77+ restart_pg()
78+
79+
80 @hooks.hook('stop')
81 def stop():
82 '''
83
84=== modified file 'hooks/pg_gw_utils.py'
85--- hooks/pg_gw_utils.py 2015-11-22 03:26:23 +0000
86+++ hooks/pg_gw_utils.py 2016-03-14 04:18:28 +0000
87@@ -2,8 +2,18 @@
88
89 # This file contains functions used by the hooks to deploy PLUMgrid Gateway.
90
91+import pg_gw_context
92+import subprocess
93+import time
94+import os
95+import json
96+from collections import OrderedDict
97+from socket import gethostname as get_unit_hostname
98+from copy import deepcopy
99 from charmhelpers.contrib.openstack.neutron import neutron_plugin_attribute
100-from copy import deepcopy
101+from charmhelpers.contrib.storage.linux.ceph import modprobe
102+from charmhelpers.core.host import set_nic_mtu
103+from charmhelpers.contrib.openstack import templating
104 from charmhelpers.core.hookenv import (
105 log,
106 config,
107@@ -22,33 +32,22 @@
108 service_stop,
109 )
110 from charmhelpers.fetch import (
111- apt_cache
112+ apt_cache,
113+ apt_install
114 )
115-from charmhelpers.contrib.storage.linux.ceph import modprobe
116-from charmhelpers.core.host import set_nic_mtu
117-from charmhelpers.contrib.openstack import templating
118-from collections import OrderedDict
119 from charmhelpers.contrib.openstack.utils import (
120 os_release,
121 )
122-from socket import gethostname as get_unit_hostname
123-import pg_gw_context
124-import subprocess
125-import time
126-import os
127-import json
128
129 LXC_CONF = "/etc/libvirt/lxc.conf"
130 TEMPLATES = 'templates/'
131 PG_LXC_DATA_PATH = '/var/lib/libvirt/filesystems/plumgrid-data'
132-
133 PG_CONF = '%s/conf/pg/plumgrid.conf' % PG_LXC_DATA_PATH
134 PG_HN_CONF = '%s/conf/etc/hostname' % PG_LXC_DATA_PATH
135 PG_HS_CONF = '%s/conf/etc/hosts' % PG_LXC_DATA_PATH
136 PG_IFCS_CONF = '%s/conf/pg/ifcs.conf' % PG_LXC_DATA_PATH
137 AUTH_KEY_PATH = '%s/root/.ssh/authorized_keys' % PG_LXC_DATA_PATH
138 IFC_LIST_GW = '/var/run/plumgrid/lxc/ifc_list_gateway'
139-
140 SUDOERS_CONF = '/etc/sudoers.d/ifc_ctl_sudoers'
141
142 BASE_RESOURCE_MAP = OrderedDict([
143@@ -141,9 +140,7 @@
144 '''
145 Stops and Starts PLUMgrid service after flushing iptables.
146 '''
147- service_stop('plumgrid')
148- time.sleep(30)
149- _exec_cmd(cmd=['iptables', '-F'])
150+ stop_pg()
151 service_start('plumgrid')
152 time.sleep(30)
153
154@@ -153,7 +150,7 @@
155 Stops PLUMgrid service.
156 '''
157 service_stop('plumgrid')
158- time.sleep(2)
159+ time.sleep(30)
160
161
162 def load_iovisor():
163@@ -168,7 +165,7 @@
164 Removes iovisor kernel module.
165 '''
166 _exec_cmd(cmd=['rmmod', 'iovisor'],
167- error_msg='Error Loading Iovisor Kernel Module')
168+ error_msg='Error Removing IOVisor Kernel Module')
169 time.sleep(1)
170
171
172@@ -327,3 +324,48 @@
173 fa.write('\n')
174 fa.close()
175 return 1
176+
177+
178+def load_iptables():
179+ '''
180+ Loads iptables rules to allow all PLUMgrid communication.
181+ '''
182+ network = get_cidr_from_iface(get_mgmt_interface())
183+ if network:
184+ _exec_cmd(['sudo', 'iptables', '-A', 'INPUT', '-p', 'tcp',
185+ '-j', 'ACCEPT', '-s', network, '-d',
186+ network, '-m', 'state', '--state', 'NEW'])
187+ _exec_cmd(['sudo', 'iptables', '-A', 'INPUT', '-p', 'udp', '-j',
188+ 'ACCEPT', '-s', network, '-d', network,
189+ '-m', 'state', '--state', 'NEW'])
190+ apt_install('iptables-persistent')
191+
192+
193+def get_cidr_from_iface(interface):
194+ '''
195+ Determines Network CIDR from interface.
196+ '''
197+ if not interface:
198+ return None
199+ apt_install('ohai')
200+ try:
201+ os_info = subprocess.check_output(['ohai', '-l', 'fatal'])
202+ except OSError:
203+ log('Unable to get operating system information')
204+ return None
205+ try:
206+ os_info_json = json.loads(os_info)
207+ except ValueError:
208+ log('Unable to determine network')
209+ return None
210+ device = os_info_json['network']['interfaces'].get(interface)
211+ if device is not None:
212+ if device.get('routes'):
213+ routes = device['routes']
214+ for net in routes:
215+ if 'scope' in net:
216+ return net.get('destination')
217+ else:
218+ return None
219+ else:
220+ return None
221
222=== added symlink 'hooks/upgrade-charm'
223=== target is u'pg_gw_hooks.py'
224=== modified file 'templates/kilo/hosts'
225--- templates/kilo/hosts 2015-07-29 18:23:55 +0000
226+++ templates/kilo/hosts 2016-03-14 04:18:28 +0000
227@@ -1,5 +1,5 @@
228 127.0.0.1 localhost
229-127.0.1.1 {{ pg_hostname }}
230+127.0.1.1 {{ pg_fqdn }} {{ pg_hostname }}
231
232 # The following lines are desirable for IPv6 capable hosts
233 ::1 ip6-localhost ip6-loopback
234
235=== modified file 'unit_tests/test_pg_gw_context.py'
236--- unit_tests/test_pg_gw_context.py 2015-11-22 03:26:23 +0000
237+++ unit_tests/test_pg_gw_context.py 2016-03-14 04:18:28 +0000
238@@ -5,7 +5,8 @@
239 import charmhelpers
240
241 TO_PATCH = [
242- 'get_unit_hostname',
243+ 'gethostname',
244+ 'getfqdn'
245 ]
246
247
248@@ -55,7 +56,8 @@
249 _npa.side_effect = mock_npa
250 _unit_get.return_value = '192.168.100.201'
251 _unit_priv_ip.return_value = '192.168.100.201'
252- self.get_unit_hostname.return_value = 'node0'
253+ self.gethostname.return_value = 'node0'
254+ self.getfqdn.return_value = 'node0'
255 _is_clus.return_value = False
256 _config_flag.return_value = False
257 _pg_dir_settings.return_value = {'pg_dir_ip': '192.168.100.201'}
258@@ -73,6 +75,7 @@
259 'neutron_security_groups': None,
260 'neutron_url': 'https://192.168.100.201:9696',
261 'pg_hostname': 'node0',
262+ 'pg_fqdn': 'node0',
263 'interface': 'juju-br0',
264 'fabric_interface': 'juju-br0',
265 'label': 'node0',
266
267=== modified file 'unit_tests/test_pg_gw_hooks.py'
268--- unit_tests/test_pg_gw_hooks.py 2016-03-03 21:49:53 +0000
269+++ unit_tests/test_pg_gw_hooks.py 2016-03-14 04:18:28 +0000
270@@ -30,6 +30,7 @@
271 'ensure_mtu',
272 'add_lcm_key',
273 'determine_packages',
274+ 'load_iptables'
275 ]
276 NEUTRON_CONF_DIR = "/etc/neutron"
277

Subscribers

People subscribed via source and target branches