Skip to content

Commit

Permalink
[BGBUILD-320] Support variable substitution in any string value field…
Browse files Browse the repository at this point in the history
… of appliance definition files

Update specs
  • Loading branch information
msavy committed Dec 8, 2011
1 parent 19a04be commit 4f43029
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -1,5 +1,6 @@
v0.3.10
* [BGBUILD-324] Add wildcard to packages schema
* [BGBUILD-320] Support variable substitution in any string value field of appliance definition

v0.3.9

Expand Down
26 changes: 10 additions & 16 deletions lib/boxgrinder-core/helpers/appliance-config-helper.rb
Expand Up @@ -39,6 +39,7 @@ def merge(appliance_config)
merge_packages
merge_files
merge_post_operations
substitute_variables

@appliance_config
end
Expand Down Expand Up @@ -90,6 +91,12 @@ def resolve(resolve_stack = nil, resolved_set = Set.new())
end
end

def substitute_variables
@appliance_config.all_values.each do |value|
value.gsub!(/(?:#(.*?)#)+?/){ |m| @appliance_config.variables.has_key?($1) ? @appliance_config.variables[$1] : m }
end
end

def merge_hardware
merge_cpus
merge_partitions
Expand Down Expand Up @@ -162,25 +169,12 @@ def merge_repos
@appliance_config.repos.clear

@appliance_configs.each do |appliance_config|
for repo in appliance_config.repos
repo['name'] = substitute_vars(repo['name'])
['baseurl', 'mirrorlist'].each do |type|
repo[type] = substitute_vars(repo[type]) unless repo[type].nil?
end

appliance_config.repos.each do |repo|
@appliance_config.repos << repo
end
end
end

def substitute_vars(str)
return if str.nil?
@appliance_config.variables.keys.each do |var|
str = str.gsub("##{var}#", @appliance_config.variables[var])
end
str
end

def merge_packages
@appliance_config.packages.clear

Expand All @@ -202,7 +196,7 @@ def merge_files
next if included.include?(appliance_config)
appliance_config.files.each do |dir, files|
@appliance_config.files[dir] = [] if @appliance_config.files[dir].nil?
files.each { |f| @appliance_config.files[dir] << substitute_vars(f) }
files.each { |f| @appliance_config.files[dir] << f }
end
included << appliance_config
end
Expand All @@ -217,7 +211,7 @@ def merge_post_operations
next if included.include?(appliance_config)
appliance_config.post.each do |platform, cmds|
@appliance_config.post[platform] = [] if @appliance_config.post[platform].nil?
cmds.each { |cmd| @appliance_config.post[platform] << substitute_vars(cmd) }
cmds.each { |cmd| @appliance_config.post[platform] << cmd }
end
included << appliance_config
end
Expand Down
19 changes: 19 additions & 0 deletions lib/boxgrinder-core/models/appliance-config.rb
Expand Up @@ -94,6 +94,25 @@ def initialize_paths
self
end

def all_values(input=nil)
avoid = ['@variables']
input = (self.instance_variables - avoid).
collect{ |v| self.instance_variable_get(v) } if input.nil?

vars = input.inject([]) do |arr, var|
case var
when Hash
arr.concat(all_values(var.values))
when Array
arr.concat(all_values(var))
when String
arr.push var
end
arr
end
vars
end

# Returns default filesystem type for current OS
def default_filesystem_type
fs = 'ext4'
Expand Down
1 change: 1 addition & 0 deletions rubygem-boxgrinder-core.spec
Expand Up @@ -79,6 +79,7 @@ popd
* Thu Dec 1 2011 Marc Savy <mgoldman@redhat.com> - 0.3.10
- Upstream release: 0.3.10
- [BGBUILD-324] Add wildcard to packages schema
- [BGBUILD-320] Support variable substitution in any string value field of appliance definition

* Fri Oct 14 2011 Marc Savy <msavy@redhat.com> - 0.3.9-1
- Upstream release: 0.3.9
Expand Down
25 changes: 25 additions & 0 deletions spec/helpers/appliance-config-helper-spec.rb
Expand Up @@ -405,6 +405,7 @@ def prepare_helper(configs)

@helper.merge_variables
@helper.merge_repos
@helper.substitute_variables

config = @helper.instance_variable_get(:@appliance_config)
config.repos.size.should == 1
Expand All @@ -425,6 +426,7 @@ def prepare_helper(configs)

@helper.merge_variables
@helper.merge_post_operations
@helper.substitute_variables

config = @helper.instance_variable_get(:@appliance_config)
config.post.size.should == 2
Expand All @@ -447,12 +449,35 @@ def prepare_helper(configs)

@helper.merge_variables
@helper.merge_post_operations
@helper.substitute_variables

config = @helper.instance_variable_get(:@appliance_config)
config.post.size.should == 1
config.post['base'].should == [@arch, @base_arch, '12', 'fedora', 'AAA', 'BBB']
end

it "should allow variable substitution for any string value" do
config_a = ApplianceConfig.new
config_a.name = 'a'
config_a.os.name = "fedora-#CUSTOM_B#"
config_a.summary = "boxgrinder-#CUSTOM_A#"
config_a.os.version = '12'
config_a.variables['CUSTOM_A'] = "AAA"
config_a.variables['CUSTOM_B'] = "BBB"
config_a.init_arch

prepare_helper([config_a])
@helper.instance_variable_set(:@appliance_config, config_a.clone)

@helper.merge_variables
@helper.merge_post_operations
@helper.substitute_variables

config = @helper.instance_variable_get(:@appliance_config)
config.os.name.should == "fedora-BBB"
config.summary.should == "boxgrinder-AAA"
end

describe ".merge_default_repos" do
it "should set default_repos option to true when not specified" do
config_a = ApplianceConfig.new
Expand Down

0 comments on commit 4f43029

Please sign in to comment.