ia64/xen-unstable

view tools/python/xen/xend/server/SrvDomain.py @ 1770:1331621fe269

bitkeeper revision 1.1076 (40f3f7b4Cj4P09-PODiTO2MMFlKicw)

Add device create and device destroy for an existing domain.
author mjw@wray-m-3.hpl.hp.com
date Tue Jul 13 14:54:44 2004 +0000 (2004-07-13)
parents b7c469ef6b99
children e4a326567073
line source
1 # Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
3 from twisted.protocols import http
5 from xen.xend import sxp
6 from xen.xend import XendDomain
7 from xen.xend import XendConsole
8 from xen.xend import PrettyPrint
9 from xen.xend.Args import FormFn
11 from SrvDir import SrvDir
13 class SrvDomain(SrvDir):
14 """Service managing a single domain.
15 """
17 def __init__(self, dom):
18 SrvDir.__init__(self)
19 self.dom = dom
20 self.xd = XendDomain.instance()
21 self.xconsole = XendConsole.instance()
23 def op_configure(self, op, req):
24 fn = FormFn(self.xd.domain_configure,
25 [['dom', 'int'],
26 ['config', 'sxpr']])
27 deferred = fn(req.args, {'dom': self.dom.id})
28 deferred.addErrback(self._op_configure_err, req)
29 return deferred
31 def _op_configure_err(self, err, req):
32 req.setResponseCode(http.BAD_REQUEST, "Error: "+ str(err))
33 return str(err)
35 def op_unpause(self, op, req):
36 val = self.xd.domain_unpause(self.dom.id)
37 return val
39 def op_pause(self, op, req):
40 val = self.xd.domain_pause(self.dom.id)
41 return val
43 def op_shutdown(self, op, req):
44 fn = FormFn(self.xd.domain_shutdown,
45 [['dom', 'int'],
46 ['reason', 'str']])
47 val = fn(req.args, {'dom': self.dom.id})
48 req.setResponseCode(http.ACCEPTED)
49 req.setHeader("Location", "%s/.." % req.prePathURL())
50 return val
52 def op_destroy(self, op, req):
53 val = self.xd.domain_destroy(self.dom.id)
54 req.setHeader("Location", "%s/.." % req.prePathURL())
55 return val
57 def op_save(self, op, req):
58 fn = FormFn(self.xd.domain_save,
59 [['dom', 'int'],
60 ['file', 'str']])
61 deferred = fn(req.args, {'dom': self.dom.id})
62 deferred.addCallback(self._op_save_cb, req)
63 deferred.addErrback(self._op_save_err, req)
64 return deferred
66 def _op_save_cb(self, val, req):
67 return 0
69 def _op_save_err(self, err, req):
70 req.setResponseCode(http.BAD_REQUEST, "Error: "+ str(err))
71 return str(err)
73 def op_migrate(self, op, req):
74 fn = FormFn(self.xd.domain_migrate,
75 [['dom', 'int'],
76 ['destination', 'str']])
77 deferred = fn(req.args, {'dom': self.dom.id})
78 print 'op_migrate>', deferred
79 deferred.addCallback(self._op_migrate_cb, req)
80 deferred.addErrback(self._op_migrate_err, req)
81 return deferred
83 def _op_migrate_cb(self, info, req):
84 print '_op_migrate_cb>', info, req
85 #req.setResponseCode(http.ACCEPTED)
86 host = info.dst_host
87 port = info.dst_port
88 dom = info.dst_dom
89 url = "http://%s:%d/xend/domain/%d" % (host, port, dom)
90 req.setHeader("Location", url)
91 print '_op_migrate_cb> url=', url
92 return url
94 def _op_migrate_err(self, err, req):
95 print '_op_migrate_err>', err, req
96 req.setResponseCode(http.BAD_REQUEST, "Error: "+ str(err))
97 return str(err)
99 def op_device_create(self, op, req):
100 fn = FormFn(self.xd.domain_device_create,
101 [['dom', 'int'],
102 ['config', 'sxpr']])
103 try:
104 d = fn(req.args, {'dom': self.dom.id})
105 d.addErrback(self._op_device_create_err, req)
106 return d
107 except ValueError, ex:
108 return ['err', str(ex)]
110 def _op_device_create_err(self, err, req):
111 return ['err', str(err)]
113 def op_device_destroy(self, op, req):
114 fn = FormFn(self.xd.domain_device_destroy,
115 [['dom', 'int'],
116 ['type', 'str'],
117 ['index', 'int']])
118 val = fn(req.args, {'dom': self.dom.id})
119 return val
121 def op_pincpu(self, op, req):
122 fn = FormFn(self.xd.domain_pincpu,
123 [['dom', 'int'],
124 ['cpu', 'int']])
125 val = fn(req.args, {'dom': self.dom.id})
126 return val
128 def op_cpu_bvt_set(self, op, req):
129 fn = FormFn(self.xd.domain_cpu_bvt_set,
130 [['dom', 'int'],
131 ['mcuadv', 'int'],
132 ['warp', 'int'],
133 ['warpl', 'int'],
134 ['warpu', 'int']])
135 val = fn(req.args, {'dom': self.dom.id})
136 return val
138 def op_cpu_fbvt_set(self, op, req):
139 fn = FormFn(self.xd.domain_cpu_fbvt_set,
140 [['dom', 'int'],
141 ['mcuadv', 'int'],
142 ['warp', 'int'],
143 ['warpl', 'int'],
144 ['warpu', 'int']])
145 val = fn(req.args, {'dom': self.dom.id})
146 return val
148 def op_cpu_atropos_set(self, op, req):
149 fn = FormFn(self.xd.domain_cpu_atropos_set,
150 [['dom', 'int'],
151 ['period', 'int'],
152 ['slice', 'int'],
153 ['latency', 'int'],
154 ['xtratime', 'int']])
155 val = fn(req.args, {'dom': self.dom.id})
156 return val
158 def op_vifs(self, op, req):
159 return self.xd.domain_vif_ls(self.dom.id)
161 def op_vif(self, op, req):
162 fn = FormFn(self.xd.domain_vif_get,
163 [['dom', 'int'],
164 ['vif', 'int']])
165 val = fn(req.args, {'dom': self.dom.id})
166 return val
168 def op_vbds(self, op, req):
169 return self.xd.domain_vbd_ls(self.dom.id)
171 def op_vbd(self, op, req):
172 fn = FormFn(self.xd.domain_vbd_get,
173 [['dom', 'int'],
174 ['vbd', 'int']])
175 val = fn(req.args, {'dom': self.dom.id})
176 return val
178 def op_vbd_add(self, op, req):
179 fn = FormFn(self.xd.domain_vbd_add,
180 [['dom', 'int'],
181 ['uname', 'str'],
182 ['dev', 'str'],
183 ['mode', 'str']])
184 val = fn(req.args, {'dom': self.dom.id})
185 return val
187 def op_vbd_remove(self, op, req):
188 fn = FormFn(self.xd.domain_vbd_remove,
189 [['dom', 'int'],
190 ['dev', 'str']])
191 val = fn(req.args, {'dom': self.dom.id})
192 return val
194 def render_POST(self, req):
195 return self.perform(req)
197 def render_GET(self, req):
198 op = req.args.get('op')
199 if op and op[0] in ['vifs', 'vif', 'vbds', 'vbd']:
200 return self.perform(req)
201 if self.use_sxp(req):
202 req.setHeader("Content-Type", sxp.mime_type)
203 sxp.show(self.dom.sxpr(), out=req)
204 else:
205 req.write('<html><head></head><body>')
206 self.print_path(req)
207 #self.ls()
208 req.write('<p>%s</p>' % self.dom)
209 if self.dom.console:
210 cinfo = self.dom.console
211 cid = cinfo.id
212 #todo: Local xref: need to know server prefix.
213 req.write('<p><a href="/xend/console/%s">Console %s</a></p>'
214 % (cid, cid))
215 req.write('<p><a href="%s">Connect to console</a></p>'
216 % cinfo.uri())
217 if self.dom.config:
218 req.write("<code><pre>")
219 PrettyPrint.prettyprint(self.dom.config, out=req)
220 req.write("</pre></code>")
221 self.form(req)
222 req.write('</body></html>')
223 return ''
225 def form(self, req):
226 url = req.prePathURL()
227 req.write('<form method="post" action="%s">' % url)
228 req.write('<input type="submit" name="op" value="unpause">')
229 req.write('<input type="submit" name="op" value="pause">')
230 req.write('<input type="submit" name="op" value="destroy">')
231 req.write('</form>')
233 req.write('<form method="post" action="%s">' % url)
234 req.write('<input type="submit" name="op" value="shutdown">')
235 req.write('<input type="radio" name="reason" value="poweroff" checked>Poweroff')
236 req.write('<input type="radio" name="reason" value="halt">Halt')
237 req.write('<input type="radio" name="reason" value="reboot">Reboot')
238 req.write('</form>')
240 req.write('<form method="post" action="%s">' % url)
241 req.write('<br><input type="submit" name="op" value="save">')
242 req.write(' To file: <input type="text" name="file">')
243 req.write('</form>')
245 req.write('<form method="post" action="%s">' % url)
246 req.write('<br><input type="submit" name="op" value="migrate">')
247 req.write(' To host: <input type="text" name="destination">')
248 req.write('</form>')