ia64/xen-unstable

view tools/xm-test/lib/XmTestLib/XenManagedDomain.py @ 12791:a2618d3912e7

This adds a recently added parameter to the vm.start call.

Signed-off-by: Stefan Berger <stefanb@us.ibm.com>
author Ewan Mellor <ewan@xensource.com>
date Mon Dec 04 19:13:55 2006 +0000 (2006-12-04)
parents b37e66b08bf5
children
line source
1 #!/usr/bin/python
2 """
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
21 """
22 import os
23 import sys
24 from XmTestLib import *
25 from xen.util.xmlrpclib2 import ServerProxy
26 from types import DictType
29 class XenManagedConfig:
30 """An object to help create a VM configuration usable via Xen-API"""
31 def __init__(self):
32 self.opts = {}
33 #Array to translate old option to new ones
34 self.opttrlate = { 'name' : 'name_label' ,
35 'memory' : [ 'memory_static_max' ,
36 'memory_static_min' ,
37 'memory_dynamic_min',
38 'memory_dynamic_max' ],
39 'kernel' : 'kernel_kernel',
40 'ramdisk': 'kernel_initrd',
41 'root' : 'kernel_args'}
43 def setOpt(self, name, value):
44 """Set an option in the config"""
45 if name in self.opttrlate.keys():
46 _name = self.opttrlate[name]
47 else:
48 _name = name
50 if isinstance(_name, list):
51 for _n in _name:
52 self.opts[_n] = value
53 else:
54 self.opts[_name] = value
56 def getOpt(self, name):
57 """Return the value of a config option"""
58 if name in self.opts.keys():
59 return self.opts[name]
60 else:
61 return None
63 def setOpts(self, opts):
64 """Batch-set options from a dictionary"""
65 for k, v in opts.items():
66 self.setOpt(k, v)
68 def getOpts(self):
69 return self.opts
72 class XenManagedDomain(XenDomain):
74 def __init__(self, name=None, config=None):
75 if name:
76 self.name = name
77 else:
78 self.name = getUniqueName()
80 self.config = config
81 self.console = None
82 self.netEnv = "bridge"
84 self.server, self.session = xapi._connect()
85 server = self.server
86 try:
87 self.vm_uuid = xapi.execute(server.VM.create, self.session,
88 self.config.getOpts())
89 xapi._VMuuids.append(self.vm_uuid)
90 except:
91 raise DomainError("Could not create VM config file for "
92 "managed domain.")
94 #Only support PV for now.
95 self.type = "PV"
97 def start(self, noConsole=False, startpaused=False):
98 #start the VM
99 server = self.server
100 if self.vm_uuid:
101 try:
102 xapi.execute(server.VM.start, self.session, self.vm_uuid,
103 startpaused)
104 except:
105 raise DomainError("Could not start domain")
106 else:
107 raise DomainError("VM has not UUID - VM config does not exist?")
109 if self.getDomainType() == "HVM":
110 waitForBoot()
112 if self.console and noConsole == True:
113 self.closeConsole()
115 elif self.console and noConsole == False:
116 return self.console
118 elif not self.console and noConsole == False:
119 return self.getConsole()
121 def stop(self):
122 if self.vm_uuid:
123 server = self.server
124 xapi.execute(server.VM.hard_shutdown, self.session, self.vm_uuid)
125 else:
126 raise DomainError("VM has not UUID - VM config does not exist?")
128 def destroy(self):
129 #Stop VM first.
130 self.stop()
131 if self.vm_uuid:
132 server = self.server
133 xapi.execute(server.VM.destroy, self.session, self.vm_uuid)
134 xapi._VMuuids.remove(self.vm_uuid)
135 else:
136 raise DomainError("VM has not UUID - VM config does not exist?")
138 def get_uuid(self):
139 return self.vm_uuid
141 def newDevice(self, Device, *args):
142 raise DomainError("No support for newDevice().")
144 def removeDevice(self, id):
145 raise DomainError("No support for removeDevice().")
147 def removeAllDevices(self, id):
148 raise DomainError("No support for removeAllDevices().")
150 def isRunning(self):
151 return isDomainRunning(self.name)
153 def getDevice(self, id):
154 raise DomainError("No support for getDevice().")
157 class XmTestManagedDomain(XenManagedDomain):
159 """Create a new managed xm-test domain
160 @param name: The requested domain name
161 @param extraConfig: Additional configuration options
162 @param baseConfig: The initial configuration defaults to use
163 """
164 def __init__(self, name=None, extraConfig=None,
165 baseConfig=arch.configDefaults):
166 config = XenManagedConfig()
167 config.setOpts(baseConfig)
168 if extraConfig:
169 config.setOpts(extraConfig)
171 if name:
172 config.setOpt("name_label", name)
173 elif not config.getOpt("name_label"):
174 config.setOpt("name_label", getUniqueName())
176 XenManagedDomain.__init__(self, config.getOpt("name_label"),
177 config=config)