root / image_creator / dialog_wizard.py @ 66719e36
History | View | Annotate | Download (10.4 kB)
1 |
#!/usr/bin/env python
|
---|---|
2 |
|
3 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
4 |
#
|
5 |
# Redistribution and use in source and binary forms, with or
|
6 |
# without modification, are permitted provided that the following
|
7 |
# conditions are met:
|
8 |
#
|
9 |
# 1. Redistributions of source code must retain the above
|
10 |
# copyright notice, this list of conditions and the following
|
11 |
# disclaimer.
|
12 |
#
|
13 |
# 2. Redistributions in binary form must reproduce the above
|
14 |
# copyright notice, this list of conditions and the following
|
15 |
# disclaimer in the documentation and/or other materials
|
16 |
# provided with the distribution.
|
17 |
#
|
18 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
19 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
20 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
21 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
22 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
23 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
24 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
25 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
26 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
27 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
28 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
29 |
# POSSIBILITY OF SUCH DAMAGE.
|
30 |
#
|
31 |
# The views and conclusions contained in the software and
|
32 |
# documentation are those of the authors and should not be
|
33 |
# interpreted as representing official policies, either expressed
|
34 |
# or implied, of GRNET S.A.
|
35 |
|
36 |
import dialog |
37 |
import time |
38 |
import StringIO |
39 |
|
40 |
from image_creator.kamaki_wrapper import Kamaki, ClientError |
41 |
from image_creator.util import MD5, FatalError |
42 |
from image_creator.output.cli import OutputWthProgress |
43 |
from image_creator.dialog_util import extract_image, update_background_title |
44 |
|
45 |
PAGE_WIDTH = 70
|
46 |
|
47 |
|
48 |
class WizardExit(Exception): |
49 |
pass
|
50 |
|
51 |
|
52 |
class WizardInvalidData(Exception): |
53 |
pass
|
54 |
|
55 |
|
56 |
class Wizard: |
57 |
def __init__(self, session): |
58 |
self.session = session
|
59 |
self.pages = []
|
60 |
self.session['wizard'] = {} |
61 |
self.d = session['dialog'] |
62 |
|
63 |
def add_page(self, page): |
64 |
self.pages.append(page)
|
65 |
|
66 |
def run(self): |
67 |
idx = 0
|
68 |
while True: |
69 |
try:
|
70 |
idx += self.pages[idx].run(self.session, idx, len(self.pages)) |
71 |
except WizardExit:
|
72 |
return False |
73 |
except WizardInvalidData:
|
74 |
continue
|
75 |
|
76 |
if idx >= len(self.pages): |
77 |
msg = "All necessary information has been gathered:\n\n"
|
78 |
for page in self.pages: |
79 |
msg += " * %s\n" % page.info
|
80 |
msg += "\nContinue with the image creation process?"
|
81 |
|
82 |
ret = self.d.yesno(
|
83 |
msg, width=PAGE_WIDTH, height=8 + len(self.pages), |
84 |
ok_label="Yes", cancel="Back", extra_button=1, |
85 |
extra_label="Quit", title="Confirmation") |
86 |
|
87 |
if ret == self.d.DIALOG_CANCEL: |
88 |
idx -= 1
|
89 |
elif ret == self.d.DIALOG_EXTRA: |
90 |
return False |
91 |
elif ret == self.d.DIALOG_OK: |
92 |
return True |
93 |
|
94 |
if idx < 0: |
95 |
return False |
96 |
|
97 |
|
98 |
class WizardPage(object): |
99 |
NEXT = 1
|
100 |
PREV = -1
|
101 |
|
102 |
def __init__(self, **kargs): |
103 |
validate = kargs['validate'] if 'validate' in kargs else lambda x: x |
104 |
setattr(self, "validate", validate) |
105 |
|
106 |
display = kargs['display'] if 'display' in kargs else lambda x: x |
107 |
setattr(self, "display", display) |
108 |
|
109 |
def run(self, session, index, total): |
110 |
raise NotImplementedError |
111 |
|
112 |
|
113 |
class WizardRadioListPage(WizardPage): |
114 |
|
115 |
def __init__(self, name, printable, message, choices, **kargs): |
116 |
super(WizardRadioListPage, self).__init__(**kargs) |
117 |
self.name = name
|
118 |
self.printable = printable
|
119 |
self.message = message
|
120 |
self.choices = choices
|
121 |
self.title = kargs['title'] if 'title' in kargs else '' |
122 |
self.default = kargs['default'] if 'default' in kargs else "" |
123 |
|
124 |
def run(self, session, index, total): |
125 |
d = session['dialog']
|
126 |
w = session['wizard']
|
127 |
|
128 |
choices = [] |
129 |
for i in range(len(self.choices)): |
130 |
default = 1 if self.choices[i][0] == self.default else 0 |
131 |
choices.append((self.choices[i][0], self.choices[i][1], default)) |
132 |
|
133 |
(code, answer) = d.radiolist( |
134 |
self.message, height=10, width=PAGE_WIDTH, ok_label="Next", |
135 |
cancel="Back", choices=choices,
|
136 |
title="(%d/%d) %s" % (index + 1, total, self.title)) |
137 |
|
138 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
139 |
return self.PREV |
140 |
|
141 |
w[self.name] = self.validate(answer) |
142 |
self.default = answer
|
143 |
self.info = "%s: %s" % (self.printable, self.display(w[self.name])) |
144 |
|
145 |
return self.NEXT |
146 |
|
147 |
|
148 |
class WizardInputPage(WizardPage): |
149 |
|
150 |
def __init__(self, name, printable, message, **kargs): |
151 |
super(WizardInputPage, self).__init__(**kargs) |
152 |
self.name = name
|
153 |
self.printable = printable
|
154 |
self.message = message
|
155 |
self.title = kargs['title'] if 'title' in kargs else '' |
156 |
self.init = kargs['init'] if 'init' in kargs else '' |
157 |
|
158 |
def run(self, session, index, total): |
159 |
d = session['dialog']
|
160 |
w = session['wizard']
|
161 |
|
162 |
(code, answer) = d.inputbox( |
163 |
self.message, init=self.init, width=PAGE_WIDTH, ok_label="Next", |
164 |
cancel="Back", title="(%d/%d) %s" % (index + 1, total, self.title)) |
165 |
|
166 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
167 |
return self.PREV |
168 |
|
169 |
value = answer.strip() |
170 |
self.init = value
|
171 |
w[self.name] = self.validate(value) |
172 |
self.info = "%s: %s" % (self.printable, self.display(w[self.name])) |
173 |
|
174 |
return self.NEXT |
175 |
|
176 |
|
177 |
def wizard(session): |
178 |
|
179 |
init_token = Kamaki.get_token() |
180 |
if init_token is None: |
181 |
init_token = ""
|
182 |
|
183 |
name = WizardInputPage( |
184 |
"ImageName", "Image Name", "Please provide a name for the image:", |
185 |
title="Image Name", init=session['device'].distro) |
186 |
|
187 |
descr = WizardInputPage( |
188 |
"ImageDescription", "Image Description", |
189 |
"Please provide a description for the image:",
|
190 |
title="Image Description", init=session['metadata']['DESCRIPTION'] if |
191 |
'DESCRIPTION' in session['metadata'] else '') |
192 |
|
193 |
registration = WizardRadioListPage( |
194 |
"ImageRegistration", "Registration Type", |
195 |
"Please provide a registration type:",
|
196 |
[("Private", "Image is accessible only by this user"), |
197 |
("Public", "Everyone can create VMs from this image")], |
198 |
title="Registration Type", default="Private") |
199 |
|
200 |
def validate_account(token): |
201 |
if len(token) == 0: |
202 |
d.msgbox("The token cannot be empty", width=PAGE_WIDTH)
|
203 |
raise WizardInvalidData
|
204 |
|
205 |
account = Kamaki.get_account(token) |
206 |
if account is None: |
207 |
session['dialog'].msgbox("The token you provided in not valid!", |
208 |
width=PAGE_WIDTH) |
209 |
raise WizardInvalidData
|
210 |
|
211 |
return account
|
212 |
|
213 |
account = WizardInputPage( |
214 |
"Account", "Account", |
215 |
"Please provide your ~okeanos authentication token:",
|
216 |
title="~okeanos account", init=init_token, validate=validate_account,
|
217 |
display=lambda account: account['username']) |
218 |
|
219 |
w = Wizard(session) |
220 |
|
221 |
w.add_page(name) |
222 |
w.add_page(descr) |
223 |
w.add_page(registration) |
224 |
w.add_page(account) |
225 |
|
226 |
if w.run():
|
227 |
create_image(session) |
228 |
else:
|
229 |
return False |
230 |
|
231 |
return True |
232 |
|
233 |
|
234 |
def create_image(session): |
235 |
d = session['dialog']
|
236 |
disk = session['disk']
|
237 |
device = session['device']
|
238 |
snapshot = session['snapshot']
|
239 |
image_os = session['image_os']
|
240 |
wizard = session['wizard']
|
241 |
|
242 |
# Save Kamaki credentials
|
243 |
Kamaki.save_token(wizard['Account']['auth_token']) |
244 |
|
245 |
with_progress = OutputWthProgress(True)
|
246 |
out = disk.out |
247 |
out.add(with_progress) |
248 |
try:
|
249 |
out.clear() |
250 |
|
251 |
#Sysprep
|
252 |
device.mount(False)
|
253 |
image_os.do_sysprep() |
254 |
metadata = image_os.meta |
255 |
device.umount() |
256 |
|
257 |
#Shrink
|
258 |
size = device.shrink() |
259 |
session['shrinked'] = True |
260 |
update_background_title(session) |
261 |
|
262 |
metadata.update(device.meta) |
263 |
metadata['DESCRIPTION'] = wizard['ImageDescription'] |
264 |
|
265 |
#MD5
|
266 |
md5 = MD5(out) |
267 |
session['checksum'] = md5.compute(snapshot, size)
|
268 |
|
269 |
#Metadata
|
270 |
metastring = '\n'.join(
|
271 |
['%s=%s' % (key, value) for (key, value) in metadata.items()]) |
272 |
metastring += '\n'
|
273 |
|
274 |
out.output() |
275 |
try:
|
276 |
out.output("Uploading image to pithos:")
|
277 |
kamaki = Kamaki(wizard['Account'], out)
|
278 |
|
279 |
name = "%s-%s.diskdump" % (wizard['ImageName'], |
280 |
time.strftime("%Y%m%d%H%M"))
|
281 |
pithos_file = ""
|
282 |
with open(snapshot, 'rb') as f: |
283 |
pithos_file = kamaki.upload(f, size, name, |
284 |
"(1/4) Calculating block hashes",
|
285 |
"(2/4) Uploading missing blocks")
|
286 |
|
287 |
out.output("(3/4) Uploading metadata file ...", False) |
288 |
kamaki.upload(StringIO.StringIO(metastring), size=len(metastring),
|
289 |
remote_path="%s.%s" % (name, 'meta')) |
290 |
out.success('done')
|
291 |
out.output("(4/4) Uploading md5sum file ...", False) |
292 |
md5sumstr = '%s %s\n' % (session['checksum'], name) |
293 |
kamaki.upload(StringIO.StringIO(md5sumstr), size=len(md5sumstr),
|
294 |
remote_path="%s.%s" % (name, 'md5sum')) |
295 |
out.success('done')
|
296 |
out.output() |
297 |
|
298 |
is_public = True if wizard['ImageRegistration'] == "Public" else \ |
299 |
False
|
300 |
out.output('Registering %s image with ~okeanos ...' %
|
301 |
wizard['ImageRegistration'].lower(), False) |
302 |
kamaki.register(wizard['ImageName'], pithos_file, metadata,
|
303 |
is_public) |
304 |
out.success('done')
|
305 |
out.output() |
306 |
|
307 |
except ClientError as e: |
308 |
raise FatalError("Pithos client: %d %s" % (e.status, e.message)) |
309 |
finally:
|
310 |
out.remove(with_progress) |
311 |
|
312 |
msg = "The %s image was successfully uploaded and registered with " \
|
313 |
"~okeanos. Would you like to keep a local copy of the image?" \
|
314 |
% wizard['ImageRegistration'].lower()
|
315 |
if not d.yesno(msg, width=PAGE_WIDTH): |
316 |
extract_image(session) |
317 |
|
318 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
|