<%#
kind: snippet
name: kickstart_networking_setup
description: this will configure your host networking, it configures your primary interface as well
    as other configures NICs. It supports physical, VLAN and Alias interfaces. It's intended to be
    called from %post in your kickstart template. Note that this snippet can be used with Foreman 1.7
    and later
%>
<% subnet = @host.subnet -%>
<% dhcp = subnet.dhcp_boot_mode? -%>

<% if @host.respond_to?(:has_primary_interface?) %>
<%# Foreman 1.7 - primary interface contained in @host %>
# primary interface
real=`ip -o link | grep <%= @host.mac -%> | awk '{print $2;}' | sed s/://`
<% if @host.has_primary_interface? %>
cat << EOF > /etc/sysconfig/network-scripts/ifcfg-$real
BOOTPROTO="<%= dhcp ? 'dhcp' : 'none' -%>"
<% unless dhcp -%>
IPADDR="<%= @host.ip -%>"
NETMASK="<%= subnet.mask -%>"
<% if !subnet.gateway.nil? && !subnet.gateway.empty? -%>
GATEWAY="<%= subnet.gateway %>"
<% end -%>
<% end -%>
DEVICE="$real"
HWADDR="<%= @host.mac -%>"
ONBOOT=yes
EOF
<% end -%>
<% end -%>

<% bonded_interfaces = [] %>
<% bonds = @host.bond_interfaces %>
<% bonds.each do |bond| %>
<% subnet = bond.subnet -%>
<% dhcp = subnet.nil? ? false : subnet.dhcp_boot_mode? -%>
# <%= bond.identifier %> interface
real="<%= bond.identifier -%>"
cat << EOF > /etc/sysconfig/network-scripts/ifcfg-$real
BOOTPROTO="<%= dhcp ? 'dhcp' : 'none' -%>"
<% unless dhcp || subnet.nil? -%>
IPADDR="<%= bond.ip -%>"
NETMASK="<%= subnet.mask -%>"
<% if !subnet.gateway.nil? && !subnet.gateway.empty? -%>
GATEWAY="<%= subnet.gateway %>"
<% end -%>
<% end -%>
DEVICE="$real"
ONBOOT=yes
PEERDNS=no
PEERROUTES=no
DEFROUTE=no
TYPE=Bond
BONDING_OPTS="<%= bond.bond_options -%> mode=<%= bond.mode -%>"
BONDING_MASTER=yes
NM_CONTROLLED=no
EOF

<% @host.interfaces_with_identifier(bond.attached_devices_identifiers).each do |interface| -%>
<% next if !interface.managed? -%>

<% subnet = interface.subnet -%>
<% virtual = interface.virtual? -%>
<% vlan = virtual && subnet.has_vlanid? -%>
<% alias_type = virtual && !subnet.nil? && !subnet.has_vlanid? && interface.identifier.include?(':') -%>
<% dhcp = !subnet.nil? && subnet.dhcp_boot_mode? -%>

# <%= interface.identifier %> interface
real=`ip -o link | grep <%= interface.respond_to?(:inheriting_mac) ? interface.inheriting_mac : interface.mac -%> | awk '{print $2;}' | sed s/:$//`
<% if virtual -%>
real=`echo <%= interface.identifier -%> | sed s/<%= interface.attached_to -%>/$real/`
<% end -%>

# ifcfg files are ignored by NM if their name contains colons so we convert colons to underscore
<% if alias_type -%>
sanitized_real=`echo $real | sed s/:/_/`
<% else -%>
sanitized_real=$real
<% end -%>

cat << EOF > /etc/sysconfig/network-scripts/ifcfg-$sanitized_real
BOOTPROTO="none"
DEVICE="$real"
<% unless virtual -%>
HWADDR="<%= interface.mac -%>"
<% end -%>
ONBOOT=yes
PEERDNS=no
PEERROUTES=no
<% if vlan -%>
VLAN=yes
<% elsif alias_type -%>
TYPE=Alias
<% end -%>
NM_CONTROLLED=no
MASTER=<%= bond.identifier %>
SLAVE=yes
EOF

<% bonded_interfaces.push(interface.identifier) -%>
<% end %>
<% end %>

<% @host.managed_interfaces.each do |interface| %>
<% next if !interface.managed? || interface.subnet.nil? -%>
<% next if bonded_interfaces.include?(interface.identifier) -%>

<% subnet = interface.subnet -%>
<% virtual = interface.virtual? -%>
<% vlan = virtual && subnet.has_vlanid? -%>
<% alias_type = virtual && !subnet.has_vlanid? && interface.identifier.include?(':') -%>
<% dhcp = subnet.dhcp_boot_mode? -%>

# <%= interface.identifier %> interface
real=`ip -o link | grep <%= interface.mac -%> | awk '{print $2;}' | sed s/:$//`
<% if virtual -%>
  real=`echo <%= interface.identifier -%> | sed s/<%= interface.attached_to -%>/$real/`
<% end -%>

# ifcfg files are ignored by NM if their name contains colons so we convert colons to underscore
<% if alias_type -%>
sanitized_real=`echo $real | sed s/:/_/`
<% else -%>
sanitized_real=$real
<% end -%>

cat << EOF > /etc/sysconfig/network-scripts/ifcfg-$sanitized_real
BOOTPROTO="<%= dhcp ? 'dhcp' : 'none' -%>"
<% unless dhcp -%>
IPADDR="<%= interface.ip -%>"
NETMASK="<%= subnet.mask -%>"
<% if !subnet.gateway.nil? && !subnet.gateway.empty? -%>
GATEWAY="<%= subnet.gateway %>"
<% end -%>
<% end -%>
DEVICE="$real"
<% unless virtual -%>
HWADDR="<%= interface.mac -%>"
<% end -%>
ONBOOT=yes
<% if interface.respond_to?(:primary) && interface.primary -%>
PEERDNS=yes
PEERROUTES=yes
<% else -%>
PEERDNS=no
PEERROUTES=no
<% end -%>
<% if vlan -%>
VLAN=yes
<% elsif alias_type -%>
TYPE=Alias
<% end -%>
EOF

<% end %>
