diff --git a/Rakefile b/Rakefile index f553d75ba466b9d39d347cae4789b7aeeb791257..8c9424c7f6c935072e2c2a5134e36464ab05142d 100644 --- a/Rakefile +++ b/Rakefile @@ -207,11 +207,20 @@ namespace :gen do namespace :puppet do - all_puppet_tasks = [:bindg5k, :conmang5k, :dhcpg5k, :kadeployg5k, :lanpowerg5k, :kavlang5k, :kwollectg5k, :network_monitoring, :'refapi-subset', :oxidizedg5k, :'oarsub-simplifier-aliases', :kavlanngg5k] + all_puppet_tasks = [:bindg5k, :conmang5k, :dhcpg5k, :kadeployg5k, :lanpowerg5k, :kavlang5k, :kwollectg5k, :network_monitoring, :'refapi-subset', :oxidizedg5k, :'oarsub-simplifier-aliases', :kavlanngg5k, :stitcherg5k] all_puppet_tasks.each { |t| generated_desc = (t == :'refapi-subset') ? 'description' : 'configuration' - desc "Generate #{t} #{generated_desc} -- parameters: [SITE={grenoble,...}] [OUTPUTDIR=(default: #{PUPPET_ODIR})] [CONFDIR=...] [VERBOSE=1]" + + # tasks oxidizedg5k, kavlanng5k and stitcherg5K doesn't use the parameters + # SITE, CLUSTER and VERBOSE. So we don't print them in the 'rake -T' for + # theses tasks. + parameters = "[OUTPUTDIR=(default: #{PUPPET_ODIR})] [CONFDIR=...]" + if not [:oxidizedg5k, :kavlanngg5k, :stitcherg5k].include? t + parameters = "[SITE={grenoble,...}] #{parameters} [VERBOSE=1]" + end + desc "Generate #{t} #{generated_desc} -- parameters: #{parameters}" + task t do require "refrepo/gen/puppet/#{t}" options = {} diff --git a/lib/refrepo/gen/puppet/stitcherg5k.rb b/lib/refrepo/gen/puppet/stitcherg5k.rb new file mode 100644 index 0000000000000000000000000000000000000000..0b4f323618d9a4238016b1ee15e18e211b011173 --- /dev/null +++ b/lib/refrepo/gen/puppet/stitcherg5k.rb @@ -0,0 +1,55 @@ + +VLANS_FILE = "input/grid5000/vlans.yaml" + +PLATFORM = "platforms/production" +HIERA_STITCHER_TEMPLATE = "#{PLATFORM}/generators/stitcher/stitcher.yaml" +HIERA_STITCHER_OUTPUT_PATH = "#{PLATFORM}/modules/generated/files/grid5000/stitcher/stitcher.yml" + +STITCHER_MODES = ['production', 'development', 'test'] + +# main method +def generate_puppet_stitcherg5k(options) + $options = options + output_file_path = "#{$options[:output_dir]}/#{HIERA_STITCHER_OUTPUT_PATH}" + puts "Writing stitcher configuration in #{output_file_path}" + output = File.new(output_file_path, "w+") + refapi = load_data_hierarchy + + base_config = YAML.load_file("#{$options[:output_dir]}/#{HIERA_STITCHER_TEMPLATE}") + sorted_kavlans = Hash.new + + # Ruby sorting dark magic happening below: + # The goal is to order the vlans entries by vlan id to have an easy to read conf file. + # Since ruby hashes enumerate their values in the order + # that the corresponding keys were inserted, we sort the + # vlan keys and reinsert them back with their values in a + # new hash that is de facto sorted. + build_stitcher_kavlans_hash(refapi) + .sort_by {|key, _value| key} + .each {|a| sorted_kavlans[a[0]] = a[1]} + + output_hash = merge_config(base_config, sorted_kavlans) + output.write("# MANAGED BY PUPPET\n") + output.write(output_hash.to_yaml) +end + +def build_stitcher_kavlans_hash(refapi) + stitcher_kavlans_hash = {} + refapi['sites'].each do |name, site_hash| + global_kavlan = site_hash['kavlans'].select { | id, _hash | id =~ /\d\d/ }.first + global_kavlan_id = global_kavlan.first.to_i + stitcher_kavlans_hash[global_kavlan_id] = {"vlan" => 700 + global_kavlan_id, "site" => name} + end + return stitcher_kavlans_hash +end + +def merge_config(base_config, kavlans) + output_hash = {} + STITCHER_MODES.each do |mode| + output_hash[mode] = {'kavlans' => kavlans} + base_config.each do |key, value| + output_hash[mode][key] = value + end + end + return output_hash +end