~james-page/charm-helpers/haproxy-stats-1.6

« back to all changes in this revision

Viewing changes to charmhelpers/fetch/__init__.py

  • Committer: Matthew Wedgwood
  • Date: 2013-05-19 18:17:25 UTC
  • mto: (11.1.9 refactor-to-core)
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: matthew.wedgwood@canonical.com-20130519181725-k3c8t0pdjt6dfsur
remove redundant code from contrib.charmhelpers

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from yaml import safe_load
 
2
from core.hookenv import config_get
 
3
from subprocess import check_call
 
4
 
 
5
 
 
6
def add_source(source, key=None):
 
7
    if ((source.startswith('ppa:') or
 
8
         source.startswith('cloud:') or
 
9
         source.startswith('http:'))):
 
10
        check_call('add-apt-repository', source)
 
11
    if key:
 
12
        check_call('apt-key', 'import', key)
 
13
 
 
14
 
 
15
class SourceConfigError(Exception):
 
16
    pass
 
17
 
 
18
 
 
19
def configure_sources(update=False,
 
20
                      sources_var='install_sources',
 
21
                      keys_var='install_keys'):
 
22
    """
 
23
    Configure multiple sources from charm configuration
 
24
 
 
25
    Example config:
 
26
        install_sources:
 
27
          - "ppa:foo"
 
28
          - "http://example.com/repo precise main"
 
29
        install_keys:
 
30
          - null
 
31
          - "a1b2c3d4"
 
32
 
 
33
    Note that 'null' (a.k.a. None) should not be quoted.
 
34
    """
 
35
    sources = safe_load(config_get(sources_var))
 
36
    keys = safe_load(config_get(keys_var))
 
37
    if isinstance(sources, basestring) and isinstance(keys, basestring):
 
38
        add_source(sources, keys)
 
39
    else:
 
40
        if not len(sources) == len(keys):
 
41
            msg = 'Install sources and keys lists are different lengths'
 
42
            raise SourceConfigError(msg)
 
43
        for src_num in range(len(sources)):
 
44
            add_source(sources[src_num], sources[src_num])
 
45
    if update:
 
46
        check_call(('apt-get', 'update'))