ia64/xen-unstable

view tools/python/xen/xend/server/XMLRPCServer.py @ 9450:33df7d4eff23

1) Introduce new exception type XendInvalidDomain that maps to the high level
XEND_INVALID_DOMAIN faultType.
2) Fix exception logic in XMLRPCServer
3) Fix TCP server
4) Remove catching of ProtocolError in main.py. ProtocolErrors only occur
when there is an exception in the exception handling code which shouldn't
ever happen. I've reproduced the error cases described by Ewan with
xend_domain_setTargetMemory and once I fixed the exception logic, I get a
normal faultType of 1 as would be expected.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
author anthony@rhesis.austin.ibm.com
date Fri Mar 24 14:31:12 2006 +0100 (2006-03-24)
parents da24df1ea484
children 58938b561b66
line source
1 #============================================================================
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of version 2.1 of the GNU Lesser General Public
4 # License as published by the Free Software Foundation.
5 #
6 # This library is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser General Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #============================================================================
15 # Copyright (C) 2006 Anthony Liguori <aliguori@us.ibm.com>
16 # Copyright (C) 2006 XenSource Ltd.
17 #============================================================================
19 import xmlrpclib
21 from xen.xend import XendDomain, XendDomainInfo, XendNode, \
22 XendLogging, XendDmesg
23 from xen.util.xmlrpclib2 import UnixXMLRPCServer, TCPXMLRPCServer
25 from xen.xend.XendClient import XML_RPC_SOCKET, ERROR_INVALID_DOMAIN
26 from xen.xend.XendError import *
28 def lookup(domid):
29 info = XendDomain.instance().domain_lookup_by_name_or_id(domid)
30 if not info:
31 raise XendInvalidDomain(str(domid))
32 return info
34 def dispatch(domid, fn, args):
35 info = lookup(domid)
36 return getattr(info, fn)(*args)
38 def domain(domid):
39 info = lookup(domid)
40 return info.sxpr()
42 def domains(detail=1):
43 if detail < 1:
44 return XendDomain.instance().list_names()
45 else:
46 domains = XendDomain.instance().list_sorted()
47 return map(lambda dom: dom.sxpr(), domains)
49 def domain_create(config):
50 info = XendDomain.instance().domain_create(config)
51 return info.sxpr()
53 def domain_restore(src):
54 info = XendDomain.instance().domain_restore(src)
55 return info.sxpr()
57 def get_log():
58 f = open(XendLogging.getLogFilename(), 'r')
59 try:
60 return f.read()
61 finally:
62 f.close()
64 methods = ['device_create', 'destroyDevice', 'getDeviceSxprs',
65 'setMemoryTarget', 'setName', 'setVCpuCount', 'shutdown',
66 'send_sysrq', 'getVCPUInfo', 'waitForDevices']
68 exclude = ['domain_create', 'domain_restore']
70 class XMLRPCServer:
71 def __init__(self, use_tcp=False):
72 self.ready = False
73 self.use_tcp = use_tcp
75 def run(self):
76 if self.use_tcp:
77 # bind to something fixed for now as we may eliminate
78 # tcp support completely.
79 self.server = TCPXMLRPCServer(("localhost", 8005), logRequests=False)
80 else:
81 self.server = UnixXMLRPCServer(XML_RPC_SOCKET, False)
83 # Functions in XendDomainInfo
84 for name in methods:
85 fn = eval("lambda domid, *args: dispatch(domid, '%s', args)"%name)
86 self.server.register_function(fn, "xend.domain.%s" % name)
88 # Functions in XendDomain
89 inst = XendDomain.instance()
90 for name in dir(inst):
91 fn = getattr(inst, name)
92 if name.startswith("domain_") and callable(fn):
93 if name not in exclude:
94 self.server.register_function(fn, "xend.domain.%s" % name[7:])
96 # Functions in XendNode and XendDmesg
97 for type, lst, n in [(XendNode, ['info', 'cpu_bvt_slice_set'], 'node'),
98 (XendDmesg, ['info', 'clear'], 'node.dmesg')]:
99 inst = type.instance()
100 for name in lst:
101 self.server.register_function(getattr(inst, name),
102 "xend.%s.%s" % (n, name))
104 # A few special cases
105 self.server.register_function(domain, 'xend.domain')
106 self.server.register_function(domains, 'xend.domains')
107 self.server.register_function(get_log, 'xend.node.log')
108 self.server.register_function(domain_create, 'xend.domain.create')
109 self.server.register_function(domain_restore, 'xend.domain.restore')
111 self.server.register_introspection_functions()
112 self.ready = True
113 self.server.serve_forever()