Merge lp:~lazypower/charm-helpers/add-workload-version into lp:charm-helpers

Proposed by Charles Butler
Status: Merged
Merged at revision: 622
Proposed branch: lp:~lazypower/charm-helpers/add-workload-version
Merge into: lp:charm-helpers
Diff against target: 40 lines (+19/-0)
2 files modified
charmhelpers/core/hookenv.py (+14/-0)
tests/core/test_hookenv.py (+5/-0)
To merge this branch: bzr merge lp:~lazypower/charm-helpers/add-workload-version
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
charmers Pending
Review via email: mp+305062@code.launchpad.net

Description of the change

Adds:

- application-version-set hookenv command
- Test coverage to support the added code

To post a comment you must log in.
Revision history for this message
Stuart Bishop (stub) wrote :

I'd rather see this do nothing than fail if it is called from 1.25. It currently raises a not implemented error, which will bite people maintaining charms that need to support old Juju. Unlike other tools like leader-set, I think this is more like status_set where it is better to just make do (in this case, do nothing). I'll let this approach through though, as it is somewhat a matter of opinion. But my personal opinion is I'd rather not have to guard all my calls in the PostgreSQL charm and have charm-helpers DWIM if it happens to be running with Juju 1.25.

I personally hate the @translate_exc approach, since it masks lots of other errors than 'not running a recent enough version of juju'. I much prefer "if has_juju_version('2.0')", even though group think is that you should do it this way with all its caveats and hidden bugs rather than to concisely state that this is a Juju 2.0 feature. But I won't block review on this point since someone else has gone to the trouble of rewriting things using the less readable and more error prone approach :-/

review: Approve
Revision history for this message
Stuart Bishop (stub) wrote :

Per irc discussion.

I have charms that support both 1.25 and 2.0
If I want to call application_version_set, with what you have here I need to do:

try:
    application_version_set(ver)
except NotImplementedError:
    pass

I think charmhelpers should do that, so in the PostgreSQL charm I can just do 'application_version_set(ver)' and it works under 2.0 and does nothing under 1.25

Ignore my rant about version sniffing, since this way is consistent with the rest of the code base (someone has 'fixed' the counter examples).

review: Needs Fixing
Revision history for this message
Charles Butler (lazypower) wrote :

I'm not against that proposal stub. Just to be precise, if we go through to modify the other methods, we're looking at changing the behavior of:

- leader_set
- leader_get
- is_leader
- payload_register
- payload_unregister
- payload_status_set
- resource_get
- network_get_primary_address

We should probably file an issue and work through that elsewhere, but I don't know that we can reasonably change the behavior of charm-helpers.core.hookenv at this point as it will ripple throughout every charm that uses charm-helpers, and that's most if not all of them.

623. By Charles Butler

Refactor the application-version-set stanza to only log if not on a 2.0+ host.

Revision history for this message
Stuart Bishop (stub) wrote :

I'm not suggesting changing the behaviour of any other functions. Calling any of the ones you cite from an old version of Juju should most certainly fail, because charms calling them are relying on their behavour and effects. application_version_set() is like status_set(), where we can provide fall back behaviour for old versions of Juju.

Revision history for this message
Stuart Bishop (stub) wrote :

This looks good. I'll land it.

review: Approve
Revision history for this message
Charles Butler (lazypower) wrote :

Ok, I misunderstood the context of your reply, but seems like we got there anyway! \o/ Thanks stub.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/core/hookenv.py'
2--- charmhelpers/core/hookenv.py 2016-07-06 14:41:05 +0000
3+++ charmhelpers/core/hookenv.py 2016-09-07 16:37:52 +0000
4@@ -843,6 +843,20 @@
5 return inner_translate_exc1
6
7
8+def application_version_set(version):
9+ """Charm authors may trigger this command from any hook to output what
10+ version of the application is running. This could be a package version,
11+ for instance postgres version 9.5. It could also be a build number or
12+ version control revision identifier, for instance git sha 6fb7ba68. """
13+
14+ cmd = ['application-version-set']
15+ cmd.append(version)
16+ try:
17+ subprocess.check_call(cmd)
18+ except OSError:
19+ log("Application Version: {}".format(version))
20+
21+
22 @translate_exc(from_exc=OSError, to_exc=NotImplementedError)
23 def is_leader():
24 """Does the current unit hold the juju leadership
25
26=== modified file 'tests/core/test_hookenv.py'
27--- tests/core/test_hookenv.py 2016-06-23 07:10:31 +0000
28+++ tests/core/test_hookenv.py 2016-09-07 16:37:52 +0000
29@@ -1204,6 +1204,11 @@
30 check_call_.assert_called_with(['payload-status-set', 'monitoring',
31 'abc123', 'Running'])
32
33+ @patch('subprocess.check_call')
34+ def test_application_version_set(self, check_call_):
35+ hookenv.application_version_set('v1.2.3')
36+ check_call_.assert_called_with(['application-version-set', 'v1.2.3'])
37+
38
39 class HooksTest(TestCase):
40 def setUp(self):

Subscribers

People subscribed via source and target branches