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
1=== modified file 'hooks/hooks.py'
2--- hooks/hooks.py 2014-06-13 15:43:58 +0000
3+++ hooks/hooks.py 2014-06-19 10:48:12 +0000
4@@ -4,13 +4,9 @@
5 import socket
6 import os
7 import base64
8-import subprocess
9
10 from charmhelpers.core.hookenv import (
11 Hooks, UnregisteredHookError, log)
12-from charmhelpers.fetch import (
13- apt_install, _run_apt_command, add_source, apt_update)
14-from charmhelpers.core.hookenv import config
15 from shutil import rmtree
16 from subprocess import CalledProcessError
17
18@@ -29,31 +25,6 @@
19 hooks = Hooks()
20
21
22-@hooks.hook("install")
23-def install():
24- charm_config = config()
25- apt_install(["apt-transport-https", "wget"])
26-
27- origin = charm_config.get("origin")
28- if origin == "distro":
29- origin = None
30-
31- if origin is not None:
32- add_apt_source(origin)
33-
34- apt_update()
35- apt_install(["landscape-client"])
36- data_path = charm_config.get("data-path")
37- if not data_path:
38- data_path = "/var/lib/landscape/client"
39-
40- result = subprocess.call(["landscape-config", "--init", "-d", data_path])
41- if result != 0:
42- result = subprocess.check_call([
43- "install", "-o", "landscape", "-g", "root", "-m", "755", "-d",
44- data_path])
45-
46-
47 @hooks.hook("registration-relation-joined", "registration-relation-changed")
48 def registration_relation(juju_broker=JUJU_BROKER,
49 landscape_broker=LANDSCAPE_BROKER):
50@@ -228,48 +199,6 @@
51 return 0
52
53
54-def build_from_launchpad(url):
55- """The charm will install the code from the passed lp branch.
56- """
57- apt_install(["devscripts", "bzr", "pbuilder"], fatal=True)
58- subprocess.check_call(["rm", "-rf", "landscape-client-source"])
59- subprocess.check_call(["bzr", "branch", url, "landscape-client-source"])
60- os.chdir("landscape-client-source")
61- subprocess.check_call("/usr/lib/pbuilder/pbuilder-satisfydepends")
62- env = {"DEBUILD_OPTS": "-uc -us"}
63- subprocess.check_call(["make", "package"], env=env)
64- #TODO: The following call should be retried (potential race condition to
65- # acquire the dpkg lock).
66- subprocess.call(["dpkg", "-i", "../landscape-client_*.deb",
67- "../landscape-common_*.deb"])
68- # The _run_apt_command will ensure the command is retried in case we cannot
69- # acquire the lock for some reason.
70- _run_apt_command(["apt-get", "-f", "install"], fatal=True)
71- os.chdir("..")
72-
73-
74-def add_apt_source(url):
75- """Add an apt source entry, with passed in key.
76-
77- This is a thin wrapper over the charmhelper "add_source" method to allow
78- for the URLs to contain the key explicitely, like:
79- - https://blah/blah|KEY
80- - deb https://blah/blah|KEY
81- - ppa:landscape/blah
82- """
83- key = None
84- if url.startswith("lp"):
85- return build_from_launchpad(url)
86-
87- if "|" in url:
88- # We have a particular key specified. Url = url without the key now.
89- url, key = url.split("|", 1) # Maximum of 1 split.
90- if key == "":
91- log("Archive key for '%s' is empty.", level="WARNING")
92-
93- return add_source(url, key)
94-
95-
96 def _write_certificate(certificate, filename):
97 """
98 @param certificate Text of the certificate, base64 encoded.
99
100=== added symlink 'hooks/install'
101=== target is u'install.py'
102=== removed symlink 'hooks/install'
103=== target was u'hooks.py'
104=== added file 'hooks/install.py'
105--- hooks/install.py 1970-01-01 00:00:00 +0000
106+++ hooks/install.py 2014-06-19 10:48:12 +0000
107@@ -0,0 +1,90 @@
108+#!/usr/bin/python
109+"""This is the install hook file.
110+
111+It is its own file because the rest of the hooks import landscape classes and
112+functions, and landscape is not installed until this hook runs.
113+"""
114+
115+import os
116+import subprocess
117+import sys
118+from charmhelpers.core.hookenv import (
119+ Hooks, UnregisteredHookError, log)
120+from charmhelpers.fetch import (
121+ apt_install, _run_apt_command, add_source, apt_update)
122+from charmhelpers.core.hookenv import config
123+
124+
125+hooks = Hooks()
126+
127+
128+@hooks.hook("install")
129+def install():
130+ charm_config = config()
131+ apt_install(["apt-transport-https", "wget"])
132+
133+ origin = charm_config.get("origin")
134+ if origin == "distro":
135+ origin = None
136+
137+ if origin is not None:
138+ add_apt_source(origin)
139+
140+ apt_update()
141+ apt_install(["landscape-client"])
142+ data_path = charm_config.get("data-path")
143+ if not data_path:
144+ data_path = "/var/lib/landscape/client"
145+
146+ result = subprocess.call(["landscape-config", "--init", "-d", data_path])
147+ if result != 0:
148+ result = subprocess.check_call([
149+ "install", "-o", "landscape", "-g", "root", "-m", "755", "-d",
150+ data_path])
151+
152+def build_from_launchpad(url):
153+ """The charm will install the code from the passed lp branch.
154+ """
155+ apt_install(["devscripts", "bzr", "pbuilder"], fatal=True)
156+ subprocess.check_call(["rm", "-rf", "landscape-client-source"])
157+ subprocess.check_call(["bzr", "branch", url, "landscape-client-source"])
158+ os.chdir("landscape-client-source")
159+ subprocess.check_call("/usr/lib/pbuilder/pbuilder-satisfydepends")
160+ env = {"DEBUILD_OPTS": "-uc -us"}
161+ subprocess.check_call(["make", "package"], env=env)
162+ #TODO: The following call should be retried (potential race condition to
163+ # acquire the dpkg lock).
164+ subprocess.call(["dpkg", "-i", "../landscape-client_*.deb",
165+ "../landscape-common_*.deb"])
166+ # The _run_apt_command will ensure the command is retried in case we cannot
167+ # acquire the lock for some reason.
168+ _run_apt_command(["apt-get", "-f", "install"], fatal=True)
169+ os.chdir("..")
170+
171+
172+def add_apt_source(url):
173+ """Add an apt source entry, with passed in key.
174+
175+ This is a thin wrapper over the charmhelper "add_source" method to allow
176+ for the URLs to contain the key explicitely, like:
177+ - https://blah/blah|KEY
178+ - deb https://blah/blah|KEY
179+ - ppa:landscape/blah
180+ """
181+ key = None
182+ if url.startswith("lp"):
183+ return build_from_launchpad(url)
184+
185+ if "|" in url:
186+ # We have a particular key specified. Url = url without the key now.
187+ url, key = url.split("|", 1) # Maximum of 1 split.
188+ if key == "":
189+ log("Archive key for '%s' is empty.", level="WARNING")
190+
191+ return add_source(url, key)
192+
193+if __name__ == '__main__':
194+ try:
195+ sys.exit(hooks.execute(sys.argv))
196+ except UnregisteredHookError as e:
197+ log('Unknown hook {} - skipping.'.format(e))

Subscribers

People subscribed via source and target branches