-
Notifications
You must be signed in to change notification settings - Fork 23
/
rhev-ovf.py
executable file
·143 lines (126 loc) · 5.23 KB
/
rhev-ovf.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
#!/usr/bin/env python
#
# Description: Parse ovf to start VM
# Author: Pablo.Iranzo@gmail.com
#
# Based on Karim Boumedhel's code at
# https://github.com/karmab/ovirt/blob/master/hypervisor.py
#
# 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.
import optparse
import sys
import xml.etree.ElementTree as elementtree
description = """
Parse an OVF file on disk to show the information needed to start
the VM via command line
"""
# Option parsing
p = optparse.OptionParser("rhev-ovf.py [arguments]", description=description)
p.add_option('-f', "--file", dest="file", help="OVF file to parse",
metavar='ovf', default=None)
(options, args) = p.parse_args()
def getvminfo(host, vmid, display, root):
"""Parses XML tree to gather all the information from VM"""
global diskdomids
cmd = {"vmId": vmid, "kvmEnable": "True", "vmType": "kvm", "tabletEnable": "True", "vmEnable": "True",
"irqChip": "True", "nice": 0, "keyboardLayout": "en-us", "acpiEnable": "True", "display": "qxl",
"displayIp": host, "spiceMonitors": "1", "displayNetwork": display}
disks = []
diskboot = None
diskvolid = None
diskimageid = None
diskformat = None
for child in root:
if child.tag == "Section" and "ovf:DiskSection_Type" in child.attrib.values():
for disk in child.findall("Disk"):
for info in disk.attrib:
if "boot" in info:
diskboot = disk.attrib[info]
if "fileRef" in info:
diskimageid, diskvolid = disk.attrib[info].split("/")
if "volume-format" in info:
diskformat = disk.attrib[info].lower()
disks.append({"boot": diskboot, "volumeID": diskvolid,
"imageID": diskimageid, "format": diskformat})
for content in root.findall("Content"):
name = content.findall("Name")[0].text
display = content.findall("DefaultDisplayType")[0].text
if display != "1":
cmd["display"] = "vnc"
else:
cmd["display"] = "qxl"
sections = content.findall("Section")
for hardware in sections:
if "ovf:VirtualHardwareSection_Type" in hardware.attrib.values():
macs = []
nicnames = []
bridges = []
nicmodels = []
diskdomids = []
diskpoolids = []
for item in hardware.findall("Item"):
for element in item:
if "num_of_sockets" in element.tag:
smp = element.text
if "cpu_per_socket" in element.tag:
cpuspersocket = element.text
if "VirtualQuantity" in element.tag:
memory = element.text
if "AllocationUnits" in element.tag:
memoryunits = element.text
if "MACAddress" in element.tag:
macs.append(element.text)
if "Name" in element.tag:
nicnames.append(element.text)
if "Connection" in element.tag:
bridges.append(element.text)
if "ResourceSubType" in element.tag:
if element.text == "1":
nicmodels.append("rtl8139")
if element.text == "2":
nicmodels.append("e1000")
if element.text == "3":
nicmodels.append("pv")
if "StorageId" in element.tag:
diskdomids.append(element.text)
if "StoragePoolId" in element.tag:
diskpoolids.append(element.text)
counter = 0
cmd["drives"] = []
for disk in disks:
cmd["drives"].append(disk)
cmd["drives"][counter]["domainID"] = diskdomids[counter]
cmd["drives"][counter]["poolID"] = diskpoolids[counter]
counter += 1
cmd["memSize"] = memory
cmd["smpCoresPerSocket"] = cpuspersocket
cmd["smp"] = smp
cmd["bridge"] = ",".join(bridges)
cmd["macAddr"] = ",".join(macs)
cmd["vmName"] = name
cmd["nicModel"] = ",".join(nicmodels)
return cmd
try:
tree = elementtree.parse(options.file)
except:
print("Error opening the ovf file %s" % options.file)
sys.exit(1)
root = tree.getroot()
for content in root.findall('Content'):
vmname = content.findall("Name")[0].text
template = content.findall("TemplateId")[0].text
if template == "00000000-0000-0000-0000-000000000000":
break
# Create VM
vmid = "00"
display = "spice"
host = "localhost"
cmd = getvminfo(host, vmid, display, root)
print(cmd)