-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathrhev-vm-start.py
executable file
·182 lines (155 loc) · 7.23 KB
/
rhev-vm-start.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
#!/usr/bin/env python
#
# Author: Pablo Iranzo Gomez ([email protected])
#
# Description: Script for starting VM's using rhevm-sdk
# api based on single VM dependency
#
# Requires rhevm-sdk to work
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# Goals:
# - Do not manage any VM without tag elas_manage
# - reverse missing -> start VM specified
# - reverse 1 -> start all VM's if VM specified is up and running
# tags behaviour
# elas_manage: this machine is being managed by this script
import optparse
from rhev_functions import *
description = """
RHEV-vm-start is a script for managing via API the VMs under RHEV command in both RHEV-H and RHEL hosts.
It's goal is to keep some VM's started if another VM is running and host status has not changed
"""
# Option parsing
p = optparse.OptionParser("rhev-vm-start.py [arguments]", description=description)
p.add_option("-u", "--user", dest="username", help="Username to connect to RHEVM API", metavar="admin@internal",
default="admin@internal")
p.add_option("-w", "--password", dest="password", help="Password to use with username", metavar="admin",
default="admin")
p.add_option("-k", action="store_true", dest="keyring", help="use python keyring for user/password", metavar="keyring",
default=False)
p.add_option("-W", action="store_true", dest="askpassword", help="Ask for password", metavar="admin", default=False)
p.add_option("-s", "--server", dest="server", help="RHEV-M server address/hostname to contact", metavar="127.0.0.1",
default="127.0.0.1")
p.add_option("-p", "--port", dest="port", help="API port to contact", metavar="443", default="443")
p.add_option('-v', "--verbosity", dest="verbosity", help="Show messages while running", metavar='[0-n]', default=0,
type='int')
p.add_option('-t', "--tagall", dest="tagall", help="Tag all hosts with elas_manage", metavar='0/1', default=0,
type='int')
p.add_option('-c', "--cluster", dest="cluster", help="Select cluster name to process", metavar='cluster', default=None)
p.add_option('-m', "--machine", dest="machine", help="Machine name beggining", metavar="machine", default=None)
p.add_option('-r', "--reverse", dest="reverse", help="Reverse behaviour with machine name", metavar="reverse",
default=0)
(options, args) = p.parse_args()
options.username, options.password = getuserpass(options)
baseurl = "https://%s:%s/ovirt-engine/api" % (options.server, options.port)
api = apilogin(url=baseurl, username=options.username, password=options.password)
# FUNCTIONS
def process_cluster(cluster):
"""Processes cluster
@param cluster: Cluster to process
"""
# Emtpy vars for further processing
hosts_in_cluster = []
vms_in_cluster = []
# Get host list from this cluster
query = "cluster = %s" % api.clusters.get(id=cluster.id).name
for host in paginate(api.hosts, query):
if host.cluster.id == cluster.id:
hosts_in_cluster.append(host.id)
if options.verbosity > 2:
print("\nProcessing cluster %s..." % cluster.name)
print("##############################################")
# Populate the list of tags and VM's
query = "cluster = %s" % api.clusters.get(id=cluster.id).name
for vm in paginate(api.vms, query):
if vm.cluster.id == cluster.id:
vms_in_cluster.append(vm.id)
query = "cluster = %s and tag = elas_manage" % api.clusters.get(id=cluster.id).name
for vm in paginate(api.vms, query):
if vm.cluster.id == cluster.id:
if vm.tags.get("elas_manage"):
# Add the VM Id to the list of VMS to manage in this cluster
vms_in_cluster.append(vm.id)
if options.verbosity > 3:
print("Hosts in cluster:")
print(hosts_in_cluster)
print("Vm's in cluster")
print(vms_in_cluster)
destino = None
for vm in vms_in_cluster:
# Iterate until we get our target machine to monitor
maquina = api.vms.get(id=vm)
if maquina.name.startswith(options.machine):
if maquina.tags.get("elas_manage"):
destino = maquina
else:
if options.verbosity > 4:
print("Specified target machine has no elas_manage tag attached")
sys.exit(1)
# Iterate for all the machines in our cluster and check behaviour based on reverse value
one_is_up = False
for host in hosts_in_cluster:
if api.hosts.get(id=host).status.state == "up":
one_is_up = True
if destino:
if options.reverse == 0:
if destino.status.state == "down":
if options.verbosity > 3:
print("Our VM is down... try to start it if possible")
if one_is_up:
try:
destino.start()
except:
if options.verbosity > 3:
print("Error starting up machine %s" % destino.name)
else:
# Reverse is != 0... then just boot if target machine is already up
if destino.status.state == "up":
# Our target VM is not down, it's safe to start our machines up!
for vm in vms_in_cluster:
maquina = api.vms.get(id=vm)
if maquina.tags.get("elas_manage"):
if maquina.status.state != "up":
if maquina.id != destino.id:
try:
maquina.start()
except:
if options.verbosity > 3:
print("Error starting %s" % maquina.name)
else:
if options.verbosity > 4:
print("VM %s has no elas_manage tag associated" % maquina.name)
else:
if options.verbosity > 3:
print("Target machine is not up, not starting vm")
# MAIN PROGRAM
if __name__ == "__main__":
# Check if we have defined needed tags and create them if missing
check_tags(api, options)
# TAGALL?
# Add elas_maint TAG to every single vm to automate the management
if options.tagall == 1:
if options.verbosity >= 1:
print("Tagging all VM's with elas_manage")
for vm in paginate(api.vms):
try:
vm.tags.add(params.Tag(name="elas_manage"))
except:
print("Error adding elas_manage tag to vm %s" % vm.name)
if options.machine == "":
print("Error machine name must be defined")
sys.exit(1)
if not options.cluster:
# Processing each cluster of our RHEVM
for cluster in api.clusters.list():
process_cluster(cluster)
else:
process_cluster(api.clusters.get(name=options.cluster))