Browse Source

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
tags/14.04-eol
Bilal Baqar 3 years ago
parent
commit
c4868ea751

+ 9
- 5
hooks/pg_gw_context.py View File

@@ -3,14 +3,17 @@
3 3
 # This file contains the class that generates context for
4 4
 # PLUMgrid template files.
5 5
 
6
+from charmhelpers.contrib.openstack import context
7
+from charmhelpers.contrib.openstack.utils import get_host_ip
6 8
 from charmhelpers.core.hookenv import (
7 9
     relation_ids,
8 10
     related_units,
9 11
     relation_get,
10 12
 )
11
-from charmhelpers.contrib.openstack import context
12
-from charmhelpers.contrib.openstack.utils import get_host_ip
13
-from socket import gethostname as get_unit_hostname
13
+from socket import (
14
+    gethostname,
15
+    getfqdn
16
+)
14 17
 
15 18
 
16 19
 def _pg_dir_settings():
@@ -60,7 +63,7 @@ class PGGwContext(context.NeutronContext):
60 63
             return {}
61 64
 
62 65
         pg_dir_ips = ''
63
-        pg_dir_settings = _pg_dir_settings()
66
+        pg_dir_settings = sorted(_pg_dir_settings())
64 67
         single_ip = True
65 68
         for ip in pg_dir_settings:
66 69
             if single_ip:
@@ -69,8 +72,9 @@ class PGGwContext(context.NeutronContext):
69 72
             else:
70 73
                 pg_dir_ips = pg_dir_ips + ',' + str(ip)
71 74
         pg_ctxt['local_ip'] = pg_dir_ips
72
-        unit_hostname = get_unit_hostname()
75
+        unit_hostname = gethostname()
73 76
         pg_ctxt['pg_hostname'] = unit_hostname
77
+        pg_ctxt['pg_fqdn'] = getfqdn()
74 78
         from pg_gw_utils import (
75 79
             get_mgmt_interface,
76 80
             get_gw_interfaces,

+ 12
- 1
hooks/pg_gw_hooks.py View File

@@ -30,7 +30,8 @@ from pg_gw_utils import (
30 30
     remove_iovisor,
31 31
     ensure_mtu,
32 32
     add_lcm_key,
33
-    fabric_interface_changed
33
+    fabric_interface_changed,
34
+    load_iptables,
34 35
 )
35 36
 
36 37
 hooks = Hooks()
@@ -42,6 +43,7 @@ def install():
42 43
     '''
43 44
     Install hook is run when the charm is first deployed on a node.
44 45
     '''
46
+    load_iptables()
45 47
     configure_sources(update=True)
46 48
     pkgs = determine_packages()
47 49
     for pkg in pkgs:
@@ -98,6 +100,15 @@ def config_changed():
98 100
     restart_pg()
99 101
 
100 102
 
103
+@hooks.hook('upgrade-charm')
104
+def upgrade_charm():
105
+    load_iptables()
106
+    ensure_mtu()
107
+    ensure_files()
108
+    CONFIGS.write_all()
109
+    restart_pg()
110
+
111
+
101 112
 @hooks.hook('stop')
102 113
 def stop():
103 114
     '''

+ 61
- 19
hooks/pg_gw_utils.py View File

@@ -2,8 +2,18 @@
2 2
 
3 3
 # This file contains functions used by the hooks to deploy PLUMgrid Gateway.
4 4
 
5
-from charmhelpers.contrib.openstack.neutron import neutron_plugin_attribute
5
+import pg_gw_context
6
+import subprocess
7
+import time
8
+import os
9
+import json
10
+from collections import OrderedDict
11
+from socket import gethostname as get_unit_hostname
6 12
 from copy import deepcopy
13
+from charmhelpers.contrib.openstack.neutron import neutron_plugin_attribute
14
+from charmhelpers.contrib.storage.linux.ceph import modprobe
15
+from charmhelpers.core.host import set_nic_mtu
16
+from charmhelpers.contrib.openstack import templating
7 17
 from charmhelpers.core.hookenv import (
8 18
     log,
9 19
     config,
@@ -22,33 +32,22 @@ from charmhelpers.core.host import (
22 32
     service_stop,
23 33
 )
24 34
 from charmhelpers.fetch import (
25
-    apt_cache
35
+    apt_cache,
36
+    apt_install
26 37
 )
27
-from charmhelpers.contrib.storage.linux.ceph import modprobe
28
-from charmhelpers.core.host import set_nic_mtu
29
-from charmhelpers.contrib.openstack import templating
30
-from collections import OrderedDict
31 38
 from charmhelpers.contrib.openstack.utils import (
32 39
     os_release,
33 40
 )
34
-from socket import gethostname as get_unit_hostname
35
-import pg_gw_context
36
-import subprocess
37
-import time
38
-import os
39
-import json
40 41
 
41 42
 LXC_CONF = "/etc/libvirt/lxc.conf"
42 43
 TEMPLATES = 'templates/'
43 44
 PG_LXC_DATA_PATH = '/var/lib/libvirt/filesystems/plumgrid-data'
44
-
45 45
 PG_CONF = '%s/conf/pg/plumgrid.conf' % PG_LXC_DATA_PATH
46 46
 PG_HN_CONF = '%s/conf/etc/hostname' % PG_LXC_DATA_PATH
47 47
 PG_HS_CONF = '%s/conf/etc/hosts' % PG_LXC_DATA_PATH
48 48
 PG_IFCS_CONF = '%s/conf/pg/ifcs.conf' % PG_LXC_DATA_PATH
49 49
 AUTH_KEY_PATH = '%s/root/.ssh/authorized_keys' % PG_LXC_DATA_PATH
50 50
 IFC_LIST_GW = '/var/run/plumgrid/lxc/ifc_list_gateway'
51
-
52 51
 SUDOERS_CONF = '/etc/sudoers.d/ifc_ctl_sudoers'
53 52
 
54 53
 BASE_RESOURCE_MAP = OrderedDict([
@@ -141,9 +140,7 @@ def restart_pg():
141 140
     '''
142 141
     Stops and Starts PLUMgrid service after flushing iptables.
143 142
     '''
144
-    service_stop('plumgrid')
145
-    time.sleep(30)
146
-    _exec_cmd(cmd=['iptables', '-F'])
143
+    stop_pg()
147 144
     service_start('plumgrid')
148 145
     time.sleep(30)
149 146
 
@@ -153,7 +150,7 @@ def stop_pg():
153 150
     Stops PLUMgrid service.
154 151
     '''
155 152
     service_stop('plumgrid')
156
-    time.sleep(2)
153
+    time.sleep(30)
157 154
 
158 155
 
159 156
 def load_iovisor():
@@ -168,7 +165,7 @@ def remove_iovisor():
168 165
     Removes iovisor kernel module.
169 166
     '''
170 167
     _exec_cmd(cmd=['rmmod', 'iovisor'],
171
-              error_msg='Error Loading Iovisor Kernel Module')
168
+              error_msg='Error Removing IOVisor Kernel Module')
172 169
     time.sleep(1)
173 170
 
174 171
 
@@ -327,3 +324,48 @@ def add_lcm_key():
327 324
     fa.write('\n')
328 325
     fa.close()
329 326
     return 1
327
+
328
+
329
+def 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
+
344
+def 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

+ 1
- 0
hooks/upgrade-charm View File

@@ -0,0 +1 @@
1
+pg_gw_hooks.py

+ 1
- 1
templates/kilo/hosts View File

@@ -1,5 +1,5 @@
1 1
 127.0.0.1   localhost
2
-127.0.1.1   {{ pg_hostname }}
2
+127.0.1.1   {{ pg_fqdn }}   {{ pg_hostname }}
3 3
 
4 4
 # The following lines are desirable for IPv6 capable hosts
5 5
 ::1     ip6-localhost ip6-loopback

+ 5
- 2
unit_tests/test_pg_gw_context.py View File

@@ -5,7 +5,8 @@ import pg_gw_utils as utils
5 5
 import charmhelpers
6 6
 
7 7
 TO_PATCH = [
8
-    'get_unit_hostname',
8
+    'gethostname',
9
+    'getfqdn'
9 10
 ]
10 11
 
11 12
 
@@ -55,7 +56,8 @@ class PGGwContextTest(CharmTestCase):
55 56
         _npa.side_effect = mock_npa
56 57
         _unit_get.return_value = '192.168.100.201'
57 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 61
         _is_clus.return_value = False
60 62
         _config_flag.return_value = False
61 63
         _pg_dir_settings.return_value = {'pg_dir_ip': '192.168.100.201'}
@@ -73,6 +75,7 @@ class PGGwContextTest(CharmTestCase):
73 75
             'neutron_security_groups': None,
74 76
             'neutron_url': 'https://192.168.100.201:9696',
75 77
             'pg_hostname': 'node0',
78
+            'pg_fqdn': 'node0',
76 79
             'interface': 'juju-br0',
77 80
             'fabric_interface': 'juju-br0',
78 81
             'label': 'node0',

+ 1
- 0
unit_tests/test_pg_gw_hooks.py View File

@@ -30,6 +30,7 @@ TO_PATCH = [
30 30
     'ensure_mtu',
31 31
     'add_lcm_key',
32 32
     'determine_packages',
33
+    'load_iptables'
33 34
 ]
34 35
 NEUTRON_CONF_DIR = "/etc/neutron"
35 36
 

Loading…
Cancel
Save