Merge lp:~tribaal/charms/trusty/nova-cloud-controller/sync-charm-helpers into lp:~openstack-charmers-archive/charms/trusty/nova-cloud-controller/trunk
- Trusty Tahr (14.04)
- sync-charm-helpers
- Merge into trunk
Proposed by
Chris Glass
Status: | Merged |
---|---|
Merged at revision: | 76 |
Proposed branch: | lp:~tribaal/charms/trusty/nova-cloud-controller/sync-charm-helpers |
Merge into: | lp:~openstack-charmers-archive/charms/trusty/nova-cloud-controller/trunk |
Diff against target: |
530 lines (+254/-74) 8 files modified
hooks/charmhelpers/contrib/openstack/context.py (+1/-1) hooks/charmhelpers/contrib/openstack/neutron.py (+17/-1) hooks/charmhelpers/contrib/openstack/utils.py (+8/-1) hooks/charmhelpers/contrib/storage/linux/lvm.py (+1/-1) hooks/charmhelpers/contrib/storage/linux/utils.py (+19/-5) hooks/charmhelpers/core/hookenv.py (+98/-1) hooks/charmhelpers/core/host.py (+14/-0) hooks/charmhelpers/fetch/__init__.py (+96/-64) |
To merge this branch: | bzr merge lp:~tribaal/charms/trusty/nova-cloud-controller/sync-charm-helpers |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
OpenStack Charmers | Pending | ||
Review via email: mp+220025@code.launchpad.net |
Commit message
Description of the change
This branch syncs back the changes from charm-helpers as of http://
To post a comment you must log in.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'hooks/charmhelpers/contrib/openstack/context.py' | |||
2 | --- hooks/charmhelpers/contrib/openstack/context.py 2014-04-16 08:25:14 +0000 | |||
3 | +++ hooks/charmhelpers/contrib/openstack/context.py 2014-05-19 12:48:12 +0000 | |||
4 | @@ -570,7 +570,7 @@ | |||
5 | 570 | 570 | ||
6 | 571 | if self.plugin == 'ovs': | 571 | if self.plugin == 'ovs': |
7 | 572 | ctxt.update(self.ovs_ctxt()) | 572 | ctxt.update(self.ovs_ctxt()) |
9 | 573 | elif self.plugin == 'nvp': | 573 | elif self.plugin in ['nvp', 'nsx']: |
10 | 574 | ctxt.update(self.nvp_ctxt()) | 574 | ctxt.update(self.nvp_ctxt()) |
11 | 575 | 575 | ||
12 | 576 | alchemy_flags = config('neutron-alchemy-flags') | 576 | alchemy_flags = config('neutron-alchemy-flags') |
13 | 577 | 577 | ||
14 | === modified file 'hooks/charmhelpers/contrib/openstack/neutron.py' | |||
15 | --- hooks/charmhelpers/contrib/openstack/neutron.py 2014-04-16 08:25:14 +0000 | |||
16 | +++ hooks/charmhelpers/contrib/openstack/neutron.py 2014-05-19 12:48:12 +0000 | |||
17 | @@ -114,14 +114,30 @@ | |||
18 | 114 | 'server_packages': ['neutron-server', | 114 | 'server_packages': ['neutron-server', |
19 | 115 | 'neutron-plugin-nicira'], | 115 | 'neutron-plugin-nicira'], |
20 | 116 | 'server_services': ['neutron-server'] | 116 | 'server_services': ['neutron-server'] |
21 | 117 | }, | ||
22 | 118 | 'nsx': { | ||
23 | 119 | 'config': '/etc/neutron/plugins/vmware/nsx.ini', | ||
24 | 120 | 'driver': 'vmware', | ||
25 | 121 | 'contexts': [ | ||
26 | 122 | context.SharedDBContext(user=config('neutron-database-user'), | ||
27 | 123 | database=config('neutron-database'), | ||
28 | 124 | relation_prefix='neutron', | ||
29 | 125 | ssl_dir=NEUTRON_CONF_DIR)], | ||
30 | 126 | 'services': [], | ||
31 | 127 | 'packages': [], | ||
32 | 128 | 'server_packages': ['neutron-server', | ||
33 | 129 | 'neutron-plugin-vmware'], | ||
34 | 130 | 'server_services': ['neutron-server'] | ||
35 | 117 | } | 131 | } |
36 | 118 | } | 132 | } |
37 | 119 | # NOTE: patch in ml2 plugin for icehouse onwards | ||
38 | 120 | if release >= 'icehouse': | 133 | if release >= 'icehouse': |
39 | 134 | # NOTE: patch in ml2 plugin for icehouse onwards | ||
40 | 121 | plugins['ovs']['config'] = '/etc/neutron/plugins/ml2/ml2_conf.ini' | 135 | plugins['ovs']['config'] = '/etc/neutron/plugins/ml2/ml2_conf.ini' |
41 | 122 | plugins['ovs']['driver'] = 'neutron.plugins.ml2.plugin.Ml2Plugin' | 136 | plugins['ovs']['driver'] = 'neutron.plugins.ml2.plugin.Ml2Plugin' |
42 | 123 | plugins['ovs']['server_packages'] = ['neutron-server', | 137 | plugins['ovs']['server_packages'] = ['neutron-server', |
43 | 124 | 'neutron-plugin-ml2'] | 138 | 'neutron-plugin-ml2'] |
44 | 139 | # NOTE: patch in vmware renames nvp->nsx for icehouse onwards | ||
45 | 140 | plugins['nvp'] = plugins['nsx'] | ||
46 | 125 | return plugins | 141 | return plugins |
47 | 126 | 142 | ||
48 | 127 | 143 | ||
49 | 128 | 144 | ||
50 | === modified file 'hooks/charmhelpers/contrib/openstack/utils.py' | |||
51 | --- hooks/charmhelpers/contrib/openstack/utils.py 2014-04-16 08:25:14 +0000 | |||
52 | +++ hooks/charmhelpers/contrib/openstack/utils.py 2014-05-19 12:48:12 +0000 | |||
53 | @@ -131,6 +131,11 @@ | |||
54 | 131 | def get_os_codename_package(package, fatal=True): | 131 | def get_os_codename_package(package, fatal=True): |
55 | 132 | '''Derive OpenStack release codename from an installed package.''' | 132 | '''Derive OpenStack release codename from an installed package.''' |
56 | 133 | apt.init() | 133 | apt.init() |
57 | 134 | |||
58 | 135 | # Tell apt to build an in-memory cache to prevent race conditions (if | ||
59 | 136 | # another process is already building the cache). | ||
60 | 137 | apt.config.set("Dir::Cache::pkgcache", "") | ||
61 | 138 | |||
62 | 134 | cache = apt.Cache() | 139 | cache = apt.Cache() |
63 | 135 | 140 | ||
64 | 136 | try: | 141 | try: |
65 | @@ -183,7 +188,7 @@ | |||
66 | 183 | if cname == codename: | 188 | if cname == codename: |
67 | 184 | return version | 189 | return version |
68 | 185 | #e = "Could not determine OpenStack version for package: %s" % pkg | 190 | #e = "Could not determine OpenStack version for package: %s" % pkg |
70 | 186 | #error_out(e) | 191 | # error_out(e) |
71 | 187 | 192 | ||
72 | 188 | 193 | ||
73 | 189 | os_rel = None | 194 | os_rel = None |
74 | @@ -401,6 +406,8 @@ | |||
75 | 401 | rtype = 'PTR' | 406 | rtype = 'PTR' |
76 | 402 | elif isinstance(address, basestring): | 407 | elif isinstance(address, basestring): |
77 | 403 | rtype = 'A' | 408 | rtype = 'A' |
78 | 409 | else: | ||
79 | 410 | return None | ||
80 | 404 | 411 | ||
81 | 405 | answers = dns.resolver.query(address, rtype) | 412 | answers = dns.resolver.query(address, rtype) |
82 | 406 | if answers: | 413 | if answers: |
83 | 407 | 414 | ||
84 | === modified file 'hooks/charmhelpers/contrib/storage/linux/lvm.py' | |||
85 | --- hooks/charmhelpers/contrib/storage/linux/lvm.py 2013-08-02 03:42:16 +0000 | |||
86 | +++ hooks/charmhelpers/contrib/storage/linux/lvm.py 2014-05-19 12:48:12 +0000 | |||
87 | @@ -62,7 +62,7 @@ | |||
88 | 62 | pvd = check_output(['pvdisplay', block_device]).splitlines() | 62 | pvd = check_output(['pvdisplay', block_device]).splitlines() |
89 | 63 | for l in pvd: | 63 | for l in pvd: |
90 | 64 | if l.strip().startswith('VG Name'): | 64 | if l.strip().startswith('VG Name'): |
92 | 65 | vg = ' '.join(l.split()).split(' ').pop() | 65 | vg = ' '.join(l.strip().split()[2:]) |
93 | 66 | return vg | 66 | return vg |
94 | 67 | 67 | ||
95 | 68 | 68 | ||
96 | 69 | 69 | ||
97 | === modified file 'hooks/charmhelpers/contrib/storage/linux/utils.py' | |||
98 | --- hooks/charmhelpers/contrib/storage/linux/utils.py 2014-04-16 08:25:14 +0000 | |||
99 | +++ hooks/charmhelpers/contrib/storage/linux/utils.py 2014-05-19 12:48:12 +0000 | |||
100 | @@ -1,4 +1,5 @@ | |||
102 | 1 | from os import stat | 1 | import os |
103 | 2 | import re | ||
104 | 2 | from stat import S_ISBLK | 3 | from stat import S_ISBLK |
105 | 3 | 4 | ||
106 | 4 | from subprocess import ( | 5 | from subprocess import ( |
107 | @@ -14,7 +15,9 @@ | |||
108 | 14 | 15 | ||
109 | 15 | :returns: boolean: True if path is a block device, False if not. | 16 | :returns: boolean: True if path is a block device, False if not. |
110 | 16 | ''' | 17 | ''' |
112 | 17 | return S_ISBLK(stat(path).st_mode) | 18 | if not os.path.exists(path): |
113 | 19 | return False | ||
114 | 20 | return S_ISBLK(os.stat(path).st_mode) | ||
115 | 18 | 21 | ||
116 | 19 | 22 | ||
117 | 20 | def zap_disk(block_device): | 23 | def zap_disk(block_device): |
118 | @@ -29,7 +32,18 @@ | |||
119 | 29 | '--clear', block_device]) | 32 | '--clear', block_device]) |
120 | 30 | dev_end = check_output(['blockdev', '--getsz', block_device]) | 33 | dev_end = check_output(['blockdev', '--getsz', block_device]) |
121 | 31 | gpt_end = int(dev_end.split()[0]) - 100 | 34 | gpt_end = int(dev_end.split()[0]) - 100 |
123 | 32 | check_call(['dd', 'if=/dev/zero', 'of=%s'%(block_device), | 35 | check_call(['dd', 'if=/dev/zero', 'of=%s' % (block_device), |
124 | 33 | 'bs=1M', 'count=1']) | 36 | 'bs=1M', 'count=1']) |
127 | 34 | check_call(['dd', 'if=/dev/zero', 'of=%s'%(block_device), | 37 | check_call(['dd', 'if=/dev/zero', 'of=%s' % (block_device), |
128 | 35 | 'bs=512', 'count=100', 'seek=%s'%(gpt_end)]) | 38 | 'bs=512', 'count=100', 'seek=%s' % (gpt_end)]) |
129 | 39 | |||
130 | 40 | def is_device_mounted(device): | ||
131 | 41 | '''Given a device path, return True if that device is mounted, and False | ||
132 | 42 | if it isn't. | ||
133 | 43 | |||
134 | 44 | :param device: str: Full path of the device to check. | ||
135 | 45 | :returns: boolean: True if the path represents a mounted device, False if | ||
136 | 46 | it doesn't. | ||
137 | 47 | ''' | ||
138 | 48 | out = check_output(['mount']) | ||
139 | 49 | return bool(re.search(device + r"[0-9]+\b", out)) | ||
140 | 36 | 50 | ||
141 | === modified file 'hooks/charmhelpers/core/hookenv.py' | |||
142 | --- hooks/charmhelpers/core/hookenv.py 2014-03-27 11:02:24 +0000 | |||
143 | +++ hooks/charmhelpers/core/hookenv.py 2014-05-19 12:48:12 +0000 | |||
144 | @@ -155,6 +155,100 @@ | |||
145 | 155 | return os.path.basename(sys.argv[0]) | 155 | return os.path.basename(sys.argv[0]) |
146 | 156 | 156 | ||
147 | 157 | 157 | ||
148 | 158 | class Config(dict): | ||
149 | 159 | """A Juju charm config dictionary that can write itself to | ||
150 | 160 | disk (as json) and track which values have changed since | ||
151 | 161 | the previous hook invocation. | ||
152 | 162 | |||
153 | 163 | Do not instantiate this object directly - instead call | ||
154 | 164 | ``hookenv.config()`` | ||
155 | 165 | |||
156 | 166 | Example usage:: | ||
157 | 167 | |||
158 | 168 | >>> # inside a hook | ||
159 | 169 | >>> from charmhelpers.core import hookenv | ||
160 | 170 | >>> config = hookenv.config() | ||
161 | 171 | >>> config['foo'] | ||
162 | 172 | 'bar' | ||
163 | 173 | >>> config['mykey'] = 'myval' | ||
164 | 174 | >>> config.save() | ||
165 | 175 | |||
166 | 176 | |||
167 | 177 | >>> # user runs `juju set mycharm foo=baz` | ||
168 | 178 | >>> # now we're inside subsequent config-changed hook | ||
169 | 179 | >>> config = hookenv.config() | ||
170 | 180 | >>> config['foo'] | ||
171 | 181 | 'baz' | ||
172 | 182 | >>> # test to see if this val has changed since last hook | ||
173 | 183 | >>> config.changed('foo') | ||
174 | 184 | True | ||
175 | 185 | >>> # what was the previous value? | ||
176 | 186 | >>> config.previous('foo') | ||
177 | 187 | 'bar' | ||
178 | 188 | >>> # keys/values that we add are preserved across hooks | ||
179 | 189 | >>> config['mykey'] | ||
180 | 190 | 'myval' | ||
181 | 191 | >>> # don't forget to save at the end of hook! | ||
182 | 192 | >>> config.save() | ||
183 | 193 | |||
184 | 194 | """ | ||
185 | 195 | CONFIG_FILE_NAME = '.juju-persistent-config' | ||
186 | 196 | |||
187 | 197 | def __init__(self, *args, **kw): | ||
188 | 198 | super(Config, self).__init__(*args, **kw) | ||
189 | 199 | self._prev_dict = None | ||
190 | 200 | self.path = os.path.join(charm_dir(), Config.CONFIG_FILE_NAME) | ||
191 | 201 | if os.path.exists(self.path): | ||
192 | 202 | self.load_previous() | ||
193 | 203 | |||
194 | 204 | def load_previous(self, path=None): | ||
195 | 205 | """Load previous copy of config from disk so that current values | ||
196 | 206 | can be compared to previous values. | ||
197 | 207 | |||
198 | 208 | :param path: | ||
199 | 209 | |||
200 | 210 | File path from which to load the previous config. If `None`, | ||
201 | 211 | config is loaded from the default location. If `path` is | ||
202 | 212 | specified, subsequent `save()` calls will write to the same | ||
203 | 213 | path. | ||
204 | 214 | |||
205 | 215 | """ | ||
206 | 216 | self.path = path or self.path | ||
207 | 217 | with open(self.path) as f: | ||
208 | 218 | self._prev_dict = json.load(f) | ||
209 | 219 | |||
210 | 220 | def changed(self, key): | ||
211 | 221 | """Return true if the value for this key has changed since | ||
212 | 222 | the last save. | ||
213 | 223 | |||
214 | 224 | """ | ||
215 | 225 | if self._prev_dict is None: | ||
216 | 226 | return True | ||
217 | 227 | return self.previous(key) != self.get(key) | ||
218 | 228 | |||
219 | 229 | def previous(self, key): | ||
220 | 230 | """Return previous value for this key, or None if there | ||
221 | 231 | is no "previous" value. | ||
222 | 232 | |||
223 | 233 | """ | ||
224 | 234 | if self._prev_dict: | ||
225 | 235 | return self._prev_dict.get(key) | ||
226 | 236 | return None | ||
227 | 237 | |||
228 | 238 | def save(self): | ||
229 | 239 | """Save this config to disk. | ||
230 | 240 | |||
231 | 241 | Preserves items in _prev_dict that do not exist in self. | ||
232 | 242 | |||
233 | 243 | """ | ||
234 | 244 | if self._prev_dict: | ||
235 | 245 | for k, v in self._prev_dict.iteritems(): | ||
236 | 246 | if k not in self: | ||
237 | 247 | self[k] = v | ||
238 | 248 | with open(self.path, 'w') as f: | ||
239 | 249 | json.dump(self, f) | ||
240 | 250 | |||
241 | 251 | |||
242 | 158 | @cached | 252 | @cached |
243 | 159 | def config(scope=None): | 253 | def config(scope=None): |
244 | 160 | """Juju charm configuration""" | 254 | """Juju charm configuration""" |
245 | @@ -163,7 +257,10 @@ | |||
246 | 163 | config_cmd_line.append(scope) | 257 | config_cmd_line.append(scope) |
247 | 164 | config_cmd_line.append('--format=json') | 258 | config_cmd_line.append('--format=json') |
248 | 165 | try: | 259 | try: |
250 | 166 | return json.loads(subprocess.check_output(config_cmd_line)) | 260 | config_data = json.loads(subprocess.check_output(config_cmd_line)) |
251 | 261 | if scope is not None: | ||
252 | 262 | return config_data | ||
253 | 263 | return Config(config_data) | ||
254 | 167 | except ValueError: | 264 | except ValueError: |
255 | 168 | return None | 265 | return None |
256 | 169 | 266 | ||
257 | 170 | 267 | ||
258 | === modified file 'hooks/charmhelpers/core/host.py' | |||
259 | --- hooks/charmhelpers/core/host.py 2014-03-27 11:02:24 +0000 | |||
260 | +++ hooks/charmhelpers/core/host.py 2014-05-19 12:48:12 +0000 | |||
261 | @@ -12,6 +12,7 @@ | |||
262 | 12 | import string | 12 | import string |
263 | 13 | import subprocess | 13 | import subprocess |
264 | 14 | import hashlib | 14 | import hashlib |
265 | 15 | import apt_pkg | ||
266 | 15 | 16 | ||
267 | 16 | from collections import OrderedDict | 17 | from collections import OrderedDict |
268 | 17 | 18 | ||
269 | @@ -295,3 +296,16 @@ | |||
270 | 295 | if 'link/ether' in words: | 296 | if 'link/ether' in words: |
271 | 296 | hwaddr = words[words.index('link/ether') + 1] | 297 | hwaddr = words[words.index('link/ether') + 1] |
272 | 297 | return hwaddr | 298 | return hwaddr |
273 | 299 | |||
274 | 300 | |||
275 | 301 | def cmp_pkgrevno(package, revno, pkgcache=None): | ||
276 | 302 | '''Compare supplied revno with the revno of the installed package | ||
277 | 303 | 1 => Installed revno is greater than supplied arg | ||
278 | 304 | 0 => Installed revno is the same as supplied arg | ||
279 | 305 | -1 => Installed revno is less than supplied arg | ||
280 | 306 | ''' | ||
281 | 307 | if not pkgcache: | ||
282 | 308 | apt_pkg.init() | ||
283 | 309 | pkgcache = apt_pkg.Cache() | ||
284 | 310 | pkg = pkgcache[package] | ||
285 | 311 | return apt_pkg.version_compare(pkg.current_ver.ver_str, revno) | ||
286 | 298 | 312 | ||
287 | === modified file 'hooks/charmhelpers/fetch/__init__.py' | |||
288 | --- hooks/charmhelpers/fetch/__init__.py 2014-04-24 17:28:23 +0000 | |||
289 | +++ hooks/charmhelpers/fetch/__init__.py 2014-05-19 12:48:12 +0000 | |||
290 | @@ -1,4 +1,5 @@ | |||
291 | 1 | import importlib | 1 | import importlib |
292 | 2 | import time | ||
293 | 2 | from yaml import safe_load | 3 | from yaml import safe_load |
294 | 3 | from charmhelpers.core.host import ( | 4 | from charmhelpers.core.host import ( |
295 | 4 | lsb_release | 5 | lsb_release |
296 | @@ -15,6 +16,7 @@ | |||
297 | 15 | import apt_pkg | 16 | import apt_pkg |
298 | 16 | import os | 17 | import os |
299 | 17 | 18 | ||
300 | 19 | |||
301 | 18 | CLOUD_ARCHIVE = """# Ubuntu Cloud Archive | 20 | CLOUD_ARCHIVE = """# Ubuntu Cloud Archive |
302 | 19 | deb http://ubuntu-cloud.archive.canonical.com/ubuntu {} main | 21 | deb http://ubuntu-cloud.archive.canonical.com/ubuntu {} main |
303 | 20 | """ | 22 | """ |
304 | @@ -56,10 +58,62 @@ | |||
305 | 56 | 'precise-proposed/icehouse': 'precise-proposed/icehouse', | 58 | 'precise-proposed/icehouse': 'precise-proposed/icehouse', |
306 | 57 | } | 59 | } |
307 | 58 | 60 | ||
308 | 61 | # The order of this list is very important. Handlers should be listed in from | ||
309 | 62 | # least- to most-specific URL matching. | ||
310 | 63 | FETCH_HANDLERS = ( | ||
311 | 64 | 'charmhelpers.fetch.archiveurl.ArchiveUrlFetchHandler', | ||
312 | 65 | 'charmhelpers.fetch.bzrurl.BzrUrlFetchHandler', | ||
313 | 66 | ) | ||
314 | 67 | |||
315 | 68 | APT_NO_LOCK = 100 # The return code for "couldn't acquire lock" in APT. | ||
316 | 69 | APT_NO_LOCK_RETRY_DELAY = 10 # Wait 10 seconds between apt lock checks. | ||
317 | 70 | APT_NO_LOCK_RETRY_COUNT = 30 # Retry to acquire the lock X times. | ||
318 | 71 | |||
319 | 72 | |||
320 | 73 | class SourceConfigError(Exception): | ||
321 | 74 | pass | ||
322 | 75 | |||
323 | 76 | |||
324 | 77 | class UnhandledSource(Exception): | ||
325 | 78 | pass | ||
326 | 79 | |||
327 | 80 | |||
328 | 81 | class AptLockError(Exception): | ||
329 | 82 | pass | ||
330 | 83 | |||
331 | 84 | |||
332 | 85 | class BaseFetchHandler(object): | ||
333 | 86 | |||
334 | 87 | """Base class for FetchHandler implementations in fetch plugins""" | ||
335 | 88 | |||
336 | 89 | def can_handle(self, source): | ||
337 | 90 | """Returns True if the source can be handled. Otherwise returns | ||
338 | 91 | a string explaining why it cannot""" | ||
339 | 92 | return "Wrong source type" | ||
340 | 93 | |||
341 | 94 | def install(self, source): | ||
342 | 95 | """Try to download and unpack the source. Return the path to the | ||
343 | 96 | unpacked files or raise UnhandledSource.""" | ||
344 | 97 | raise UnhandledSource("Wrong source type {}".format(source)) | ||
345 | 98 | |||
346 | 99 | def parse_url(self, url): | ||
347 | 100 | return urlparse(url) | ||
348 | 101 | |||
349 | 102 | def base_url(self, url): | ||
350 | 103 | """Return url without querystring or fragment""" | ||
351 | 104 | parts = list(self.parse_url(url)) | ||
352 | 105 | parts[4:] = ['' for i in parts[4:]] | ||
353 | 106 | return urlunparse(parts) | ||
354 | 107 | |||
355 | 59 | 108 | ||
356 | 60 | def filter_installed_packages(packages): | 109 | def filter_installed_packages(packages): |
357 | 61 | """Returns a list of packages that require installation""" | 110 | """Returns a list of packages that require installation""" |
358 | 62 | apt_pkg.init() | 111 | apt_pkg.init() |
359 | 112 | |||
360 | 113 | # Tell apt to build an in-memory cache to prevent race conditions (if | ||
361 | 114 | # another process is already building the cache). | ||
362 | 115 | apt_pkg.config.set("Dir::Cache::pkgcache", "") | ||
363 | 116 | |||
364 | 63 | cache = apt_pkg.Cache() | 117 | cache = apt_pkg.Cache() |
365 | 64 | _pkgs = [] | 118 | _pkgs = [] |
366 | 65 | for package in packages: | 119 | for package in packages: |
367 | @@ -87,14 +141,7 @@ | |||
368 | 87 | cmd.extend(packages) | 141 | cmd.extend(packages) |
369 | 88 | log("Installing {} with options: {}".format(packages, | 142 | log("Installing {} with options: {}".format(packages, |
370 | 89 | options)) | 143 | options)) |
379 | 90 | env = os.environ.copy() | 144 | _run_apt_command(cmd, fatal) |
372 | 91 | if 'DEBIAN_FRONTEND' not in env: | ||
373 | 92 | env['DEBIAN_FRONTEND'] = 'noninteractive' | ||
374 | 93 | |||
375 | 94 | if fatal: | ||
376 | 95 | subprocess.check_call(cmd, env=env) | ||
377 | 96 | else: | ||
378 | 97 | subprocess.call(cmd, env=env) | ||
380 | 98 | 145 | ||
381 | 99 | 146 | ||
382 | 100 | def apt_upgrade(options=None, fatal=False, dist=False): | 147 | def apt_upgrade(options=None, fatal=False, dist=False): |
383 | @@ -109,24 +156,13 @@ | |||
384 | 109 | else: | 156 | else: |
385 | 110 | cmd.append('upgrade') | 157 | cmd.append('upgrade') |
386 | 111 | log("Upgrading with options: {}".format(options)) | 158 | log("Upgrading with options: {}".format(options)) |
396 | 112 | 159 | _run_apt_command(cmd, fatal) | |
388 | 113 | env = os.environ.copy() | ||
389 | 114 | if 'DEBIAN_FRONTEND' not in env: | ||
390 | 115 | env['DEBIAN_FRONTEND'] = 'noninteractive' | ||
391 | 116 | |||
392 | 117 | if fatal: | ||
393 | 118 | subprocess.check_call(cmd, env=env) | ||
394 | 119 | else: | ||
395 | 120 | subprocess.call(cmd, env=env) | ||
397 | 121 | 160 | ||
398 | 122 | 161 | ||
399 | 123 | def apt_update(fatal=False): | 162 | def apt_update(fatal=False): |
400 | 124 | """Update local apt cache""" | 163 | """Update local apt cache""" |
401 | 125 | cmd = ['apt-get', 'update'] | 164 | cmd = ['apt-get', 'update'] |
406 | 126 | if fatal: | 165 | _run_apt_command(cmd, fatal) |
403 | 127 | subprocess.check_call(cmd) | ||
404 | 128 | else: | ||
405 | 129 | subprocess.call(cmd) | ||
407 | 130 | 166 | ||
408 | 131 | 167 | ||
409 | 132 | def apt_purge(packages, fatal=False): | 168 | def apt_purge(packages, fatal=False): |
410 | @@ -137,10 +173,7 @@ | |||
411 | 137 | else: | 173 | else: |
412 | 138 | cmd.extend(packages) | 174 | cmd.extend(packages) |
413 | 139 | log("Purging {}".format(packages)) | 175 | log("Purging {}".format(packages)) |
418 | 140 | if fatal: | 176 | _run_apt_command(cmd, fatal) |
415 | 141 | subprocess.check_call(cmd) | ||
416 | 142 | else: | ||
417 | 143 | subprocess.call(cmd) | ||
419 | 144 | 177 | ||
420 | 145 | 178 | ||
421 | 146 | def apt_hold(packages, fatal=False): | 179 | def apt_hold(packages, fatal=False): |
422 | @@ -151,6 +184,7 @@ | |||
423 | 151 | else: | 184 | else: |
424 | 152 | cmd.extend(packages) | 185 | cmd.extend(packages) |
425 | 153 | log("Holding {}".format(packages)) | 186 | log("Holding {}".format(packages)) |
426 | 187 | |||
427 | 154 | if fatal: | 188 | if fatal: |
428 | 155 | subprocess.check_call(cmd) | 189 | subprocess.check_call(cmd) |
429 | 156 | else: | 190 | else: |
430 | @@ -188,10 +222,6 @@ | |||
431 | 188 | key]) | 222 | key]) |
432 | 189 | 223 | ||
433 | 190 | 224 | ||
434 | 191 | class SourceConfigError(Exception): | ||
435 | 192 | pass | ||
436 | 193 | |||
437 | 194 | |||
438 | 195 | def configure_sources(update=False, | 225 | def configure_sources(update=False, |
439 | 196 | sources_var='install_sources', | 226 | sources_var='install_sources', |
440 | 197 | keys_var='install_keys'): | 227 | keys_var='install_keys'): |
441 | @@ -224,17 +254,6 @@ | |||
442 | 224 | if update: | 254 | if update: |
443 | 225 | apt_update(fatal=True) | 255 | apt_update(fatal=True) |
444 | 226 | 256 | ||
445 | 227 | # The order of this list is very important. Handlers should be listed in from | ||
446 | 228 | # least- to most-specific URL matching. | ||
447 | 229 | FETCH_HANDLERS = ( | ||
448 | 230 | 'charmhelpers.fetch.archiveurl.ArchiveUrlFetchHandler', | ||
449 | 231 | 'charmhelpers.fetch.bzrurl.BzrUrlFetchHandler', | ||
450 | 232 | ) | ||
451 | 233 | |||
452 | 234 | |||
453 | 235 | class UnhandledSource(Exception): | ||
454 | 236 | pass | ||
455 | 237 | |||
456 | 238 | 257 | ||
457 | 239 | def install_remote(source): | 258 | def install_remote(source): |
458 | 240 | """ | 259 | """ |
459 | @@ -265,30 +284,6 @@ | |||
460 | 265 | return install_remote(source) | 284 | return install_remote(source) |
461 | 266 | 285 | ||
462 | 267 | 286 | ||
463 | 268 | class BaseFetchHandler(object): | ||
464 | 269 | |||
465 | 270 | """Base class for FetchHandler implementations in fetch plugins""" | ||
466 | 271 | |||
467 | 272 | def can_handle(self, source): | ||
468 | 273 | """Returns True if the source can be handled. Otherwise returns | ||
469 | 274 | a string explaining why it cannot""" | ||
470 | 275 | return "Wrong source type" | ||
471 | 276 | |||
472 | 277 | def install(self, source): | ||
473 | 278 | """Try to download and unpack the source. Return the path to the | ||
474 | 279 | unpacked files or raise UnhandledSource.""" | ||
475 | 280 | raise UnhandledSource("Wrong source type {}".format(source)) | ||
476 | 281 | |||
477 | 282 | def parse_url(self, url): | ||
478 | 283 | return urlparse(url) | ||
479 | 284 | |||
480 | 285 | def base_url(self, url): | ||
481 | 286 | """Return url without querystring or fragment""" | ||
482 | 287 | parts = list(self.parse_url(url)) | ||
483 | 288 | parts[4:] = ['' for i in parts[4:]] | ||
484 | 289 | return urlunparse(parts) | ||
485 | 290 | |||
486 | 291 | |||
487 | 292 | def plugins(fetch_handlers=None): | 287 | def plugins(fetch_handlers=None): |
488 | 293 | if not fetch_handlers: | 288 | if not fetch_handlers: |
489 | 294 | fetch_handlers = FETCH_HANDLERS | 289 | fetch_handlers = FETCH_HANDLERS |
490 | @@ -306,3 +301,40 @@ | |||
491 | 306 | log("FetchHandler {} not found, skipping plugin".format( | 301 | log("FetchHandler {} not found, skipping plugin".format( |
492 | 307 | handler_name)) | 302 | handler_name)) |
493 | 308 | return plugin_list | 303 | return plugin_list |
494 | 304 | |||
495 | 305 | |||
496 | 306 | def _run_apt_command(cmd, fatal=False): | ||
497 | 307 | """ | ||
498 | 308 | Run an APT command, checking output and retrying if the fatal flag is set | ||
499 | 309 | to True. | ||
500 | 310 | |||
501 | 311 | :param: cmd: str: The apt command to run. | ||
502 | 312 | :param: fatal: bool: Whether the command's output should be checked and | ||
503 | 313 | retried. | ||
504 | 314 | """ | ||
505 | 315 | env = os.environ.copy() | ||
506 | 316 | |||
507 | 317 | if 'DEBIAN_FRONTEND' not in env: | ||
508 | 318 | env['DEBIAN_FRONTEND'] = 'noninteractive' | ||
509 | 319 | |||
510 | 320 | if fatal: | ||
511 | 321 | retry_count = 0 | ||
512 | 322 | result = None | ||
513 | 323 | |||
514 | 324 | # If the command is considered "fatal", we need to retry if the apt | ||
515 | 325 | # lock was not acquired. | ||
516 | 326 | |||
517 | 327 | while result is None or result == APT_NO_LOCK: | ||
518 | 328 | try: | ||
519 | 329 | result = subprocess.check_call(cmd, env=env) | ||
520 | 330 | except subprocess.CalledProcessError, e: | ||
521 | 331 | retry_count = retry_count + 1 | ||
522 | 332 | if retry_count > APT_NO_LOCK_RETRY_COUNT: | ||
523 | 333 | raise | ||
524 | 334 | result = e.returncode | ||
525 | 335 | log("Couldn't acquire DPKG lock. Will retry in {} seconds." | ||
526 | 336 | "".format(APT_NO_LOCK_RETRY_DELAY)) | ||
527 | 337 | time.sleep(APT_NO_LOCK_RETRY_DELAY) | ||
528 | 338 | |||
529 | 339 | else: | ||
530 | 340 | subprocess.call(cmd, env=env) |