Commit c5b004df authored by Cyril Rohr's avatar Cyril Rohr
Browse files

can now pass yaml configuration files to the generators. variables can be...

can now pass yaml configuration files to the generators. variables can be accessed via the lookup function
parent d7c0c9e9
......@@ -22,14 +22,23 @@ simulation_mode = !$*.delete("-s").nil?
if $*.empty?
puts usage
exit -1
elsif ($*.map{|file| File.exists?(file) && File.extname(file) == ".rb"}.include? false)
puts "Error: your input files do not exist or are not ruby files (.rb extension)."
elsif ($*.map{|file| File.exists?(file) && (File.extname(file) == ".rb" || File.extname(file) == ".yaml")}.include? false)
puts "Error: your input files do not exist or are not ruby files (.rb extension) or config files (.yaml extension)."
exit -1
else
description_files = $*
puts "[Input files:\t\t #{description_files.join(", ")}]"
input = {}
config = {}
description_files.each do |filename|
case File.extname(filename)
when ".rb" then input[File.basename(filename, ".rb")] = File.read(filename)
when ".yaml" then config[File.basename(filename, ".yaml")] = YAML.load_file(filename)
end
end
puts "[Input files:\t\t #{input.keys.join(", ")}]"
puts "[Config files:\t\t #{config.keys.join(", ")}]"
puts "[Simulation mode:\t #{simulation_mode}]"
generator = G5K::ReferenceGenerator.new({:uid => "grid5000", :type => "grid"}, *description_files)
generator = G5K::ReferenceGenerator.new({:uid => "grid5000", :type => "grid"}, :input => input, :config => config)
data = generator.generate
directory_to_write = File.expand_path File.join(File.dirname(__FILE__), "../data")
generator.write(directory_to_write, :simulate => simulation_mode)
......
......@@ -54,6 +54,8 @@ module G5K
class ReferenceGenerator
attr_reader :data
attr_reader :config
attr_reader :input
def method_missing(method, *args)
@context.recursive_merge!(method.to_sym => args.first)
......@@ -63,6 +65,35 @@ class ReferenceGenerator
Resolv.getaddress(network_address)
end
#
# usage:
# lookup('nancy', 'nodes', 'paramount-1', 'property_name')
# or
# lookup('nancy', 'nodes') { |result| result['paramount-1']['property_name'] }
# or
# lookup('nancy') { |result| result['nodes']['paramount-1']['property_name'] }
#
# assuming you passed a <tt>nancy.yaml</tt> file to the generator
# Be careful with null values!
#
def lookup(filename, *keys, &block)
if config.has_key?(filename)
result = config[filename]
if !keys.empty?
while !keys.empty? do
result = result[keys.shift]
break if result.nil?
end
end
if block
block.call(result)
else
result
end
else
nil
end
end
# This doesn't work with Ruby < 1.8.7. Replaced by a call to build_context (see below).
#
# %w{site cluster environment node service}.each do |method|
......@@ -126,17 +157,16 @@ class ReferenceGenerator
# Initializes a new generator that will generate data files in a hierachical way.
# The root of the tree will be named with the value of <tt>data_description[:uid]</tt>.
def initialize(data_description = {:uid => ""}, *files)
@files = files
def initialize(data_description = {:uid => ""}, options = {:input => {}, :config => {}})
@input = options[:input] || raise(ArgumentError, "INPUT cannot be null.")
@config = options[:config] || {}
@data = G5K::Tree.new.replace(data_description)
@context = @data
end
def generate
@files.each do |file|
File.open(file, 'r') do |f|
eval(f.read)
end
input.each do |filename, content|
eval(content)
end
@data
end
......
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