~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to tools/xm-test/lib/XmTestLib/XenAPIDomain.py

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
from acm import *
 
27
 
 
28
 
 
29
class XenAPIConfig:
 
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' : 'PV_kernel',
 
40
                           'ramdisk': 'PV_ramdisk',
 
41
                           'root'   : 'PV_args',
 
42
                           'extra'  : 'PV_args' }
 
43
        if isACMEnabled():
 
44
            #A default so every VM can start with ACM enabled
 
45
            self.opts["security_label"] = "ACM:xm-test:red"
 
46
 
 
47
    def setOpt(self, name, value):
 
48
        """Set an option in the config"""
 
49
        if name == "memory":
 
50
            value <<= 20
 
51
        if name == "root":
 
52
            value = "root=" + value
 
53
        if name in self.opttrlate.keys():
 
54
            _name = self.opttrlate[name]
 
55
        else:
 
56
            _name = name
 
57
 
 
58
        if isinstance(_name, list):
 
59
            for _n in _name:
 
60
                self.opts[_n] = value
 
61
        else:
 
62
            if not self.opts.get(_name) or \
 
63
               not _name in [ "PV_args" ]:
 
64
                self.opts[_name] = value
 
65
            else:
 
66
                self.opts[_name] += " " + value
 
67
 
 
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]
 
72
        else:
 
73
            return None
 
74
 
 
75
    def setOpts(self, opts):
 
76
        """Batch-set options from a dictionary"""
 
77
        for k, v in opts.items():
 
78
            self.setOpt(k, v)
 
79
 
 
80
    def getOpts(self):
 
81
        return self.opts
 
82
 
 
83
 
 
84
class XenAPIDomain(XenDomain):
 
85
 
 
86
    def __init__(self, name=None, config=None):
 
87
        if name:
 
88
            self.name = name
 
89
        else:
 
90
            self.name = getUniqueName()
 
91
 
 
92
        self.config = config
 
93
        self.console = None
 
94
        self.netEnv = "bridge"
 
95
 
 
96
        self.session = xapi.connect()
 
97
        try:
 
98
            self.vm_uuid = self.session.xenapi.VM.create(self.config.getOpts())
 
99
            addXAPIDomain(self.vm_uuid)
 
100
        except:
 
101
            raise DomainError("Could not create VM config file for "
 
102
                              "managed domain.")
 
103
 
 
104
        #Only support PV for now.
 
105
        self.type = "PV"
 
106
 
 
107
    def start(self, noConsole=False, startpaused=False):
 
108
        #start the VM
 
109
        session = self.session
 
110
        if self.vm_uuid:
 
111
            try:
 
112
                session.xenapi.VM.start(self.vm_uuid, startpaused)
 
113
            except:
 
114
                raise DomainError("Could not start domain")
 
115
        else:
 
116
            raise DomainError("VM has no UUID - does VM config exist?")
 
117
 
 
118
        if startpaused:
 
119
           return
 
120
 
 
121
        if self.getDomainType() == "HVM":
 
122
           waitForBoot()
 
123
 
 
124
        if self.console and noConsole == True:
 
125
            self.closeConsole()
 
126
 
 
127
        elif self.console and noConsole == False:
 
128
            return self.console
 
129
 
 
130
        elif not self.console and noConsole == False:
 
131
            return self.getConsole()
 
132
 
 
133
    def stop(self):
 
134
        if self.vm_uuid:
 
135
            self.session.xenapi.VM.hard_shutdown(self.vm_uuid)
 
136
        else:
 
137
            raise DomainError("VM has no UUID - does VM config exist?")
 
138
 
 
139
    def destroy(self):
 
140
        #Stop VM first.
 
141
        self.stop()
 
142
        if self.vm_uuid:
 
143
            self.session.xenapi.VM.destroy(self.vm_uuid)
 
144
            delXAPIDomain(self.vm_uuid)
 
145
        else:
 
146
            raise DomainError("VM has no UUID - does VM config exist?")
 
147
 
 
148
    def get_uuid(self):
 
149
        return self.vm_uuid
 
150
 
 
151
    def newDevice(self, Device, *args):
 
152
        raise DomainError("No support for newDevice().")
 
153
 
 
154
    def removeDevice(self, id):
 
155
        raise DomainError("No support for removeDevice().")
 
156
 
 
157
    def removeAllDevices(self, id):
 
158
        raise DomainError("No support for removeAllDevices().")
 
159
 
 
160
    def isRunning(self):
 
161
        return isDomainRunning(self.name)
 
162
 
 
163
    def getDevice(self, id):
 
164
        raise DomainError("No support for getDevice().")
 
165
 
 
166
 
 
167
class XmTestAPIDomain(XenAPIDomain):
 
168
 
 
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
 
173
    """
 
174
    def __init__(self, name=None, extraConfig=None,
 
175
                 baseConfig=arch.configDefaults):
 
176
        config = XenAPIConfig()
 
177
        config.setOpts(baseConfig)
 
178
        if extraConfig:
 
179
            config.setOpts(extraConfig)
 
180
 
 
181
        if name:
 
182
            config.setOpt("name_label", name)
 
183
        elif not config.getOpt("name_label"):
 
184
            config.setOpt("name_label", getUniqueName())
 
185
 
 
186
        XenAPIDomain.__init__(self, config.getOpt("name_label"),
 
187
                              config=config)