From d16632ad8dfd1844d265d93ab00b54d419626019 Mon Sep 17 00:00:00 2001 From: Dylan Perry Date: Fri, 7 Apr 2017 15:43:35 +1000 Subject: Fix yum repo config where keys contain array values ConfigObj produces configuration files that are incompatible with yum if multiple values are listed for a configuration key. Switch to the builtin configparser, and ConfigParser (Python 2) which correctly handles this case. Add additional test case for array values in yum_repos definition LP: #1592150 --- cloudinit/config/cc_yum_add_repo.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'cloudinit/config') diff --git a/cloudinit/config/cc_yum_add_repo.py b/cloudinit/config/cc_yum_add_repo.py index a04e1b2a..6a42f499 100644 --- a/cloudinit/config/cc_yum_add_repo.py +++ b/cloudinit/config/cc_yum_add_repo.py @@ -32,7 +32,10 @@ entry, the config entry will be skipped. import os -import configobj +try: + from configparser import ConfigParser +except ImportError: + from ConfigParser import ConfigParser import six from cloudinit import util @@ -53,7 +56,7 @@ def _format_repo_value(val): if isinstance(val, (list, tuple)): # Can handle 'lists' in certain cases # See: https://linux.die.net/man/5/yum.conf - return "\n ".join([_format_repo_value(v) for v in val]) + return "\n".join([_format_repo_value(v) for v in val]) if not isinstance(val, six.string_types): return str(val) return val @@ -62,16 +65,19 @@ def _format_repo_value(val): # TODO(harlowja): move to distro? # See man yum.conf def _format_repository_config(repo_id, repo_config): - to_be = configobj.ConfigObj() - to_be[repo_id] = {} + to_be = ConfigParser() + to_be.add_section(repo_id) # Do basic translation of the items -> values for (k, v) in repo_config.items(): # For now assume that people using this know # the format of yum and don't verify keys/values further - to_be[repo_id][k] = _format_repo_value(v) - lines = to_be.write() - lines.insert(0, "# Created by cloud-init on %s" % (util.time_rfc2822())) - return "\n".join(lines) + to_be.set(repo_id, k, _format_repo_value(v)) + to_be_stream = six.StringIO() + to_be.write(to_be_stream) + to_be_stream.seek(0) + lines = to_be_stream.readlines() + lines.insert(0, "# Created by cloud-init on %s\n" % (util.time_rfc2822())) + return "".join(lines) def handle(name, cfg, _cloud, log, _args): -- cgit v1.2.3