From c03e77a63a37ac7ab9ad12c3e42e633547aa1688 Mon Sep 17 00:00:00 2001 From: Evgeni Golov Date: Sun, 10 Apr 2016 13:33:48 +0200 Subject: [PATCH 1/2] strip whitespace from key and value before inserting it into the config before the following would produce four entries: container_config: - "lxc.network.flags=up" - "lxc.network.flags =up" - "lxc.network.flags= up" - "lxc.network.flags = up" let's strip the whitespace and insert only one "lxc.network.flags = up" into the final config Signed-off-by: Evgeni Golov --- cloud/lxc/lxc_container.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cloud/lxc/lxc_container.py b/cloud/lxc/lxc_container.py index ea4952f6b03..3cbff3314e8 100644 --- a/cloud/lxc/lxc_container.py +++ b/cloud/lxc/lxc_container.py @@ -745,6 +745,8 @@ class LxcContainerManagement(object): config_change = False for key, value in parsed_options: + key = key.strip() + value = value.strip() new_entry = '%s = %s\n' % (key, value) for option_line in container_config: # Look for key in config From 8db3a639837e836dd02030c5177301bb699e5de7 Mon Sep 17 00:00:00 2001 From: Evgeni Golov Date: Sun, 10 Apr 2016 13:37:00 +0200 Subject: [PATCH 2/2] fix handling of config options that share the same prefix container_config: - "lxc.network.ipv4.gateway=auto" - "lxc.network.ipv4=192.0.2.1" might try to override lxc.network.ipv4.gateway in the second entry as both start with "lxc.network.ipv4". use a regular expression to find a line that contains (optional) whitespace and an = after the key. Signed-off-by: Evgeni Golov --- cloud/lxc/lxc_container.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cloud/lxc/lxc_container.py b/cloud/lxc/lxc_container.py index 3cbff3314e8..d19101dd208 100644 --- a/cloud/lxc/lxc_container.py +++ b/cloud/lxc/lxc_container.py @@ -424,6 +424,8 @@ lxc_container: sample: True """ +import re + try: import lxc except ImportError: @@ -748,9 +750,10 @@ class LxcContainerManagement(object): key = key.strip() value = value.strip() new_entry = '%s = %s\n' % (key, value) + keyre = re.compile(r'%s(\s+)?=' % key) for option_line in container_config: # Look for key in config - if option_line.startswith(key): + if keyre.match(option_line): _, _value = option_line.split('=', 1) config_value = ' '.join(_value.split()) line_index = container_config.index(option_line)