56
58
'precise-proposed/icehouse': 'precise-proposed/icehouse',
61
# The order of this list is very important. Handlers should be listed in from
62
# least- to most-specific URL matching.
64
'charmhelpers.fetch.archiveurl.ArchiveUrlFetchHandler',
65
'charmhelpers.fetch.bzrurl.BzrUrlFetchHandler',
68
APT_NO_LOCK = 100 # The return code for "couldn't acquire lock" in APT.
69
APT_NO_LOCK_RETRY_DELAY = 10 # Wait 10 seconds between apt lock checks.
70
APT_NO_LOCK_RETRY_COUNT = 30 # Retry to acquire the lock X times.
73
class SourceConfigError(Exception):
77
class UnhandledSource(Exception):
81
class AptLockError(Exception):
85
class BaseFetchHandler(object):
87
"""Base class for FetchHandler implementations in fetch plugins"""
89
def can_handle(self, source):
90
"""Returns True if the source can be handled. Otherwise returns
91
a string explaining why it cannot"""
92
return "Wrong source type"
94
def install(self, source):
95
"""Try to download and unpack the source. Return the path to the
96
unpacked files or raise UnhandledSource."""
97
raise UnhandledSource("Wrong source type {}".format(source))
99
def parse_url(self, url):
102
def base_url(self, url):
103
"""Return url without querystring or fragment"""
104
parts = list(self.parse_url(url))
105
parts[4:] = ['' for i in parts[4:]]
106
return urlunparse(parts)
60
109
def filter_installed_packages(packages):
61
110
"""Returns a list of packages that require installation"""
113
# Tell apt to build an in-memory cache to prevent race conditions (if
114
# another process is already building the cache).
115
apt_pkg.config.set("Dir::Cache::pkgcache", "")
63
117
cache = apt_pkg.Cache()
65
119
for package in packages:
87
141
cmd.extend(packages)
88
142
log("Installing {} with options: {}".format(packages,
90
env = os.environ.copy()
91
if 'DEBIAN_FRONTEND' not in env:
92
env['DEBIAN_FRONTEND'] = 'noninteractive'
95
subprocess.check_call(cmd, env=env)
97
subprocess.call(cmd, env=env)
144
_run_apt_command(cmd, fatal)
100
147
def apt_upgrade(options=None, fatal=False, dist=False):
110
157
cmd.append('upgrade')
111
158
log("Upgrading with options: {}".format(options))
113
env = os.environ.copy()
114
if 'DEBIAN_FRONTEND' not in env:
115
env['DEBIAN_FRONTEND'] = 'noninteractive'
118
subprocess.check_call(cmd, env=env)
120
subprocess.call(cmd, env=env)
159
_run_apt_command(cmd, fatal)
123
162
def apt_update(fatal=False):
124
163
"""Update local apt cache"""
125
164
cmd = ['apt-get', 'update']
127
subprocess.check_call(cmd)
165
_run_apt_command(cmd, fatal)
132
168
def apt_purge(packages, fatal=False):
138
174
cmd.extend(packages)
139
175
log("Purging {}".format(packages))
141
subprocess.check_call(cmd)
176
_run_apt_command(cmd, fatal)
146
179
def apt_hold(packages, fatal=False):
191
class SourceConfigError(Exception):
195
225
def configure_sources(update=False,
196
226
sources_var='install_sources',
197
227
keys_var='install_keys'):
225
255
apt_update(fatal=True)
227
# The order of this list is very important. Handlers should be listed in from
228
# least- to most-specific URL matching.
230
'charmhelpers.fetch.archiveurl.ArchiveUrlFetchHandler',
231
'charmhelpers.fetch.bzrurl.BzrUrlFetchHandler',
235
class UnhandledSource(Exception):
239
258
def install_remote(source):
265
284
return install_remote(source)
268
class BaseFetchHandler(object):
270
"""Base class for FetchHandler implementations in fetch plugins"""
272
def can_handle(self, source):
273
"""Returns True if the source can be handled. Otherwise returns
274
a string explaining why it cannot"""
275
return "Wrong source type"
277
def install(self, source):
278
"""Try to download and unpack the source. Return the path to the
279
unpacked files or raise UnhandledSource."""
280
raise UnhandledSource("Wrong source type {}".format(source))
282
def parse_url(self, url):
285
def base_url(self, url):
286
"""Return url without querystring or fragment"""
287
parts = list(self.parse_url(url))
288
parts[4:] = ['' for i in parts[4:]]
289
return urlunparse(parts)
292
287
def plugins(fetch_handlers=None):
293
288
if not fetch_handlers:
294
289
fetch_handlers = FETCH_HANDLERS
306
301
log("FetchHandler {} not found, skipping plugin".format(
308
303
return plugin_list
306
def _run_apt_command(cmd, fatal=False):
308
Run an APT command, checking output and retrying if the fatal flag is set
311
:param: cmd: str: The apt command to run.
312
:param: fatal: bool: Whether the command's output should be checked and
315
env = os.environ.copy()
317
if 'DEBIAN_FRONTEND' not in env:
318
env['DEBIAN_FRONTEND'] = 'noninteractive'
324
# If the command is considered "fatal", we need to retry if the apt
325
# lock was not acquired.
327
while result is None or result == APT_NO_LOCK:
329
result = subprocess.check_call(cmd, env=env)
330
except subprocess.CalledProcessError, e:
331
retry_count = retry_count + 1
332
if retry_count > APT_NO_LOCK_RETRY_COUNT:
334
result = e.returncode
335
log("Couldn't acquire DPKG lock. Will retry in {} seconds."
336
"".format(APT_NO_LOCK_RETRY_DELAY))
337
time.sleep(APT_NO_LOCK_RETRY_DELAY)
340
subprocess.call(cmd, env=env)