forked from iranzo/rhevm-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rhev-vm-create.py
executable file
·149 lines (125 loc) · 6.16 KB
/
rhev-vm-create.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
#!/usr/bin/env python
#
# Description: Script for creating VM's via API
#
# This software is based on GPL code so:
#
# 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.
#
# Requires rhevm-sdk to work or RHEVM api equivalent
#
# Author: Pablo Iranzo Gomez ([email protected])
#
# Contributors:
# Vincent Van der Kussen ([email protected])
#
import optparse
description = """
vmcreate is a script for creating vm's based on specified values
vmcpu defines the number of CPUs
sdtype can be: SD to use
sdsize can be: Storage to assing
vmgest can be: rhevm, or your defined networks
vmserv can be: rhevm, or your defined networks
osver can be: rhel_6x64, etc
"""
# Option parsing
p = optparse.OptionParser("rhev-vm-create.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="redhat")
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="server",
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("-n", "--name", dest="name", help="VM name", metavar="name", default="name")
p.add_option("-c", "--cluster", dest="cluster", help="VM cluster", metavar="cluster", default="Default")
p.add_option("--vmcpu", dest="vmcpu", help="VM CPU", metavar="vmcpu", default="1")
p.add_option("--vmmem", dest="vmmem", help="VM RAM in GB", metavar="vmmem", default="1")
p.add_option("--sdtype", dest="sdtype", help="SD type", metavar="sdtype", default="Default")
p.add_option("--sdsize", dest="sdsize", help="SD size", metavar="sdsize", default="20")
p.add_option("--osver", dest="osver", help="OS version", metavar="osver", default="rhel_6x64")
#p.add_option("--vmgest", dest="vmgest", help="Management network to use", metavar="vmgest", default="rhevm")
p.add_option("--vmserv", dest="vmserv", help="Service Network to use", metavar="vmserv", default="rhevm")
p.add_option("--sdomain", dest="sdomain", help="Explicitly define storage domain", metavar="sdomain", default="disk_storage")
p.add_option("--delprotect", dest="delprotect", help="VM Delete Protection True or False", metavar="sdomain", default="True")
(options, args) = p.parse_args()
options.username, options.password = getuserpass(options)
from rhev_functions import *
baseurl = "https://%s:%s" % (options.server, options.port)
api = apilogin(url=baseurl, username=options.username, password=options.password)
try:
value = api.hosts.list()
except:
print("Error accessing RHEV-M api, please check data and connection and retry")
sys.exit(1)
# Define VM based on parameters
if __name__ == "__main__":
vmparams = params.VM(os=params.OperatingSystem(type_=options.osver),
cpu=params.CPU(topology=params.CpuTopology(cores=int(options.vmcpu))), name=options.name,
memory=1024 * 1024 * 1024 * int(options.vmmem), cluster=api.clusters.get(name=options.cluster),
template=api.templates.get(name="Blank"), type_="server")
vmdisk = params.Disk(size=1024 * 1024 * 1024 * int(options.sdsize), wipe_after_delete=True, sparse=True,
interface="virtio", type_="System", format="cow", storage_domains=params.StorageDomains(
storage_domain=[api.storagedomains.get(name=options.sdomain)]))
vmnet = params.NIC()
network_gest = params.Network(name=options.vmgest)
network_serv = params.Network(name=options.vmserv)
nic_gest = params.NIC(name='eth0', network=network_gest, interface='virtio')
nic_serv = params.NIC(name='eth1', network=network_serv, interface='virtio')
try:
api.vms.add(vmparams)
except:
print('Error creating VM with specified parameters, recheck')
sys.exit(1)
if options.verbosity > 1:
print("VM created successfuly")
if options.verbosity > 1:
print("Attaching networks and boot order...")
vm = api.vms.get(name=options.name)
# vm.nics.add(nic_gest)
vm.nics.add(nic_serv)
# Setting VM to boot always from network, then from HD
boot1 = params.Boot(dev="network")
boot2 = params.Boot(dev="hd")
vm.os.boot = [boot1, boot2]
try:
vm.update()
except:
print('Error attaching networks, please recheck and remove configurations left behind')
sys.exit(1)
if options.verbosity > 1:
print("Adding HDD")
try:
vm.disks.add(vmdisk)
except:
print('Error attaching disk, please recheck and remove any leftover configuration')
if options.verbosity > 1:
print("VM creation successful")
# Change string to boolean for delprotect option
def str_to_bool(s):
if s == 'True':
return True
elif s == 'False':
return False
else:
raise Delete protection ValueError # evil ValueError that doesn't tell you what the wrong value was
#Change additional VM attributes
vm = api.vms.get(name=options.name)
vm.memory_policy.guaranteed = 1024 * 1024 * 1024 * int(options.vmmem)
vm.high_availability.enabled = False
vm.delete_protected = str_to_bool(options.delprotect)
vm.update()
print('MAC:%s' % vm.nics.get(name="eth0").mac.get_address())