ia64/xen-unstable

view tools/python/xen/xend/server/SrvDomainDir.py @ 2571:3af1c3b54760

bitkeeper revision 1.1159.96.2 (415bed53ySdlyVimTpmTb6lR8dzOdw)

Merge xenbk@gandalf:/var/bk/xeno-unstable.bk
into wray-m-3.hpl.hp.com:/home/mjw/repos-bk/xeno-unstable.bk
author mjw@wray-m-3.hpl.hp.com
date Thu Sep 30 11:26:11 2004 +0000 (2004-09-30)
parents dae98734f12e a05a0524e3c5
children 98bdf2c88015 8bc05c989fb3
line source
1 # Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
3 import traceback
4 from StringIO import StringIO
6 from twisted.protocols import http
7 from twisted.web import error
9 from xen.xend import sxp
10 from xen.xend import XendDomain
11 from xen.xend.Args import FormFn
12 from xen.xend.XendError import XendError
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 req.setResponseCode(http.BAD_REQUEST, errmsg)
62 return errmsg
63 try:
64 deferred = self.xd.domain_create(config)
65 deferred.addCallback(self._op_create_cb, configstring, req)
66 deferred.addErrback(self._op_create_err, req)
67 return deferred
68 except Exception, ex:
69 print 'op_create> Exception creating domain:'
70 traceback.print_exc()
71 req.setResponseCode(http.BAD_REQUEST, "Error creating domain: " + str(ex))
72 return str(ex)
74 def _op_create_cb(self, dominfo, configstring, req):
75 """Callback to handle deferred domain creation.
76 """
77 dom = dominfo.name
78 domurl = "%s/%s" % (req.prePathURL(), dom)
79 req.setResponseCode(http.CREATED, "created")
80 req.setHeader("Location", domurl)
81 if self.use_sxp(req):
82 return dominfo.sxpr()
83 else:
84 out = StringIO()
85 print >> out, ('<p> Created <a href="%s">Domain %s</a></p>'
86 % (domurl, dom))
87 print >> out, '<p><pre>'
88 print >> out, configstring
89 print >> out, '</pre></p>'
90 val = out.getvalue()
91 out.close()
92 return val
94 def _op_create_err(self, err, req):
95 """Callback to handle errors in deferred domain creation.
96 """
97 print 'op_create> Deferred Exception creating domain:', err
98 req.setResponseCode(http.BAD_REQUEST, "Error creating domain: " + str(err))
99 return str(err)
101 def op_restore(self, op, req):
102 """Restore a domain from file.
104 @return: deferred
105 """
106 #todo: return is deferred. May need ok and err callbacks.
107 fn = FormFn(self.xd.domain_restore,
108 [['file', 'str']])
109 deferred = fn(req.args)
110 deferred.addCallback(self._op_restore_cb, req)
111 #deferred.addErrback(self._op_restore_err, req)
112 return deferred
114 def _op_restore_cb(self, dominfo, req):
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 _op_restore_err(self, err, req):
130 print 'op_create> Deferred Exception restoring domain:', err
131 req.setResponseCode(http.BAD_REQUEST, "Error restoring domain: "+ str(err))
132 return str(err)
134 def render_POST(self, req):
135 return self.perform(req)
137 def render_GET(self, req):
138 try:
139 if self.use_sxp(req):
140 req.setHeader("Content-Type", sxp.mime_type)
141 self.ls_domain(req, 1)
142 else:
143 req.write("<html><head></head><body>")
144 self.print_path(req)
145 self.ls(req)
146 self.ls_domain(req)
147 self.form(req)
148 req.write("</body></html>")
149 return ''
150 except Exception, ex:
151 self._perform_err(ex, req)
153 def ls_domain(self, req, use_sxp=0):
154 url = req.prePathURL()
155 if not url.endswith('/'):
156 url += '/'
157 if use_sxp:
158 domains = self.xd.domain_ls()
159 sxp.show(domains, out=req)
160 else:
161 domains = self.xd.domains()
162 domains.sort(lambda x, y: cmp(x.name, y.name))
163 req.write('<ul>')
164 for d in domains:
165 req.write('<li><a href="%s%s"> Domain %s</a>'
166 % (url, d.name, d.name))
167 req.write('id=%s' % d.id)
168 req.write('memory=%d'% d.memory)
169 req.write('</li>')
170 req.write('</ul>')
172 def form(self, req):
173 """Generate the form(s) for domain dir operations.
174 """
175 req.write('<form method="post" action="%s" enctype="multipart/form-data">'
176 % req.prePathURL())
177 req.write('<button type="submit" name="op" value="create">Create Domain</button>')
178 req.write('Config <input type="file" name="config"><br>')
179 req.write('</form>')
181 req.write('<form method="post" action="%s" enctype="multipart/form-data">'
182 % req.prePathURL())
183 req.write('<button type="submit" name="op" value="restore">Restore Domain</button>')
184 req.write('State <input type="string" name="state"><br>')
185 req.write('</form>')