debuggers.hg

view tools/python/xen/xend/server/SrvDomainDir.py @ 4655:a838a908e38e

bitkeeper revision 1.1327.2.2 (4267a9b3MhPpljnjQ5IbfLdzcW2K3w)

Remove twisted from the HTTP server and replace with a
threaded server. Add classes to provide tcp and unix servers
using threads instead of twisted. Remove use of twisted from
the consoles, event server and HTTP resources

Signed-off-by: Mike Wray <mike.wray@hp.com>
author mjw@wray-m-3.hpl.hp.com
date Thu Apr 21 13:25:07 2005 +0000 (2005-04-21)
parents c69fbe48a357
children d781b9d08e80
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 deferred 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 @return: deferred
94 """
95 fn = FormFn(self.xd.domain_restore,
96 [['file', 'str']])
97 dominfo = fn(req.args)
98 return self._op_restore_cb(dominfo, req)
100 def _op_restore_cb(self, dominfo, req):
101 dom = dominfo.name
102 domurl = "%s/%s" % (req.prePathURL(), dom)
103 req.setResponseCode(http.CREATED)
104 req.setHeader("Location", domurl)
105 if self.use_sxp(req):
106 return dominfo.sxpr()
107 else:
108 out = StringIO()
109 print >> out, ('<p> Created <a href="%s">Domain %s</a></p>'
110 % (domurl, dom))
111 val = out.getvalue()
112 out.close()
113 return val
115 def render_POST(self, req):
116 return self.perform(req)
118 def render_GET(self, req):
119 try:
120 if self.use_sxp(req):
121 req.setHeader("Content-Type", sxp.mime_type)
122 self.ls_domain(req, 1)
123 else:
124 req.write("<html><head></head><body>")
125 self.print_path(req)
126 self.ls(req)
127 self.ls_domain(req)
128 self.form(req)
129 req.write("</body></html>")
130 return ''
131 except Exception, ex:
132 self._perform_err(ex, req)
134 def ls_domain(self, req, use_sxp=0):
135 url = req.prePathURL()
136 if not url.endswith('/'):
137 url += '/'
138 if use_sxp:
139 domains = self.xd.domain_ls()
140 sxp.show(domains, out=req)
141 else:
142 domains = self.xd.domains()
143 domains.sort(lambda x, y: cmp(x.name, y.name))
144 req.write('<ul>')
145 for d in domains:
146 req.write('<li><a href="%s%s"> Domain %s</a>'
147 % (url, d.name, d.name))
148 req.write('id=%s' % d.id)
149 req.write('memory=%d'% d.memory)
150 req.write('</li>')
151 req.write('</ul>')
153 def form(self, req):
154 """Generate the form(s) for domain dir operations.
155 """
156 req.write('<form method="post" action="%s" enctype="multipart/form-data">'
157 % req.prePathURL())
158 req.write('<button type="submit" name="op" value="create">Create Domain</button>')
159 req.write('Config <input type="file" name="config"><br>')
160 req.write('</form>')
162 req.write('<form method="post" action="%s" enctype="multipart/form-data">'
163 % req.prePathURL())
164 req.write('<button type="submit" name="op" value="restore">Restore Domain</button>')
165 req.write('State <input type="string" name="state"><br>')
166 req.write('</form>')