Default handler for installing deb from resources

Add handler for installing a deb which has been provided via Juju
resources in the Ceph Storage charm.

Change-Id: I11aacdc4e05cf3db0968d66515702157ea00876c
This commit is contained in:
Liam Young 2019-01-29 11:16:04 +00:00
parent db3c8fb6c2
commit fe86cec9ae
2 changed files with 74 additions and 1 deletions

View File

@ -119,6 +119,36 @@ class OpenStackCharm(BaseOpenStackCharm,
# OpenStack component.
source_config_key = 'openstack-origin'
@property
def resource_install_map(self):
"""Return map of resource names to installation methods
:returns Map of Juju resource names to installation methods
:rtype: {'resource_name': f}
"""
install_map = {
'driver-deb': self.install_deb
}
return install_map
def install_deb(self, deb):
"""Install the given deb.
:param deb: Path to deb
:type: str
"""
# No attempt is made to deal with dependancies. These should be
# handled by the charms 'packages' list.
subprocess.check_call(['dpkg', '-i', deb])
def install_resources(self):
"""Install Juju application resources
"""
for resource_name, install_func in self.resource_install_map.items():
resource = hookenv.resource_get(resource_name)
if resource:
install_func(resource)
@property
def region(self):
"""Return the OpenStack Region as contained in the config item 'region'
@ -876,15 +906,24 @@ class CinderStoragePluginCharm(OpenStackCharm):
release = ''
def install(self):
"""Install PPA if one has been defined."""
"""Install packages and resources."""
# Install PPA if one has been defined.
if self.config.get('driver-source'):
fetch.add_source(
self.config.get('driver-source'),
key=self.config.get('driver-key'))
fetch.apt_update()
super().install()
# All package install to run first incase payload has deps.
self.install_resources()
self.assess_status()
def upgrade_charm(self):
"""Run default upgrade_charm method and reinstall resources"""
super().upgrade_charm()
# A change in resources triggers an upgrade-charm
self.install_resources()
@property
def stateless(self):
raise NotImplementedError()

View File

@ -203,6 +203,35 @@ class TestOpenStackCharm(BaseOpenStackCharmTest):
self.subprocess.check_call.assert_not_called()
self.leader_set.assert_not_called()
def test_resource_install_map(self):
self.assertEqual(
self.target.resource_install_map,
{
'driver-deb': self.target.install_deb})
def test_install_deb(self):
self.patch_object(chm.subprocess, 'check_call')
self.target.install_deb('mydeb')
self.check_call.assert_called_once_with(['dpkg', '-i', 'mydeb'])
def test_install_resources(self):
self.patch_target('install_deb')
self.patch_object(
chm.hookenv,
'resource_get',
return_value='/tmp/my.deb')
self.target.install_resources()
self.install_deb.assert_called_once_with('/tmp/my.deb')
def test_install_resources_no_resources(self):
self.patch_target('install_deb')
self.patch_object(
chm.hookenv,
'resource_get',
return_value=None)
self.target.install_resources()
self.assertFalse(self.install_deb.called)
class TestMyOpenStackCharm(BaseOpenStackCharmTest):
@ -1034,14 +1063,17 @@ class TestCinderStoragePluginCharm(BaseOpenStackCharmTest):
self.patch_object(chm_core.charmhelpers.fetch, 'add_source')
self.patch_object(chm_core.charmhelpers.fetch, 'apt_update')
self.patch_target('config', new={'driver-source': 'ppa:user/ppa'})
self.patch_target('install_resources')
self.target.install()
self.add_source.assert_called_once_with('ppa:user/ppa', key=None)
self.apt_update.assert_called_once_with()
self.install_resources.assert_called_once_with()
def test_install_with_key(self):
self.patch_object(chm.subprocess, 'check_output', return_value=b'\n')
self.patch_object(chm_core.charmhelpers.fetch, 'add_source')
self.patch_object(chm_core.charmhelpers.fetch, 'apt_update')
self.patch_target('install_resources')
self.patch_target(
'config',
new={
@ -1055,6 +1087,7 @@ class TestCinderStoragePluginCharm(BaseOpenStackCharmTest):
self.patch_object(chm.subprocess, 'check_output', return_value=b'\n')
self.patch_object(chm_core.charmhelpers.fetch, 'add_source')
self.patch_object(chm_core.charmhelpers.fetch, 'apt_update')
self.patch_target('install_resources')
self.patch_target(
'config',
new={
@ -1071,6 +1104,7 @@ class TestCinderStoragePluginCharm(BaseOpenStackCharmTest):
self.patch_object(chm_core.charmhelpers.fetch, 'add_source')
self.patch_object(chm_core.charmhelpers.fetch, 'apt_update')
self.patch_target('config', new={})
self.patch_target('install_resources')
self.target.install()
self.assertFalse(self.add_source.called)
self.assertFalse(self.apt_update.called)