diff --git a/generators/grid5000.rb b/generators/grid5000.rb index 4ac3602d7caff4a13b7919a6c199622505d119da..f8bd49eb3f3aff49db4886e9ce33c19643880c5a 100755 --- a/generators/grid5000.rb +++ b/generators/grid5000.rb @@ -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) diff --git a/generators/lib/g5k_generator.rb b/generators/lib/g5k_generator.rb index 4ed1c104c6d765b2eb449c3563fdb3266586784d..2c4ea28652b8cf1e3f9b58c41f407506a5837c40 100755 --- a/generators/lib/g5k_generator.rb +++ b/generators/lib/g5k_generator.rb @@ -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