Merge lp:~tribaal/charms/trusty/landscape-client/put-install-hook-in-its-own-file into lp:charms/trusty/landscape-client

Proposed by Chris Glass
Status: Merged
Merged at revision: 43
Proposed branch: lp:~tribaal/charms/trusty/landscape-client/put-install-hook-in-its-own-file
Merge into: lp:charms/trusty/landscape-client
Diff against target: 197 lines (+90/-71)
2 files modified
hooks/hooks.py (+0/-71)
hooks/install.py (+90/-0)
To merge this branch: bzr merge lp:~tribaal/charms/trusty/landscape-client/put-install-hook-in-its-own-file
Reviewer Review Type Date Requested Status
Adam Collard Approve
Fernando Correa Neto (community) Approve
Review via email: mp+223709@code.launchpad.net

Description of the change

This branch takes the install hook out to its own file, to prevent the install hook from depending on the landscape client code (that should by definition not be installed).

It did not create a problem so far because by default the cloud images have the landscape-common package installed. It will be a problem if the client's API changes in the future.

To post a comment you must log in.
Revision history for this message
Fernando Correa Neto (fcorrea) wrote :

Nice! Still works +1

review: Approve
Revision history for this message
Adam Collard (adam-collard) wrote :

LGTM. +1 (sorry forgot to review this)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'hooks/hooks.py'
--- hooks/hooks.py 2014-06-13 15:43:58 +0000
+++ hooks/hooks.py 2014-06-19 10:48:12 +0000
@@ -4,13 +4,9 @@
4import socket4import socket
5import os5import os
6import base646import base64
7import subprocess
87
9from charmhelpers.core.hookenv import (8from charmhelpers.core.hookenv import (
10 Hooks, UnregisteredHookError, log)9 Hooks, UnregisteredHookError, log)
11from charmhelpers.fetch import (
12 apt_install, _run_apt_command, add_source, apt_update)
13from charmhelpers.core.hookenv import config
14from shutil import rmtree10from shutil import rmtree
15from subprocess import CalledProcessError11from subprocess import CalledProcessError
1612
@@ -29,31 +25,6 @@
29hooks = Hooks()25hooks = Hooks()
3026
3127
32@hooks.hook("install")
33def install():
34 charm_config = config()
35 apt_install(["apt-transport-https", "wget"])
36
37 origin = charm_config.get("origin")
38 if origin == "distro":
39 origin = None
40
41 if origin is not None:
42 add_apt_source(origin)
43
44 apt_update()
45 apt_install(["landscape-client"])
46 data_path = charm_config.get("data-path")
47 if not data_path:
48 data_path = "/var/lib/landscape/client"
49
50 result = subprocess.call(["landscape-config", "--init", "-d", data_path])
51 if result != 0:
52 result = subprocess.check_call([
53 "install", "-o", "landscape", "-g", "root", "-m", "755", "-d",
54 data_path])
55
56
57@hooks.hook("registration-relation-joined", "registration-relation-changed")28@hooks.hook("registration-relation-joined", "registration-relation-changed")
58def registration_relation(juju_broker=JUJU_BROKER,29def registration_relation(juju_broker=JUJU_BROKER,
59 landscape_broker=LANDSCAPE_BROKER):30 landscape_broker=LANDSCAPE_BROKER):
@@ -228,48 +199,6 @@
228 return 0199 return 0
229200
230201
231def build_from_launchpad(url):
232 """The charm will install the code from the passed lp branch.
233 """
234 apt_install(["devscripts", "bzr", "pbuilder"], fatal=True)
235 subprocess.check_call(["rm", "-rf", "landscape-client-source"])
236 subprocess.check_call(["bzr", "branch", url, "landscape-client-source"])
237 os.chdir("landscape-client-source")
238 subprocess.check_call("/usr/lib/pbuilder/pbuilder-satisfydepends")
239 env = {"DEBUILD_OPTS": "-uc -us"}
240 subprocess.check_call(["make", "package"], env=env)
241 #TODO: The following call should be retried (potential race condition to
242 # acquire the dpkg lock).
243 subprocess.call(["dpkg", "-i", "../landscape-client_*.deb",
244 "../landscape-common_*.deb"])
245 # The _run_apt_command will ensure the command is retried in case we cannot
246 # acquire the lock for some reason.
247 _run_apt_command(["apt-get", "-f", "install"], fatal=True)
248 os.chdir("..")
249
250
251def add_apt_source(url):
252 """Add an apt source entry, with passed in key.
253
254 This is a thin wrapper over the charmhelper "add_source" method to allow
255 for the URLs to contain the key explicitely, like:
256 - https://blah/blah|KEY
257 - deb https://blah/blah|KEY
258 - ppa:landscape/blah
259 """
260 key = None
261 if url.startswith("lp"):
262 return build_from_launchpad(url)
263
264 if "|" in url:
265 # We have a particular key specified. Url = url without the key now.
266 url, key = url.split("|", 1) # Maximum of 1 split.
267 if key == "":
268 log("Archive key for '%s' is empty.", level="WARNING")
269
270 return add_source(url, key)
271
272
273def _write_certificate(certificate, filename):202def _write_certificate(certificate, filename):
274 """203 """
275 @param certificate Text of the certificate, base64 encoded.204 @param certificate Text of the certificate, base64 encoded.
276205
=== added symlink 'hooks/install'
=== target is u'install.py'
=== removed symlink 'hooks/install'
=== target was u'hooks.py'
=== added file 'hooks/install.py'
--- hooks/install.py 1970-01-01 00:00:00 +0000
+++ hooks/install.py 2014-06-19 10:48:12 +0000
@@ -0,0 +1,90 @@
1#!/usr/bin/python
2"""This is the install hook file.
3
4It is its own file because the rest of the hooks import landscape classes and
5functions, and landscape is not installed until this hook runs.
6"""
7
8import os
9import subprocess
10import sys
11from charmhelpers.core.hookenv import (
12 Hooks, UnregisteredHookError, log)
13from charmhelpers.fetch import (
14 apt_install, _run_apt_command, add_source, apt_update)
15from charmhelpers.core.hookenv import config
16
17
18hooks = Hooks()
19
20
21@hooks.hook("install")
22def install():
23 charm_config = config()
24 apt_install(["apt-transport-https", "wget"])
25
26 origin = charm_config.get("origin")
27 if origin == "distro":
28 origin = None
29
30 if origin is not None:
31 add_apt_source(origin)
32
33 apt_update()
34 apt_install(["landscape-client"])
35 data_path = charm_config.get("data-path")
36 if not data_path:
37 data_path = "/var/lib/landscape/client"
38
39 result = subprocess.call(["landscape-config", "--init", "-d", data_path])
40 if result != 0:
41 result = subprocess.check_call([
42 "install", "-o", "landscape", "-g", "root", "-m", "755", "-d",
43 data_path])
44
45def build_from_launchpad(url):
46 """The charm will install the code from the passed lp branch.
47 """
48 apt_install(["devscripts", "bzr", "pbuilder"], fatal=True)
49 subprocess.check_call(["rm", "-rf", "landscape-client-source"])
50 subprocess.check_call(["bzr", "branch", url, "landscape-client-source"])
51 os.chdir("landscape-client-source")
52 subprocess.check_call("/usr/lib/pbuilder/pbuilder-satisfydepends")
53 env = {"DEBUILD_OPTS": "-uc -us"}
54 subprocess.check_call(["make", "package"], env=env)
55 #TODO: The following call should be retried (potential race condition to
56 # acquire the dpkg lock).
57 subprocess.call(["dpkg", "-i", "../landscape-client_*.deb",
58 "../landscape-common_*.deb"])
59 # The _run_apt_command will ensure the command is retried in case we cannot
60 # acquire the lock for some reason.
61 _run_apt_command(["apt-get", "-f", "install"], fatal=True)
62 os.chdir("..")
63
64
65def add_apt_source(url):
66 """Add an apt source entry, with passed in key.
67
68 This is a thin wrapper over the charmhelper "add_source" method to allow
69 for the URLs to contain the key explicitely, like:
70 - https://blah/blah|KEY
71 - deb https://blah/blah|KEY
72 - ppa:landscape/blah
73 """
74 key = None
75 if url.startswith("lp"):
76 return build_from_launchpad(url)
77
78 if "|" in url:
79 # We have a particular key specified. Url = url without the key now.
80 url, key = url.split("|", 1) # Maximum of 1 split.
81 if key == "":
82 log("Archive key for '%s' is empty.", level="WARNING")
83
84 return add_source(url, key)
85
86if __name__ == '__main__':
87 try:
88 sys.exit(hooks.execute(sys.argv))
89 except UnregisteredHookError as e:
90 log('Unknown hook {} - skipping.'.format(e))

Subscribers

People subscribed via source and target branches