-
Notifications
You must be signed in to change notification settings - Fork 0
/
azmaintenance
executable file
·204 lines (179 loc) · 5.16 KB
/
azmaintenance
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
#!/bin/bash
#
# Enable or Disable the maintenance mode on Azure servers
# https://github.com/luisbunuel/aztools
VERSION="1.0"
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
rule="RemoveAllActionGroups"
me=`basename "$0"`
start_date=`date -u +"%Y-%m-%d %H:%M:%S"`
temp_finish_date=`date -u -d "+1 hour" +"%Y-%m-%d %H:%M:%S"`
help() {
echo
echo "Syntax:"
echo
echo " $me -o organization -e environment -a action [-s start_date] [-f finish_date] [-d description]"
echo
echo "Options:"
echo
echo " -o Enter the organizatino IIIM|IIMM"
echo
echo " -e Enter the environment prod|test"
echo
echo " -a Enter the action enable|disable"
echo
echo -e " -s Enter the start date of the maintenance \033[33;5mUSE UTC!\033[0m"
echo " Format: \"yyyy-mm-dd hh:mm:ss\""
echo " Default: Current date"
echo
echo -e " -f Enter the finish date of the maintenance \033[33;5mUSE UTC!\033[0m"
echo " Format: \"yyyy-mm-dd hh:mm:ss\""
echo " Default: Start date + 1 hour"
echo
echo " -d Enter a description of the maintenance"
echo " Default: empty"
echo
echo "Examples:"
echo
echo " $me -o IIIM -e prod -a enable -s \"$start_date\" -f \"$temp_finish_date\" -d \"Monthly patching\" "
echo " $me -o IIMM -e test -a disable"
echo
}
if [ $# -eq 0 ]; then
help
exit 1
fi
while getopts o:e:a:s:f:d: flag
do
case "${flag}" in
o) organization=${OPTARG};;
e) environment=${OPTARG};;
a) action=${OPTARG};;
s) start_date=${OPTARG};;
f) finish_date=${OPTARG};;
d) description=${OPTARG};;
esac
done
if [ -z "$organization" ]; then
echo
echo "Please enter the organization, -o [IIIM|IIMM]"
exit 1
fi
if [ -z "$environment" ]; then
echo
echo "The environment can't be empty, enter -e [prod|test]"
exit 1
fi
if [ -z "$action" ]; then
echo
echo "Specify the action to take, enter -a [enable|disable]"
exit 1
fi
if [ -z "$finish_date" ]; then
finish_date=`date -d "$start_date +hour" +"%Y-%m-%d %H:%M:%S"`
fi
case $organization in
IIIM)
export AZURE_CONFIG_DIR=~/.azure_IIIM
az config set extension.use_dynamic_install=yes_without_prompt &>/dev/null
resource_group="RG-IIIM-cloudops"
scope="/subscriptions/5638ba20-5e01-4f95-ac49-a4c1f1f2af66/resourceGroups/RG-IIIM-cloudops"
org="iiim"
;;
IIMM)
export AZURE_CONFIG_DIR=~/.azure_IIMM
az config set extension.use_dynamic_install=yes_without_prompt &>/dev/null
resource_group="RG-IIMM-cloudops"
scope="/subscriptions/17e000ff-d917-43d8-9d1a-eaf799757636/resourceGroups/RG-IIMM-cloudops"
org="iimm"
;;
*)
echo
echo "Enter a valid organization: IIIM or IIMM"
exit 1
;;
esac
case $environment in
prod)
env="p"
servers=($org-dw-$env-appsrv-01 $org-dw-$env-appsrv-02 $org-dw-$env-compsrv-01 $org-dw-$env-compsrv-02 $org-dw-$env-compsrv-03 $org-dw-$env-intsrv-01 $org-dw-$env-intsrv-02)
servers=("${servers[@]}" $org-dw-p-monsrv-01)
;;
test)
env="t"
servers=($org-dw-$env-appsrv-01 $org-dw-$env-appsrv-02 $org-dw-$env-compsrv-01 $org-dw-$env-compsrv-02 $org-dw-$env-compsrv-03 $org-dw-$env-intsrv-01 $org-dw-$env-intsrv-02)
;;
*)
echo
echo "Enter a valid environment name: prod or test"
exit 1
;;
esac
case $action in
enable)
if ! az account get-access-token &>/dev/null; then
az login &>/dev/null
fi
az account set -s "$organization - Shared Management"
echo
echo "Enabling $organization servers maintenance"
echo
echo " Environment: $environment";
echo " Start date: $start_date" UTC;
echo " Finish date: $finish_date" UTC;
echo " Description: $description";
echo
echo "You have 10 seconds to cancel this operation ..."
secs=10
while [ $secs -gt 0 ]; do
echo -ne "$secs\033[0K\r"
sleep 1
: $((secs--))
done
echo
echo
for server in ${servers[@]}
do
az monitor alert-processing-rule create --enabled true --name "$server - Server Maintenance Mode" --description "$description" --resource-group $resource_group --scope $scope --filter-alert-context Contains $server --rule-type $rule --schedule-start-datetime "$start_date" --schedule-end-datetime "$finish_date" &>/dev/null
if [ $? == 0 ]; then
echo -e "$server: ${GREEN}Success${NC}"
else
echo -e "$server: ${RED}Error${NC}"
fi
done
;;
disable)
if ! az account get-access-token &>/dev/null ; then
az login &>/dev/null
fi
az account set -s "$organization - Shared Management"
echo
echo "Disabling $organization servers maintenance ..."
echo
echo "You have 10 seconds to cancel this operation ..."
secs=10
while [ $secs -gt 0 ]; do
echo -ne "$secs\033[0K\r"
sleep 1
: $((secs--))
done
echo
echo
for server in ${servers[@]}
do
az monitor alert-processing-rule update --resource-group "RG-$organization-cloudops" --name "$server - Server Maintenance Mode" --enabled false &>/dev/null
if [ $? == 0 ]; then
echo -e "$server: ${GREEN}Success${NC}"
else
echo -e "$server: ${RED}Error${NC}"
fi
done
;;
*)
echo
echo "Enter a valid action: enable or disable"
exit 1
;;
esac