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

This commit is contained in:
Bilal Baqar 2016-03-25 10:33:22 -07:00
commit a8f68eae1e
3 changed files with 34 additions and 23 deletions

View File

@ -14,6 +14,8 @@ from charmhelpers.core.hookenv import (
config, config,
) )
from charmhelpers.core.host import service_running
from charmhelpers.fetch import ( from charmhelpers.fetch import (
apt_install, apt_install,
apt_purge, apt_purge,
@ -74,30 +76,32 @@ def config_changed():
This hook is run when a config parameter is changed. This hook is run when a config parameter is changed.
It also runs on node reboot. It also runs on node reboot.
''' '''
if add_lcm_key():
log("PLUMgrid LCM Key added")
return 1
charm_config = config() charm_config = config()
if charm_config.changed('lcm-ssh-key'):
if add_lcm_key():
log("PLUMgrid LCM Key added")
if charm_config.changed('fabric-interfaces'): if charm_config.changed('fabric-interfaces'):
if not fabric_interface_changed(): if not fabric_interface_changed():
log("Fabric interface already set") log("Fabric interface already set")
return 1 else:
if charm_config.changed('os-data-network'): stop_pg()
if charm_config['fabric-interfaces'] == 'MANAGEMENT': if charm_config.changed('external-interfaces'):
log('Fabric running on managment network') stop_pg()
return 1 if (charm_config.changed('install_sources') or
stop_pg() charm_config.changed('plumgrid-build') or
configure_sources(update=True) charm_config.changed('install_keys') or
pkgs = determine_packages() charm_config.changed('iovisor-build')):
for pkg in pkgs: stop_pg()
apt_install(pkg, options=['--force-yes'], fatal=True) configure_sources(update=True)
remove_iovisor() pkgs = determine_packages()
load_iovisor() for pkg in pkgs:
apt_install(pkg, options=['--force-yes'], fatal=True)
remove_iovisor()
load_iovisor()
ensure_mtu() ensure_mtu()
ensure_files()
add_lcm_key()
CONFIGS.write_all() CONFIGS.write_all()
restart_pg() if not service_running('plumgrid'):
restart_pg()
@hooks.hook('upgrade-charm') @hooks.hook('upgrade-charm')

View File

@ -30,6 +30,7 @@ from charmhelpers.core.host import (
write_file, write_file,
service_start, service_start,
service_stop, service_stop,
service_running
) )
from charmhelpers.fetch import ( from charmhelpers.fetch import (
apt_cache, apt_cache,
@ -142,7 +143,17 @@ def restart_pg():
''' '''
stop_pg() stop_pg()
service_start('plumgrid') service_start('plumgrid')
time.sleep(30) time.sleep(3)
if not service_running('plumgrid'):
if service_running('libvirt-bin'):
raise ValueError("plumgrid service couldn't be started")
else:
if service_start('libvirt-bin'):
time.sleep(3)
if not service_running('plumgrid'):
raise ValueError("plumgrid service couldn't be started")
else:
raise ValueError("libvirt-bin service couldn't be started")
def stop_pg(): def stop_pg():

View File

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