~corey.bryant/charms/trusty/neutron-api/db-stamp

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/fetch/__init__.py

  • Committer: james.page at ubuntu
  • Date: 2014-10-01 21:05:24 UTC
  • mfrom: (39.3.25 ipv6)
  • Revision ID: james.page@ubuntu.com-20141001210524-z6uqyljzorphrhy6
[xianghui,dosaboy,r=james-page,t=gema] Add IPv6 support using prefer-ipv6 flag

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import importlib
 
2
from tempfile import NamedTemporaryFile
2
3
import time
3
4
from yaml import safe_load
4
5
from charmhelpers.core.host import (
116
117
 
117
118
def filter_installed_packages(packages):
118
119
    """Returns a list of packages that require installation"""
119
 
    import apt_pkg
120
 
    apt_pkg.init()
121
 
 
122
 
    # Tell apt to build an in-memory cache to prevent race conditions (if
123
 
    # another process is already building the cache).
124
 
    apt_pkg.config.set("Dir::Cache::pkgcache", "")
125
 
 
126
 
    cache = apt_pkg.Cache()
 
120
    cache = apt_cache()
127
121
    _pkgs = []
128
122
    for package in packages:
129
123
        try:
136
130
    return _pkgs
137
131
 
138
132
 
 
133
def apt_cache(in_memory=True):
 
134
    """Build and return an apt cache"""
 
135
    import apt_pkg
 
136
    apt_pkg.init()
 
137
    if in_memory:
 
138
        apt_pkg.config.set("Dir::Cache::pkgcache", "")
 
139
        apt_pkg.config.set("Dir::Cache::srcpkgcache", "")
 
140
    return apt_pkg.Cache()
 
141
 
 
142
 
139
143
def apt_install(packages, options=None, fatal=False):
140
144
    """Install one or more packages"""
141
145
    if options is None:
201
205
 
202
206
 
203
207
def add_source(source, key=None):
 
208
    """Add a package source to this system.
 
209
 
 
210
    @param source: a URL or sources.list entry, as supported by
 
211
    add-apt-repository(1). Examples::
 
212
 
 
213
        ppa:charmers/example
 
214
        deb https://stub:key@private.example.com/ubuntu trusty main
 
215
 
 
216
    In addition:
 
217
        'proposed:' may be used to enable the standard 'proposed'
 
218
        pocket for the release.
 
219
        'cloud:' may be used to activate official cloud archive pockets,
 
220
        such as 'cloud:icehouse'
 
221
 
 
222
    @param key: A key to be added to the system's APT keyring and used
 
223
    to verify the signatures on packages. Ideally, this should be an
 
224
    ASCII format GPG public key including the block headers. A GPG key
 
225
    id may also be used, but be aware that only insecure protocols are
 
226
    available to retrieve the actual public key from a public keyserver
 
227
    placing your Juju environment at risk. ppa and cloud archive keys
 
228
    are securely added automtically, so sould not be provided.
 
229
    """
204
230
    if source is None:
205
231
        log('Source is not present. Skipping')
206
232
        return
225
251
        release = lsb_release()['DISTRIB_CODENAME']
226
252
        with open('/etc/apt/sources.list.d/proposed.list', 'w') as apt:
227
253
            apt.write(PROPOSED_POCKET.format(release))
 
254
    else:
 
255
        raise SourceConfigError("Unknown source: {!r}".format(source))
 
256
 
228
257
    if key:
229
 
        subprocess.check_call(['apt-key', 'adv', '--keyserver',
230
 
                               'hkp://keyserver.ubuntu.com:80', '--recv',
231
 
                               key])
 
258
        if '-----BEGIN PGP PUBLIC KEY BLOCK-----' in key:
 
259
            with NamedTemporaryFile() as key_file:
 
260
                key_file.write(key)
 
261
                key_file.flush()
 
262
                key_file.seek(0)
 
263
                subprocess.check_call(['apt-key', 'add', '-'], stdin=key_file)
 
264
        else:
 
265
            # Note that hkp: is in no way a secure protocol. Using a
 
266
            # GPG key id is pointless from a security POV unless you
 
267
            # absolutely trust your network and DNS.
 
268
            subprocess.check_call(['apt-key', 'adv', '--keyserver',
 
269
                                   'hkp://keyserver.ubuntu.com:80', '--recv',
 
270
                                   key])
232
271
 
233
272
 
234
273
def configure_sources(update=False,
238
277
    Configure multiple sources from charm configuration.
239
278
 
240
279
    The lists are encoded as yaml fragments in the configuration.
241
 
    The frament needs to be included as a string.
 
280
    The frament needs to be included as a string. Sources and their
 
281
    corresponding keys are of the types supported by add_source().
242
282
 
243
283
    Example config:
244
284
        install_sources: |
272
312
        apt_update(fatal=True)
273
313
 
274
314
 
275
 
def install_remote(source):
 
315
def install_remote(source, *args, **kwargs):
276
316
    """
277
317
    Install a file tree from a remote source
278
318
 
279
319
    The specified source should be a url of the form:
280
320
        scheme://[host]/path[#[option=value][&...]]
281
321
 
282
 
    Schemes supported are based on this modules submodules
283
 
    Options supported are submodule-specific"""
 
322
    Schemes supported are based on this modules submodules.
 
323
    Options supported are submodule-specific.
 
324
    Additional arguments are passed through to the submodule.
 
325
 
 
326
    For example::
 
327
 
 
328
        dest = install_remote('http://example.com/archive.tgz',
 
329
                              checksum='deadbeef',
 
330
                              hash_type='sha1')
 
331
 
 
332
    This will download `archive.tgz`, validate it using SHA1 and, if
 
333
    the file is ok, extract it and return the directory in which it
 
334
    was extracted.  If the checksum fails, it will raise
 
335
    :class:`charmhelpers.core.host.ChecksumError`.
 
336
    """
284
337
    # We ONLY check for True here because can_handle may return a string
285
338
    # explaining why it can't handle a given source.
286
339
    handlers = [h for h in plugins() if h.can_handle(source) is True]
287
340
    installed_to = None
288
341
    for handler in handlers:
289
342
        try:
290
 
            installed_to = handler.install(source)
 
343
            installed_to = handler.install(source, *args, **kwargs)
291
344
        except UnhandledSource:
292
345
            pass
293
346
    if not installed_to: