3
Copyright (C) International Business Machines Corp., 2005
4
Author: Stefan Berger <stefanb@us.ibm.com>
6
Based on XenDomain.py by Dan Smith <danms@us.ibm.com>
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; under version 2 of the License.
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
from XmTestLib import *
25
from types import DictType
30
"""An object to help create a VM configuration usable via Xen-API"""
33
#Array to translate old option to new ones
34
self.opttrlate = { 'name' : 'name_label' ,
35
'memory' : [ 'memory_static_max' ,
38
'memory_dynamic_max' ],
39
'kernel' : 'PV_kernel',
40
'ramdisk': 'PV_ramdisk',
44
#A default so every VM can start with ACM enabled
45
self.opts["security_label"] = "ACM:xm-test:red"
47
def setOpt(self, name, value):
48
"""Set an option in the config"""
52
value = "root=" + value
53
if name in self.opttrlate.keys():
54
_name = self.opttrlate[name]
58
if isinstance(_name, list):
62
if not self.opts.get(_name) or \
63
not _name in [ "PV_args" ]:
64
self.opts[_name] = value
66
self.opts[_name] += " " + value
68
def getOpt(self, name):
69
"""Return the value of a config option"""
70
if name in self.opts.keys():
71
return self.opts[name]
75
def setOpts(self, opts):
76
"""Batch-set options from a dictionary"""
77
for k, v in opts.items():
84
class XenAPIDomain(XenDomain):
86
def __init__(self, name=None, config=None):
90
self.name = getUniqueName()
94
self.netEnv = "bridge"
96
self.session = xapi.connect()
98
self.vm_uuid = self.session.xenapi.VM.create(self.config.getOpts())
99
addXAPIDomain(self.vm_uuid)
101
raise DomainError("Could not create VM config file for "
104
#Only support PV for now.
107
def start(self, noConsole=False, startpaused=False):
109
session = self.session
112
session.xenapi.VM.start(self.vm_uuid, startpaused)
114
raise DomainError("Could not start domain")
116
raise DomainError("VM has no UUID - does VM config exist?")
121
if self.getDomainType() == "HVM":
124
if self.console and noConsole == True:
127
elif self.console and noConsole == False:
130
elif not self.console and noConsole == False:
131
return self.getConsole()
135
self.session.xenapi.VM.hard_shutdown(self.vm_uuid)
137
raise DomainError("VM has no UUID - does VM config exist?")
143
self.session.xenapi.VM.destroy(self.vm_uuid)
144
delXAPIDomain(self.vm_uuid)
146
raise DomainError("VM has no UUID - does VM config exist?")
151
def newDevice(self, Device, *args):
152
raise DomainError("No support for newDevice().")
154
def removeDevice(self, id):
155
raise DomainError("No support for removeDevice().")
157
def removeAllDevices(self, id):
158
raise DomainError("No support for removeAllDevices().")
161
return isDomainRunning(self.name)
163
def getDevice(self, id):
164
raise DomainError("No support for getDevice().")
167
class XmTestAPIDomain(XenAPIDomain):
169
"""Create a new managed xm-test domain
170
@param name: The requested domain name
171
@param extraConfig: Additional configuration options
172
@param baseConfig: The initial configuration defaults to use
174
def __init__(self, name=None, extraConfig=None,
175
baseConfig=arch.configDefaults):
176
config = XenAPIConfig()
177
config.setOpts(baseConfig)
179
config.setOpts(extraConfig)
182
config.setOpt("name_label", name)
183
elif not config.getOpt("name_label"):
184
config.setOpt("name_label", getUniqueName())
186
XenAPIDomain.__init__(self, config.getOpt("name_label"),