debuggers.hg

view tools/python/xen/xend/server/SrvDomainDir.py @ 4672:d781b9d08e80

bitkeeper revision 1.1327.2.4 (426918a34Af7gihN8mTkq-P3KrAZXg)

Remove twisted from save/migrate handling.
This needs to use threads, so add thread support for
http server requests.

Signed-off-by: Mike Wray <mike.wray@hp.com>
author mjw@wray-m-3.hpl.hp.com
date Fri Apr 22 15:30:43 2005 +0000 (2005-04-22)
parents a838a908e38e
children 369e382b2f81
line source
1 # Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
3 import traceback
4 from StringIO import StringIO
6 from xen.web import http
8 from xen.xend import sxp
9 from xen.xend import XendDomain
10 from xen.xend.Args import FormFn
11 from xen.xend.XendError import XendError
12 from xen.xend.XendLogging import log
14 from SrvDir import SrvDir
15 from SrvDomain import SrvDomain
17 class SrvDomainDir(SrvDir):
18 """Service that manages the domain directory.
19 """
21 def __init__(self):
22 SrvDir.__init__(self)
23 self.xd = XendDomain.instance()
25 def domain(self, x):
26 val = None
27 dom = self.xd.domain_lookup(x)
28 if not dom:
29 raise XendError('No such domain ' + str(x))
30 val = SrvDomain(dom)
31 return val
33 def get(self, x):
34 v = SrvDir.get(self, x)
35 if v is not None:
36 return v
37 v = self.domain(x)
38 return v
40 def op_create(self, op, req):
41 """Create a domain.
42 Expects the domain config in request parameter 'config' in SXP format.
43 """
44 ok = 0
45 errmsg = ''
46 try:
47 configstring = req.args.get('config')[0]
48 #print 'op_create>', 'config:', configstring
49 pin = sxp.Parser()
50 pin.input(configstring)
51 pin.input_eof()
52 config = pin.get_val()
53 ok = 1
54 except Exception, ex:
55 print 'op_create> Exception in config', ex
56 traceback.print_exc()
57 errmsg = 'Configuration error ' + str(ex)
58 except sxp.ParseError, ex:
59 errmsg = 'Invalid configuration ' + str(ex)
60 if not ok:
61 raise XendError(errmsg)
62 try:
63 dominfo = self.xd.domain_create(config)
64 return self._op_create_cb(dominfo, configstring, req)
65 except Exception, ex:
66 print 'op_create> Exception creating domain:'
67 traceback.print_exc()
68 raise XendError("Error creating domain: " + str(ex))
70 def _op_create_cb(self, dominfo, configstring, req):
71 """Callback to handle domain creation.
72 """
73 dom = dominfo.name
74 domurl = "%s/%s" % (req.prePathURL(), dom)
75 req.setResponseCode(http.CREATED, "created")
76 req.setHeader("Location", domurl)
77 if self.use_sxp(req):
78 return dominfo.sxpr()
79 else:
80 out = StringIO()
81 print >> out, ('<p> Created <a href="%s">Domain %s</a></p>'
82 % (domurl, dom))
83 print >> out, '<p><pre>'
84 print >> out, configstring
85 print >> out, '</pre></p>'
86 val = out.getvalue()
87 out.close()
88 return val
90 def op_restore(self, op, req):
91 """Restore a domain from file.
93 """
94 return req.threadRequest(self.do_restore, op, req)
96 def do_restore(self, op, req):
97 fn = FormFn(self.xd.domain_restore,
98 [['file', 'str']])
99 dominfo = fn(req.args)
100 dom = dominfo.name
101 domurl = "%s/%s" % (req.prePathURL(), dom)
102 req.setResponseCode(http.CREATED)
103 req.setHeader("Location", domurl)
104 if self.use_sxp(req):
105 return dominfo.sxpr()
106 else:
107 out = StringIO()
108 print >> out, ('<p> Created <a href="%s">Domain %s</a></p>'
109 % (domurl, dom))
110 val = out.getvalue()
111 out.close()
112 return val
114 def render_POST(self, req):
115 return self.perform(req)
117 def render_GET(self, req):
118 if self.use_sxp(req):
119 req.setHeader("Content-Type", sxp.mime_type)
120 self.ls_domain(req, 1)
121 else:
122 req.write("<html><head></head><body>")
123 self.print_path(req)
124 self.ls(req)
125 self.ls_domain(req)
126 self.form(req)
127 req.write("</body></html>")
129 def ls_domain(self, req, use_sxp=0):
130 url = req.prePathURL()
131 if not url.endswith('/'):
132 url += '/'
133 if use_sxp:
134 domains = self.xd.domain_ls()
135 sxp.show(domains, out=req)
136 else:
137 domains = self.xd.domains()
138 domains.sort(lambda x, y: cmp(x.name, y.name))
139 req.write('<ul>')
140 for d in domains:
141 req.write('<li><a href="%s%s"> Domain %s</a>'
142 % (url, d.name, d.name))
143 req.write('id=%s' % d.id)
144 req.write('memory=%d'% d.memory)
145 req.write('</li>')
146 req.write('</ul>')
148 def form(self, req):
149 """Generate the form(s) for domain dir operations.
150 """
151 req.write('<form method="post" action="%s" enctype="multipart/form-data">'
152 % req.prePathURL())
153 req.write('<button type="submit" name="op" value="create">Create Domain</button>')
154 req.write('Config <input type="file" name="config"><br>')
155 req.write('</form>')
157 req.write('<form method="post" action="%s" enctype="multipart/form-data">'
158 % req.prePathURL())
159 req.write('<button type="submit" name="op" value="restore">Restore Domain</button>')
160 req.write('State <input type="string" name="state"><br>')
161 req.write('</form>')