1 | #!/usr/bin/python |
---|
2 | """ |
---|
3 | Copyright (C) International Business Machines Corp., 2005 |
---|
4 | Author: Stefan Berger <stefanb@us.ibm.com> |
---|
5 | |
---|
6 | Based on XenDomain.py by Dan Smith <danms@us.ibm.com> |
---|
7 | |
---|
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. |
---|
11 | |
---|
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. |
---|
16 | |
---|
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 |
---|
20 | |
---|
21 | """ |
---|
22 | import os |
---|
23 | import sys |
---|
24 | from XmTestLib import * |
---|
25 | from types import DictType |
---|
26 | |
---|
27 | |
---|
28 | class XenAPIConfig: |
---|
29 | """An object to help create a VM configuration usable via Xen-API""" |
---|
30 | def __init__(self): |
---|
31 | self.opts = {} |
---|
32 | #Array to translate old option to new ones |
---|
33 | self.opttrlate = { 'name' : 'name_label' , |
---|
34 | 'memory' : [ 'memory_static_max' , |
---|
35 | 'memory_static_min' , |
---|
36 | 'memory_dynamic_min', |
---|
37 | 'memory_dynamic_max' ], |
---|
38 | 'kernel' : 'PV_kernel', |
---|
39 | 'ramdisk': 'PV_ramdisk', |
---|
40 | 'root' : 'PV_args'} |
---|
41 | |
---|
42 | def setOpt(self, name, value): |
---|
43 | """Set an option in the config""" |
---|
44 | if name == "memory": |
---|
45 | value <<= 20 |
---|
46 | if name in self.opttrlate.keys(): |
---|
47 | _name = self.opttrlate[name] |
---|
48 | else: |
---|
49 | _name = name |
---|
50 | |
---|
51 | if isinstance(_name, list): |
---|
52 | for _n in _name: |
---|
53 | self.opts[_n] = value |
---|
54 | else: |
---|
55 | self.opts[_name] = value |
---|
56 | |
---|
57 | def getOpt(self, name): |
---|
58 | """Return the value of a config option""" |
---|
59 | if name in self.opts.keys(): |
---|
60 | return self.opts[name] |
---|
61 | else: |
---|
62 | return None |
---|
63 | |
---|
64 | def setOpts(self, opts): |
---|
65 | """Batch-set options from a dictionary""" |
---|
66 | for k, v in opts.items(): |
---|
67 | self.setOpt(k, v) |
---|
68 | |
---|
69 | def getOpts(self): |
---|
70 | return self.opts |
---|
71 | |
---|
72 | |
---|
73 | class XenAPIDomain(XenDomain): |
---|
74 | |
---|
75 | def __init__(self, name=None, config=None): |
---|
76 | if name: |
---|
77 | self.name = name |
---|
78 | else: |
---|
79 | self.name = getUniqueName() |
---|
80 | |
---|
81 | self.config = config |
---|
82 | self.console = None |
---|
83 | self.netEnv = "bridge" |
---|
84 | |
---|
85 | self.session = xapi.connect() |
---|
86 | session = self.session |
---|
87 | try: |
---|
88 | self.vm_uuid = session.xenapi.VM.create(self.config.getOpts()) |
---|
89 | addXAPIDomain(self.vm_uuid) |
---|
90 | except: |
---|
91 | raise DomainError("Could not create VM config file for " |
---|
92 | "managed domain.") |
---|
93 | |
---|
94 | #Only support PV for now. |
---|
95 | self.type = "PV" |
---|
96 | |
---|
97 | def start(self, noConsole=False, startpaused=False): |
---|
98 | #start the VM |
---|
99 | session = self.session |
---|
100 | if self.vm_uuid: |
---|
101 | try: |
---|
102 | session.xenapi.VM.start(self.vm_uuid, startpaused) |
---|
103 | except: |
---|
104 | raise DomainError("Could not start domain") |
---|
105 | else: |
---|
106 | raise DomainError("VM has no UUID - does VM config exist?") |
---|
107 | |
---|
108 | if startpaused: |
---|
109 | return |
---|
110 | |
---|
111 | if self.getDomainType() == "HVM": |
---|
112 | waitForBoot() |
---|
113 | |
---|
114 | if self.console and noConsole == True: |
---|
115 | self.closeConsole() |
---|
116 | |
---|
117 | elif self.console and noConsole == False: |
---|
118 | return self.console |
---|
119 | |
---|
120 | elif not self.console and noConsole == False: |
---|
121 | return self.getConsole() |
---|
122 | |
---|
123 | def stop(self): |
---|
124 | if self.vm_uuid: |
---|
125 | self.session.xenapi.VM.hard_shutdown(self.vm_uuid) |
---|
126 | else: |
---|
127 | raise DomainError("VM has no UUID - does VM config exist?") |
---|
128 | |
---|
129 | def destroy(self): |
---|
130 | #Stop VM first. |
---|
131 | self.stop() |
---|
132 | if self.vm_uuid: |
---|
133 | self.session.xenapi.VM.destroy(self.vm_uuid) |
---|
134 | delXAPIDomain(self.vm_uuid) |
---|
135 | else: |
---|
136 | raise DomainError("VM has no UUID - does VM config exist?") |
---|
137 | |
---|
138 | def get_uuid(self): |
---|
139 | return self.vm_uuid |
---|
140 | |
---|
141 | def newDevice(self, Device, *args): |
---|
142 | raise DomainError("No support for newDevice().") |
---|
143 | |
---|
144 | def removeDevice(self, id): |
---|
145 | raise DomainError("No support for removeDevice().") |
---|
146 | |
---|
147 | def removeAllDevices(self, id): |
---|
148 | raise DomainError("No support for removeAllDevices().") |
---|
149 | |
---|
150 | def isRunning(self): |
---|
151 | return isDomainRunning(self.name) |
---|
152 | |
---|
153 | def getDevice(self, id): |
---|
154 | raise DomainError("No support for getDevice().") |
---|
155 | |
---|
156 | |
---|
157 | class XmTestAPIDomain(XenAPIDomain): |
---|
158 | |
---|
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 = XenAPIConfig() |
---|
167 | config.setOpts(baseConfig) |
---|
168 | if extraConfig: |
---|
169 | config.setOpts(extraConfig) |
---|
170 | |
---|
171 | if name: |
---|
172 | config.setOpt("name_label", name) |
---|
173 | elif not config.getOpt("name_label"): |
---|
174 | config.setOpt("name_label", getUniqueName()) |
---|
175 | |
---|
176 | XenAPIDomain.__init__(self, config.getOpt("name_label"), |
---|
177 | config=config) |
---|