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