Commit c41d0964 authored by Jérémie Gaidamour's avatar Jérémie Gaidamour
Browse files

[dev] Using a .yaml.erb file to bootstrap the installation of a cluster

parent 0b449f42
require 'optparse'
require 'erb'
require 'pp'
# Process command-line options
#pp ARGV
erb = ERB.new(File.read("input/graoully.yaml.erb"))
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: bootstrap-cluster.rb [options]"
# default
options[:cluster_name] = "cluster"
options[:output_dir] = "."
opts.on("-n", "--cluster-name [CLUSTER]", "Cluster name (ex: graoully)") do |n|
options[:cluster_name] = n
end
opts.on("-o", "--output-dir [DIR]", "Output Directory (ex: input/grid5000/sites/nancy/clusters/graoully/)") do |n|
options[:output_dir] = n
end
opts.on("-e", "--file-eth0 FILE", "File containing the list of mac addresses for eth0") do |n|
options[:feth0] = n
end
opts.on("-b", "--file-bmc FILE", "File containing List of mac addresses for bmc") do |n|
options[:fbmc] = n
end
opts.on("-h", "--help", "Prints this help") do
puts opts
exit
end
end.parse!
raise OptionParser::MissingArgument,"Missing --file-eth0 (-e) command line argument" if options[:feth0].nil?
raise OptionParser::MissingArgument,"Missing --file-bmc (-b) command line argument" if options[:fbmc].nil?
#pp options
# Output file
output_file = File.join(options[:output_dir], options[:cluster_name] + '.yaml')
dirname = File.dirname(output_file)
FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
#puts "Output file: #{output_file}"
eth0_list = File.readlines(options[:feth0]).map { |n| n.strip }
bmc_list = File.readlines(options[:fbmc] ).map { |n| n.strip }
# Add ":" separator in mac addresses
eth0_list = eth0_list.map{|mac| mac.split(%r{(..)}).reject(&:empty?).join(":") }
bmc_list = bmc_list.map{|mac| mac.split(%r{(..)}).reject(&:empty?).join(":") }
cluster_name = options[:cluster_name]
erb = ERB.new(File.read("templates/cluster.yaml.erb"))
File.open(output_file, "w+") { |f|
f.write(erb.result(binding))
}
puts erb.result(binding)
---
nodes:
<% eth0_list.each_with_index{|eth0, i| %>
<%= cluster_name %>-<%= i+1 %>:
network_interfaces:
eth0:
ip:
mac: <%= eth0_list[i] %>
bmc:
ip:
mac: <%= bmc_list[i] %>
<% } %>
# Load a hierarchy of YAML file into a Ruby hash
require 'yaml'
require 'erb'
require '../lib/hash/hash.rb'
def load_yaml_file_hierarchy(directory)
......@@ -12,13 +13,19 @@ def load_yaml_file_hierarchy(directory)
# Recursively list the .yaml files.
# The order in which the results are returned depends on the system (http://ruby-doc.org/core-2.2.3/Dir.html).
# => List deepest files first as they have lowest priority when hash keys are duplicated.
list_of_yaml_files = Dir['**/*.y*ml'].sort_by { |x| -x.count('/') }
list_of_yaml_files = Dir['**/*.y*ml', '**/*.y*ml.erb'].sort_by { |x| -x.count('/') }
list_of_yaml_files.each { |filename|
# Load YAML
file_hash = YAML::load_file(filename)
if not file_hash
if /\.y.*ml\.erb$/.match(filename)
# For files with .erb.yaml extensions, process the template before loading the YAML.
file_hash = YAML::load(ERB.new(File.read(filename)).result(binding))
else
file_hash = YAML::load_file(filename)
end
if not file_hash
puts "Error loading '#{filename}'"
next
end
......
nodes:
<% (1..16).each { |i| %>
graoully-<%= i %>:
network_interfaces:
eth0:
mounted: true
ip: 172.16.70.<%= i %>
mac: <%= %w(
24:6E:96:03:0D:08
24:6E:96:03:0A:28
24:6E:96:03:11:08
24:6E:96:03:0E:58
24:6E:96:03:1F:90
24:6E:96:03:0C:20
24:6E:96:03:1E:08
24:6E:96:03:12:70
24:6E:96:03:1E:D0
24:6E:96:03:0D:F8
24:6E:96:03:1F:98
24:6E:96:03:09:98
24:6E:96:03:24:08
24:6E:96:03:12:D8
24:6E:96:03:09:F8
24:6E:96:03:0F:50
24:6E:96:03:0B:A0
24:6E:96:03:24:20
)[i] %>
bmc:
ip: 172.17.70.<%= i %>
mac: <%= %w(
64:00:6A:C1:57:D2
64:00:6A:C1:56:A8
64:00:6A:C1:56:7E
64:00:6A:C1:56:C6
64:00:6A:C0:DD:5E
64:00:6A:C0:F1:F4
64:00:6A:C0:F2:5E
64:00:6A:C0:DC:F4
64:00:6A:C0:F2:A6
64:00:6A:C0:F1:E4
64:00:6A:C0:F2:2E
64:00:6A:C0:DC:36
64:00:6A:C0:DC:A4
64:00:6A:C1:56:C8
64:00:6A:C1:56:EE
64:00:6A:C1:55:FE
64:00:6A:C1:56:9A
64:00:6A:C0:E5:BA
)[i] %>
<% } %>
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