conmang5k.rb 2.8 KB
Newer Older
1
2
#!/usr/bin/ruby

Jérémie Gaidamour's avatar
Jérémie Gaidamour committed
3
4
5
6
7
if RUBY_VERSION < "2.1"
  puts "This script requires ruby >= 2.1"
  exit
end

8
# This script generates conmang5k/files/<site_uid>/conman.conf from input/, conf/console.yaml and conf/console-password.yaml
9

10
11
require 'pp'
require 'erb'
12
require 'pathname'
13
require 'optparse'
14
15
require_relative '../lib/input_loader'
require_relative '../lib/hash/hash.rb'
16

17
18
19
options = {}
options[:sites] = %w{grenoble lille luxembourg lyon nancy nantes rennes sophia}
options[:output_dir] = "/tmp/puppet-repo"
20
options[:conf_dir] = File.expand_path("conf-examples/", File.dirname(__FILE__))
21
22
23
24
25

OptionParser.new do |opts|
  opts.banner = "Usage: conmang5k.rb [options]"

  opts.separator ""
26
  opts.separator "Example: ruby conmang5k.rb -s nancy -o /tmp/puppet-repo"
27
  opts.separator "Example: ruby conmang5k.rb -s nancy -o $GPUPPET_PREFIX/repo"
28
29
30

  opts.on('-o', '--output-dir dir', String, 'Select the puppet repo path', "Default: " + options[:output_dir]) do |d|
    options[:output_dir] = d
Pascal Morillon's avatar
Pascal Morillon committed
31
    options[:conf_dir] = "#{options[:output_dir]}/platforms/production/generators/ipmitools/"
32
33
  end

34
  opts.on('-c', '--conf-dir dir', String, 'Select the conman configuration path', "Default: #{options[:conf_dir]}") do |d|
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
    options[:conf_dir] = d
  end

  opts.separator ""
  opts.separator "Filters:"

  opts.on('-s', '--sites a,b,c', Array, 'Select site(s)', "Default: " + options[:sites].join(", ")) do |s|
    raise "Wrong argument for -s option." unless (s - options[:sites]).empty?
    options[:sites] = s
  end

  opts.on_tail("-h", "--help", "Show this message") do
    puts opts
    exit
  end
end.parse!

52
raise("Error: #{options[:conf_dir]} does not exist. The given configuration path is incorrect") unless Pathname(options[:conf_dir].to_s).exist?
53
54
55
56

puts "Writing Conman configuration files to: #{options[:output_dir]}"
puts "Using configuration directory: #{options[:conf_dir]}"
puts "For site(s): #{options[:sites].join(', ')}"
57

58
59
60
61
#Input
input_data_dir = "../../input/grid5000/"
refapi = load_yaml_file_hierarchy(File.expand_path(input_data_dir, File.dirname(__FILE__)))

62
63
config      = YAML::load_file(options[:conf_dir] + 'console.yaml')
credentials = YAML::load_file(options[:conf_dir] + 'console-password.yaml')
64

65
# Apply ERB template and save result to file
66
def write_conman_file(site_uid, site_refapi, site_config, site_credentials, options)
67
  output = ERB.new(File.read(File.expand_path('templates/conman.erb', File.dirname(__FILE__)))).result(binding)
68

69
70
  output_file = Pathname("#{options[:output_dir]}/platforms/production/modules/generated/files/conman/server/#{site_uid}.conf")

71
72
  output_file.dirname.mkpath()
  File.write(output_file, output)
73
74
end

75
76
# Loop over each site
refapi["sites"].each { |site_uid, site_refapi|
77
78
79
80

  next unless options[:sites].include?(site_uid)

  write_conman_file(site_uid, site_refapi, config[site_uid], credentials[site_uid], options)
81
}