debuggers.hg

view tools/python/xen/xend/server/SrvDomainDir.py @ 2645:8bc05c989fb3

bitkeeper revision 1.1159.102.1 (4163f3e1F0LimXbocHs-QcPviz39JA)

Improve error messages by unwrapping Failures.
author mjw@wray-m-3.hpl.hp.com
date Wed Oct 06 13:32:17 2004 +0000 (2004-10-06)
parents 3af1c3b54760
children b44a94edc865
line source
1 # Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
3 import traceback
4 from StringIO import StringIO
6 from twisted.protocols import http
7 from twisted.web import error
8 from twisted.python.failure import Failure
10 from xen.xend import sxp
11 from xen.xend import XendDomain
12 from xen.xend.Args import FormFn
13 from xen.xend.XendError import XendError
15 from SrvDir import SrvDir
16 from SrvDomain import SrvDomain
18 class SrvDomainDir(SrvDir):
19 """Service that manages the domain directory.
20 """
22 def __init__(self):
23 SrvDir.__init__(self)
24 self.xd = XendDomain.instance()
26 def domain(self, x):
27 val = None
28 dom = self.xd.domain_lookup(x)
29 if not dom:
30 raise XendError('No such domain ' + str(x))
31 val = SrvDomain(dom)
32 return val
34 def get(self, x):
35 v = SrvDir.get(self, x)
36 if v is not None:
37 return v
38 v = self.domain(x)
39 return v
41 def op_create(self, op, req):
42 """Create a domain.
43 Expects the domain config in request parameter 'config' in SXP format.
44 """
45 ok = 0
46 errmsg = ''
47 try:
48 configstring = req.args.get('config')[0]
49 #print 'op_create>', 'config:', configstring
50 pin = sxp.Parser()
51 pin.input(configstring)
52 pin.input_eof()
53 config = pin.get_val()
54 ok = 1
55 except Exception, ex:
56 print 'op_create> Exception in config', ex
57 traceback.print_exc()
58 errmsg = 'Configuration error ' + str(ex)
59 except sxp.ParseError, ex:
60 errmsg = 'Invalid configuration ' + str(ex)
61 if not ok:
62 req.setResponseCode(http.BAD_REQUEST, errmsg)
63 return errmsg
64 try:
65 deferred = self.xd.domain_create(config)
66 deferred.addCallback(self._op_create_cb, configstring, req)
67 deferred.addErrback(self._op_create_err, req)
68 return deferred
69 except Exception, ex:
70 print 'op_create> Exception creating domain:'
71 traceback.print_exc()
72 req.setResponseCode(http.BAD_REQUEST, "Error creating domain: " + str(ex))
73 return str(ex)
75 def _op_create_cb(self, dominfo, configstring, req):
76 """Callback to handle deferred domain creation.
77 """
78 dom = dominfo.name
79 domurl = "%s/%s" % (req.prePathURL(), dom)
80 req.setResponseCode(http.CREATED, "created")
81 req.setHeader("Location", domurl)
82 if self.use_sxp(req):
83 return dominfo.sxpr()
84 else:
85 out = StringIO()
86 print >> out, ('<p> Created <a href="%s">Domain %s</a></p>'
87 % (domurl, dom))
88 print >> out, '<p><pre>'
89 print >> out, configstring
90 print >> out, '</pre></p>'
91 val = out.getvalue()
92 out.close()
93 return val
95 def _op_create_err(self, err, req):
96 """Callback to handle errors in deferred domain creation.
97 """
98 if isinstance(err, Failure):
99 err = err.getErrorMessage()
100 print 'op_create> Deferred Exception creating domain:', err
101 req.setResponseCode(http.BAD_REQUEST, "Error creating domain: " + str(err))
102 return str(err)
104 def op_restore(self, op, req):
105 """Restore a domain from file.
107 @return: deferred
108 """
109 #todo: return is deferred. May need ok and err callbacks.
110 fn = FormFn(self.xd.domain_restore,
111 [['file', 'str']])
112 deferred = fn(req.args)
113 deferred.addCallback(self._op_restore_cb, req)
114 #deferred.addErrback(self._op_restore_err, req)
115 return deferred
117 def _op_restore_cb(self, dominfo, req):
118 dom = dominfo.name
119 domurl = "%s/%s" % (req.prePathURL(), dom)
120 req.setResponseCode(http.CREATED)
121 req.setHeader("Location", domurl)
122 if self.use_sxp(req):
123 return dominfo.sxpr()
124 else:
125 out = StringIO()
126 print >> out, ('<p> Created <a href="%s">Domain %s</a></p>'
127 % (domurl, dom))
128 val = out.getvalue()
129 out.close()
130 return val
132 def _op_restore_err(self, err, req):
133 if isinstance(err, Failure):
134 err = err.getErrorMessage()
135 print 'op_create> Deferred Exception restoring domain:', err
136 req.setResponseCode(http.BAD_REQUEST, "Error restoring domain: "+ str(err))
137 return str(err)
139 def render_POST(self, req):
140 return self.perform(req)
142 def render_GET(self, req):
143 try:
144 if self.use_sxp(req):
145 req.setHeader("Content-Type", sxp.mime_type)
146 self.ls_domain(req, 1)
147 else:
148 req.write("<html><head></head><body>")
149 self.print_path(req)
150 self.ls(req)
151 self.ls_domain(req)
152 self.form(req)
153 req.write("</body></html>")
154 return ''
155 except Exception, ex:
156 self._perform_err(ex, req)
158 def ls_domain(self, req, use_sxp=0):
159 url = req.prePathURL()
160 if not url.endswith('/'):
161 url += '/'
162 if use_sxp:
163 domains = self.xd.domain_ls()
164 sxp.show(domains, out=req)
165 else:
166 domains = self.xd.domains()
167 domains.sort(lambda x, y: cmp(x.name, y.name))
168 req.write('<ul>')
169 for d in domains:
170 req.write('<li><a href="%s%s"> Domain %s</a>'
171 % (url, d.name, d.name))
172 req.write('id=%s' % d.id)
173 req.write('memory=%d'% d.memory)
174 req.write('</li>')
175 req.write('</ul>')
177 def form(self, req):
178 """Generate the form(s) for domain dir operations.
179 """
180 req.write('<form method="post" action="%s" enctype="multipart/form-data">'
181 % req.prePathURL())
182 req.write('<button type="submit" name="op" value="create">Create Domain</button>')
183 req.write('Config <input type="file" name="config"><br>')
184 req.write('</form>')
186 req.write('<form method="post" action="%s" enctype="multipart/form-data">'
187 % req.prePathURL())
188 req.write('<button type="submit" name="op" value="restore">Restore Domain</button>')
189 req.write('State <input type="string" name="state"><br>')
190 req.write('</form>')