-
Notifications
You must be signed in to change notification settings - Fork 29
/
hypervisorMaintenance.py
executable file
·252 lines (217 loc) · 8.65 KB
/
hypervisorMaintenance.py
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
247
248
249
250
251
252
#!/usr/bin/python
# Copyright 2015, Schuberg Philis BV
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# Script to put a host in maintenance, and if that fails migrate vm's one by one
# so that it will enter maintenance after all
# Remi Bergsma - [email protected]
import time
import sys
import getopt
from cloudstackops import cloudstackops
from cloudstackops import cloudstackopsssh
from cloudstackops import xenserver
from cloudstackops import kvm
import os.path
import getpass
# Function to handle our arguments
def handleArguments(argv):
global DEBUG
DEBUG = 0
global DRYRUN
DRYRUN = 1
global hostname
hostname = ''
global configProfileName
configProfileName = ''
global cancelmaintenance
cancelmaintenance = 0
global force
force = 0
global checkBonds
checkBonds = True
# Usage message
help = "Usage: ./" + os.path.basename(__file__) + ' [options] ' + \
'\n --config-profile -c <profilename>\tSpecify the CloudMonkey profile name to get the credentials from (or specify in ./config file)' + \
'\n --hostname|-n <hostname>\t\tWork with this hypervisor' + \
'\n --cancel-maintenance\t\t\tCancel maintenance for this hypervisor' + \
'\n --no-bond-check\t\t\tSkip the bond check' + \
'\n --force\t\t\t\tForce put in maintenance, even when there is already a hypervisor in maintenance' + \
'\n --debug\t\t\t\tEnable debug mode' + \
'\n --exec\t\t\t\tExecute for real'
try:
opts, args = getopt.getopt(
argv, "hc:n:", [
"credentials-file=", "hostname=", "debug", "exec", "cancel-maintenance", "force", "no-bond-check"])
except getopt.GetoptError as e:
print "Error: " + str(e)
print help
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print help
sys.exit()
elif opt in ("-c", "--config-profile"):
configProfileName = arg
elif opt in ("-n", "--hostname"):
hostname = arg
elif opt in ("--cancel-maintenance"):
cancelmaintenance = 1
elif opt in ("--debug"):
DEBUG = 1
elif opt in ("--exec"):
DRYRUN = 0
elif opt in ("--force"):
force = 1
elif opt in ("--no-bond-check"):
checkBonds = False
# Default to cloudmonkey default config file
if len(configProfileName) == 0:
configProfileName = "config"
# We need at least these vars
if len(hostname) == 0:
print help
sys.exit()
# Parse arguments
if __name__ == "__main__":
handleArguments(sys.argv[1:])
# Init our classes
c = cloudstackops.CloudStackOps(DEBUG, DRYRUN)
ssh = cloudstackopsssh.CloudStackOpsSSH(DEBUG, DRYRUN)
c.ssh = ssh
# Init XenServer class
x = xenserver.xenserver('root')
x.DEBUG = DEBUG
x.DRYRUN = DRYRUN
c.xenserver = x
# Init KVM class
k = kvm.Kvm(ssh_user=getpass.getuser())
k.DEBUG = DEBUG
k.DRYRUN = DRYRUN
c.kvm = k
if DEBUG == 1:
print "Warning: Debug mode is enabled!"
if DRYRUN == 1:
print "Warning: dry-run mode is enabled, not running any commands!"
# make credentials file known to our class
c.configProfileName = configProfileName
# Init the CloudStack API
c.initCloudStackAPI()
if DEBUG == 1:
print "API address: " + c.apiurl
print "ApiKey: " + c.apikey
print "SecretKey: " + c.secretkey
# Check cloudstack IDs
if DEBUG == 1:
print "Note: Checking CloudStack IDs of provided input.."
hostID = c.checkCloudStackName({'csname': hostname, 'csApiCall': 'listHosts'})
if hostID == 1:
print "Error: Host " + hostname + " could not be found."
sys.exit(1)
# Get hosts data
hostData = c.getHostData({'hostname': hostname})
for host in hostData:
if host.name == hostname:
foundHostData = host
# Get hosts that belong to toCluster
clusterHostsData = c.getAllHostsFromCluster(foundHostData.clusterid)
print "Note: Host '" + hostname + "' belongs to cluster '" + foundHostData.clustername + "'"
if foundHostData.hypervisor not in ("XenServer", "KVM"):
print "Error: This is only tested for KVM and XenServer at the moment!"
sys.exit(1)
# Test SSH connection
retcode, output = ssh.testSSHConnection(foundHostData.ipaddress)
if retcode != 0:
sys.exit(1)
# Poolmaster
poolmaster = "n/a"
if foundHostData.hypervisor == "XenServer":
retcode, poolmaster = ssh.getPoolmaster(foundHostData.ipaddress)
print "Note: Poolmaster is: '" + poolmaster + "'"
print "Note: Looking for other hosts in this cluster and checking their health.."
# Print overview
c.printHypervisors(foundHostData.clusterid, poolmaster, checkBonds, foundHostData.hypervisor)
# Look for hosts without XenTools
if foundHostData.hypervisor == "XenServer":
retcode, output = ssh.fakePVTools(foundHostData.ipaddress)
if retcode != 0:
print "Error: something went wrong. Got return code " + str(retcode)
else:
print "Note: Command executed OK."
# Cancel maintenance
if cancelmaintenance == 1 and DRYRUN == 0:
print "Note: You want to cancel maintenance for host '" + hostname + "'"
# Does it make sense?
if foundHostData.resourcestate != "Maintenance" and foundHostData.resourcestate != "PrepareForMaintenance":
print "Error: Host '" + hostname + "' is not in maintenance, so can not cancel. Halting."
sys.exit(1)
# Cancel maintenance
cancelresult = c.cancelHostMaintenance(hostID)
if cancelresult is None or cancelresult == 1:
print "Error: Cancel maintenance failed. Please investigate manually. Halting."
# Check result
while True:
hostData = c.getHostData({'hostname': hostname})
for host in hostData:
if host.name == hostname:
foundHostData = host
if foundHostData.resourcestate != "Enabled":
print "Note: Resource state currently is '" + foundHostData.resourcestate + "', waiting some more.."
time.sleep(5)
else:
print "Note: Resource state currently is '" + foundHostData.resourcestate + "', returning"
break
print "Note: Cancel maintenance succeeded for host '" + hostname + "'"
# Print overview
c.printHypervisors(foundHostData.clusterid, poolmaster, checkBonds, foundHostData.hypervisor)
print "Note: We're done!"
sys.exit(0)
elif cancelmaintenance == 1 and DRYRUN == 1:
print "Note: Would have cancelled maintenance for host '" + hostname + "'."
sys.exit(0)
elif DRYRUN == 1:
print "Note: Would have enabled maintenance for host '" + hostname + "'."
# Check if we are safe to put a hypervisor in Maintenance
safe = c.safeToPutInMaintenance(foundHostData.clusterid)
if safe == False and force == 0:
print "Error: All hosts should be in resouce state 'Enabled' before putting a host to maintenance. " \
"Use --force to to ignore WARNING states. Halting."
sys.exit(1)
elif safe == False and force == 1:
print "Warning: To be safe, all hosts should be in resouce state 'Enabled' before putting a host to maintenance"
print "Warning: You used --force to to ignore WARNING states. Assuming you know what you are doing.."
else:
print "Note: All resource states are 'Enabled', we can safely put one to maintenance"
if DEBUG == 1:
print "Debug: Host to put in maintenance: " + hostID
# Migrate all vm's and empty hypervisor
c.emptyHypervisor(hostID)
# Put host in CloudStack Maintenance
maintenanceresult = c.startMaintenance(hostID, hostname)
if maintenanceresult:
# Print overview
c.printHypervisors(foundHostData.clusterid, poolmaster, checkBonds, foundHostData.hypervisor)
print "Note: We're done!"
sys.exit(0)
elif DRYRUN == 0:
print "Error: Could not enable Maintenance for host '" + hostname + "'. Please investigate manually. Halting."
elif DRYRUN == 1:
print "Note: We're done!"
if DRYRUN == 0:
# Print overview
c.printHypervisors(foundHostData.clusterid, poolmaster, checkBonds, foundHostData.hypervisor)