~ionutbalutoiu/charms/trusty/neutron-api/next

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/openstack/utils.py

  • Committer: Edward Hope-Morley
  • Date: 2016-02-12 09:52:56 UTC
  • mfrom: (172.1.2 trunk)
  • Revision ID: edward.hope-morley@canonical.com-20160212095256-r93nqfhnlw3di3l8
[hopem,r=jamespage]

Sync charmhelpers to get support for supplying a Ascii Armor PGP key to
openstack-origin (currently only supports Radix64)
Closes-Bug: 1518975

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import re
26
26
 
27
27
import six
 
28
import tempfile
28
29
import traceback
29
30
import uuid
30
31
import yaml
41
42
    config,
42
43
    log as juju_log,
43
44
    charm_dir,
 
45
    DEBUG,
44
46
    INFO,
45
47
    related_units,
46
48
    relation_ids,
347
349
 
348
350
 
349
351
def import_key(keyid):
350
 
    cmd = "apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 " \
351
 
          "--recv-keys %s" % keyid
352
 
    try:
353
 
        subprocess.check_call(cmd.split(' '))
354
 
    except subprocess.CalledProcessError:
355
 
        error_out("Error importing repo key %s" % keyid)
 
352
    key = keyid.strip()
 
353
    if (key.startswith('-----BEGIN PGP PUBLIC KEY BLOCK-----') and
 
354
            key.endswith('-----END PGP PUBLIC KEY BLOCK-----')):
 
355
        juju_log("PGP key found (looks like ASCII Armor format)", level=DEBUG)
 
356
        juju_log("Importing ASCII Armor PGP key", level=DEBUG)
 
357
        with tempfile.NamedTemporaryFile() as keyfile:
 
358
            with open(keyfile.name, 'w') as fd:
 
359
                fd.write(key)
 
360
                fd.write("\n")
 
361
 
 
362
            cmd = ['apt-key', 'add', keyfile.name]
 
363
            try:
 
364
                subprocess.check_call(cmd)
 
365
            except subprocess.CalledProcessError:
 
366
                error_out("Error importing PGP key '%s'" % key)
 
367
    else:
 
368
        juju_log("PGP key found (looks like Radix64 format)", level=DEBUG)
 
369
        juju_log("Importing PGP key from keyserver", level=DEBUG)
 
370
        cmd = ['apt-key', 'adv', '--keyserver',
 
371
               'hkp://keyserver.ubuntu.com:80', '--recv-keys', key]
 
372
        try:
 
373
            subprocess.check_call(cmd)
 
374
        except subprocess.CalledProcessError:
 
375
            error_out("Error importing PGP key '%s'" % key)
 
376
 
 
377
 
 
378
def get_source_and_pgp_key(input):
 
379
    """Look for a pgp key ID or ascii-armor key in the given input."""
 
380
    index = input.strip()
 
381
    index = input.rfind('|')
 
382
    if index < 0:
 
383
        return input, None
 
384
 
 
385
    key = input[index + 1:].strip('|')
 
386
    source = input[:index]
 
387
    return source, key
356
388
 
357
389
 
358
390
def configure_installation_source(rel):
364
396
        with open('/etc/apt/sources.list.d/juju_deb.list', 'w') as f:
365
397
            f.write(DISTRO_PROPOSED % ubuntu_rel)
366
398
    elif rel[:4] == "ppa:":
367
 
        src = rel
 
399
        src, key = get_source_and_pgp_key(rel)
 
400
        if key:
 
401
            import_key(key)
 
402
 
368
403
        subprocess.check_call(["add-apt-repository", "-y", src])
369
404
    elif rel[:3] == "deb":
370
 
        l = len(rel.split('|'))
371
 
        if l == 2:
372
 
            src, key = rel.split('|')
373
 
            juju_log("Importing PPA key from keyserver for %s" % src)
 
405
        src, key = get_source_and_pgp_key(rel)
 
406
        if key:
374
407
            import_key(key)
375
 
        elif l == 1:
376
 
            src = rel
 
408
 
377
409
        with open('/etc/apt/sources.list.d/juju_deb.list', 'w') as f:
378
410
            f.write(src)
379
411
    elif rel[:6] == 'cloud:':