Infolib

From wiki
Jump to: navigation, search
  1. !/bin/bash
    1. Requires a correct set of environment variables.


function get_email(){

        email=$(curl -s -u $OS_USERNAME:$OS_PASSWORD "https://admtools.lab.fi-ware.org/mailtron/?tenant=$1")
    1. Add Error checking
        echo "$1 $email"

}


myExternalNetwork="4c77aa53-4ecb-4296-8f2c-c0d9626ba1f8"


function clearIdleRouters(){

    1. Check Quantum; Routers

routers=` quantum router-list | grep "$myExternalNetwork" | grep -v '\-\-' | awk 'BEGIN {FS = "|"}; { if (NR>1) { print $2;}}'| sort` echo "Checking Routers." myRouters="" myTenants=""

for router in $routers; do

   echo -n "."
   tenR=`quantum router-port-list $router | grep subnet_id | wc -l`
   if [[ $tenR > 1 ]]
   then
       echo -n "$router has $tenR ports, "
       tenid=`quantum router-show $router | grep tenant_id | awk '{print $4}'`
       echo -n "owned by $tenid "
       instances=`nova list --tenant $tenid --all-tenants 1 | awk 'BEGIN {FS = "|"}; { if (NR>3) { print $2; }}' | head -1` 
  1. echo -e "\t\t Tenant is = $tenid."
  2. echo -e "\t\t Instances $instances."
       if  -z $instances  
       then
           echo "Candidate to remove, tenant has no active instances."
           myTenants+=" $tenid"
           myRouters+=" $router"
           echo -e "\t Clearing gateway from $router."
           resp=`quantum router-gateway-clear $router`
           echo "Done, $resp." 
       else
           tenCount=`echo $instances | wc -w `
           echo "Considered active, $tenCount instances to tenantid"
       fi
   fi

done

echo "Tenants : " echo -e "\t\t $myTenants." echo "Routers : " echo -e "\t\t $myRouters."


}

function removeUnusedFloatingIP() { echo "Obtain list of IPs" echo "obtain Unused IPs" unUsedIPs=`quantum floatingip-list | grep "|" | awk '{print $2," ",$4," ",$6, " ",$8}' | awk '{ if (length($4)==0) print $1}'`

  1. echo "obtain Used IPs"
  2. quantum floatingip-list | grep "|" | awk '{print $2," ",$4," ",$6, " ",$8}' | awk '{ if (length($4)>0) print $0}'

echo "These are unused:" quantum floatingip-list | grep "|" | awk '{print $2," ",$4," ",$6, " ",$8}' | awk '{ if (length($4)==0) print $0}'


while IFS= read -r instanceID do

echo "instance id: $instanceID; removing"
echo -e "\t":
quantum floatingip-delete $instanceID
echo -e "\t Done"

done <<< "${unUsedIPs}"

}