ia64/xen-unstable

view tools/python/xen/xend/server/SrvDomainDir.py @ 12839:5bf91da9797d

Remove broken call to XendDomainInfo.getSsidref(), fixing the obsolete HTTP
server.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author Ewan Mellor <ewan@xensource.com>
date Thu Dec 07 11:45:04 2006 +0000 (2006-12-07)
parents 9a932b5c7947
children e98c84468b72
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) 2004, 2005 Mike Wray <mike.wray@hp.com>
16 #============================================================================
18 import traceback
19 from StringIO import StringIO
21 from xen.web import http
23 from xen.xend import sxp
24 from xen.xend import XendDomain
25 from xen.xend.XendDomainInfo import XendDomainInfo
26 from xen.xend.Args import FormFn
27 from xen.xend.XendError import XendError
29 from xen.web.SrvDir import SrvDir
30 from SrvDomain import SrvDomain
33 class SrvDomainDir(SrvDir):
34 """Service that manages the domain directory.
35 """
37 def __init__(self):
38 SrvDir.__init__(self)
39 self.xd = XendDomain.instance()
41 def domain(self, x):
42 dom = self.xd.domain_lookup(x)
43 if not dom:
44 raise XendError('No such domain ' + str(x))
45 return SrvDomain(dom)
47 def get(self, x):
48 v = SrvDir.get(self, x)
49 if v is not None:
50 return v
51 else:
52 return self.domain(x)
54 def op_create(self, _, req):
55 """Create a domain.
56 Expects the domain config in request parameter 'config' in SXP format.
57 """
58 ok = 0
59 errmsg = ''
60 try:
61 configstring = req.args.get('config')[0]
62 #print 'op_create>', 'config:', configstring
63 pin = sxp.Parser()
64 pin.input(configstring)
65 pin.input_eof()
66 config = pin.get_val()
67 ok = 1
68 except sxp.ParseError, ex:
69 errmsg = 'Invalid configuration ' + str(ex)
70 except Exception, ex:
71 print 'op_create> Exception in config', ex
72 traceback.print_exc()
73 errmsg = 'Configuration error ' + str(ex)
74 if not ok:
75 raise XendError(errmsg)
76 try:
77 dominfo = self.xd.domain_create(config)
78 return self._op_create_cb(dominfo, configstring, req)
79 except Exception, ex:
80 print 'op_create> Exception creating domain:'
81 traceback.print_exc()
82 raise XendError("Error creating domain: " + str(ex))
84 def _op_create_cb(self, dominfo, configstring, req):
85 """Callback to handle domain creation.
86 """
87 dom = dominfo.getName()
88 domurl = "%s/%s" % (req.prePathURL(), dom)
89 req.setResponseCode(http.CREATED, "created")
90 req.setHeader("Location", domurl)
91 if self.use_sxp(req):
92 return dominfo.sxpr()
93 else:
94 out = StringIO()
95 print >> out, ('<p> Created <a href="%s">Domain %s</a></p>'
96 % (domurl, dom))
97 print >> out, '<p><pre>'
98 print >> out, configstring
99 print >> out, '</pre></p>'
100 val = out.getvalue()
101 out.close()
102 return val
104 def op_restore(self, op, req):
105 """Restore a domain from file.
107 """
108 return req.threadRequest(self.do_restore, op, req)
110 def do_restore(self, _, req):
111 fn = FormFn(self.xd.domain_restore,
112 [['file', 'str']])
113 dominfo = fn(req.args)
114 dom = dominfo.getName()
115 domurl = "%s/%s" % (req.prePathURL(), dom)
116 req.setResponseCode(http.CREATED)
117 req.setHeader("Location", domurl)
118 if self.use_sxp(req):
119 return dominfo.sxpr()
120 else:
121 out = StringIO()
122 print >> out, ('<p> Created <a href="%s">Domain %s</a></p>'
123 % (domurl, dom))
124 val = out.getvalue()
125 out.close()
126 return val
129 def op_list(self, _, req):
130 """List the details for this domain."""
131 self._list(req, True)
134 def render_POST(self, req):
135 return self.perform(req)
137 def render_GET(self, req):
138 self._list(req, 'detail' in req.args and req.args['detail'] == ['1'])
141 def _list(self, req, detail):
142 if self.use_sxp(req):
143 req.setHeader("Content-Type", sxp.mime_type)
144 self.ls_domain(req, detail, True)
145 else:
146 req.write("<html><head></head><body>")
147 self.print_path(req)
148 self.ls(req)
149 self.ls_domain(req, detail, False)
150 self.form(req)
151 req.write("</body></html>")
154 def ls_domain(self, req, detail, use_sxp):
155 url = req.prePathURL()
156 if not url.endswith('/'):
157 url += '/'
158 if use_sxp:
159 if detail:
160 sxp.show(map(XendDomainInfo.sxpr, self.xd.list()), out=req)
161 else:
162 sxp.show(self.xd.list_names(), out=req)
163 else:
164 domains = self.xd.list_sorted()
165 req.write('<ul>')
166 for d in domains:
167 req.write(
168 '<li><a href="%s%s">Domain %s</a>: id = %s, memory = %d'
169 % (url, d.getName(), d.getName(), d.getDomid(),
170 d.getMemoryTarget()))
171 req.write('</li>')
172 req.write('</ul>')
175 def form(self, req):
176 """Generate the form(s) for domain dir operations.
177 """
178 req.write('<form method="post" action="%s" enctype="multipart/form-data">'
179 % req.prePathURL())
180 req.write('<button type="submit" name="op" value="create">Create Domain</button>')
181 req.write('Config <input type="file" name="config"><br>')
182 req.write('</form>')
184 req.write('<form method="post" action="%s" enctype="multipart/form-data">'
185 % req.prePathURL())
186 req.write('<button type="submit" name="op" value="restore">Restore Domain</button>')
187 req.write('State <input type="string" name="state"><br>')
188 req.write('</form>')