Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /bin/bash
- ######################################
- ## NodeWeaver/OpenNebula Packet.net Elastic IP VNet Manager
- ######################################
- # Author: Lorenzo Faleschini (lorenzo.faleschini@nordeweaver.eu)
- # © NodeWeaver 2019
- ######################################
- # Assumes that on br1 there's packet.net public IP interface (tested on NodeWeaver packet host configured in hybrid L3/L2 mode)
- # just assign to this host a public IP block and run this script (works with IPv4 and IPv6)
- # automatic creation of vNet will occour and you'll be able to access these IPs with vms sitting on this host.
- # Unless you route interanlly the IPs with a virtual router you'll access the gateway for the IP blocks only on this host where you ran the script
- # Note if you're running this script on a non-opennebula server, you should redirect the `one` commands via ssh to the opennebula server
- ######################################
- ELASTIC_IP_LIST="$(curl -s https://metadata.packet.net/metadata | jq '.' | grep -B 5 -A 4 '"management": false,' | grep 'network\|cidr\|address\|netmask\|gateway\|public' | awk '{print toupper($0)}' | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/: /=/' | paste - - - - - - - | tr -d '-' | tr ' ' ',' | sed -e 's/^ //' -e 's/,$//')"
- echo "$ELASTIC_IP_LIST" > /tmp/elastic_ip_list
- while read ELASTIC_IP; do
- echo $ELASTIC_IP | sed 's/ /\n/g' > /tmp/elastic_ip_settings
- source /tmp/elastic_ip_settings
- NETTYPE="$(if [ "$PUBLIC" = "TRUE" ]; then echo Public; else echo Private; fi)"
- NETNAME="Packet.net_Elastic_IPv"$ADDRESS_FAMILY"_"$NETTYPE"_"$(echo $NETWORK | sed -s 's/:/./g')"-"$CIDR""
- if [ "$ADDRESS_FAMILY" = "6" ]; then
- ARSIZE="$(expr $(/opt/bin/nwipcalc $NETWORK/$CIDR | grep Hosts | awk {'print $4'}) - 1)"
- # avoid illegal AR size in One
- if [ "$CIDR" -lt "99" ]; then
- ARSIZE=999999999
- else
- ARSIZE="$(expr $(/opt/bin/nwipcalc $NETWORK/$CIDR | grep Hosts | awk {'print $4'}) - 1)"
- fi
- echo '
- NAME="'$NETNAME'"
- DESCRIPTION="Auto generated VNet from Packet.net Elastic IP assignment"
- BRIDGE="br1"
- VN_MAD="fw"
- DNS="1.1.1.1 8.8.8.8"
- GATEWAY="'$GATEWAY'"
- NETWORK="'$NETWORK'"
- AR=[ TYPE = "IP'$ADDRESS_FAMILY'", GLOBAL_PREFIX = "'$ADDRESS'", SIZE = "'$ARSIZE'"]
- ' > /tmp/$NETNAME
- su -c 'onevnet create /tmp/'$NETNAME'' admin > /dev/null 2>&1
- ip -6 addr add $GATEWAY/$CIDR dev br1 > /dev/null 2>&1
- else
- ARSIZE="$(expr $(/opt/bin/nwipcalc $NETWORK/$CIDR | grep Hosts | awk {'print $2'}) - 1)"
- echo '
- NAME="'$NETNAME'"
- DESCRIPTION="Auto generated VNet from Packet.net Elastic IP assignment"
- BRIDGE="br1"
- VN_MAD="fw"
- DNS="1.1.1.1 8.8.8.8"
- GATEWAY="'$GATEWAY'"
- NETWORK="'$NETWORK'"
- AR=[ TYPE = "IP'$ADDRESS_FAMILY'", IP = "'$ADDRESS'", SIZE = "'$ARSIZE'"]
- ' > /tmp/$NETNAME
- su -c 'onevnet create /tmp/'$NETNAME'' admin > /dev/null 2>&1
- ip addr add $GATEWAY/$CIDR dev br1 > /dev/null 2>&1
- fi
- done < /tmp/elastic_ip_list
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement