ia64/xen-unstable

view tools/python/xen/xend/server/SrvDomainDir.py @ 7192:ca6930f82682

Merged.
author emellor@ewan
date Tue Oct 04 11:35:21 2005 +0100 (2005-10-04)
parents 9e0b6fbab872
children
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.Args import FormFn
26 from xen.xend.XendError import XendError
27 from xen.xend.XendLogging import log
29 from xen.web.SrvDir import SrvDir
30 from SrvDomain import SrvDomain
32 class SrvDomainDir(SrvDir):
33 """Service that manages the domain directory.
34 """
36 def __init__(self):
37 SrvDir.__init__(self)
38 self.xd = XendDomain.instance()
40 def domain(self, x):
41 dom = self.xd.domain_lookup_by_name(x)
42 if not dom:
43 raise XendError('No such domain ' + str(x))
44 return SrvDomain(dom)
46 def get(self, x):
47 v = SrvDir.get(self, x)
48 if v is not None:
49 return v
50 else:
51 return self.domain(x)
53 def op_create(self, op, req):
54 """Create a domain.
55 Expects the domain config in request parameter 'config' in SXP format.
56 """
57 ok = 0
58 errmsg = ''
59 try:
60 configstring = req.args.get('config')[0]
61 #print 'op_create>', 'config:', configstring
62 pin = sxp.Parser()
63 pin.input(configstring)
64 pin.input_eof()
65 config = pin.get_val()
66 ok = 1
67 except Exception, ex:
68 print 'op_create> Exception in config', ex
69 traceback.print_exc()
70 errmsg = 'Configuration error ' + str(ex)
71 except sxp.ParseError, ex:
72 errmsg = 'Invalid configuration ' + str(ex)
73 if not ok:
74 raise XendError(errmsg)
75 try:
76 dominfo = self.xd.domain_create(config)
77 return self._op_create_cb(dominfo, configstring, req)
78 except Exception, ex:
79 print 'op_create> Exception creating domain:'
80 traceback.print_exc()
81 raise XendError("Error creating domain: " + str(ex))
83 def _op_create_cb(self, dominfo, configstring, req):
84 """Callback to handle domain creation.
85 """
86 dom = dominfo.getName()
87 domurl = "%s/%s" % (req.prePathURL(), dom)
88 req.setResponseCode(http.CREATED, "created")
89 req.setHeader("Location", domurl)
90 if self.use_sxp(req):
91 return dominfo.sxpr()
92 else:
93 out = StringIO()
94 print >> out, ('<p> Created <a href="%s">Domain %s</a></p>'
95 % (domurl, dom))
96 print >> out, '<p><pre>'
97 print >> out, configstring
98 print >> out, '</pre></p>'
99 val = out.getvalue()
100 out.close()
101 return val
103 def op_restore(self, op, req):
104 """Restore a domain from file.
106 """
107 return req.threadRequest(self.do_restore, op, req)
109 def do_restore(self, op, req):
110 fn = FormFn(self.xd.domain_restore,
111 [['file', 'str']])
112 dominfo = fn(req.args)
113 dom = dominfo.getName()
114 domurl = "%s/%s" % (req.prePathURL(), dom)
115 req.setResponseCode(http.CREATED)
116 req.setHeader("Location", domurl)
117 if self.use_sxp(req):
118 return dominfo.sxpr()
119 else:
120 out = StringIO()
121 print >> out, ('<p> Created <a href="%s">Domain %s</a></p>'
122 % (domurl, dom))
123 val = out.getvalue()
124 out.close()
125 return val
127 def render_POST(self, req):
128 return self.perform(req)
130 def render_GET(self, req):
131 if self.use_sxp(req):
132 req.setHeader("Content-Type", sxp.mime_type)
133 self.ls_domain(req, 1)
134 else:
135 req.write("<html><head></head><body>")
136 self.print_path(req)
137 self.ls(req)
138 self.ls_domain(req)
139 self.form(req)
140 req.write("</body></html>")
142 def ls_domain(self, req, use_sxp=0):
143 url = req.prePathURL()
144 if not url.endswith('/'):
145 url += '/'
146 if use_sxp:
147 domains = self.xd.list_names()
148 sxp.show(domains, out=req)
149 else:
150 domains = self.xd.list_sorted()
151 req.write('<ul>')
152 for d in domains:
153 req.write(
154 '<li><a href="%s%s">Domain %s</a>: id = %s, memory = %d, '
155 'ssidref = %d.'
156 % (url, d.getName(), d.getName(), d.getDomid(),
157 d.getMemoryTarget(), d.getSsidref()))
158 req.write('</li>')
159 req.write('</ul>')
161 def form(self, req):
162 """Generate the form(s) for domain dir operations.
163 """
164 req.write('<form method="post" action="%s" enctype="multipart/form-data">'
165 % req.prePathURL())
166 req.write('<button type="submit" name="op" value="create">Create Domain</button>')
167 req.write('Config <input type="file" name="config"><br>')
168 req.write('</form>')
170 req.write('<form method="post" action="%s" enctype="multipart/form-data">'
171 % req.prePathURL())
172 req.write('<button type="submit" name="op" value="restore">Restore Domain</button>')
173 req.write('State <input type="string" name="state"><br>')
174 req.write('</form>')