Description:
Make charm work without environment Juju PPA
As discovered by therve and myself, the charm does not work if
"juju-origin: ppa" is not in your environments.yaml for the given Juju
environment. These changes work around the problem.
Index: hooks/bootstrap_utils.py
=== added file 'hooks/bootstrap_utils.py'
--- hooks/bootstrap_utils.py 1970-01-01 00:00:00 +0000
+++ hooks/bootstrap_utils.py 2013-01-09 18:12:09 +0000
@@ -0,0 +1,54 @@
+# These are actually maintained in python-shelltoolbox. Precise does not
have
+# that package, so we need to bootstrap the process by copying the
functions
+# we need here.
+
+import subprocess
+
+try:
+ import shelltoolbox
+except ImportError:
+ def run(*args, **kwargs):
+ """Run the command with the given arguments.
+
+ The first argument is the path to the command to run.
+ Subsequent arguments are command-line arguments to be passed.
+
+ This function accepts all optional keyword arguments accepted by
+ `subprocess.Popen`.
+ """
+ args = [i for i in args if i is not None]
+ pipe = subprocess.PIPE
+ process = subprocess.Popen(
+ args, stdout=kwargs.pop('stdout', pipe),
+ stderr=kwargs.pop('stderr', pipe),
+ close_fds=kwargs.pop('close_fds', True), **kwargs)
+ stdout, stderr = process.communicate()
+ if process.returncode:
+ exception = subprocess.CalledProcessError(
+ process.returncode, repr(args))
+ # The output argument of `CalledProcessError` was introduced
in Python
+ # 2.7. Monkey patch the output here to avoid TypeErrors in
older
+ # versions of Python, still preserving the output in Python
2.7.
+ exception.output = ''.join(filter(None, [stdout, stderr]))
+ raise exception
+ return stdout
+
+ def install_extra_repositories(*repositories):
+ """Install all of the extra repositories and update apt.
+
+ Given repositories can contain a "{distribution}" placeholder,
that will
+ be replaced by current distribution codename.
+
+ :raises: subprocess.CalledProcessError
+ """
+ distribution = run('lsb_release', '-cs').strip()
+ # Starting from Oneiric, `apt-add-repository` is interactive by
+ # default, and requires a "-y" flag to be set.
+ assume_yes = None if distribution == 'lucid' else '-y'
+ for repo in repositories:
+ repository = repo.format(distribution=distribution)
+ run('apt-add-repository', assume_yes, repository)
+ run('apt-get', 'clean')
+ run('apt-get', 'update')
+else:
+ install_extra_repositories = shelltoolbox.install_extra_repositories
+# If the user's environment has "juju-origin: ppa" set, they will
+# automatically have access to python-charmhelpers and python-shelltoolbox.
+# However, we want to support environments that use the non-PPA
environment as
+# well. To do so, we need to install the Juju PPA, which we will do with a
+# couple of functions that are actually maintained in python-shelltoolbox.
+import bootstrap_utils
+bootstrap_utils.install_extra_repositories('ppa:juju/pkgs')
+
# python-shelltoolbox is installed as a dependency of python-charmhelpers.
check_call(['apt-get', 'install', '-y', 'python-charmhelpers'])
Reviewers: mp+142568_ code.launchpad. net,
Message:
Please take a look.
Description:
Make charm work without environment Juju PPA
As discovered by therve and myself, the charm does not work if
"juju-origin: ppa" is not in your environments.yaml for the given Juju
environment. These changes work around the problem.
https:/ /code.launchpad .net/~gary/ charms/ precise/ juju-gui/ run-without- ppa/+merge/ 142568
(do not edit description out of merge proposal)
Please review this at https:/ /codereview. appspot. com/7073051/
Affected files: _utils. py
A [revision details]
A hooks/bootstrap
M hooks/install
M revision
Index: [revision details]
=== added file '[revision details]'
--- [revision details] 2012-01-01 00:00:00 +0000
+++ [revision details] 2012-01-01 00:00:00 +0000
@@ -0,0 +1,2 @@
+Old revision: <email address hidden>
+New revision: <email address hidden>
Index: revision
=== modified file 'revision'
--- revision 2013-01-03 20:16:28 +0000
+++ revision 2013-01-09 18:12:09 +0000
@@ -1,1 +1,1 @@
-18
+19
Index: hooks/bootstrap _utils. py bootstrap_ utils.py' _utils. py 1970-01-01 00:00:00 +0000 _utils. py 2013-01-09 18:12:09 +0000 shelltoolbox. Precise does not kwargs. pop('stdout' , pipe), kwargs. pop('stderr' , pipe), kwargs. pop('close_ fds', True), **kwargs) communicate( ) CalledProcessEr ror( rror` was introduced filter( None, [stdout, stderr])) extra_repositor ies(*repositori es): CalledProcessEr ror repository` is interactive by distribution= distribution) add-repository' , assume_yes, repository) extra_repositor ies = shelltoolbox. install_ extra_repositor ies
=== added file 'hooks/
--- hooks/bootstrap
+++ hooks/bootstrap
@@ -0,0 +1,54 @@
+# These are actually maintained in python-
have
+# that package, so we need to bootstrap the process by copying the
functions
+# we need here.
+
+import subprocess
+
+try:
+ import shelltoolbox
+except ImportError:
+ def run(*args, **kwargs):
+ """Run the command with the given arguments.
+
+ The first argument is the path to the command to run.
+ Subsequent arguments are command-line arguments to be passed.
+
+ This function accepts all optional keyword arguments accepted by
+ `subprocess.Popen`.
+ """
+ args = [i for i in args if i is not None]
+ pipe = subprocess.PIPE
+ process = subprocess.Popen(
+ args, stdout=
+ stderr=
+ close_fds=
+ stdout, stderr = process.
+ if process.returncode:
+ exception = subprocess.
+ process.returncode, repr(args))
+ # The output argument of `CalledProcessE
in Python
+ # 2.7. Monkey patch the output here to avoid TypeErrors in
older
+ # versions of Python, still preserving the output in Python
2.7.
+ exception.output = ''.join(
+ raise exception
+ return stdout
+
+ def install_
+ """Install all of the extra repositories and update apt.
+
+ Given repositories can contain a "{distribution}" placeholder,
that will
+ be replaced by current distribution codename.
+
+ :raises: subprocess.
+ """
+ distribution = run('lsb_release', '-cs').strip()
+ # Starting from Oneiric, `apt-add-
+ # default, and requires a "-y" flag to be set.
+ assume_yes = None if distribution == 'lucid' else '-y'
+ for repo in repositories:
+ repository = repo.format(
+ run('apt-
+ run('apt-get', 'clean')
+ run('apt-get', 'update')
+else:
+ install_
Index: hooks/install
=== modified file 'hooks/install'
--- hooks/install 2012-12-21 15:09:47 +0000
+++ hooks/install 2013-01-09 18:12:09 +0000
@@ -6,6 +6,14 @@
check_call,
)
+# If the user's environment has "juju-origin: ppa" set, they will shelltoolbox. shelltoolbox. utils.install_ extra_repositor ies('ppa: juju/pkgs' ) charmhelpers. call([' apt-get' , 'install', '-y', 'python- charmhelpers' ])
+# automatically have access to python-charmhelpers and python-
+# However, we want to support environments that use the non-PPA
environment as
+# well. To do so, we need to install the Juju PPA, which we will do with a
+# couple of functions that are actually maintained in python-
+import bootstrap_utils
+bootstrap_
+
# python-shelltoolbox is installed as a dependency of python-
check_