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

Proposed by Bilal Baqar
Status: Merged
Merged at revision: 27
Proposed branch: lp:~bbaqar/charms/trusty/plumgrid-director/ip-sort
Merge into: lp:~plumgrid-team/charms/trusty/plumgrid-director/trunk
Diff against target: 312 lines (+106/-37)
6 files modified
hooks/pg_dir_context.py (+11/-7)
hooks/pg_dir_hooks.py (+13/-1)
hooks/pg_dir_utils.py (+71/-22)
templates/kilo/hosts (+1/-1)
unit_tests/test_pg_dir_context.py (+8/-5)
unit_tests/test_pg_dir_hooks.py (+2/-1)
To merge this branch: bzr merge lp:~bbaqar/charms/trusty/plumgrid-director/ip-sort
Reviewer Review Type Date Requested Status
Abdullah Khan Wazir Approve
Nabeel Afzal Approve
Review via email: mp+288564@code.launchpad.net

Description of the change

Sorted the director IPs.
Ticket: [CFB-829]

To post a comment you must log in.
Revision history for this message
Nabeel Afzal (nabeel-afzal) :
review: Approve
Revision history for this message
Abdullah Khan Wazir (abdullah-khan) wrote :

Looks good.

review: Approve
22. By Bilal Baqar

Fixing sleep

23. By Bilal Baqar

Fixing sleep

Preview Diff

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

Subscribers

People subscribed via source and target branches