Skip to content

Instantly share code, notes, and snippets.

@fortitudepub
Created February 23, 2017 07:55
Show Gist options
  • Save fortitudepub/a062169a7e625deb6931d5ef49613842 to your computer and use it in GitHub Desktop.
Save fortitudepub/a062169a7e625deb6931d5ef49613842 to your computer and use it in GitHub Desktop.
network provision shell script.
#!/bin/bash
ROUTER_ID="f2c8e7a4-ce7f-41cb-8c92-6e85a3453e57"
NS="qrouter-$ROUTER_ID"
LOG="/data/log/neutron/$NS-manual.log"
TIME=`date`
echo "Manual provision of $NS started at $TIME." > $LOG
# VLAN tag which is allocated by network admin.
VLAN="100"
# Physical if connected to the L2 switch which is used to route packet through the direct link .
DIRECTLINK_PHY_IF="eth3"
DOT1Q_IF="$DIRECTLINK_PHY_IF.$VLAN"
# REMOTE SUBNET which is routable through the direct link.
REMOTE_SUBNET="1.1.1.0/24"
DIRECTLINK_LOCAL_IP="169.254.1.1/24"
# This ip will serve as nexthop to REMOTE_CIDR.
DIRECTLINK_REMOTE_IP="169.254.1.2"
function ensure_dot1q_if() {
ip netns exec $NS ip link show $DOT1Q_IF
if [ $? -ne 0 ]; then
ip link show $DOT1Q_IF
if [ $? -ne 0 ]; then
echo "vlan is not created yet, create it and move it into namespace." >> $LOG
ip link add link $DIRECTLINK_PHY_IF name $DOT1Q_IF type vlan id $VLAN
ip link set dev $DOT1Q_IF netns $NS
ip netns exec $NS ip link set $DOT1Q_IF up
ip netns exec $NS ip addr replace $DIRECTLINK_LOCAL_IP dev $DOT1Q_IF
else
echo "vlan is is not present in namespace, move it in." >> $LOG
ip link set dev $DOT1Q_IF netns $NS
ip netns exec $NS ip link set $DOT1Q_IF up
ip netns exec $NS ip addr replace $DIRECTLINK_LOCAL_IP dev $DOT1Q_IF
fi
else
echo "vlan if is already present in namespace." >> $LOG
ip netns exec $NS ip link set $DOT1Q_IF up
ip netns exec $NS ip addr replace $DIRECTLINK_LOCAL_IP dev $DOT1Q_IF
fi
}
function ensure_routes() {
ip netns exec $NS ip route replace $REMOTE_SUBNET dev $DOT1Q_IF via $DIRECTLINK_REMOTE_IP
}
ensure_dot1q_if
ensure_routes
echo "provision of $NS finished." >> $LOG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment