Matteo Scandolo | aca8665 | 2017-08-08 13:05:27 -0700 | [diff] [blame^] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
AyumuUeha | 76a01bc | 2017-05-18 13:34:13 +0900 | [diff] [blame] | 17 | #!/usr/bin/env bash |
| 18 | #************************************************************/ |
| 19 | #** File: nova_vsg_monitor.sh */ |
| 20 | #** Contents: Contains shell script to periodically */ |
| 21 | #** (every minute) check VSG status. If VSG is*/ |
| 22 | #** up calls, prepare vSG with the */ |
| 23 | #** necessary packages and start vCPE */ |
| 24 | #** program inside VSG. When vCPE comes up */ |
| 25 | #** execute vsg_vcep_setup.sh script */ |
| 26 | #************************************************************/ |
| 27 | function is_vsg_active() { |
| 28 | |
| 29 | echo "Checking vSG status..." |
| 30 | vcpe=0 |
| 31 | ping=0 |
| 32 | active=0 |
| 33 | |
| 34 | # check if vsg is active, if vsg is not active return |
| 35 | if [ 'nova list --all-tenants | grep $1 | grep ACTIVE' ]; then |
| 36 | active=1 |
| 37 | #echo "VSG is ACTIVE" |
| 38 | else |
| 39 | #echo "VSG is not active, exit" |
| 40 | return 0 |
| 41 | fi |
| 42 | |
| 43 | # check if ping is ok |
| 44 | if ssh ubuntu@$MGMTIP "ping -c 3 8.8.8.8" > /dev/null; then |
| 45 | ping=1 |
| 46 | #echo "PING OK" |
| 47 | fi |
| 48 | |
| 49 | # if all the above checks are ok then vsg is active |
| 50 | if [[ "$active" == 1 && "$ping" == 1 ]]; then |
| 51 | #echo "VSG is ACTIVE" |
| 52 | return 1 |
| 53 | else |
| 54 | return 0 |
| 55 | fi |
| 56 | |
| 57 | } |
| 58 | |
| 59 | # Read the temp_id.txt file and fill the array named "array" |
| 60 | getArray() { |
| 61 | array=() # Create array |
| 62 | while IFS= read -r line # Read a line |
| 63 | do |
| 64 | array+=("$line") # Append line to the array |
| 65 | done < "$1" |
| 66 | } |
| 67 | |
| 68 | function check_vsg_status() { |
| 69 | |
| 70 | source ${HOME_DIR}/admin-openrc.sh |
| 71 | |
| 72 | echo "Checking for new VSG..." |
| 73 | |
| 74 | file_temp="${HOME_DIR}/temp_id.txt" |
| 75 | file_id="${HOME_DIR}/$PPPOE_VSG_ID_FILE_NAME" |
| 76 | |
| 77 | nova list --all-tenants|grep mysite_vsg|awk '{print $2}' > $file_temp |
| 78 | |
| 79 | getArray $file_temp |
| 80 | |
| 81 | for id in "${array[@]}" |
| 82 | do |
| 83 | # if VSG Id does not exist, add it if VSG is active |
| 84 | if ! grep -q $id "$file_id" > /dev/null; then |
| 85 | echo "Found new VSG" |
| 86 | echo "VSG ID=$id" |
| 87 | |
| 88 | MGMTIP=$( nova interface-list $id|grep 172.27|awk '{print $8}' ) |
| 89 | |
| 90 | echo "MGMTIP: $MGMTIP" |
| 91 | if [ ! "$MGMTIP" ];then |
| 92 | echo "MGMTIP:$MGMTIP is null, continue" |
| 93 | continue |
| 94 | fi |
| 95 | |
| 96 | is_vsg_active $MGMTIP $id |
| 97 | is_active=$? |
| 98 | |
| 99 | if [[ $is_active -eq 1 ]]; then |
| 100 | echo "VSG is active" |
| 101 | # add vsg ID to the file |
| 102 | echo "$id" >> $file_id |
| 103 | |
| 104 | echo "Calling VSG Setup script" |
| 105 | vsg_id=`cat $file_id|wc -l` |
| 106 | echo "Setting up vsg_id:.....$vsg_id CP_PREFIX= $VCPEPROXY_CP_IP_PREFIX" |
| 107 | source ./nova_vsg_setup.sh $MGMTIP $vsg_id |
| 108 | echo "VSG Instance $vsg_id setup is complete" |
| 109 | else |
| 110 | echo "VSG is not active" |
| 111 | fi |
| 112 | fi |
| 113 | done |
| 114 | } |
| 115 | |
| 116 | echo "nova_vsg_monitor: Execution Begin" |
| 117 | |
| 118 | if [ -z $HOME_DIR ]; then |
| 119 | echo "HOME_DIR env variable is not...Using current directory as home" |
| 120 | HOME_DIR=`pwd` |
| 121 | fi |
| 122 | |
| 123 | while true |
| 124 | do |
| 125 | echo "Periodically checking for new VSG" |
| 126 | |
| 127 | MGMTIP="" |
| 128 | #echo "BEFORE: $MGMTIP" |
| 129 | check_vsg_status |
| 130 | |
| 131 | date |
| 132 | printf "\n" |
| 133 | sleep 60 |
| 134 | |
| 135 | done |
| 136 | echo "nova_vsg_monitor.sh: Execution End" |