Merge lp:~james-page/charm-helpers/os-application-version into lp:charm-helpers

Proposed by James Page
Status: Merged
Merged at revision: 627
Proposed branch: lp:~james-page/charm-helpers/os-application-version
Merge into: lp:charm-helpers
Diff against target: 67 lines (+42/-1)
2 files modified
charmhelpers/contrib/openstack/utils.py (+28/-1)
tests/contrib/openstack/test_openstack_utils.py (+14/-0)
To merge this branch: bzr merge lp:~james-page/charm-helpers/os-application-version
Reviewer Review Type Date Requested Status
Liam Young (community) Approve
Review via email: mp+305922@code.launchpad.net

Description of the change

Add basic helper to set OpenStack application version

Juju 2.0 supports setting of the version of the application that is
deployed; add an openstack centric helper which will set version based on:

a) The provided package, if installed - e.g. 13.1.1
b) Using os_release to determing the codename of the install e.g. newton

This is designed to be called after every hook execution, including assess
status so that if the package version changes outside of charm lifecycle
events, this is also detected and the juju application version is updated.

To post a comment you must log in.
Revision history for this message
Liam Young (gnuoy) wrote :

Please consider using fetch.apt_cache it has some extra smarts. fwiw its what get_os_codename_package etc uses

review: Needs Fixing
Revision history for this message
Liam Young (gnuoy) wrote :

oh, doh. you are

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/contrib/openstack/utils.py'
2--- charmhelpers/contrib/openstack/utils.py 2016-07-20 15:44:32 +0000
3+++ charmhelpers/contrib/openstack/utils.py 2016-09-16 09:56:30 +0000
4@@ -51,7 +51,8 @@
5 relation_set,
6 service_name,
7 status_set,
8- hook_name
9+ hook_name,
10+ application_version_set,
11 )
12
13 from charmhelpers.contrib.storage.linux.lvm import (
14@@ -1889,3 +1890,29 @@
15 flags[key.strip(post_strippers)] = value.rstrip(post_strippers)
16
17 return flags
18+
19+
20+def os_application_version_set(package):
21+ '''Set version of application for Juju 2.0 and later'''
22+ import apt_pkg as apt
23+ cache = apt_cache()
24+ application_version = None
25+ application_codename = os_release(package)
26+
27+ try:
28+ pkg = cache[package]
29+ if not pkg.current_ver:
30+ juju_log('Package {} is not currently installed.'.format(package),
31+ DEBUG)
32+ else:
33+ application_version = apt.upstream_version(pkg.current_ver.ver_str)
34+ except:
35+ juju_log('Package {} has no installation candidate.'.format(package),
36+ DEBUG)
37+
38+ # NOTE(jamespage) if not able to figure out package version, fallback to
39+ # openstack codename version detection.
40+ if not application_version:
41+ application_version_set(application_codename)
42+ else:
43+ application_version_set(application_version)
44
45=== modified file 'tests/contrib/openstack/test_openstack_utils.py'
46--- tests/contrib/openstack/test_openstack_utils.py 2016-07-12 19:54:24 +0000
47+++ tests/contrib/openstack/test_openstack_utils.py 2016-09-16 09:56:30 +0000
48@@ -1788,5 +1788,19 @@
49 self.assertTrue(action_set.called)
50 self.assertTrue(traceback.called)
51
52+ @patch.object(openstack, 'os_release')
53+ @patch.object(openstack, 'application_version_set')
54+ def test_os_application_version_set(self,
55+ mock_application_version_set,
56+ mock_os_release):
57+ with patch('apt_pkg.Cache') as cache:
58+ cache.return_value = self._apt_cache()
59+ mock_os_release.return_value = 'mitaka'
60+ openstack.os_application_version_set('neutron-common')
61+ mock_application_version_set.assert_called_with('7.0.1')
62+ openstack.os_application_version_set('cinder-common')
63+ mock_application_version_set.assert_called_with('mitaka')
64+
65+
66 if __name__ == '__main__':
67 unittest.main()

Subscribers

People subscribed via source and target branches