-
Notifications
You must be signed in to change notification settings - Fork 0
/
evacuate.sh
executable file
·207 lines (183 loc) · 5.95 KB
/
evacuate.sh
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
#!/bin/bash
###############################################################################
# This script is used to put an OpenStack Compute Node into maintenance mode.
# In this case, that means disabling scheduling to the node and migrating all
# instances off of it
# NOTE: This requires shared storage for instances.
###############################################################################
usage ()
{
echo "Usage: $0 [OPTIONS]"
echo " -h Get help"
echo " -n <node> Hypervisor Node to change (in form of hostname)"
echo " -a <action> check - check status of node"
echo " disable - disable node and migrate instances off"
echo " enable - enable node"
echo " -k <keystonerc file> Path to the keystone credentials file"
echo " OpenStack credentials are needed"
echo " -s <sleep seconds> Time to sleep between migration and check"
echo " -t <live|nonlive> live migration by default. nonlive if specified"
}
while getopts 'h:n:a:k:t:' OPTION
do
case $OPTION in
h)
usage
exit 0
;;
n)
export NODE=$OPTARG
;;
a)
export ACTION=$OPTARG
if [[ ! $ACTION =~ ^(check|disable|enable)$ ]]; then
usage
exit 3
fi
;;
k)
export KEYSTONE=$OPTARG
if [[ ! -f $KEYSTONE ]]; then
echo "ERROR: Keystone file ($KEYSTONE) does not exist"
exit 3
else
source $KEYSTONE
fi
;;
s)
export SLEEP=$OPTARG
;;
t)
export TYPE=$OPTARG
if [[ $TYPE == "" ]]; then
TYPE=live
fi
if [[ ! $TYPE =~ ^(live|nonlive)$ ]]; then
usage
exit 3
fi
;;
*)
usage
exit 3
;;
esac
done
check_hypervisor ()
{
# Check scheduling to this node
NOVA=$(nova hypervisor-list | grep $NODE)
NOVASTATE=$(echo $NOVA | awk '{print $8}')
echo "+---------------------------------------------------------------------------+"
echo "Scheduling Status for $NODE: $NOVASTATE"
echo "+---------------------------------------------------------------------------+"
echo $NOVA
echo ""
# Check instances running on this node
INSTANCE_COUNT=$(nova hypervisor-servers $NODE | grep -v "\-\-\-\-\-" | grep -v "Hostname" | wc -l)
echo "+---------------------------------------------------------------------------+"
echo "$NODE is currently running $INSTANCE_COUNT instances"
echo "+---------------------------------------------------------------------------+"
nova hypervisor-servers $NODE | grep -v "\-\-\-\-\-" | awk '{print $2" "$4}' | column -t
echo ""
}
disable_hypervisor ()
{
# Disable Scheduling to the node
nova service-disable $NODE nova-compute
# Verify Scheduling is disabled
if [[ $(nova service-list | grep $NODE | grep disabled | wc -l) -eq 0 ]] ; then
echo "Error: nova-compute not disabled on $NODE"
nova service-list | grep $NODE
exit 1
fi
# Check instances on the host
INSTANCE_COUNT=$(nova hypervisor-servers $NODE | grep -v "\-\-\-\-\-" | grep -v "Hostname" | wc -l)
if [[ $INSTANCE_COUNT -eq 0 ]]; then
echo "No instances running on host $NODE."
echo "OK to begin maintenance on $NODE"
else
# Check instances on Host
echo "$NODE is currently running $INSTANCE_COUNT instances"
echo ""
echo "Migration will be attempted for the following instances:"
echo ""
nova hypervisor-servers $NODE
# Live migrate instances off the host
if [[ $TYPE == "nonlive" ]] ; then
echo "Beginning non-live migration. Instances will be restarted on an alternate hypervisor"
nova host-evacuate $NODE
#nova live-migration <instance> <host> # Another option here
else
echo "Beginning live migration"
nova host-evacuate-live $NODE
fi
echo "Sleeping for $SLEEP seconds while instances migrate"
sleep $SLEEP
# Validate no instances remain
INSTANCE_COUNT=0
INSTANCE_COUNT=$(nova hypervisor-servers $NODE | grep -v "\-\-\-\-\-" | grep -v "Hostname" | wc -l)
if [[ $INSTANCE_COUNT -eq 0 ]]; then
echo "Migration(s) successful. No instances running on host $NODE."
echo "OK to begin maintenance on $NODE"
else
echo "ERROR: Migration failed. $INSTANCE_COUNT instances still remain on $NODE"
echo ""
echo "List of remaining instances"
echo ""
nova hypervisor-servers $NODE
exit 1
fi
fi
}
enable_hypervisor ()
{
# Enable Scheduling to the node
nova service-enable $NODE nova-compute
# Verify Scheduling is enabled
if [[ $(nova service-list | grep $NODE | grep enabled | wc -l) -eq 0 ]] ; then
echo "Error: nova-compute not enabled on $NODE"
nova service-list | grep $NODE
exit 1
fi
}
# Main
if [[ $NODE == "" ]]; then
echo "ERROR: Node is null"
echo ""
usage
exit 1
fi
if [[ $SLEEP == "" ]]; then
# Sleep 60 seconds by default
export SLEEP=60
fi
if [[ $TYPE == "" ]]; then
TYPE=live
fi
# Validate OpenStack credentials
if [[ -z $OS_AUTH_URL || -z $OS_TENANT_NAME || -z $OS_USERNAME || -z $OS_PASSWORD ]] ; then
echo "ERROR: One or more OpenStack variables not specified"
exit 1
fi
# Validate you can access nova
nova hypervisor-list > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "ERROR: nova not responding. Are your OpenStack credentials correct?"
exit 1
fi
# Validate you can locate the hypervisor specified
if [[ $(nova hypervisor-list | grep " $NODE " | wc -l) -eq 0 ]]; then
echo "ERROR: nova hypervisor ($NODE) not found"
exit 1
fi
if [[ $ACTION == "check" ]]; then
check_hypervisor
elif [[ $ACTION == "disable" ]]; then
disable_hypervisor
elif [[ $ACTION == "enable" ]]; then
enable_hypervisor
else
echo "ERROR: Action ($ACTION) not known"
exit 1
fi