~chad.smith/charms/precise/block-storage-broker/bsb-use-charmhelpers-to-set-openstack-origin

« back to all changes in this revision

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

  • Committer: Chad Smith
  • Date: 2014-09-10 21:17:43 UTC
  • mfrom: (54.2.2 trunk)
  • Revision ID: chad.smith@canonical.com-20140910211743-9lgxuwkvwmhm911u
merge block-storage-broker trunk resolve conflicts and fix unit tests to avoid mocker use

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
 
    apt_pkg.config.set("Dir::Cache::srcpkgcache", "")
126
 
 
127
 
    cache = apt_pkg.Cache()
 
120
    cache = apt_cache()
128
121
    _pkgs = []
129
122
    for package in packages:
130
123
        try:
137
130
    return _pkgs
138
131
 
139
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
 
140
143
def apt_install(packages, options=None, fatal=False):
141
144
    """Install one or more packages"""
142
145
    if options is None:
202
205
 
203
206
 
204
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
        ppa:charmers/example
 
213
        deb https://stub:key@private.example.com/ubuntu trusty main
 
214
 
 
215
    In addition:
 
216
        'proposed:' may be used to enable the standard 'proposed'
 
217
        pocket for the release.
 
218
        'cloud:' may be used to activate official cloud archive pockets,
 
219
        such as 'cloud:icehouse'
 
220
 
 
221
    @param key: A key to be added to the system's APT keyring and used
 
222
    to verify the signatures on packages. Ideally, this should be an
 
223
    ASCII format GPG public key including the block headers. A GPG key
 
224
    id may also be used, but be aware that only insecure protocols are
 
225
    available to retrieve the actual public key from a public keyserver
 
226
    placing your Juju environment at risk. ppa and cloud archive keys
 
227
    are securely added automtically, so sould not be provided.
 
228
    """
205
229
    if source is None:
206
230
        log('Source is not present. Skipping')
207
231
        return
226
250
        release = lsb_release()['DISTRIB_CODENAME']
227
251
        with open('/etc/apt/sources.list.d/proposed.list', 'w') as apt:
228
252
            apt.write(PROPOSED_POCKET.format(release))
 
253
    else:
 
254
        raise SourceConfigError("Unknown source: {!r}".format(source))
 
255
 
229
256
    if key:
230
 
        subprocess.check_call(['apt-key', 'adv', '--keyserver',
231
 
                               'hkp://keyserver.ubuntu.com:80', '--recv',
232
 
                               key])
 
257
        if '-----BEGIN PGP PUBLIC KEY BLOCK-----' in key:
 
258
            with NamedTemporaryFile() as key_file:
 
259
                key_file.write(key)
 
260
                key_file.flush()
 
261
                key_file.seek(0)
 
262
                subprocess.check_call(['apt-key', 'add', '-'], stdin=key_file)
 
263
        else:
 
264
            # Note that hkp: is in no way a secure protocol. Using a
 
265
            # GPG key id is pointless from a security POV unless you
 
266
            # absolutely trust your network and DNS.
 
267
            subprocess.check_call(['apt-key', 'adv', '--keyserver',
 
268
                                   'hkp://keyserver.ubuntu.com:80', '--recv',
 
269
                                   key])
233
270
 
234
271
 
235
272
def configure_sources(update=False,
239
276
    Configure multiple sources from charm configuration.
240
277
 
241
278
    The lists are encoded as yaml fragments in the configuration.
242
 
    The frament needs to be included as a string.
 
279
    The frament needs to be included as a string. Sources and their
 
280
    corresponding keys are of the types supported by add_source().
243
281
 
244
282
    Example config:
245
283
        install_sources: |