1
from yaml import safe_load
2
from core.hookenv import config_get
3
from subprocess import check_call
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)
12
check_call('apt-key', 'import', key)
15
class SourceConfigError(Exception):
19
def configure_sources(update=False,
20
sources_var='install_sources',
21
keys_var='install_keys'):
23
Configure multiple sources from charm configuration
28
- "http://example.com/repo precise main"
33
Note that 'null' (a.k.a. None) should not be quoted.
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)
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])
46
check_call(('apt-get', 'update'))