Merge lp:~mattyw/charm-tools/legacy_juju_call into lp:~charmers/charm-tools/trunk

Proposed by Matthew Williams
Status: Merged
Merged at revision: 179
Proposed branch: lp:~mattyw/charm-tools/legacy_juju_call
Merge into: lp:~charmers/charm-tools/trunk
Diff against target: 103 lines (+57/-0)
2 files modified
helpers/python/charmhelpers/__init__.py (+15/-0)
helpers/python/charmhelpers/tests/test_charmhelpers.py (+42/-0)
To merge this branch: bzr merge lp:~mattyw/charm-tools/legacy_juju_call
Reviewer Review Type Date Requested Status
charmers Pending
Review via email: mp+161087@code.launchpad.net

Commit message

Added legacy_juju function with tests

Description of the change

A few charms I'm working on need to know if they're running under juju (python) or juju-core. This functionality is also needed in the juju-gui charm https://bazaar.launchpad.net/~juju-gui/charms/precise/juju-gui/trunk/view/head:/hooks/utils.py line 103.

It seems to me that there are a number of ways to work out which version of juju you're working under. But there should only be one correct way which should be documented (and implemented) somewhere common. I thought putting something here might be quite useful

To post a comment you must log in.
Revision history for this message
Kapil Thangavelu (hazmat) wrote :
Download full text (3.8 KiB)

Please use CHARM_DIR env var explicitly instead of assuming the hook hasn't
change the working dir.

On Fri, Apr 26, 2013 at 6:35 AM, Matthew Williams <
<email address hidden>> wrote:

> Matthew Williams has proposed merging
> lp:~mattyw/charm-tools/legacy_juju_call into lp:charm-tools.
>
> Commit message:
> Added legacy_juju function with tests
>
> Requested reviews:
> charmers (charmers)
>
> For more details, see:
>
> https://code.launchpad.net/~mattyw/charm-tools/legacy_juju_call/+merge/161087
>
> A few charms I'm working on need to know if they're running under juju
> (python) or juju-core. This functionality is also needed in the juju-gui
> charm
> https://bazaar.launchpad.net/~juju-gui/charms/precise/juju-gui/trunk/view/head:/hooks/utils.pyline 103.
>
> It seems to me that there are a number of ways to work out which version
> of juju you're working under. But there should only be one correct way
> which should be documented (and implemented) somewhere common. I thought
> putting something here might be quite useful
> --
>
> https://code.launchpad.net/~mattyw/charm-tools/legacy_juju_call/+merge/161087
> You are subscribed to branch lp:charm-tools.
>
> === modified file 'helpers/python/charmhelpers/__init__.py'
> --- helpers/python/charmhelpers/__init__.py 2013-04-02 21:30:49 +0000
> +++ helpers/python/charmhelpers/__init__.py 2013-04-26 10:34:24 +0000
> @@ -22,6 +22,7 @@
> 'wait_for_page_contents',
> 'wait_for_relation',
> 'wait_for_unit',
> + 'legacy_juju',
> ]
>
> from collections import namedtuple
> @@ -37,6 +38,7 @@
> import urllib2
> import yaml
> from subprocess import CalledProcessError
> +import os
>
>
> SLEEP_AMOUNT = 0.1
> @@ -275,3 +277,8 @@
> if time.time() - start_time >= timeout:
> raise RuntimeError('timeout waiting for contents of ' + url)
> time.sleep(SLEEP_AMOUNT)
> +
> +
> +def legacy_juju():
> + """Return True if the charm was deployed using python juju."""
> + return not os.path.exists(os.path.join(os.getcwd(), '..',
> 'agent.conf'))
>
> === modified file 'helpers/python/charmhelpers/tests/test_charmhelpers.py'
> --- helpers/python/charmhelpers/tests/test_charmhelpers.py 2013-04-02
> 21:30:49 +0000
> +++ helpers/python/charmhelpers/tests/test_charmhelpers.py 2013-04-26
> 10:34:24 +0000
> @@ -8,6 +8,7 @@
> from testtools import TestCase
>
> import sys
> +import os
> # Path hack to ensure we test the local code, not a version installed in
> # /usr/local/lib. This is necessary since /usr/local/lib is prepended
> before
> # what is specified in PYTHONPATH.
> @@ -470,6 +471,25 @@
> charmhelpers.log_exit()
> self.assertEqual(['<-- Exiting SCRIPT-NAME'], logged)
>
> + def test_not_legacy_juju(self):
> + # The Go version of juju install an agent.conf file in the
> directory
> + # above the charm. If that file exists then we must be running
> against
> + # the go version of juju
> + agent_file_path = "%s/../agent.conf" % os.getcwd()
> + with open(agent_file_path, 'w') as agent_file:
> + agent_file.write("")
> + self.ass...

Read more...

Revision history for this message
Matthew Williams (mattyw) wrote :
Download full text (4.1 KiB)

That's a much better idea thanks, should I defult to the working dir if CHARM_DIR hasn't been set?

> Please use CHARM_DIR env var explicitly instead of assuming the hook hasn't
> change the working dir.
>
>
> On Fri, Apr 26, 2013 at 6:35 AM, Matthew Williams <
> <email address hidden>> wrote:
>
> > Matthew Williams has proposed merging
> > lp:~mattyw/charm-tools/legacy_juju_call into lp:charm-tools.
> >
> > Commit message:
> > Added legacy_juju function with tests
> >
> > Requested reviews:
> > charmers (charmers)
> >
> > For more details, see:
> >
> > https://code.launchpad.net/~mattyw/charm-
> tools/legacy_juju_call/+merge/161087
> >
> > A few charms I'm working on need to know if they're running under juju
> > (python) or juju-core. This functionality is also needed in the juju-gui
> > charm
> > https://bazaar.launchpad.net/~juju-gui/charms/precise/juju-
> gui/trunk/view/head:/hooks/utils.pyline 103.
> >
> > It seems to me that there are a number of ways to work out which version
> > of juju you're working under. But there should only be one correct way
> > which should be documented (and implemented) somewhere common. I thought
> > putting something here might be quite useful
> > --
> >
> > https://code.launchpad.net/~mattyw/charm-
> tools/legacy_juju_call/+merge/161087
> > You are subscribed to branch lp:charm-tools.
> >
> > === modified file 'helpers/python/charmhelpers/__init__.py'
> > --- helpers/python/charmhelpers/__init__.py 2013-04-02 21:30:49 +0000
> > +++ helpers/python/charmhelpers/__init__.py 2013-04-26 10:34:24 +0000
> > @@ -22,6 +22,7 @@
> > 'wait_for_page_contents',
> > 'wait_for_relation',
> > 'wait_for_unit',
> > + 'legacy_juju',
> > ]
> >
> > from collections import namedtuple
> > @@ -37,6 +38,7 @@
> > import urllib2
> > import yaml
> > from subprocess import CalledProcessError
> > +import os
> >
> >
> > SLEEP_AMOUNT = 0.1
> > @@ -275,3 +277,8 @@
> > if time.time() - start_time >= timeout:
> > raise RuntimeError('timeout waiting for contents of ' + url)
> > time.sleep(SLEEP_AMOUNT)
> > +
> > +
> > +def legacy_juju():
> > + """Return True if the charm was deployed using python juju."""
> > + return not os.path.exists(os.path.join(os.getcwd(), '..',
> > 'agent.conf'))
> >
> > === modified file 'helpers/python/charmhelpers/tests/test_charmhelpers.py'
> > --- helpers/python/charmhelpers/tests/test_charmhelpers.py 2013-04-02
> > 21:30:49 +0000
> > +++ helpers/python/charmhelpers/tests/test_charmhelpers.py 2013-04-26
> > 10:34:24 +0000
> > @@ -8,6 +8,7 @@
> > from testtools import TestCase
> >
> > import sys
> > +import os
> > # Path hack to ensure we test the local code, not a version installed in
> > # /usr/local/lib. This is necessary since /usr/local/lib is prepended
> > before
> > # what is specified in PYTHONPATH.
> > @@ -470,6 +471,25 @@
> > charmhelpers.log_exit()
> > self.assertEqual(['<-- Exiting SCRIPT-NAME'], logged)
> >
> > + def test_not_legacy_juju(self):
> > + # The Go version of juju install an agent.conf file in the
> > directory
> > + # a...

Read more...

Revision history for this message
Kapil Thangavelu (hazmat) wrote :
Download full text (4.8 KiB)

charm dir is set in any hook, i'd consider raising an exception if its not
set. working dir feels very implicit. ie if in an unexpected place refuse
the temptation to guess. alternatively work out detection that's against an
absolute path.

On Fri, Apr 26, 2013 at 8:50 AM, Matthew Williams <
<email address hidden>> wrote:

> That's a much better idea thanks, should I defult to the working dir if
> CHARM_DIR hasn't been set?
>
> > Please use CHARM_DIR env var explicitly instead of assuming the hook
> hasn't
> > change the working dir.
> >
> >
> > On Fri, Apr 26, 2013 at 6:35 AM, Matthew Williams <
> > <email address hidden>> wrote:
> >
> > > Matthew Williams has proposed merging
> > > lp:~mattyw/charm-tools/legacy_juju_call into lp:charm-tools.
> > >
> > > Commit message:
> > > Added legacy_juju function with tests
> > >
> > > Requested reviews:
> > > charmers (charmers)
> > >
> > > For more details, see:
> > >
> > > https://code.launchpad.net/~mattyw/charm-
> > tools/legacy_juju_call/+merge/161087
> > >
> > > A few charms I'm working on need to know if they're running under juju
> > > (python) or juju-core. This functionality is also needed in the
> juju-gui
> > > charm
> > > https://bazaar.launchpad.net/~juju-gui/charms/precise/juju-
> > gui/trunk/view/head:/hooks/utils.pyline 103.
> > >
> > > It seems to me that there are a number of ways to work out which
> version
> > > of juju you're working under. But there should only be one correct way
> > > which should be documented (and implemented) somewhere common. I
> thought
> > > putting something here might be quite useful
> > > --
> > >
> > > https://code.launchpad.net/~mattyw/charm-
> > tools/legacy_juju_call/+merge/161087
> > > You are subscribed to branch lp:charm-tools.
> > >
> > > === modified file 'helpers/python/charmhelpers/__init__.py'
> > > --- helpers/python/charmhelpers/__init__.py 2013-04-02 21:30:49
> +0000
> > > +++ helpers/python/charmhelpers/__init__.py 2013-04-26 10:34:24
> +0000
> > > @@ -22,6 +22,7 @@
> > > 'wait_for_page_contents',
> > > 'wait_for_relation',
> > > 'wait_for_unit',
> > > + 'legacy_juju',
> > > ]
> > >
> > > from collections import namedtuple
> > > @@ -37,6 +38,7 @@
> > > import urllib2
> > > import yaml
> > > from subprocess import CalledProcessError
> > > +import os
> > >
> > >
> > > SLEEP_AMOUNT = 0.1
> > > @@ -275,3 +277,8 @@
> > > if time.time() - start_time >= timeout:
> > > raise RuntimeError('timeout waiting for contents of ' +
> url)
> > > time.sleep(SLEEP_AMOUNT)
> > > +
> > > +
> > > +def legacy_juju():
> > > + """Return True if the charm was deployed using python juju."""
> > > + return not os.path.exists(os.path.join(os.getcwd(), '..',
> > > 'agent.conf'))
> > >
> > > === modified file
> 'helpers/python/charmhelpers/tests/test_charmhelpers.py'
> > > --- helpers/python/charmhelpers/tests/test_charmhelpers.py
> 2013-04-02
> > > 21:30:49 +0000
> > > +++ helpers/python/charmhelpers/tests/test_charmhelpers.py
> 2013-04-26
> > > 10:34:24 +0000
> > > @@ -8,6 +8,7 @@
> > > from testtools import TestCase
> > >
> > > import ...

Read more...

Revision history for this message
Matthew Williams (mattyw) wrote :
Download full text (5.3 KiB)

I'll submit a version raising an exception. The absolute path approach does sound tempting. But it appears that checking the contents of /var/lib/juju/ is another indirect way of checking what version you have (if the agents is present juju-core else legacy)

> charm dir is set in any hook, i'd consider raising an exception if its not
> set. working dir feels very implicit. ie if in an unexpected place refuse
> the temptation to guess. alternatively work out detection that's against an
> absolute path.
>
>
> On Fri, Apr 26, 2013 at 8:50 AM, Matthew Williams <
> <email address hidden>> wrote:
>
> > That's a much better idea thanks, should I defult to the working dir if
> > CHARM_DIR hasn't been set?
> >
> > > Please use CHARM_DIR env var explicitly instead of assuming the hook
> > hasn't
> > > change the working dir.
> > >
> > >
> > > On Fri, Apr 26, 2013 at 6:35 AM, Matthew Williams <
> > > <email address hidden>> wrote:
> > >
> > > > Matthew Williams has proposed merging
> > > > lp:~mattyw/charm-tools/legacy_juju_call into lp:charm-tools.
> > > >
> > > > Commit message:
> > > > Added legacy_juju function with tests
> > > >
> > > > Requested reviews:
> > > > charmers (charmers)
> > > >
> > > > For more details, see:
> > > >
> > > > https://code.launchpad.net/~mattyw/charm-
> > > tools/legacy_juju_call/+merge/161087
> > > >
> > > > A few charms I'm working on need to know if they're running under juju
> > > > (python) or juju-core. This functionality is also needed in the
> > juju-gui
> > > > charm
> > > > https://bazaar.launchpad.net/~juju-gui/charms/precise/juju-
> > > gui/trunk/view/head:/hooks/utils.pyline 103.
> > > >
> > > > It seems to me that there are a number of ways to work out which
> > version
> > > > of juju you're working under. But there should only be one correct way
> > > > which should be documented (and implemented) somewhere common. I
> > thought
> > > > putting something here might be quite useful
> > > > --
> > > >
> > > > https://code.launchpad.net/~mattyw/charm-
> > > tools/legacy_juju_call/+merge/161087
> > > > You are subscribed to branch lp:charm-tools.
> > > >
> > > > === modified file 'helpers/python/charmhelpers/__init__.py'
> > > > --- helpers/python/charmhelpers/__init__.py 2013-04-02 21:30:49
> > +0000
> > > > +++ helpers/python/charmhelpers/__init__.py 2013-04-26 10:34:24
> > +0000
> > > > @@ -22,6 +22,7 @@
> > > > 'wait_for_page_contents',
> > > > 'wait_for_relation',
> > > > 'wait_for_unit',
> > > > + 'legacy_juju',
> > > > ]
> > > >
> > > > from collections import namedtuple
> > > > @@ -37,6 +38,7 @@
> > > > import urllib2
> > > > import yaml
> > > > from subprocess import CalledProcessError
> > > > +import os
> > > >
> > > >
> > > > SLEEP_AMOUNT = 0.1
> > > > @@ -275,3 +277,8 @@
> > > > if time.time() - start_time >= timeout:
> > > > raise RuntimeError('timeout waiting for contents of ' +
> > url)
> > > > time.sleep(SLEEP_AMOUNT)
> > > > +
> > > > +
> > > > +def legacy_juju():
> > > > + """Return True if the charm was deployed using python juju."""
> > > > + return not os.path.e...

Read more...

179. By Matthew Williams

get charm directory from env variable, if it isn't set raise an exception

Revision history for this message
Matthew Williams (mattyw) wrote :

Does this look better?

Revision history for this message
Marco Ceppi (marcoceppi) wrote :

Hey Matty, sorry for the delay in response on this. This looks good per Kapil's original feedback, I'm going to merge it here just, however, as we're moving these charmhelpers to the charm-helpers project could you also patch lp:charm-helpers/charmhelpers/contrib/charmhelpers/__init__.py and lp:charm-helpers/tests/contrib/charmhelpers/test_charmhelpers.py ? That'll make sure these make it in to the new project for review.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'helpers/python/charmhelpers/__init__.py'
2--- helpers/python/charmhelpers/__init__.py 2013-04-02 21:30:49 +0000
3+++ helpers/python/charmhelpers/__init__.py 2013-04-26 16:11:39 +0000
4@@ -22,6 +22,7 @@
5 'wait_for_page_contents',
6 'wait_for_relation',
7 'wait_for_unit',
8+ 'legacy_juju',
9 ]
10
11 from collections import namedtuple
12@@ -37,6 +38,7 @@
13 import urllib2
14 import yaml
15 from subprocess import CalledProcessError
16+import os
17
18
19 SLEEP_AMOUNT = 0.1
20@@ -46,6 +48,10 @@
21 juju_status = lambda: command('juju')('status')
22
23
24+class CantDetermineJujuVersionException(Exception):
25+ """ Raised when we can't tell what version of juju we're running under."""
26+
27+
28 def log(message, juju_log=command('juju-log')):
29 return juju_log('--', message)
30
31@@ -275,3 +281,12 @@
32 if time.time() - start_time >= timeout:
33 raise RuntimeError('timeout waiting for contents of ' + url)
34 time.sleep(SLEEP_AMOUNT)
35+
36+
37+def legacy_juju():
38+ """Return True if the charm was deployed using python juju."""
39+ cwd = os.getenv("CHARM_DIR")
40+ if cwd == None:
41+ raise CantDetermineJujuVersionException(
42+ "CHARM_DIR not set, can't determine juju version")
43+ return not os.path.exists(os.path.join(os.getcwd(), '..', 'agent.conf'))
44
45=== modified file 'helpers/python/charmhelpers/tests/test_charmhelpers.py'
46--- helpers/python/charmhelpers/tests/test_charmhelpers.py 2013-04-02 21:30:49 +0000
47+++ helpers/python/charmhelpers/tests/test_charmhelpers.py 2013-04-26 16:11:39 +0000
48@@ -8,6 +8,7 @@
49 from testtools import TestCase
50
51 import sys
52+import os
53 # Path hack to ensure we test the local code, not a version installed in
54 # /usr/local/lib. This is necessary since /usr/local/lib is prepended before
55 # what is specified in PYTHONPATH.
56@@ -470,6 +471,47 @@
57 charmhelpers.log_exit()
58 self.assertEqual(['<-- Exiting SCRIPT-NAME'], logged)
59
60+ def test_not_legacy_juju(self):
61+ # The Go version of juju install an agent.conf file in the directory
62+ # above the charm. If that file exists then we must be running against
63+ # the go version of juju
64+ os.environ['CHARM_DIR'] = os.getcwd()
65+
66+ def cleanup():
67+ del os.environ["CHARM_DIR"]
68+ self.addCleanup(cleanup)
69+
70+ agent_file_path = "%s/../agent.conf" % os.getcwd()
71+ with open(agent_file_path, 'w') as agent_file:
72+ agent_file.write("")
73+
74+ self.assertEqual(False, charmhelpers.legacy_juju())
75+ os.remove(agent_file_path)
76+ self.assertEqual(False, os.path.exists(agent_file_path))
77+
78+ def test_legacy_juju(self):
79+ # The Go version of juju install an agent.conf file in the directory
80+ # above the charm. If that file doesn't exist then we must be running
81+ # against the legacy version of juju
82+ os.environ['CHARM_DIR'] = os.getcwd()
83+
84+ def cleanup():
85+ del os.environ["CHARM_DIR"]
86+ self.addCleanup(cleanup)
87+
88+ agent_file_exists = os.path.exists("%s/../agent.conf" % os.getcwd())
89+ self.assertEqual(False, agent_file_exists)
90+ self.assertEqual(True, charmhelpers.legacy_juju())
91+
92+ def test_legacy_raises_exception(self):
93+ # The Go version of juju install an agent.conf file in the directory
94+ # above the charm. If the env variable CHARM_DIR isn't set we can't
95+ # reasonably work out which version of juju we're running
96+ self.assertRaises(KeyError, lambda: os.environ["CHARM_DIR"])
97+ self.assertRaises(
98+ charmhelpers.CantDetermineJujuVersionException,
99+ charmhelpers.legacy_juju)
100+
101
102 if __name__ == '__main__':
103 unittest.main()

Subscribers

People subscribed via source and target branches