#! /bin/bash
set -xe
-
# change the password because this script is run on a passwordless cloud-image
echo 'ubuntu:opnfv' | chpasswd
# Wait for a floating IP
# as a workaround to NAT breakage
-sleep 20
+sleep 100
# Variables to be filled in with python
-NEIGHBOR_IP=%s
-OWN_IP=%s
-
-ZEBRA_CONFIG_LOCATION="/etc/quagga/zebra.conf"
-DAEMONS_FILE_LOCATION="/etc/quagga/daemons"
-BGPD_CONFIG_LOCATION="/etc/quagga/daemons"
-BGPD_LOG_FILE="/var/log/bgpd.log"
-
-DEBIAN_FONTEND=noninteractive apt-get update
-DEBIAN_FRONTEND=noninteractive apt-get install quagga -y
+NEIGHBOR_IP={0}
+OWN_IP={1}
+# directly access the instance from the external net without NAT
+EXT_NET_MASK={2}
+IP_PREFIX={3}
+RD={4}
+IRT={5}
+ERT={6}
-touch $BGPD_LOG_FILE
-chown quagga:quagga $BGPD_LOG_FILE
+if [[ $(getent hosts | awk '{{print $2}}') != *"$(cat /etc/hostname | awk '{{print $1}}')"* ]]
+then
+echo "127.0.1.1 $(cat /etc/hostname | awk '{{print $1}}')" | tee -a /etc/hosts
+fi
-chown quagga:quagga $DAEMONS_FILE_LOCATION
-cat <<CATEOF > $DAEMONS_FILE_LOCATION
-zebra=yes
-bgpd=yes
-ospfd=no
-ospf6d=no
-ripd=no
-ripngd=no
-isisd=no
-babeld=no
-CATEOF
+quagga_int=''
+for net_int in $(netstat -ia | awk 'NR>2{{print $1}}');
+do
+if [ -z "$(ifconfig | grep $net_int)" ]
+then
+quagga_int=$net_int
+break
+fi
+done
+if [ -z "$quagga_int" ]
+then
+echo 'No available network interface'
+fi
-touch $ZEBRA_CONFIG_LOCATION
-chown quagga:quagga $ZEBRA_CONFIG_LOCATION
+ip link set $quagga_int up
+ip addr add $OWN_IP/$EXT_NET_MASK dev $quagga_int
-cat <<CATEOF > $BGPD_CONFIG_LOCATION
-! -*- bgp -*-
+# Download quagga/zrpc rpms
+cd /root
+wget http://artifacts.opnfv.org/sdnvpn/quagga4/quagga-ubuntu-updated.tar.gz
+tar -xvf quagga-ubuntu-updated.tar.gz
+cd /root/quagga
+dpkg -i c-capnproto_1.0.2.75f7901.Ubuntu16.04_amd64.deb
+dpkg -i zmq_4.1.3.56b71af.Ubuntu16.04_amd64.deb
+dpkg -i quagga_1.1.0.cd8ab40.Ubuntu16.04_amd64.deb
+dpkg -i thrift_1.0.0.b2a4d4a.Ubuntu16.04_amd64.deb
+dpkg -i zrpc_0.2.0efd19f.thriftv4.Ubuntu16.04_amd64.deb
-hostname bgpd
-password sdncbgpc
+nohup /opt/quagga/sbin/bgpd &
+cat > /tmp/quagga-config << EOF1
+config terminal
router bgp 200
- bgp router-id ${OWN_IP}
- neighbor ${NEIGHBOR_IP} remote-as 100
- no neighbor ${NEIGHBOR_IP} activate
+ bgp router-id $OWN_IP
+ no bgp log-neighbor-changes
+ bgp graceful-restart stalepath-time 90
+ bgp graceful-restart restart-time 900
+ bgp graceful-restart
+ bgp graceful-restart preserve-fw-state
+ bgp bestpath as-path multipath-relax
+ neighbor $NEIGHBOR_IP remote-as 100
+ no neighbor $NEIGHBOR_IP activate
+ vrf $RD
+ rd $RD
+ rt import $IRT
+ rt export $ERT
+ exit
!
- address-family vpnv4 unicast
- neighbor ${NEIGHBOR_IP} activate
- exit-address-family
+address-family vpnv4
+neighbor $NEIGHBOR_IP activate
+neighbor $NEIGHBOR_IP attribute-unchanged next-hop
+exit
!
-line vty
- exec-timeout 0 0
+route-map map permit 1
+ set ip next-hop $OWN_IP
+exit
!
-debug bgp events
-debug bgp updates
-log file ${BGPD_LOG_FILE}
-end
-CATEOF
-chown quagga:quagga $BGPD_CONFIG_LOCATION
+router bgp 200
+address-family vpnv4
+network $IP_PREFIX rd $RD tag 100 route-map map
+exit
+!
+EOF1
+
+sleep 20
-pgrep bgpd
-pgrep zebra
+(sleep 1;echo "sdncbgpc";sleep 1;cat /tmp/quagga-config;sleep 1; echo "exit") |nc -q1 localhost 2605