penzoiders

opennebula-nodeweaver-elastic-ip-configuration.sh

Oct 3rd, 2019
243
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /bin/bash
  2. ######################################
  3. ## NodeWeaver/OpenNebula Packet.net Elastic IP VNet Manager
  4. ######################################
  5. # Author: Lorenzo Faleschini (lorenzo.faleschini@nordeweaver.eu)
  6. # © NodeWeaver 2019
  7. ######################################
  8. # Assumes that on br1 there's packet.net public IP interface (tested on NodeWeaver packet host configured in hybrid L3/L2 mode)
  9. # just assign to this host a public IP block and run this script (works with IPv4 and IPv6)
  10. # automatic creation of vNet will occour and you'll be able to access these IPs with vms sitting on this host.
  11. # 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
  12. # Note if you're running this script on a non-opennebula server, you should redirect the `one` commands via ssh to the opennebula server
  13. ######################################
  14.  
  15.  
  16.  
  17. 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/,$//')"
  18.  
  19. echo "$ELASTIC_IP_LIST" > /tmp/elastic_ip_list
  20.  
  21. while read ELASTIC_IP; do
  22.     echo $ELASTIC_IP | sed 's/ /\n/g' > /tmp/elastic_ip_settings
  23.     source /tmp/elastic_ip_settings
  24.     NETTYPE="$(if [ "$PUBLIC" = "TRUE" ]; then echo Public; else echo Private; fi)"
  25.     NETNAME="Packet.net_Elastic_IPv"$ADDRESS_FAMILY"_"$NETTYPE"_"$(echo $NETWORK | sed -s 's/:/./g')"-"$CIDR""
  26.     if [ "$ADDRESS_FAMILY" = "6" ]; then
  27.         ARSIZE="$(expr $(/opt/bin/nwipcalc $NETWORK/$CIDR  | grep Hosts | awk {'print $4'}) - 1)"
  28.         # avoid illegal AR size in One
  29.         if [ "$CIDR" -lt "99" ]; then
  30.             ARSIZE=999999999
  31.         else
  32.             ARSIZE="$(expr $(/opt/bin/nwipcalc $NETWORK/$CIDR  | grep Hosts | awk {'print $4'}) - 1)"
  33.         fi
  34.         echo '
  35.         NAME="'$NETNAME'"
  36.         DESCRIPTION="Auto generated VNet from Packet.net Elastic IP assignment"
  37.         BRIDGE="br1"
  38.         VN_MAD="fw"
  39.         DNS="1.1.1.1 8.8.8.8"
  40.         GATEWAY="'$GATEWAY'"
  41.         NETWORK="'$NETWORK'"
  42.         AR=[ TYPE = "IP'$ADDRESS_FAMILY'", GLOBAL_PREFIX = "'$ADDRESS'", SIZE = "'$ARSIZE'"]
  43.         ' > /tmp/$NETNAME
  44.         su -c 'onevnet create /tmp/'$NETNAME'' admin > /dev/null 2>&1
  45.         ip -6 addr add $GATEWAY/$CIDR dev br1 > /dev/null 2>&1
  46.     else
  47.         ARSIZE="$(expr $(/opt/bin/nwipcalc $NETWORK/$CIDR  | grep Hosts | awk {'print $2'}) - 1)"
  48.         echo '
  49.         NAME="'$NETNAME'"
  50.         DESCRIPTION="Auto generated VNet from Packet.net Elastic IP assignment"
  51.         BRIDGE="br1"
  52.         VN_MAD="fw"
  53.         DNS="1.1.1.1 8.8.8.8"
  54.         GATEWAY="'$GATEWAY'"
  55.         NETWORK="'$NETWORK'"
  56.         AR=[ TYPE = "IP'$ADDRESS_FAMILY'", IP = "'$ADDRESS'", SIZE = "'$ARSIZE'"]
  57.         ' > /tmp/$NETNAME
  58.         su -c 'onevnet create /tmp/'$NETNAME'' admin > /dev/null 2>&1
  59.         ip addr add $GATEWAY/$CIDR dev br1  > /dev/null 2>&1
  60.     fi
  61. done < /tmp/elastic_ip_list
RAW Paste Data