~gnuoy/charms/trusty/ceph/fix-six-bug

« back to all changes in this revision

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

  • Committer: Liam Young
  • Date: 2014-09-17 06:36:31 UTC
  • mfrom: (78.1.5 ceph)
  • Revision ID: liam.young@canonical.com-20140917063631-sh28aabz283xfxyo
[corey.bryant,r=gnuoy] Add Amulet basic tests

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