From f3b00c2da148b165b8b0f9e09705eba3d4ba0a93 Mon Sep 17 00:00:00 2001
From: Matthieu Imbert <matthieu.imbert@inria.fr>
Date: Thu, 5 Sep 2019 17:30:46 +0200
Subject: [PATCH] [ipv6, dhcp] add generation of dhcpv6 records

---
 lib/refrepo/gen/puppet/dhcpg5k.rb         | 32 +++++++++++++----------
 lib/refrepo/gen/puppet/templates/dhcp.erb |  9 ++++---
 2 files changed, 23 insertions(+), 18 deletions(-)

diff --git a/lib/refrepo/gen/puppet/dhcpg5k.rb b/lib/refrepo/gen/puppet/dhcpg5k.rb
index be0821cf59a..735f33924e1 100644
--- a/lib/refrepo/gen/puppet/dhcpg5k.rb
+++ b/lib/refrepo/gen/puppet/dhcpg5k.rb
@@ -1,6 +1,6 @@
-# Get the mac and ip of a node. Throw exception if error.
+# Get the mac, ipv4, ipv6 of a node. Throw exception if error.
 def get_network_info(node_hash, network_interface)
-  # Get node_hash["network_adapters"][network_interface]["ip"] and node_hash["network_adapters"][network_interface]["mac"]
+  # Get node_hash["network_adapters"][network_interface]["ip"], node_hash["network_adapters"][network_interface]["ip6"] and node_hash["network_adapters"][network_interface]["mac"]
   node_network_adapters = node_hash.fetch("network_adapters")
 
   # For the production network, find the mounted interface (either eth0 or eth1)
@@ -21,24 +21,28 @@ def get_network_info(node_hash, network_interface)
   end
 
   node_mac = node_network_interface.fetch('mac')
-  node_ip  = node_network_interface.fetch('ip')
+  node_ipv4 = node_network_interface.fetch('ip')
+  node_ipv6 = node_network_interface.fetch('ip6', nil)
 
-  raise '"mac" is nil' unless node_mac
-  raise '"ip" is nil'  unless node_ip
+  raise '"mac" is nil'  unless node_mac
+  raise '"ip" is nil'   unless node_ipv4
+  #raise '"ipv6" is nil' unless node_ipv6 # commented right now, we don't yet impose ipv6 for everything
 
-  return [node_ip, node_mac]
+  return [node_ipv4, node_ipv6, node_mac]
 end
 
-def write_dhcp_file(data, options)
+def write_dhcp_files(data, options)
   if data["nodes"].nil?
     puts "Error in #{__method__}: no entry for \"#{data['filename']}\" at #{data['site_uid']} (#{data['network_adapters']})."
     return ""
   end
 
-  output = ERB.new(File.read(File.expand_path('templates/dhcp.erb', File.dirname(__FILE__)))).result(binding)
-  output_file = Pathname("#{options[:output_dir]}/platforms/production/modules/generated/files/grid5000/dhcp/#{data.fetch("site_uid")}/#{data.fetch('filename')}")
-  output_file.dirname.mkpath()
-  File.write(output_file, output)
+  ["dhcp", "dhcpv6"].each { |dhcpkind|
+    output = ERB.new(File.read(File.expand_path("templates/dhcp.erb", File.dirname(__FILE__)))).result(binding)
+    output_file = Pathname("#{options[:output_dir]}/platforms/production/modules/generated/files/grid5000/#{dhcpkind}/#{data.fetch("site_uid")}/#{data.fetch('filename')}")
+    output_file.dirname.mkpath()
+    File.write(output_file, output)
+  }
 end
 
 def generate_puppet_dhcpg5k(options)
@@ -79,7 +83,7 @@ def generate_puppet_dhcpg5k(options)
     site_hash.fetch("clusters").each { |cluster_uid, cluster_hash|
       # networks = ["eth", "bmc"]
       # networks << 'mic0' if cluster_hash['nodes'].values.any? {|x| x['network_adapters']['mic0'] }
-      write_dhcp_file({
+      write_dhcp_files({
         "filename"            => "cluster-" + cluster_uid + ".conf",
         "site_uid"            => site_uid,
         "nodes"               => cluster_hash.fetch('nodes'),
@@ -91,7 +95,7 @@ def generate_puppet_dhcpg5k(options)
 
     # Other dhcp files
     ["networks", "laptops", "servers"].each { |key|
-      write_dhcp_file({
+      write_dhcp_files({
         "filename"            => key + ".conf",
         "site_uid"            => site_uid,
         "nodes"               => site_hash[key],
@@ -116,7 +120,7 @@ def generate_puppet_dhcpg5k(options)
       }
 
       key = 'pdus'
-      write_dhcp_file({
+      write_dhcp_files({
         "filename"            => key + ".conf",
         "site_uid"            => site_uid,
         "nodes"               => site_hash['pdus'],
diff --git a/lib/refrepo/gen/puppet/templates/dhcp.erb b/lib/refrepo/gen/puppet/templates/dhcp.erb
index 853ef49c6ba..8c34bbec049 100644
--- a/lib/refrepo/gen/puppet/templates/dhcp.erb
+++ b/lib/refrepo/gen/puppet/templates/dhcp.erb
@@ -18,10 +18,11 @@
           uid_net = node_uid
       end
 
-      # Get ip and mac addresses
+      # Get ipv4, ipv6 and mac addresses
       begin
-        ip, mac = get_network_info(node, network_interface)
-        dhcp_entries.push({ 'uid_net' => uid_net, 'ip' => ip, 'mac' => mac })
+        ipv4, ipv6, mac = get_network_info(node, network_interface)
+        next if dhcpkind == 'dhcpv6' && ipv6 == nil
+        dhcp_entries.push({ 'uid_net' => uid_net, 'ipv4' => ipv4, 'ipv6' => ipv6, 'mac' => mac })
       rescue => e
         next if data.fetch('optional_network_adapters').include?(network_interface)
 
@@ -36,7 +37,7 @@ group {
 <% dhcp_entries.each {|node| %>
   host <%= node['uid_net'] %> {
     hardware ethernet       <%= node['mac'].upcase %>;
-    fixed-address           <%= node['ip']         %>;
+    fixed-address           <%= dhcpkind == 'dhcpv6' ? node['ipv6'] : node['ipv4'] %>;
   }
 <%    } %>
 }
-- 
GitLab