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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
#!/usr/bin/python
"""
Copyright (C) International Business Machines Corp., 2005
Author: Stefan Berger <stefanb@us.ibm.com>
Based on XenDomain.py by Dan Smith <danms@us.ibm.com>
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; under 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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
import os
import sys
from XmTestLib import *
from types import DictType
from acm import *
class XenAPIConfig:
"""An object to help create a VM configuration usable via Xen-API"""
def __init__(self):
self.opts = {}
#Array to translate old option to new ones
self.opttrlate = { 'name' : 'name_label' ,
'memory' : [ 'memory_static_max' ,
'memory_static_min' ,
'memory_dynamic_min',
'memory_dynamic_max' ],
'kernel' : 'PV_kernel',
'ramdisk': 'PV_ramdisk',
'root' : 'PV_args'}
if isACMEnabled():
#A default so every VM can start with ACM enabled
self.opts["security_label"] = "ACM:xm-test:red"
def setOpt(self, name, value):
"""Set an option in the config"""
if name == "memory":
value <<= 20
if name in self.opttrlate.keys():
_name = self.opttrlate[name]
else:
_name = name
if isinstance(_name, list):
for _n in _name:
self.opts[_n] = value
else:
self.opts[_name] = value
def getOpt(self, name):
"""Return the value of a config option"""
if name in self.opts.keys():
return self.opts[name]
else:
return None
def setOpts(self, opts):
"""Batch-set options from a dictionary"""
for k, v in opts.items():
self.setOpt(k, v)
def getOpts(self):
return self.opts
class XenAPIDomain(XenDomain):
def __init__(self, name=None, config=None):
if name:
self.name = name
else:
self.name = getUniqueName()
self.config = config
self.console = None
self.netEnv = "bridge"
self.session = xapi.connect()
session = self.session
try:
self.vm_uuid = session.xenapi.VM.create(self.config.getOpts())
addXAPIDomain(self.vm_uuid)
except:
raise DomainError("Could not create VM config file for "
"managed domain.")
#Only support PV for now.
self.type = "PV"
def start(self, noConsole=False, startpaused=False):
#start the VM
session = self.session
if self.vm_uuid:
try:
session.xenapi.VM.start(self.vm_uuid, startpaused)
except:
raise DomainError("Could not start domain")
else:
raise DomainError("VM has no UUID - does VM config exist?")
if startpaused:
return
if self.getDomainType() == "HVM":
waitForBoot()
if self.console and noConsole == True:
self.closeConsole()
elif self.console and noConsole == False:
return self.console
elif not self.console and noConsole == False:
return self.getConsole()
def stop(self):
if self.vm_uuid:
self.session.xenapi.VM.hard_shutdown(self.vm_uuid)
else:
raise DomainError("VM has no UUID - does VM config exist?")
def destroy(self):
#Stop VM first.
self.stop()
if self.vm_uuid:
self.session.xenapi.VM.destroy(self.vm_uuid)
delXAPIDomain(self.vm_uuid)
else:
raise DomainError("VM has no UUID - does VM config exist?")
def get_uuid(self):
return self.vm_uuid
def newDevice(self, Device, *args):
raise DomainError("No support for newDevice().")
def removeDevice(self, id):
raise DomainError("No support for removeDevice().")
def removeAllDevices(self, id):
raise DomainError("No support for removeAllDevices().")
def isRunning(self):
return isDomainRunning(self.name)
def getDevice(self, id):
raise DomainError("No support for getDevice().")
class XmTestAPIDomain(XenAPIDomain):
"""Create a new managed xm-test domain
@param name: The requested domain name
@param extraConfig: Additional configuration options
@param baseConfig: The initial configuration defaults to use
"""
def __init__(self, name=None, extraConfig=None,
baseConfig=arch.configDefaults):
config = XenAPIConfig()
config.setOpts(baseConfig)
if extraConfig:
config.setOpts(extraConfig)
if name:
config.setOpt("name_label", name)
elif not config.getOpt("name_label"):
config.setOpt("name_label", getUniqueName())
XenAPIDomain.__init__(self, config.getOpt("name_label"),
config=config)
|