-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure_ahold_lb
executable file
·246 lines (223 loc) · 6.3 KB
/
azure_ahold_lb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/bin/bash
###############################################################################
red='\033[0;31m'
green='\033[0;32m'
NC='\033[0m' # No Color
VMs="ahold-rc-eus-01 ahold-rc-eus-02"
PORTS="80 443"
MINIMUM_ACTIVE=2
INSTANCE_NAME=$2
OPERATION=$1
WAIT_SEC=20
LB_PORTS="80 443"
EMAIL="[email protected], [email protected]"
function expected_ports() {
words=( $PORTS )
echo ${#words[@]}
}
function notify() {
echo $1 | /usr/bin/mailx -a "From: Rms Production <[email protected]>" -s "[Ahold rms auto-publish] Notification" $EMAIL
}
echored() { echo -e "${red}$@${NC}"; }
echogreen() { echo -e "${green}$@${NC}"; }
echoerr() { echored "$@"; notify "$@";}
die() { echored "$@" 1>&2 ; exit 1; }
############################
#PRINT STATUS###########
###########################
function get_status() {
local vm
local port
local output
echo "For vm in $VMs"
for vm in $VMs
do
echo "####################"
echo "####################"
echo "VM $vm"
echo "---For port in $PORTS..."
output=`azure vm endpoint list $vm`
for port in $PORTS
do
grep -E "\"?\b$port\b\"?" > /dev/null 2>&1 <<< "$output"
if [ $? -eq 0 ]; then
echogreen "------Port $port present"
else
echored "------Port $port not present"
fi
done
done
}
############################
#COUNT IN SERVICE###########
###########################
function count_inservice() {
local count=0
local vm
local port
local PORT_DISABLED
local output
result=true
vm_count=0
echo "For vm in $VMs..."
for vm in $VMs
do
echo "Current vm is $vm..."
echo "For port in $PORTS..."
PORT_DISABLED="false"
output=""
output=`azure vm endpoint list $vm`
for port in $PORTS
do
grep -E "\"?\b$port\b\"?" > /dev/null 2>&1 <<< "$output"
if [ ! $? -eq 0 ]; then
PORT_DISABLED="true"
echoerr "------Port $port not present"
break
else
echogreen "------Port $port OK"
fi
done
#Check that the number of ports match the expected number of ports
if [ "$PORT_DISABLED" = "false" ]; then
vm_count=$(($vm_count+1))
echo "VM count updated to ${vm_count}"
fi
#If there are enough VMs in the load balancer stop counting
if [ $vm_count -ge $MINIMUM_ACTIVE ]; then
echogreen "VM count is more or equal than the minumum (${MINIMUM_ACTIVE})"
break
fi
done
#Check the vm_count
if [ $vm_count -lt $MINIMUM_ACTIVE ]; then
echoerr "The number of the active vm (${vm_count}) is less than ${MINIMUM_ACTIVE}"
result=false
fi
}
#############################
#REMOVE INSTANCE FROM THE LB#
#############################
remove_from_lb() {
local INSTANCE_NAME=$1
shift
local LB_PORTS=$@
local output
output=`azure vm endpoint show $INSTANCE_NAME`
for CURRENT_PORT in $LB_PORTS
do
ENDPOINT_NUMBER=$( echo "$output" |
grep -E --ignore-case "LocalPort \"?\b$CURRENT_PORT\b\"?" |
awk '{print $4}')
if [ -z "$ENDPOINT_NUMBER" ]; then
echo "ENDPOINT $CURRENT_PORT not present. Skipping removal..."
continue
fi
ENDPOINT_NAME=$( echo "$output" |
grep --ignore-case "$ENDPOINT_NUMBER Name" |
awk '{print $6}' |
sed 's/^"\(.*\)"$/\1/')
azure vm endpoint delete $INSTANCE_NAME $ENDPOINT_NAME ||
die "Instance $INSTANCE_NAME could not be removed from load balancer, aborting."
echo "Instance $INSTANCE_NAME removed from load balancer"
done
}
########################
#ADD INSTANCE TO THE LB#
########################
add_to_lb() {
local INSTANCE_NAME=$1
shift
local LB_PORTS=$@
local SHARED_ENDPOINT=""
local ENDPOINT_NAME=""
for CURRENT_PORT in $LB_PORTS
do
if [ "$CURRENT_PORT" == "80" ]; then
SHARED_ENDPOINT="HTTP-SHARED"
ENDPOINT_NAME="HTTP"
elif [ "$CURRENT_PORT" == "443" ]; then
SHARED_ENDPOINT="HTTPS-SHARED"
ENDPOINT_NAME="HTTPS"
fi
if [ -z $SHARED_ENDPOINT ]; then
die "Port is not one of: 80 443. Input was $LB_PORTS. Aborting..."
fi
azure vm endpoint create $INSTANCE_NAME ${CURRENT_PORT} ${CURRENT_PORT} --endpoint-name $ENDPOINT_NAME --lb-set-name $SHARED_ENDPOINT --probe-port $CURRENT_PORT --endpoint-protocol tcp
done
}
########################
#GATHER AZURE INFO#####
#######################
retrieve_azure_info() {
local output
output=`azure vm endpoint show $INSTANCE_NAME`
ENDPOINT_NUMBER=$( echo "$output" |
grep --ignore-case SSH |
awk '{print $4}' )
SSH_IP=$( echo "$output" |
grep -E --ignore-case "$ENDPOINT_NUMBER (Vip|virtualIPAddress)" |
awk '{print $6}' |
sed 's/^"\(.*\)"$/\1/' )
SSH_PORT=$( echo "$output" |
grep --ignore-case "$ENDPOINT_NUMBER port" |
awk '{print $6}' |
sed 's/^"\(.*\)"$/\1/' )
if [ -z "$SSH_IP" ] || [ -z "$SSH_PORT" ]
then
die "Could not determine the IP address of the instance $INSTANCE_NAME, aborting."
fi
echo "Instance $INSTANCE_NAME has IP address $SSH_IP"
}
########################
##CHECK INSTANCE NAME###
########################
check_instance_in_VMs() {
local match=0
local CURRENT_INSTANCE=$1
for name in $VMs; do
if [[ "${CURRENT_INSTANCE}" = "$name" ]]; then
match=1
break
fi
done
if [[ $match -eq 0 ]]; then
die "Instance name must be one among $VMs"
fi
}
#########################
####READ INSTANCE NAME###
#########################
read_instance_name() {
if [ "x${INSTANCE_NAME}" = "x" ]; then
echo "Enter the vm name and press [ENTER]. Possible choices: ${VMs}"
read INSTANCE_NAME
fi
check_instance_in_VMs "${INSTANCE_NAME}"
}
################################
############### MAIN PART ######
################################
case "$OPERATION" in
add)
read_instance_name
retrieve_azure_info
add_to_lb $INSTANCE_NAME $LB_PORTS
;;
remove)
read_instance_name
notify "Removal request for instance $INSTANCE_NAME"
count_inservice
if [ "$result" != "true" ]; then
echoerr "Insufficient number of in service VMs for the endpoint. Aborting"
exit 1
fi
retrieve_azure_info
remove_from_lb $INSTANCE_NAME $LB_PORTS
;;
status)
get_status
;;
*)
die "Usage:\n $0 {add|remove} [$VMs] \n $0 status"
esac