forked from iranzo/rhevm-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathovirt-vm-create.py
executable file
·109 lines (90 loc) · 3.8 KB
/
ovirt-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
#!/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])
#
from ovirt_functions import *
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
"""
#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)
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.vmname,
memory=1024 * 1024 * 1024 * int(options.vmmem), cluster=api.clusters.get(name=options.cluster),
template=api.templates.get(name=options.templatename), type_=options.templatetype)
vmdisk = params.Disk(size=1024 * 1024 * 1024 * int(options.sdsize), wipe_after_delete=True, sparse=True,
interface=options.vnictype, type_="System", format="cow", storage_domains=params.StorageDomains(
storage_domain=[api.storagedomains.get(name=options.storagedomain)]))
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=options.vnictype)
nic_serv = params.NIC(name='eth1', network=network_serv, interface=options.vnictype)
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.vmname)
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")
vm = api.vms.get(name=options.vmname)
vm.memory_policy.guaranteed = 1 * 1024 * 1024
vm.high_availability.enabled = True
vm.update()
print('MAC:%s' % vm.nics.get(name="eth0").mac.get_address())