Browse Source

Improved config-changed hook to perform steps according to the config changed

Bilal Baqar 3 years ago
parent
commit
a8f68eae1e
3 changed files with 34 additions and 23 deletions
  1. 22
    18
      hooks/pg_gw_hooks.py
  2. 12
    1
      hooks/pg_gw_utils.py
  3. 0
    4
      unit_tests/test_pg_gw_hooks.py

+ 22
- 18
hooks/pg_gw_hooks.py View File

@@ -14,6 +14,8 @@ from charmhelpers.core.hookenv import (
14 14
     config,
15 15
 )
16 16
 
17
+from charmhelpers.core.host import service_running
18
+
17 19
 from charmhelpers.fetch import (
18 20
     apt_install,
19 21
     apt_purge,
@@ -74,30 +76,32 @@ def config_changed():
74 76
     This hook is run when a config parameter is changed.
75 77
     It also runs on node reboot.
76 78
     '''
77
-    if add_lcm_key():
78
-        log("PLUMgrid LCM Key added")
79
-        return 1
80 79
     charm_config = config()
80
+    if charm_config.changed('lcm-ssh-key'):
81
+        if add_lcm_key():
82
+            log("PLUMgrid LCM Key added")
81 83
     if charm_config.changed('fabric-interfaces'):
82 84
         if not fabric_interface_changed():
83 85
             log("Fabric interface already set")
84
-            return 1
85
-    if charm_config.changed('os-data-network'):
86
-        if charm_config['fabric-interfaces'] == 'MANAGEMENT':
87
-            log('Fabric running on managment network')
88
-            return 1
89
-    stop_pg()
90
-    configure_sources(update=True)
91
-    pkgs = determine_packages()
92
-    for pkg in pkgs:
93
-        apt_install(pkg, options=['--force-yes'], fatal=True)
94
-    remove_iovisor()
95
-    load_iovisor()
86
+        else:
87
+            stop_pg()
88
+    if charm_config.changed('external-interfaces'):
89
+        stop_pg()
90
+    if (charm_config.changed('install_sources') or
91
+        charm_config.changed('plumgrid-build') or
92
+        charm_config.changed('install_keys') or
93
+            charm_config.changed('iovisor-build')):
94
+        stop_pg()
95
+        configure_sources(update=True)
96
+        pkgs = determine_packages()
97
+        for pkg in pkgs:
98
+            apt_install(pkg, options=['--force-yes'], fatal=True)
99
+            remove_iovisor()
100
+            load_iovisor()
96 101
     ensure_mtu()
97
-    ensure_files()
98
-    add_lcm_key()
99 102
     CONFIGS.write_all()
100
-    restart_pg()
103
+    if not service_running('plumgrid'):
104
+        restart_pg()
101 105
 
102 106
 
103 107
 @hooks.hook('upgrade-charm')

+ 12
- 1
hooks/pg_gw_utils.py View File

@@ -30,6 +30,7 @@ from charmhelpers.core.host import (
30 30
     write_file,
31 31
     service_start,
32 32
     service_stop,
33
+    service_running
33 34
 )
34 35
 from charmhelpers.fetch import (
35 36
     apt_cache,
@@ -142,7 +143,17 @@ def restart_pg():
142 143
     '''
143 144
     stop_pg()
144 145
     service_start('plumgrid')
145
-    time.sleep(30)
146
+    time.sleep(3)
147
+    if not service_running('plumgrid'):
148
+        if service_running('libvirt-bin'):
149
+            raise ValueError("plumgrid service couldn't be started")
150
+        else:
151
+            if service_start('libvirt-bin'):
152
+                time.sleep(3)
153
+                if not service_running('plumgrid'):
154
+                    raise ValueError("plumgrid service couldn't be started")
155
+            else:
156
+                raise ValueError("libvirt-bin service couldn't be started")
146 157
 
147 158
 
148 159
 def stop_pg():

+ 0
- 4
unit_tests/test_pg_gw_hooks.py View File

@@ -70,10 +70,6 @@ class PGGwHooksTests(CharmTestCase):
70 70
         self.CONFIGS.write_all.assert_called_with()
71 71
         self.restart_pg.assert_called_with()
72 72
 
73
-    def test_config_changed_hook(self):
74
-        self.add_lcm_key.return_value = 1
75
-        self._call_hook('config-changed')
76
-
77 73
     def test_stop(self):
78 74
         _pkgs = ['plumgrid-lxc', 'iovisor-dkms']
79 75
         self._call_hook('stop')

Loading…
Cancel
Save