-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprovision.py
126 lines (113 loc) · 4.7 KB
/
provision.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
#!/usr/bin/env python
# Copyright (c) Cloud Software Group, Inc.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1) Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2) Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.
# Parse/regenerate the "disk provisioning" XML contained within templates
# NB this provisioning XML refers to disks which should be created when
# a VM is installed from this template. It does not apply to templates
# which have been created from real VMs -- they have their own disks.
import XenAPI
import xml.dom.minidom
class Disk:
"""Represents a disk which should be created for this VM"""
def __init__(self, device, size, sr, bootable):
self.device = device # 0, 1, 2, ...
self.size = size # in bytes
self.sr = sr # uuid of SR
self.bootable = bootable
def toElement(self, doc):
disk = doc.createElement("disk")
disk.setAttribute("device", self.device)
disk.setAttribute("size", self.size)
disk.setAttribute("sr", self.sr)
b = "false"
if self.bootable: b = "true"
disk.setAttribute("bootable", b)
return disk
def parseDisk(element):
device = element.getAttribute("device")
size = element.getAttribute("size")
sr = element.getAttribute("sr")
b = element.getAttribute("bootable") == "true"
return Disk(device, size, sr, b)
class ProvisionSpec:
"""Represents a provisioning specification: currently a list of required disks"""
def __init__(self):
self.disks = []
def toElement(self, doc):
element = doc.createElement("provision")
for disk in self.disks:
element.appendChild(disk.toElement(doc))
return element
def setSR(self, sr):
"""Set the requested SR for each disk"""
for disk in self.disks:
disk.sr = sr
def parseProvisionSpec(txt):
"""Return an instance of type ProvisionSpec given XML text"""
doc = xml.dom.minidom.parseString(txt)
all = doc.getElementsByTagName("provision")
if len(all) <> 1:
raise "Expected to find exactly one <provision> element"
ps = ProvisionSpec()
disks = all[0].getElementsByTagName("disk")
for disk in disks:
ps.disks.append(parseDisk(disk))
return ps
def printProvisionSpec(ps):
"""Return a string containing pretty-printed XML corresponding to the supplied provisioning spec"""
doc = xml.dom.minidom.Document()
doc.appendChild(ps.toElement(doc))
return doc.toprettyxml()
def getProvisionSpec(session, vm):
"""Read the provision spec of a template/VM"""
other_config = session.xenapi.VM.get_other_config(vm)
return parseProvisionSpec(other_config['disks'])
def setProvisionSpec(session, vm, ps):
"""Set the provision spec of a template/VM"""
txt = printProvisionSpec(ps)
try:
session.xenapi.VM.remove_from_other_config(vm, "disks")
except:
pass
session.xenapi.VM.add_to_other_config(vm, "disks", txt)
if __name__ == "__main__":
print "Unit test of provision XML spec module"
print "--------------------------------------"
ps = ProvisionSpec()
ps.disks.append(Disk("0", "1024", "0000-0000", True))
ps.disks.append(Disk("1", "2048", "1111-1111", False))
print "* Pretty-printing spec"
txt = printProvisionSpec(ps)
print txt
print "* Re-parsing output"
ps2 = parseProvisionSpec(txt)
print "* Pretty-printing spec"
txt2 = printProvisionSpec(ps)
print txt2
if txt <> txt2:
raise "Sanity-check failed: print(parse(print(x))) <> print(x)"
print "* OK: print(parse(print(x))) == print(x)"