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

Proposed by Bilal Baqar
Status: Merged
Merged at revision: 23
Proposed branch: lp:~bbaqar/charms/trusty/plumgrid-edge/ip-sort
Merge into: lp:~plumgrid-team/charms/trusty/plumgrid-edge/trunk
Diff against target: 268 lines (+88/-28)
6 files modified
hooks/pg_edge_context.py (+9/-5)
hooks/pg_edge_hooks.py (+12/-1)
hooks/pg_edge_utils.py (+60/-19)
templates/kilo/hosts (+1/-1)
unit_tests/test_pg_edge_context.py (+5/-2)
unit_tests/test_pg_edge_hooks.py (+1/-0)
To merge this branch: bzr merge lp:~bbaqar/charms/trusty/plumgrid-edge/ip-sort
Reviewer Review Type Date Requested Status
Abdullah Khan Wazir Approve
Review via email: mp+288717@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.
Revision history for this message
Abdullah Khan Wazir (abdullah-khan) :
review: Approve
18. By Bilal Baqar

local commit

19. By Bilal Baqar

Fixing unit-test

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hooks/pg_edge_context.py'
2--- hooks/pg_edge_context.py 2016-01-26 19:06:29 +0000
3+++ hooks/pg_edge_context.py 2016-03-14 06:34:07 +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_edge_utils import get_mgmt_interface, get_fabric_interface
43 pg_ctxt['interface'] = get_mgmt_interface()
44 pg_ctxt['fabric_interface'] = get_fabric_interface()
45
46=== modified file 'hooks/pg_edge_hooks.py'
47--- hooks/pg_edge_hooks.py 2015-11-22 03:25:51 +0000
48+++ hooks/pg_edge_hooks.py 2016-03-14 06:34:07 +0000
49@@ -32,7 +32,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@@ -44,6 +45,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@@ -116,6 +118,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_edge_utils.py'
85--- hooks/pg_edge_utils.py 2015-11-22 03:25:51 +0000
86+++ hooks/pg_edge_utils.py 2016-03-14 06:34:07 +0000
87@@ -2,8 +2,18 @@
88
89 # This file contains functions used by the hooks to deploy PLUMgrid Edge.
90
91+import pg_edge_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@@ -23,34 +33,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.fetch import apt_install
118-from charmhelpers.contrib.openstack import templating
119-from collections import OrderedDict
120 from charmhelpers.contrib.openstack.utils import (
121 os_release,
122 )
123-from socket import gethostname as get_unit_hostname
124-import pg_edge_context
125-import subprocess
126-import time
127-import os
128-import json
129
130 SHARED_SECRET = "/etc/nova/secret.txt"
131 LXC_CONF = '/etc/libvirt/lxc.conf'
132 TEMPLATES = 'templates/'
133 PG_LXC_DATA_PATH = '/var/lib/libvirt/filesystems/plumgrid-data'
134-
135 PG_CONF = '%s/conf/pg/plumgrid.conf' % PG_LXC_DATA_PATH
136 PG_HN_CONF = '%s/conf/etc/hostname' % PG_LXC_DATA_PATH
137 PG_HS_CONF = '%s/conf/etc/hosts' % PG_LXC_DATA_PATH
138 PG_IFCS_CONF = '%s/conf/pg/ifcs.conf' % PG_LXC_DATA_PATH
139 AUTH_KEY_PATH = '%s/root/.ssh/authorized_keys' % PG_LXC_DATA_PATH
140-
141 SUDOERS_CONF = '/etc/sudoers.d/ifc_ctl_sudoers'
142 FILTERS_CONF_DIR = '/etc/nova/rootwrap.d'
143 FILTERS_CONF = '%s/network.filters' % FILTERS_CONF_DIR
144@@ -156,9 +154,7 @@
145 '''
146 Stops and Starts PLUMgrid service after flushing iptables.
147 '''
148- service_stop('plumgrid')
149- time.sleep(2)
150- _exec_cmd(cmd=['iptables', '-F'])
151+ stop_pg()
152 service_start('plumgrid')
153 time.sleep(5)
154
155@@ -183,7 +179,7 @@
156 Removes iovisor kernel module.
157 '''
158 _exec_cmd(cmd=['rmmod', 'iovisor'],
159- error_msg='Error Loading Iovisor Kernel Module')
160+ error_msg='Error Removing IOVisor Kernel Module')
161 time.sleep(1)
162
163
164@@ -358,3 +354,48 @@
165 fa.write('\n')
166 fa.close()
167 return 1
168+
169+
170+def load_iptables():
171+ '''
172+ Loads iptables rules to allow all PLUMgrid communication.
173+ '''
174+ network = get_cidr_from_iface(get_mgmt_interface())
175+ if network:
176+ _exec_cmd(['sudo', 'iptables', '-A', 'INPUT', '-p', 'tcp',
177+ '-j', 'ACCEPT', '-s', network, '-d',
178+ network, '-m', 'state', '--state', 'NEW'])
179+ _exec_cmd(['sudo', 'iptables', '-A', 'INPUT', '-p', 'udp', '-j',
180+ 'ACCEPT', '-s', network, '-d', network,
181+ '-m', 'state', '--state', 'NEW'])
182+ apt_install('iptables-persistent')
183+
184+
185+def get_cidr_from_iface(interface):
186+ '''
187+ Determines Network CIDR from interface.
188+ '''
189+ if not interface:
190+ return None
191+ apt_install('ohai')
192+ try:
193+ os_info = subprocess.check_output(['ohai', '-l', 'fatal'])
194+ except OSError:
195+ log('Unable to get operating system information')
196+ return None
197+ try:
198+ os_info_json = json.loads(os_info)
199+ except ValueError:
200+ log('Unable to determine network')
201+ return None
202+ device = os_info_json['network']['interfaces'].get(interface)
203+ if device is not None:
204+ if device.get('routes'):
205+ routes = device['routes']
206+ for net in routes:
207+ if 'scope' in net:
208+ return net.get('destination')
209+ else:
210+ return None
211+ else:
212+ return None
213
214=== added symlink 'hooks/upgrade-charm'
215=== target is u'pg_edge_hooks.py'
216=== modified file 'templates/kilo/hosts'
217--- templates/kilo/hosts 2015-07-29 18:19:18 +0000
218+++ templates/kilo/hosts 2016-03-14 06:34:07 +0000
219@@ -1,5 +1,5 @@
220 127.0.0.1 localhost
221-127.0.1.1 {{ pg_hostname }}
222+127.0.1.1 {{ pg_fqdn }} {{ pg_hostname }}
223
224 # The following lines are desirable for IPv6 capable hosts
225 ::1 ip6-localhost ip6-loopback
226
227=== modified file 'unit_tests/test_pg_edge_context.py'
228--- unit_tests/test_pg_edge_context.py 2016-01-26 22:19:39 +0000
229+++ unit_tests/test_pg_edge_context.py 2016-03-14 06:34:07 +0000
230@@ -5,7 +5,8 @@
231 import charmhelpers
232
233 TO_PATCH = [
234- 'get_unit_hostname',
235+ 'gethostname',
236+ 'getfqdn'
237 ]
238
239
240@@ -62,7 +63,8 @@
241 _npa.side_effect = mock_npa
242 _unit_get.return_value = '192.168.100.201'
243 _unit_priv_ip.return_value = '192.168.100.201'
244- self.get_unit_hostname.return_value = 'node0'
245+ self.gethostname.return_value = 'node0'
246+ self.getfqdn.return_value = 'node0.maas'
247 _is_clus.return_value = False
248 _config_flag.return_value = False
249 _pg_dir_settings.return_value = {'pg_dir_ip': '192.168.100.201'}
250@@ -78,6 +80,7 @@
251 'neutron_security_groups': None,
252 'neutron_url': 'https://192.168.100.201:9696',
253 'pg_hostname': 'node0',
254+ 'pg_fqdn': 'node0.maas',
255 'interface': 'juju-br0',
256 'fabric_interface': 'juju-br0',
257 'label': 'node0',
258
259=== modified file 'unit_tests/test_pg_edge_hooks.py'
260--- unit_tests/test_pg_edge_hooks.py 2015-11-22 03:25:51 +0000
261+++ unit_tests/test_pg_edge_hooks.py 2016-03-14 06:34:07 +0000
262@@ -32,6 +32,7 @@
263 'config',
264 'relation_set',
265 'relation_ids',
266+ 'load_iptables'
267 ]
268 NEUTRON_CONF_DIR = "/etc/neutron"
269

Subscribers

People subscribed via source and target branches