dhcp.rb 4.18 KB
Newer Older
1
2
3
4
#!/usr/bin/ruby

require 'pp'
require 'erb'
5
require 'pathname'
6
7
require '../lib/input_loader'

8
9
global_hash = load_yaml_file_hierarchy("../input/grid5000/")
$output_dir = ENV['puppet_repo'] || 'output'
10

11
12
# Get the mac and ip of a node. Throw exception if error.
def get_network_info(node_hash, network_interface)
Jérémie Gaidamour's avatar
Jérémie Gaidamour committed
13
14
  # Get node_hash["network_adapters"][network_interface]["ip"] and node_hash["network_adapters"][network_interface]["mac"]
  node_network_adapters = node_hash.fetch("network_adapters")
15
16
17
18
  
  # For the production network, find the mounted interface (either eth0 or eth1)
  neti = network_interface
  if neti == "eth" then
19
20
21
22
    (0..4).each {|i|
      if node_network_adapters.fetch("eth#{i}").fetch("mounted")
        neti = "eth#{i}"
        break
23
      end
24
25
    }
    raise 'none of the eth[0-4] interfaces have the property "mounted" set to "true"' if neti == 'eth'
26
  end
27
  
Jérémie Gaidamour's avatar
Jérémie Gaidamour committed
28
  node_network_interface = node_network_adapters.fetch(neti)
29
30
31
32
33
  
  raise '"mac" is nil' unless node_mac = node_network_interface.fetch("mac")
  raise '"ip" is nil'  unless node_ip  = node_network_interface.fetch("ip")
  
  return [node_ip, node_mac]
34
35
end

36
37
def write_dhcp_file(data)
  if data["nodes"].nil?
Jérémie Gaidamour's avatar
Jérémie Gaidamour committed
38
    puts "Error in #{__method__}: no entry for \"#{data['filename']}\" at #{data['site_uid']} (#{data['network_adapters']})."
39
40
    return "" 
  end
41

42
43
44
45
  output = ERB.new(File.read('templates/dhcp.erb')).result(binding)
  output_file = Pathname("#{$output_dir}/modules/dhcpg5k/files/#{data.fetch("site_uid")}/dhcpd.conf.d/#{data.fetch('filename')}")
  output_file.dirname.mkpath()
  File.write(output_file, output)
46
47
48
49
end
 
# Loop over Grid'5000 sites
global_hash["sites"].each { |site_uid, site_hash|
50
51
52
  puts site_uid
  next if site_uid != 'nancy'

Jérémie Gaidamour's avatar
Jérémie Gaidamour committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  #
  # eth, bmc and mic0
  #

  # Relocate ip/mac info of MIC
  site_hash.fetch("clusters").each { |cluster_uid, cluster_hash|
    cluster_hash.fetch('nodes').each { |node_uid, node_hash| 
      site_hash.fetch("clusters").each {
        if node_hash['mic'] && node_hash['mic']['ip'] && node_hash['mic']['mac']
          node_hash['network_adapters'] ||= {}
          node_hash['network_adapters']['mic0'] ||= {}
          node_hash['network_adapters']['mic0']['ip']  = node_hash['mic'].delete('ip')
          node_hash['network_adapters']['mic0']['mac'] = node_hash['mic'].delete('mac')
        end
      }
    }
  }

  # One file for each clusters
72
  site_hash.fetch("clusters").each { |cluster_uid, cluster_hash|
Jérémie Gaidamour's avatar
Jérémie Gaidamour committed
73
74
75
    networks = ["eth", "bmc"]
    networks << 'mic0' if cluster_hash['nodes'].values.any? {|x| x['network_adapters']['mic0'] }

76
    write_dhcp_file({
77
                      "filename"            => "cluster-" + cluster_uid + ".conf",
78
                      "site_uid"            => site_uid,
79
                      "nodes"               => cluster_hash.fetch('nodes'),
Jérémie Gaidamour's avatar
Jérémie Gaidamour committed
80
                      "network_adapters"    => networks,
81
                    })
82
  }
83

Jérémie Gaidamour's avatar
Jérémie Gaidamour committed
84
85
86
87
88
  #
  # PDUs
  #

  # Relocate ip/mac info of PDUS
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  site_hash['pdus'].each { |pdu_uid, pdu_hash|
    if pdu_hash['ip'] && pdu_hash['mac']
      pdu_hash['network_adapters'] ||= {}
      pdu_hash['network_adapters']['pdu'] ||= {}
      pdu_hash['network_adapters']['pdu']['ip']  = pdu_hash.delete('ip')
      pdu_hash['network_adapters']['pdu']['mac'] = pdu_hash.delete('mac')
    end
  }

  key = 'pdus'
  write_dhcp_file({
                    "filename"            => key + ".conf",
                    "site_uid"            => site_uid,
                    "nodes"               => site_hash['pdus'],
                    "network_adapters"  => ['pdu'],
                  })

Jérémie Gaidamour's avatar
Jérémie Gaidamour committed
106
107
108
109
110
111
112
113
114
115
116
117
118
  #
  #
  #

# && node_hash['mic']
#     if pdu_hash['ip'] && pdu_hash['mac']
#       pdu_hash['network_adapters'] ||= {}
#       pdu_hash['network_adapters']['pdu'] ||= {}
#       pdu_hash['network_adapters']['pdu']['ip']  = pdu_hash.delete('ip')
#       pdu_hash['network_adapters']['pdu']['mac'] = pdu_hash.delete('mac')
#     end
#  }

119
120
  next

121
  # Other dhcp files
Jérémie Gaidamour's avatar
Jérémie Gaidamour committed
122
  ["networks", "laptops", "dom0"].each { |key|
123
124
125
126
    write_dhcp_file({
                      "filename"            => key + ".conf",
                      "site_uid"            => site_uid,
                      "nodes"               => site_hash['nodes'],
Jérémie Gaidamour's avatar
Jérémie Gaidamour committed
127
                      "network_adapters"  => ["eth"],
128
129
                    })
  }
130

131
}