#! /bin/bash

set -eux

### --start_docs

## Set up the undercloud for installation
## ======================================

## * Configure external interface
## ::

# TODO: move this to overcloud-prep-network role
sudo ip addr add {{ external_interface_ip }} dev {{ external_interface }}

## * Get mac address of external interface
## ::

export MAC_ADDR_EXT_INTERFACE=$( ip link show {{ external_interface }} | awk '/ether/ {print $2}' )

## * Set up external interface
## ::

sudo -E bash -c 'cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-{{ external_interface }}
NAME={{ external_interface }}
IPADDR={{ external_interface_ip }}
NETMASK={{ external_interface_netmask }}
NM_CONTROLLED=no
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
ONBOOT=yes
HWADDR=$MAC_ADDR_EXT_INTERFACE
PEERDNS=yes
PEERROUTES=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
EOF'

sudo ifdown {{ external_interface }}
sudo ifup {{ external_interface }}

## * Set MTU values
## ::

{% for interface in (mtu_interface) %}
  sudo ip link set {{ interface }} mtu {{ mtu }}
  echo "MTU={{ mtu }}" | sudo tee -a /etc/sysconfig/network-scripts/ifcfg-{{ interface }}
{% endfor %}

## * Add custom nameserver to resolv.conf if required
## ::

{% if custom_nameserver is defined %}
sudo sed -i '/^nameserver/d' /etc/resolv.conf
{% for name_server in custom_nameserver %}
cat <<EOF | sudo tee -a /etc/resolv.conf
nameserver {{ name_server }}
EOF
{% endfor %}
{% endif %}

### --stop_docs
