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