Browse Source

OPSVM Changes - Ticket: [SOL-830]

- Getting OPSVM IP from director relation
- Making OPSVM specific changes
- Cleaned code in various functions
- Added restart_on_change decorater function that restarts plumgrid service only when there has been any change in the configuration files
- Removed restart of plumgrid service when only two directors are available
- Fixed unit tests accordingly
Bilal Baqar 3 years ago
parent
commit
89817b2437

+ 22
- 14
hooks/pg_gw_context.py View File

@@ -16,16 +16,23 @@ from socket import (
16 16
 )
17 17
 
18 18
 
19
-def _pg_dir_settings():
19
+def _pg_dir_context():
20 20
     '''
21 21
     Inspects relation with PLUMgrid director.
22 22
     '''
23
-    director_ips = []
23
+    ctxt = {
24
+        'opsvm_ip': '127.0.0.1',
25
+        'director_ips': [],
26
+    }
24 27
     for rid in relation_ids('plumgrid'):
25 28
         for unit in related_units(rid):
26 29
             rdata = relation_get(rid=rid, unit=unit)
27
-            director_ips.append(str(get_host_ip(rdata['private-address'])))
28
-    return director_ips
30
+            ctxt['director_ips'
31
+                 ].append(str(get_host_ip(rdata['private-address'])))
32
+            if "opsvm_ip" in rdata:
33
+                ctxt['opsvm_ip'] = \
34
+                    rdata['opsvm_ip']
35
+    return ctxt
29 36
 
30 37
 
31 38
 class PGGwContext(context.NeutronContext):
@@ -62,16 +69,16 @@ class PGGwContext(context.NeutronContext):
62 69
         if not pg_ctxt:
63 70
             return {}
64 71
 
65
-        pg_dir_ips = ''
66
-        pg_dir_settings = sorted(_pg_dir_settings())
67
-        single_ip = True
68
-        for ip in pg_dir_settings:
69
-            if single_ip:
70
-                pg_dir_ips = str(ip)
71
-                single_ip = False
72
-            else:
73
-                pg_dir_ips = pg_dir_ips + ',' + str(ip)
74
-        pg_ctxt['local_ip'] = pg_dir_ips
72
+        pg_dir_context = _pg_dir_context()
73
+        pg_dir_ips = sorted(pg_dir_context['director_ips'])
74
+        dir_count = len(pg_dir_ips)
75
+        pg_ctxt['director_ips_string'] = (str(pg_dir_ips[0]) + ',' +
76
+                                          str(pg_dir_ips[1]) + ',' +
77
+                                          str(pg_dir_ips[2])
78
+                                          if dir_count == 3 else
79
+                                          str(pg_dir_ips[0])
80
+                                          if dir_count == 1 else
81
+                                          '')
75 82
         unit_hostname = gethostname()
76 83
         pg_ctxt['pg_hostname'] = unit_hostname
77 84
         pg_ctxt['pg_fqdn'] = getfqdn()
@@ -85,5 +92,6 @@ class PGGwContext(context.NeutronContext):
85 92
         pg_ctxt['label'] = unit_hostname
86 93
         pg_ctxt['fabric_mode'] = 'host'
87 94
         pg_ctxt['ext_interfaces'] = get_gw_interfaces()
95
+        pg_ctxt['opsvm_ip'] = pg_dir_context['opsvm_ip']
88 96
 
89 97
         return pg_ctxt

+ 9
- 15
hooks/pg_gw_hooks.py View File

@@ -18,7 +18,6 @@ from charmhelpers.core.host import service_running
18 18
 
19 19
 from charmhelpers.fetch import (
20 20
     apt_install,
21
-    apt_purge,
22 21
     configure_sources,
23 22
 )
24 23
 
@@ -26,6 +25,7 @@ from pg_gw_utils import (
26 25
     register_configs,
27 26
     ensure_files,
28 27
     restart_pg,
28
+    restart_map,
29 29
     stop_pg,
30 30
     determine_packages,
31 31
     load_iovisor,
@@ -34,6 +34,8 @@ from pg_gw_utils import (
34 34
     add_lcm_key,
35 35
     fabric_interface_changed,
36 36
     load_iptables,
37
+    restart_on_change,
38
+    director_cluster_ready
37 39
 )
38 40
 
39 41
 hooks = Hooks()
@@ -56,18 +58,16 @@ def install():
56 58
     add_lcm_key()
57 59
 
58 60
 
59
-@hooks.hook('plumgrid-relation-joined')
60 61
 @hooks.hook('plumgrid-relation-changed')
61
-def plumgrid_joined():
62
+@restart_on_change(restart_map())
63
+def plumgrid_changed():
62 64
     '''
63 65
     This hook is run when relation between plumgrid-gateway and
64 66
     plumgrid-director is made.
65 67
     '''
66
-    ensure_mtu()
67
-    ensure_files()
68
-    add_lcm_key()
69
-    CONFIGS.write_all()
70
-    restart_pg()
68
+    if director_cluster_ready():
69
+        ensure_mtu()
70
+        CONFIGS.write_all()
71 71
 
72 72
 
73 73
 @hooks.hook('config-changed')
@@ -105,12 +105,10 @@ def config_changed():
105 105
 
106 106
 
107 107
 @hooks.hook('upgrade-charm')
108
+@restart_on_change(restart_map())
108 109
 def upgrade_charm():
109
-    load_iptables()
110 110
     ensure_mtu()
111
-    ensure_files()
112 111
     CONFIGS.write_all()
113
-    restart_pg()
114 112
 
115 113
 
116 114
 @hooks.hook('stop')
@@ -119,10 +117,6 @@ def stop():
119 117
     This hook is run when the charm is destroyed.
120 118
     '''
121 119
     stop_pg()
122
-    remove_iovisor()
123
-    pkgs = determine_packages()
124
-    for pkg in pkgs:
125
-        apt_purge(pkg, fatal=False)
126 120
 
127 121
 
128 122
 def main():

+ 30
- 3
hooks/pg_gw_utils.py View File

@@ -12,7 +12,6 @@ from socket import gethostname as get_unit_hostname
12 12
 from copy import deepcopy
13 13
 from charmhelpers.contrib.openstack.neutron import neutron_plugin_attribute
14 14
 from charmhelpers.contrib.storage.linux.ceph import modprobe
15
-from charmhelpers.core.host import set_nic_mtu
16 15
 from charmhelpers.contrib.openstack import templating
17 16
 from charmhelpers.core.hookenv import (
18 17
     log,
@@ -30,7 +29,9 @@ from charmhelpers.core.host import (
30 29
     write_file,
31 30
     service_start,
32 31
     service_stop,
33
-    service_running
32
+    service_running,
33
+    path_hash,
34
+    set_nic_mtu
34 35
 )
35 36
 from charmhelpers.fetch import (
36 37
     apt_cache,
@@ -47,6 +48,7 @@ PG_CONF = '%s/conf/pg/plumgrid.conf' % PG_LXC_DATA_PATH
47 48
 PG_HN_CONF = '%s/conf/etc/hostname' % PG_LXC_DATA_PATH
48 49
 PG_HS_CONF = '%s/conf/etc/hosts' % PG_LXC_DATA_PATH
49 50
 PG_IFCS_CONF = '%s/conf/pg/ifcs.conf' % PG_LXC_DATA_PATH
51
+OPS_CONF = '%s/conf/etc/00-pg.conf' % PG_LXC_DATA_PATH
50 52
 AUTH_KEY_PATH = '%s/root/.ssh/authorized_keys' % PG_LXC_DATA_PATH
51 53
 IFC_LIST_GW = '/var/run/plumgrid/lxc/ifc_list_gateway'
52 54
 SUDOERS_CONF = '/etc/sudoers.d/ifc_ctl_sudoers'
@@ -64,6 +66,10 @@ BASE_RESOURCE_MAP = OrderedDict([
64 66
         'services': ['plumgrid'],
65 67
         'contexts': [pg_gw_context.PGGwContext()],
66 68
     }),
69
+    (OPS_CONF, {
70
+        'services': ['plumgrid'],
71
+        'contexts': [pg_gw_context.PGGwContext()],
72
+    }),
67 73
     (PG_IFCS_CONF, {
68 74
         'services': [],
69 75
         'contexts': [pg_gw_context.PGGwContext()],
@@ -161,7 +167,7 @@ def stop_pg():
161 167
     Stops PLUMgrid service.
162 168
     '''
163 169
     service_stop('plumgrid')
164
-    time.sleep(30)
170
+    time.sleep(2)
165 171
 
166 172
 
167 173
 def load_iovisor():
@@ -380,3 +386,24 @@ def get_cidr_from_iface(interface):
380 386
             return None
381 387
     else:
382 388
         return None
389
+
390
+
391
+def director_cluster_ready():
392
+    dirs_count = len(pg_gw_context._pg_dir_context()['director_ips'])
393
+    return True if dirs_count == 1 or dirs_count == 3 else False
394
+
395
+
396
+def restart_on_change(restart_map):
397
+    """
398
+    Restart services based on configuration files changing
399
+    """
400
+    def wrap(f):
401
+        def wrapped_f(*args, **kwargs):
402
+            checksums = {path: path_hash(path) for path in restart_map}
403
+            f(*args, **kwargs)
404
+            for path in restart_map:
405
+                if path_hash(path) != checksums[path]:
406
+                    restart_pg()
407
+                    break
408
+        return wrapped_f
409
+    return wrap

+ 0
- 1
hooks/plumgrid-relation-joined View File

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

+ 1
- 0
templates/kilo/00-pg.conf View File

@@ -0,0 +1 @@
1
+$template ls_json,"{{'{'}}{{'%'}}timestamp:::date-rfc3339,jsonf:@timestamp%,%source:::jsonf:@source_host%,%msg:::json%}":syslogtag,isequal,"pg:" @{{ opsvm_ip }}:6000;ls_json

+ 1
- 1
templates/kilo/plumgrid.conf View File

@@ -1,4 +1,4 @@
1
-plumgrid_ip={{ local_ip }}
1
+plumgrid_ip={{ director_ips_string }}
2 2
 plumgrid_port=8001
3 3
 mgmt_dev={{ interface }}
4 4
 label={{ label}}

+ 10
- 7
unit_tests/test_pg_gw_context.py View File

@@ -35,14 +35,14 @@ class PGGwContextTest(CharmTestCase):
35 35
     @patch.object(charmhelpers.contrib.openstack.context,
36 36
                   'config_flags_parser')
37 37
     @patch.object(context.PGGwContext, '_save_flag_file')
38
-    @patch.object(context, '_pg_dir_settings')
38
+    @patch.object(context, '_pg_dir_context')
39 39
     @patch.object(charmhelpers.contrib.openstack.context,
40 40
                   'neutron_plugin_attribute')
41 41
     @patch.object(utils, 'get_mgmt_interface')
42 42
     @patch.object(utils, 'get_fabric_interface')
43 43
     @patch.object(utils, 'get_gw_interfaces')
44 44
     def test_neutroncc_context_api_rel(self, _gw_int, _fabric_int,
45
-                                       _mgmt_int, _npa, _pg_dir_settings,
45
+                                       _mgmt_int, _npa, _pg_dir_context,
46 46
                                        _save_flag_file, _config_flag,
47 47
                                        _unit_get, _unit_priv_ip, _config,
48 48
                                        _is_clus, _https, _ens_pkgs):
@@ -54,13 +54,14 @@ class PGGwContextTest(CharmTestCase):
54 54
 
55 55
         self.maxDiff = None
56 56
         _npa.side_effect = mock_npa
57
-        _unit_get.return_value = '192.168.100.201'
58
-        _unit_priv_ip.return_value = '192.168.100.201'
57
+        _unit_get.return_value = '192.168.100.203'
58
+        _unit_priv_ip.return_value = '192.168.100.203'
59 59
         self.gethostname.return_value = 'node0'
60 60
         self.getfqdn.return_value = 'node0'
61 61
         _is_clus.return_value = False
62 62
         _config_flag.return_value = False
63
-        _pg_dir_settings.return_value = {'pg_dir_ip': '192.168.100.201'}
63
+        _pg_dir_context.return_value = {'director_ips': ['192.168.100.201'],
64
+                                        'opsvm_ip': '127.0.0.1'}
64 65
         _mgmt_int.return_value = 'juju-br0'
65 66
         _fabric_int.return_value = 'juju-br0'
66 67
         _gw_int.return_value = ['eth1']
@@ -69,11 +70,12 @@ class PGGwContextTest(CharmTestCase):
69 70
             'ext_interfaces': ['eth1'],
70 71
             'config': 'neutron.randomconfig',
71 72
             'core_plugin': 'neutron.randomdriver',
72
-            'local_ip': 'pg_dir_ip',
73
+            'local_ip': '192.168.100.203',
74
+            'director_ips_string': '192.168.100.201',
73 75
             'network_manager': 'neutron',
74 76
             'neutron_plugin': 'plumgrid',
75 77
             'neutron_security_groups': None,
76
-            'neutron_url': 'https://192.168.100.201:9696',
78
+            'neutron_url': 'https://192.168.100.203:9696',
77 79
             'pg_hostname': 'node0',
78 80
             'pg_fqdn': 'node0',
79 81
             'interface': 'juju-br0',
@@ -81,5 +83,6 @@ class PGGwContextTest(CharmTestCase):
81 83
             'label': 'node0',
82 84
             'fabric_mode': 'host',
83 85
             'neutron_alchemy_flags': False,
86
+            'opsvm_ip': '127.0.0.1',
84 87
         }
85 88
         self.assertEquals(expect, napi_ctxt())

+ 5
- 10
unit_tests/test_pg_gw_hooks.py View File

@@ -19,7 +19,6 @@ utils.restart_map = _map
19 19
 TO_PATCH = [
20 20
     'remove_iovisor',
21 21
     'apt_install',
22
-    'apt_purge',
23 22
     'CONFIGS',
24 23
     'log',
25 24
     'configure_sources',
@@ -30,7 +29,8 @@ TO_PATCH = [
30 29
     'ensure_mtu',
31 30
     'add_lcm_key',
32 31
     'determine_packages',
33
-    'load_iptables'
32
+    'load_iptables',
33
+    'director_cluster_ready'
34 34
 ]
35 35
 NEUTRON_CONF_DIR = "/etc/neutron"
36 36
 
@@ -62,17 +62,12 @@ class PGGwHooksTests(CharmTestCase):
62 62
         self.ensure_files.assert_called_with()
63 63
         self.add_lcm_key.assert_called_with()
64 64
 
65
-    def test_plumgrid_joined(self):
66
-        self._call_hook('plumgrid-relation-joined')
65
+    def test_plumgrid_changed(self):
66
+        self._call_hook('plumgrid-relation-changed')
67
+        self.director_cluster_ready.return_value = True
67 68
         self.ensure_mtu.assert_called_with()
68
-        self.ensure_files.assert_called_with()
69
-        self.add_lcm_key.assert_called_with()
70 69
         self.CONFIGS.write_all.assert_called_with()
71
-        self.restart_pg.assert_called_with()
72 70
 
73 71
     def test_stop(self):
74
-        _pkgs = ['plumgrid-lxc', 'iovisor-dkms']
75 72
         self._call_hook('stop')
76 73
         self.stop_pg.assert_called_with()
77
-        self.remove_iovisor.assert_called_with()
78
-        self.determine_packages.return_value = _pkgs

+ 3
- 1
unit_tests/test_pg_gw_utils.py View File

@@ -53,7 +53,8 @@ class TestPGGwUtils(CharmTestCase):
53 53
         confs = [nutils.PG_CONF,
54 54
                  nutils.PG_HN_CONF,
55 55
                  nutils.PG_HS_CONF,
56
-                 nutils.PG_IFCS_CONF]
56
+                 nutils.PG_IFCS_CONF,
57
+                 nutils.OPS_CONF]
57 58
         self.assertItemsEqual(_regconfs.configs, confs)
58 59
 
59 60
     def test_resource_map(self):
@@ -69,6 +70,7 @@ class TestPGGwUtils(CharmTestCase):
69 70
             (nutils.PG_CONF, ['plumgrid']),
70 71
             (nutils.PG_HN_CONF, ['plumgrid']),
71 72
             (nutils.PG_HS_CONF, ['plumgrid']),
73
+            (nutils.OPS_CONF, ['plumgrid']),
72 74
             (nutils.PG_IFCS_CONF, []),
73 75
         ])
74 76
         self.assertEqual(expect, _restart_map)

Loading…
Cancel
Save