ia64/xen-unstable

view tools/python/xen/xend/server/SrvDomainDir.py @ 6552:a9873d384da4

Merge.
author adsharma@los-vmm.sc.intel.com
date Thu Aug 25 12:24:48 2005 -0700 (2005-08-25)
parents 112d44270733 fa0754a9f64f
children dfaf788ab18c
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 val = None
42 dom = self.xd.domain_lookup_by_name(x)
43 if not dom:
44 raise XendError('No such domain ' + str(x))
45 val = SrvDomain(dom)
46 return val
48 def get(self, x):
49 v = SrvDir.get(self, x)
50 if v is not None:
51 return v
52 v = self.domain(x)
53 return v
55 def op_create(self, op, req):
56 """Create a domain.
57 Expects the domain config in request parameter 'config' in SXP format.
58 """
59 ok = 0
60 errmsg = ''
61 try:
62 configstring = req.args.get('config')[0]
63 #print 'op_create>', 'config:', configstring
64 pin = sxp.Parser()
65 pin.input(configstring)
66 pin.input_eof()
67 config = pin.get_val()
68 ok = 1
69 except Exception, ex:
70 print 'op_create> Exception in config', ex
71 traceback.print_exc()
72 errmsg = 'Configuration error ' + str(ex)
73 except sxp.ParseError, ex:
74 errmsg = 'Invalid configuration ' + str(ex)
75 if not ok:
76 raise XendError(errmsg)
77 try:
78 dominfo = self.xd.domain_create(config)
79 return self._op_create_cb(dominfo, configstring, req)
80 except Exception, ex:
81 print 'op_create> Exception creating domain:'
82 traceback.print_exc()
83 raise XendError("Error creating domain: " + str(ex))
85 def _op_create_cb(self, dominfo, configstring, req):
86 """Callback to handle domain creation.
87 """
88 dom = dominfo.name
89 domurl = "%s/%s" % (req.prePathURL(), dom)
90 req.setResponseCode(http.CREATED, "created")
91 req.setHeader("Location", domurl)
92 if self.use_sxp(req):
93 return dominfo.sxpr()
94 else:
95 out = StringIO()
96 print >> out, ('<p> Created <a href="%s">Domain %s</a></p>'
97 % (domurl, dom))
98 print >> out, '<p><pre>'
99 print >> out, configstring
100 print >> out, '</pre></p>'
101 val = out.getvalue()
102 out.close()
103 return val
105 def op_restore(self, op, req):
106 """Restore a domain from file.
108 """
109 return req.threadRequest(self.do_restore, op, req)
111 def do_restore(self, op, req):
112 fn = FormFn(self.xd.domain_restore,
113 [['file', 'str']])
114 dominfo = fn(req.args)
115 dom = dominfo.name
116 domurl = "%s/%s" % (req.prePathURL(), dom)
117 req.setResponseCode(http.CREATED)
118 req.setHeader("Location", domurl)
119 if self.use_sxp(req):
120 return dominfo.sxpr()
121 else:
122 out = StringIO()
123 print >> out, ('<p> Created <a href="%s">Domain %s</a></p>'
124 % (domurl, dom))
125 val = out.getvalue()
126 out.close()
127 return val
129 def render_POST(self, req):
130 return self.perform(req)
132 def render_GET(self, req):
133 if self.use_sxp(req):
134 req.setHeader("Content-Type", sxp.mime_type)
135 self.ls_domain(req, 1)
136 else:
137 req.write("<html><head></head><body>")
138 self.print_path(req)
139 self.ls(req)
140 self.ls_domain(req)
141 self.form(req)
142 req.write("</body></html>")
144 def ls_domain(self, req, use_sxp=0):
145 url = req.prePathURL()
146 if not url.endswith('/'):
147 url += '/'
148 if use_sxp:
149 domains = self.xd.domain_ls()
150 sxp.show(domains, out=req)
151 else:
152 domains = self.xd.list()
153 domains.sort(lambda x, y: cmp(x.name, y.name))
154 req.write('<ul>')
155 for d in domains:
156 req.write('<li><a href="%s%s"> Domain %s</a>'
157 % (url, d.name, d.name))
158 req.write('id=%s' % d.id)
159 req.write('memory=%d'% d.memory)
160 req.write('ssidref=%d'% d.ssidref)
161 req.write('</li>')
162 req.write('</ul>')
164 def form(self, req):
165 """Generate the form(s) for domain dir operations.
166 """
167 req.write('<form method="post" action="%s" enctype="multipart/form-data">'
168 % req.prePathURL())
169 req.write('<button type="submit" name="op" value="create">Create Domain</button>')
170 req.write('Config <input type="file" name="config"><br>')
171 req.write('</form>')
173 req.write('<form method="post" action="%s" enctype="multipart/form-data">'
174 % req.prePathURL())
175 req.write('<button type="submit" name="op" value="restore">Restore Domain</button>')
176 req.write('State <input type="string" name="state"><br>')
177 req.write('</form>')