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 | #============================================================================ |
---|
17 | |
---|
18 | import traceback |
---|
19 | from StringIO import StringIO |
---|
20 | |
---|
21 | from xen.web import http |
---|
22 | |
---|
23 | from xen.xend import sxp |
---|
24 | from xen.xend import XendDomain |
---|
25 | from xen.xend.XendDomainInfo import XendDomainInfo |
---|
26 | from xen.xend.Args import FormFn |
---|
27 | from xen.xend.XendError import XendError |
---|
28 | from xen.xend.XendLogging import log |
---|
29 | from xen.xend.XendConstants import DOM_STATE_RUNNING |
---|
30 | |
---|
31 | from xen.web.SrvDir import SrvDir |
---|
32 | from SrvDomain import SrvDomain |
---|
33 | |
---|
34 | |
---|
35 | class SrvDomainDir(SrvDir): |
---|
36 | """Service that manages the domain directory. |
---|
37 | """ |
---|
38 | |
---|
39 | def __init__(self): |
---|
40 | SrvDir.__init__(self) |
---|
41 | self.xd = XendDomain.instance() |
---|
42 | |
---|
43 | def domain(self, x): |
---|
44 | dom = self.xd.domain_lookup(x) |
---|
45 | return SrvDomain(dom) |
---|
46 | |
---|
47 | def get(self, x): |
---|
48 | v = SrvDir.get(self, x) |
---|
49 | if v is not None: |
---|
50 | return v |
---|
51 | else: |
---|
52 | return self.domain(x) |
---|
53 | |
---|
54 | def op_create(self, _, req): |
---|
55 | """Create a domain. |
---|
56 | Expects the domain config in request parameter 'config' in SXP format. |
---|
57 | """ |
---|
58 | ok = 0 |
---|
59 | errmsg = '' |
---|
60 | try: |
---|
61 | configstring = req.args.get('config')[0] |
---|
62 | #print 'op_create>', 'config:', configstring |
---|
63 | pin = sxp.Parser() |
---|
64 | pin.input(configstring) |
---|
65 | pin.input_eof() |
---|
66 | config = pin.get_val() |
---|
67 | ok = 1 |
---|
68 | except sxp.ParseError, ex: |
---|
69 | errmsg = 'Invalid configuration ' + str(ex) |
---|
70 | except Exception, ex: |
---|
71 | print 'op_create> Exception in config', ex |
---|
72 | traceback.print_exc() |
---|
73 | errmsg = 'Configuration error ' + str(ex) |
---|
74 | if not ok: |
---|
75 | raise XendError(errmsg) |
---|
76 | try: |
---|
77 | dominfo = self.xd.domain_create(config) |
---|
78 | return self._op_create_cb(dominfo, configstring, req) |
---|
79 | except Exception, ex: |
---|
80 | print 'op_create> Exception creating domain:' |
---|
81 | traceback.print_exc() |
---|
82 | raise XendError("Error creating domain: " + str(ex)) |
---|
83 | |
---|
84 | def _op_create_cb(self, dominfo, configstring, req): |
---|
85 | """Callback to handle domain creation. |
---|
86 | """ |
---|
87 | dom = dominfo.getName() |
---|
88 | domurl = "%s/%s" % (req.prePathURL(), dom) |
---|
89 | req.setResponseCode(http.CREATED, "created") |
---|
90 | req.setHeader("Location", domurl) |
---|
91 | if self.use_sxp(req): |
---|
92 | return dominfo.sxpr() |
---|
93 | else: |
---|
94 | out = StringIO() |
---|
95 | print >> out, ('<p> Created <a href="%s">Domain %s</a></p>' |
---|
96 | % (domurl, dom)) |
---|
97 | print >> out, '<p><pre>' |
---|
98 | print >> out, configstring |
---|
99 | print >> out, '</pre></p>' |
---|
100 | val = out.getvalue() |
---|
101 | out.close() |
---|
102 | return val |
---|
103 | |
---|
104 | def op_new(self, _, req): |
---|
105 | """Define a new domain. |
---|
106 | Expects the domain config in request parameter 'config' in SXP format. |
---|
107 | """ |
---|
108 | ok = 0 |
---|
109 | errmsg = '' |
---|
110 | try: |
---|
111 | configstring = req.args.get('config')[0] |
---|
112 | #print 'op_create>', 'config:', configstring |
---|
113 | pin = sxp.Parser() |
---|
114 | pin.input(configstring) |
---|
115 | pin.input_eof() |
---|
116 | config = pin.get_val() |
---|
117 | ok = 1 |
---|
118 | except sxp.ParseError, ex: |
---|
119 | errmsg = 'Invalid configuration ' + str(ex) |
---|
120 | except Exception, ex: |
---|
121 | print 'op_create> Exception in config', ex |
---|
122 | traceback.print_exc() |
---|
123 | errmsg = 'Configuration error ' + str(ex) |
---|
124 | if not ok: |
---|
125 | raise XendError(errmsg) |
---|
126 | try: |
---|
127 | self.xd.domain_new(config) |
---|
128 | except Exception, ex: |
---|
129 | print 'op_create> Exception creating domain:' |
---|
130 | traceback.print_exc() |
---|
131 | raise XendError("Error creating domain: " + str(ex)) |
---|
132 | |
---|
133 | def op_restore(self, op, req): |
---|
134 | """Restore a domain from file. |
---|
135 | |
---|
136 | """ |
---|
137 | return req.threadRequest(self.do_restore, op, req) |
---|
138 | |
---|
139 | def do_restore(self, _, req): |
---|
140 | fn = FormFn(self.xd.domain_restore, |
---|
141 | [['file', 'str']]) |
---|
142 | dominfo = fn(req.args) |
---|
143 | dom = dominfo.getName() |
---|
144 | domurl = "%s/%s" % (req.prePathURL(), dom) |
---|
145 | req.setResponseCode(http.CREATED) |
---|
146 | req.setHeader("Location", domurl) |
---|
147 | if self.use_sxp(req): |
---|
148 | return dominfo.sxpr() |
---|
149 | else: |
---|
150 | out = StringIO() |
---|
151 | print >> out, ('<p> Created <a href="%s">Domain %s</a></p>' |
---|
152 | % (domurl, dom)) |
---|
153 | val = out.getvalue() |
---|
154 | out.close() |
---|
155 | return val |
---|
156 | |
---|
157 | |
---|
158 | def op_list(self, _, req): |
---|
159 | """List the details for this domain.""" |
---|
160 | self._list(req, True) |
---|
161 | |
---|
162 | |
---|
163 | def render_POST(self, req): |
---|
164 | return self.perform(req) |
---|
165 | |
---|
166 | def render_GET(self, req): |
---|
167 | self._list(req, 'detail' in req.args and req.args['detail'] == ['1']) |
---|
168 | |
---|
169 | |
---|
170 | def _list(self, req, detail): |
---|
171 | if self.use_sxp(req): |
---|
172 | req.setHeader("Content-Type", sxp.mime_type) |
---|
173 | self.ls_domain(req, detail, True) |
---|
174 | else: |
---|
175 | req.write("<html><head></head><body>") |
---|
176 | self.print_path(req) |
---|
177 | self.ls(req) |
---|
178 | self.ls_domain(req, detail, False) |
---|
179 | self.form(req) |
---|
180 | req.write("</body></html>") |
---|
181 | |
---|
182 | |
---|
183 | def ls_domain(self, req, detail, use_sxp): |
---|
184 | url = req.prePathURL() |
---|
185 | if not url.endswith('/'): |
---|
186 | url += '/' |
---|
187 | if use_sxp: |
---|
188 | if detail: |
---|
189 | sxp.show(map(XendDomainInfo.sxpr, self.xd.list()), out=req) |
---|
190 | else: |
---|
191 | state = DOM_STATE_RUNNING |
---|
192 | if 'state' in req.args and len(req.args['state']) > 0: |
---|
193 | state = req.args['state'][0] |
---|
194 | log.trace("Listing domains in state " + str(state)) |
---|
195 | sxp.show(self.xd.list_names(state), out=req) |
---|
196 | else: |
---|
197 | domains = self.xd.list_sorted() |
---|
198 | req.write('<ul>') |
---|
199 | for d in domains: |
---|
200 | req.write( |
---|
201 | '<li><a href="%s%s">Domain %s</a>: id = %s, memory = %d' |
---|
202 | % (url, d.getName(), d.getName(), d.getDomid(), |
---|
203 | d.getMemoryTarget())) |
---|
204 | req.write('</li>') |
---|
205 | req.write('</ul>') |
---|
206 | |
---|
207 | |
---|
208 | def form(self, req): |
---|
209 | """Generate the form(s) for domain dir operations. |
---|
210 | """ |
---|
211 | req.write('<form method="post" action="%s" enctype="multipart/form-data">' |
---|
212 | % req.prePathURL()) |
---|
213 | req.write('<button type="submit" name="op" value="create">Create Domain</button>') |
---|
214 | req.write('Config <input type="file" name="config"><br>') |
---|
215 | req.write('</form>') |
---|
216 | |
---|
217 | req.write('<form method="post" action="%s" enctype="multipart/form-data">' |
---|
218 | % req.prePathURL()) |
---|
219 | req.write('<button type="submit" name="op" value="restore">Restore Domain</button>') |
---|
220 | req.write('State <input type="string" name="state"><br>') |
---|
221 | req.write('</form>') |
---|
222 | |
---|