Commit 50a12dda authored by Volker Hilsheimer's avatar Volker Hilsheimer

Implement merging of roles

If a role is defined multiple times, merge the parameters.
Otherwise, the provisioning script will only be run once, with the
parameters from the last definition, resulting in incomplete provisioning.

Add unit test for the merging of roles. The order in which the parameters
get merged is generally undefined.
parent 4c354642
......@@ -16,10 +16,11 @@ def merge_yaml(first, second)
if first.is_a?(Array)
if second.is_a?(Array)
return first + second
result = first + second
else
result = first.clone
result << second
end
result = first.clone
result << second
return result
end
......@@ -132,6 +133,38 @@ def load_includes(yaml, basedir)
return yaml
end
def merge_roles(machines)
# post processing of roles: if a role is defined multiple times, merge them
machines.each do |machine|
roles = machine["roles"]
unless roles.nil?
role_set = []
role_indices = {}
index = 0
roles.each do |role|
if role.is_a?(Hash)
rolename = role["role"]
if role_indices.has_key?(rolename)
index = role_indices[rolename]
role_set[index] = merge_yaml(role_set[index], role)
else
role_indices[rolename] = index
role_set << role
index += 1
end
else
role_set << role
role_indices[rolename] = index
index += 1
end
end
role_set.each do |role|
machine["roles"] = role_set
end
end
end
end
def load_minicoin()
begin # see tests/autotest.rb
return load_testmachines()["machines"]
......@@ -165,6 +198,9 @@ def load_minicoin()
load_settings(yaml, local_yaml)
machines = load_boxes(yaml, user_yaml, true)
machines = load_boxes(yaml, local_yaml, false)
merge_roles(machines)
load_urls(yaml, user_yaml)
load_urls(yaml, local_yaml)
......
......@@ -20,7 +20,10 @@ def load_testmachines()
user_output = load_includes(user_output, root)
load_settings(test_output, user_output)
load_boxes(test_output, user_output, true)
machines = load_boxes(test_output, user_output, true)
merge_roles(machines)
load_urls(test_output, user_output)
return test_output
......@@ -118,6 +121,9 @@ class Tester
"roles" => [{"role" => "hello-world"}, {"role" => "script", "script" => "hello"}],
"private_net" => "1.1.1.1"
},
{"name" => "merged_role", "box" => "generic",
"roles" => [{"role" => "mutagen", "paths" => ["path2", "path1"]}]
},
{"name" => "submachine", "box" => "subgeneric"},
{"name" => "machine1", "box" => "duplicate", "gui" => true }
]
......
......@@ -30,3 +30,9 @@ machines:
box: generic
roles:
- role: hello-world
- name: merged_role
box: generic
roles:
- role: mutagen
paths:
- path2
......@@ -21,3 +21,8 @@ machines:
- role: script
script: "hello"
private_net: 1.1.1.1
- name: merged_role
roles:
- role: mutagen
paths:
- path1
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment