~openstack-charmers-archive/charms/trusty/ceph-radosgw/next

« back to all changes in this revision

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

  • Committer: Liam Young
  • Date: 2014-09-17 14:11:53 UTC
  • mto: (30.2.1 ceph-radosgw)
  • mto: This revision was merged to the branch mainline in revision 26.
  • Revision ID: liam.young@canonical.com-20140917141153-4ct1krlj8on40lu2
Sync charm-helpers to switch to using in memory apt cache

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
        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
    """
204
229
    if source is None:
205
230
        log('Source is not present. Skipping')
206
231
        return
225
250
        release = lsb_release()['DISTRIB_CODENAME']
226
251
        with open('/etc/apt/sources.list.d/proposed.list', 'w') as apt:
227
252
            apt.write(PROPOSED_POCKET.format(release))
 
253
    else:
 
254
        raise SourceConfigError("Unknown source: {!r}".format(source))
 
255
 
228
256
    if key:
229
 
        subprocess.check_call(['apt-key', 'adv', '--keyserver',
230
 
                               'hkp://keyserver.ubuntu.com:80', '--recv',
231
 
                               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])
232
270
 
233
271
 
234
272
def configure_sources(update=False,
238
276
    Configure multiple sources from charm configuration.
239
277
 
240
278
    The lists are encoded as yaml fragments in the configuration.
241
 
    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().
242
281
 
243
282
    Example config:
244
283
        install_sources: |