forked from iranzo/rhevm-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathovirt-vm-start.py
executable file
·159 lines (135 loc) · 5.79 KB
/
ovirt-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
#!/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
from ovirt_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
"""
#Parse args in ovirt_functions: parseoptions(basename, description, args)
options = parseoptions(sys.argv[0], description, sys.argv[1:])
options.username, options.password = getuserpass(options)
baseurl = "https://%s:%s" % (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))