From c85279c930786384cd818d75a11922c24bd5fb1e Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Thu, 24 Oct 2013 11:45:35 -0700 Subject: support calling add-apt-repository on cloud-archive: entries LP: #1244355 --- cloudinit/config/cc_apt_configure.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/cloudinit/config/cc_apt_configure.py b/cloudinit/config/cc_apt_configure.py index 5a407016..7121ec43 100644 --- a/cloudinit/config/cc_apt_configure.py +++ b/cloudinit/config/cc_apt_configure.py @@ -20,6 +20,7 @@ import glob import os +import re from cloudinit import templater from cloudinit import util @@ -30,6 +31,9 @@ PROXY_TPL = "Acquire::HTTP::Proxy \"%s\";\n" APT_CONFIG_FN = "/etc/apt/apt.conf.d/94cloud-init-config" APT_PROXY_FN = "/etc/apt/apt.conf.d/95cloud-init-proxy" +# this will match 'XXX:YYY' (ie, 'cloud-archive:foo' or 'ppa:bar') +ADD_APT_REPO_MATCH = r"^[\w-]+:\w" + # A temporary shell program to get a given gpg key # from a given keyserver EXPORT_GPG_KEYID = """ @@ -78,7 +82,15 @@ def handle(name, cfg, cloud, log, _args): params = mirrors params['RELEASE'] = release params['MIRROR'] = mirror - errors = add_sources(cfg['apt_sources'], params) + + matchcfg = cfg.get('add_apt_repo_match', ADD_APT_REPO_MATCH) + if matchcfg: + matcher = re.compile(matchcfg) + else: + matcher = lambda f: False + + errors = add_sources(cfg['apt_sources'], params, + aa_repo_match=matcher) for e in errors: log.warn("Add source error: %s", ':'.join(e)) @@ -147,7 +159,7 @@ def generate_sources_list(codename, mirrors, cloud, log): templater.render_to_file(template_fn, '/etc/apt/sources.list', params) -def add_sources(srclist, template_params=None): +def add_sources(srclist, template_params=None, aa_repo_match=None): """ add entries in /etc/apt/sources.list.d for each abbreviated sources.list entry in 'srclist'. When rendering template, also @@ -156,6 +168,9 @@ def add_sources(srclist, template_params=None): if template_params is None: template_params = {} + if aa_repo_match is None: + aa_repo_match = lambda f: False + errorlist = [] for ent in srclist: if 'source' not in ent: @@ -163,7 +178,7 @@ def add_sources(srclist, template_params=None): continue source = ent['source'] - if source.startswith("ppa:"): + if aa_repo_match(source): try: util.subp(["add-apt-repository", source]) except: -- cgit v1.2.3 From d8307152015808fe655649488afe7fb9bf3eea92 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Thu, 24 Oct 2013 12:24:08 -0700 Subject: fix use search --- cloudinit/config/cc_apt_configure.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudinit/config/cc_apt_configure.py b/cloudinit/config/cc_apt_configure.py index 7121ec43..4f44e4af 100644 --- a/cloudinit/config/cc_apt_configure.py +++ b/cloudinit/config/cc_apt_configure.py @@ -85,7 +85,7 @@ def handle(name, cfg, cloud, log, _args): matchcfg = cfg.get('add_apt_repo_match', ADD_APT_REPO_MATCH) if matchcfg: - matcher = re.compile(matchcfg) + matcher = re.compile(matchcfg).search else: matcher = lambda f: False -- cgit v1.2.3 From a5c12d448157fe984e60c7bd84fb588b9c2ae035 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Thu, 24 Oct 2013 13:18:52 -0700 Subject: allow template rendering before passing to add-apt-repository --- cloudinit/config/cc_apt_configure.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cloudinit/config/cc_apt_configure.py b/cloudinit/config/cc_apt_configure.py index 4f44e4af..5819897d 100644 --- a/cloudinit/config/cc_apt_configure.py +++ b/cloudinit/config/cc_apt_configure.py @@ -178,6 +178,8 @@ def add_sources(srclist, template_params=None, aa_repo_match=None): continue source = ent['source'] + source = templater.render_string(source, template_params) + if aa_repo_match(source): try: util.subp(["add-apt-repository", source]) @@ -185,8 +187,6 @@ def add_sources(srclist, template_params=None, aa_repo_match=None): errorlist.append([source, "add-apt-repository failed"]) continue - source = templater.render_string(source, template_params) - if 'filename' not in ent: ent['filename'] = 'cloud_config_sources.list' -- cgit v1.2.3 From cee0343a3d09a465edaadf45749e2b7048376b0a Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Thu, 24 Oct 2013 13:19:38 -0700 Subject: mention in doc --- doc/examples/cloud-config.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/examples/cloud-config.txt b/doc/examples/cloud-config.txt index bcfd7917..546a1b98 100644 --- a/doc/examples/cloud-config.txt +++ b/doc/examples/cloud-config.txt @@ -72,6 +72,10 @@ apt_pipelining: False # then apt_mirror above will have no effect apt_preserve_sources_list: true +# 'source' entries in apt-sources that match this python regex +# expression will be passed to add-apt-repository +add_apt_repo_match = "^[\w-]+:\w" + apt_sources: - source: "deb http://ppa.launchpad.net/byobu/ppa/ubuntu karmic main" keyid: F430BBA5 # GPG key ID published on a key server -- cgit v1.2.3 From 0473a7f4acacdfe2d9e3840a6f89399f60f7a9d2 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Thu, 24 Oct 2013 13:22:00 -0700 Subject: improve error message on failure of add-apt-repository --- cloudinit/config/cc_apt_configure.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cloudinit/config/cc_apt_configure.py b/cloudinit/config/cc_apt_configure.py index 5819897d..b094ea22 100644 --- a/cloudinit/config/cc_apt_configure.py +++ b/cloudinit/config/cc_apt_configure.py @@ -183,8 +183,9 @@ def add_sources(srclist, template_params=None, aa_repo_match=None): if aa_repo_match(source): try: util.subp(["add-apt-repository", source]) - except: - errorlist.append([source, "add-apt-repository failed"]) + except util.ProcessExecutionError as e: + errorlist.append([source, + ("add-apt-repository failed. " + str(e))]) continue if 'filename' not in ent: -- cgit v1.2.3