#!/bin/bash
#
set -e
#
# Create bash profile script
#
cat <<SCRIPT >/root/.bash_profile
#!/bin/bash
#
export PS1="\h(bash)#"
echo "Use vtysh to connect to FRR daemon"
echo
SCRIPT
#
# Build hosts file
#
hostname {{ inventory_hostname }}
awk '/127.0.1.1/,/^$/' /etc/hosts >/tmp/hosts
echo "127.0.0.1 {{ inventory_hostname }}" >>/tmp/hosts
{% for k,v in hostvars.items() if k != inventory_hostname and v.af.ipv4|default(False) %}
echo "{%- if v.loopback.ipv4 is defined %}{{ v.loopback.ipv4|ipaddr('address') }} {% endif %}
{%- for l in v.interfaces|default([]) if 'ipv4' in l and l.ipv4 != True and l.ipv4|ipv4 %}{{ l.ipv4|ipaddr('address') }} {% endfor %}{{ k }}" >>/tmp/hosts
{% endfor %}
cat /etc/hosts | awk '/localhost/,/^$/' >/tmp/hosts-start
{% if ansible_connection != "docker" %}
mv /tmp/hosts-start /etc/hosts
{% endif %}
sort /tmp/hosts|uniq >>/etc/hosts
{% if clab.kind is not defined %}
{# no need to disable ZTP in a container #}
#
# Disable ZTP
#
ztp --disable
{% else %}
{# Disable DHCP on management interface #}
cat >/etc/network/interfaces <<CONFIG
auto lo
iface lo inet loopback

auto mgmt
iface mgmt
    vrf-table auto

auto eth0
iface eth0 inet static
    address {{ mgmt.ipv4 }}
    vrf mgmt

source /etc/network/interfaces.d/*.intf
CONFIG
{% endif %}
#
# Enable LLDP
#
cat <<CONFIG >/etc/lldpd.d/system.conf
configure lldp tx-interval 30
configure lldp tx-hold 3
configure system interface pattern *,!eth0,swp*
CONFIG
service lldpd restart
#
# Enable FRR modules
#
{% set modlist = {'bgp':'bgpd','ospf':['ospfd','ospf6d'],'isis':'isisd','vrf':'bgpd','gateway':'vrrpd'} %}
{% for m in module|default([]) if modlist[m] is defined %}
{%   for frr_m in [modlist[m]]|flatten %}
echo "{{ frr_m }}=yes" >>/etc/frr/daemons
{%   endfor %}
{% endfor %}
systemctl enable frr.service
systemctl start frr.service
systemctl reload frr.service
#
# Create loopback interface entry
#

{%- macro loopback_interface(name,data,create) %}
auto {{ name }}
{% if create %}
iface {{ name }}
  pre-up ip link add {{ name }} type dummy
{% endif %}
{% if data.ipv4 is defined %}
iface {{ name }} inet loopback
  address {{ data.ipv4 }}
{% endif %}
{% if data.ipv6 is defined %}
iface {{ name }} inet6 loopback
  address {{ data.ipv6 }}
{% endif %}
{% endmacro %}

cat >/etc/network/interfaces.d/10-loopback.intf <<CONFIG
{% if loopback is defined %}
{{ loopback_interface('lo',loopback,False) }}
{% endif %}
{% for l in interfaces if l.type == 'loopback' %}
{{ loopback_interface(l.ifname,l,True) }}
{% endfor %}
CONFIG
ifup lo
cat >/etc/network/interfaces.d/11-physical.intf <<CONFIG
{% for l in interfaces if l.type in ['lan','p2p','stub'] %}
auto {{ l.ifname }}
{%   if l.ipv4 is defined %}
iface {{ l.ifname }} inet static
{%     if l.ipv4 == True and loopback.ipv4 is defined %}
  address {{ loopback.ipv4 }}
{%     else %}
  address {{ l.ipv4 }}
{%     endif %}
{%   endif %}
{%   if l.ipv6 is defined and l.ipv6 is string %}
iface {{ l.ifname }} inet6 static
  address {{ l.ipv6 }}
{%   endif %}
{%   if l.mtu is defined %}
iface {{ l.ifname }}
  mtu {{ l.mtu }}
{%   endif %}
{% endfor %}
CONFIG
ifreload -a
#
# For whatever crazy reason, I had to enable IPv6 in containers
#
{% for l in interfaces if l.type in ['lan','p2p','stub'] and (l.ipv6 is defined or 'external' in l.role|default('')) %}
sysctl -qw net.ipv6.conf.{{ l.ifname }}.disable_ipv6=0
{% endfor %}
#
# Rest of initial configuration done through VTYSH
#
cat >/tmp/config <<CONFIG
{% if af.ipv6 is defined %}
ipv6 forwarding
{% endif %}
!
{% for l in interfaces %}
interface {{ l.ifname }}
! no shutdown
{% if l.name is defined %}
 description {{ l.name }}{{ " ["+l.role+"]" if l.role is defined else "" }}
{% elif l.type|default("") == "stub" %}
 description Stub interface
{% endif %}
 ipv6 nd ra-interval 5
 no ipv6 nd suppress-ra
{% if l.bandwidth is defined %}
 bandwidth {{ l.bandwidth  }}
{% endif %}
!
{% endfor %}
do write
CONFIG
vtysh -f /tmp/config
