Fix the cloud config merging so that it is backwards compat.

The new change for merging works well in the mergedict case
but the default merging type for cloud config needs to reflect
how yaml was loaded in bulk, which is the same as the replacing
keys merging type that is now provided.
This commit is contained in:
Joshua Harlow 2013-05-09 22:34:31 -07:00
parent 23b56e5ef8
commit 8867c8096b
11 changed files with 65 additions and 5 deletions

View File

@ -30,7 +30,13 @@ from cloudinit.settings import (PER_ALWAYS)
LOG = logging.getLogger(__name__)
MERGE_HEADER = 'Merge-Type'
DEF_MERGERS = mergers.default_mergers()
# Due to the way the loading of yaml configuration was done previously,
# where previously each cloud config part was appended to a larger yaml
# file and then finally that file was loaded as one big yaml file we need
# to mimic that behavior by altering the default strategy to be replacing
# keys of later mergers.
DEF_MERGERS = mergers.string_extract_mergers('dict(replace)+list()+str()')
class CloudConfigPartHandler(handlers.Handler):
@ -53,6 +59,8 @@ class CloudConfigPartHandler(handlers.Handler):
if self.file_names:
file_lines.append("# from %s files" % (len(self.file_names)))
for fn in self.file_names:
if not fn:
fn = '?'
file_lines.append("# %s" % (fn))
file_lines.append("")
if self.cloud_buf is not None:
@ -111,7 +119,10 @@ class CloudConfigPartHandler(handlers.Handler):
return
try:
self._merge_part(payload, headers)
self.file_names.append(filename)
# Ensure filename is ok to store
for i in ("\n", "\r", "\t"):
filename = filename.replace(i, " ")
self.file_names.append(filename.strip())
except:
util.logexc(LOG, "Failed at merging in cloud config part from %s",
filename)

View File

@ -0,0 +1,5 @@
#cloud-config
a: 22
b: 4
c: 3

View File

@ -0,0 +1,5 @@
#cloud-config
a:
e:
y: 2

View File

@ -1,3 +1,3 @@
Blah: 1
Blah: 3
Blah2: 2
Blah3: 3
Blah3: [1]

View File

@ -0,0 +1,5 @@
#cloud-config
a: 1
b: 2
c: 3

View File

@ -0,0 +1,3 @@
#cloud-config
b: 4

View File

@ -0,0 +1,3 @@
#cloud-config
a: 22

View File

@ -0,0 +1,8 @@
#cloud-config
a:
c: 1
d: 2
e:
z: a
y: b

View File

@ -0,0 +1,5 @@
#cloud-config
a:
e:
y: 2

View File

@ -167,6 +167,21 @@ class TestSimpleRun(helpers.ResourceUsingTestCase):
d = util.mergemanydict([a, b])
self.assertEquals(c, d)
def test_compat_merges_dict(self):
a = {
'Blah': 1,
'Blah2': 2,
'Blah3': 3,
}
b = {
'Blah': 1,
'Blah2': 2,
'Blah3': [1],
}
c = _old_mergedict(a, b)
d = util.mergemanydict([a, b])
self.assertEquals(c, d)
def test_compat_merges_list(self):
a = {'b': [1, 2, 3]}
b = {'b': [4, 5]}

View File

@ -108,7 +108,7 @@ p: 1
contents = util.load_yaml(contents)
self.assertEquals(contents['run'], ['b', 'c', 'stuff', 'morestuff'])
self.assertEquals(contents['a'], 'be')
self.assertEquals(contents['e'], 'fg')
self.assertEquals(contents['e'], [1, 2, 3])
self.assertEquals(contents['p'], 1)
def test_unhandled_type_warning(self):