root / image_creator / dialog_wizard.py @ 663f5f80
History | View | Annotate | Download (9.8 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 Wizard: |
53 |
def __init__(self, session): |
54 |
self.session = session
|
55 |
self.pages = []
|
56 |
self.session['wizard'] = {} |
57 |
|
58 |
def add_page(self, page): |
59 |
self.pages.append(page)
|
60 |
|
61 |
def run(self): |
62 |
idx = 0
|
63 |
while True: |
64 |
try:
|
65 |
idx += self.pages[idx].run(self.session, idx, len(self.pages)) |
66 |
except WizardExit:
|
67 |
return False |
68 |
|
69 |
if idx >= len(self.pages): |
70 |
break
|
71 |
|
72 |
if idx < 0: |
73 |
return False |
74 |
return True |
75 |
|
76 |
|
77 |
class WizardPage: |
78 |
NEXT = 1
|
79 |
PREV = -1
|
80 |
|
81 |
def run(self, session, index, total): |
82 |
raise NotImplementedError |
83 |
|
84 |
|
85 |
class WizardRadioListPage(WizardPage): |
86 |
|
87 |
def __init__(self, name, message, choices, **kargs): |
88 |
self.name = name
|
89 |
self.message = message
|
90 |
self.choices = choices
|
91 |
self.title = kargs['title'] if 'title' in kargs else '' |
92 |
self.default = kargs['default'] if 'default' in kargs else 0 |
93 |
|
94 |
def run(self, session, index, total): |
95 |
d = session['dialog']
|
96 |
w = session['wizard']
|
97 |
|
98 |
choices = [] |
99 |
for i in range(len(self.choices)): |
100 |
default = 1 if i == self.default else 0 |
101 |
choices.append((self.choices[i][0], self.choices[i][1], default)) |
102 |
|
103 |
while True: |
104 |
(code, answer) = \ |
105 |
d.radiolist(self.message, width=PAGE_WIDTH,
|
106 |
ok_label="Next", cancel="Back", choices=choices, |
107 |
title="(%d/%d) %s" % (index + 1, total, self.title) |
108 |
) |
109 |
|
110 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
111 |
return self.PREV |
112 |
|
113 |
for i in range(len(choices)): |
114 |
if self.choices[i] == answer: |
115 |
self.default = i
|
116 |
w[name] = i |
117 |
break
|
118 |
|
119 |
return self.NEXT |
120 |
|
121 |
|
122 |
class WizardInputPage(WizardPage): |
123 |
|
124 |
def __init__(self, name, message, **kargs): |
125 |
self.name = name
|
126 |
self.message = message
|
127 |
self.title = kargs['title'] if 'title' in kargs else '' |
128 |
self.init_value = kargs['init'] if 'init' in kargs else '' |
129 |
self.allow_empty = kargs['empty'] if 'empty' in kargs else False |
130 |
|
131 |
def run(self, session, index, total): |
132 |
d = session['dialog']
|
133 |
w = session['wizard']
|
134 |
|
135 |
init = w[self.name] if self.name in w else self.init_value |
136 |
while True: |
137 |
(code, answer) = \ |
138 |
d.inputbox(self.message, init=init,
|
139 |
width=PAGE_WIDTH, ok_label="Next", cancel="Back", |
140 |
title="(%d/%d) %s" % (index + 1, total, self.title)) |
141 |
|
142 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
143 |
return self.PREV |
144 |
|
145 |
value = answer.strip() |
146 |
if len(value) == 0 and self.allow_empty is False: |
147 |
d.msgbox("The value cannot be empty!", width=PAGE_WIDTH)
|
148 |
continue
|
149 |
w[self.name] = value
|
150 |
break
|
151 |
|
152 |
return self.NEXT |
153 |
|
154 |
|
155 |
class WizardYesNoPage(WizardPage): |
156 |
|
157 |
def __init__(self, message, **kargs): |
158 |
self.message = message
|
159 |
self.title = kargs['title'] if 'title' in kargs else '' |
160 |
|
161 |
def run(self, session, index, total): |
162 |
d = session['dialog']
|
163 |
|
164 |
while True: |
165 |
ret = d.yesno(self.message, width=PAGE_WIDTH, ok_label="Yes", |
166 |
cancel="Back", extra_button=1, extra_label="Quit", |
167 |
title="(%d/%d) %s" % (index + 1, total, self.title)) |
168 |
|
169 |
if ret == d.DIALOG_CANCEL:
|
170 |
return self.PREV |
171 |
elif ret == d.DIALOG_EXTRA:
|
172 |
raise WizardExit
|
173 |
elif ret == d.DIALOG_OK:
|
174 |
return self.NEXT |
175 |
|
176 |
|
177 |
def wizard(session): |
178 |
init_account = Kamaki.get_account() |
179 |
if init_account is None: |
180 |
init_account = ""
|
181 |
|
182 |
init_token = Kamaki.get_token() |
183 |
if init_token is None: |
184 |
init_token = ""
|
185 |
|
186 |
name = WizardInputPage("ImageName", "Please provide a name for the image:", |
187 |
title="Image Name", init=session['device'].distro) |
188 |
descr = WizardInputPage("ImageDescription",
|
189 |
"Please provide a description for the image:",
|
190 |
title="Image Description", empty=True, |
191 |
init=session['metadata']['DESCRIPTION'] if |
192 |
'DESCRIPTION' in session['metadata'] else '') |
193 |
account = WizardInputPage("account",
|
194 |
"Please provide your ~okeanos account e-mail:",
|
195 |
title="~okeanos account information",
|
196 |
init=init_account) |
197 |
token = WizardInputPage("token",
|
198 |
"Please provide your ~okeanos account token:",
|
199 |
title="~okeanos account token",
|
200 |
init=init_token) |
201 |
|
202 |
msg = "All necessary information has been gathered. Confirm and Proceed."
|
203 |
proceed = WizardYesNoPage(msg, title="Confirmation")
|
204 |
|
205 |
w = Wizard(session) |
206 |
|
207 |
w.add_page(name) |
208 |
w.add_page(descr) |
209 |
w.add_page(account) |
210 |
w.add_page(token) |
211 |
w.add_page(proceed) |
212 |
|
213 |
if w.run():
|
214 |
create_image(session) |
215 |
else:
|
216 |
return False |
217 |
|
218 |
return True |
219 |
|
220 |
|
221 |
def create_image(session): |
222 |
d = session['dialog']
|
223 |
disk = session['disk']
|
224 |
device = session['device']
|
225 |
snapshot = session['snapshot']
|
226 |
image_os = session['image_os']
|
227 |
wizard = session['wizard']
|
228 |
|
229 |
# Save Kamaki credentials
|
230 |
Kamaki.save_account(wizard['account'])
|
231 |
Kamaki.save_token(wizard['token'])
|
232 |
|
233 |
with_progress = OutputWthProgress(True)
|
234 |
out = disk.out |
235 |
out.add(with_progress) |
236 |
try:
|
237 |
out.clear() |
238 |
|
239 |
#Sysprep
|
240 |
device.mount(False)
|
241 |
image_os.do_sysprep() |
242 |
metadata = image_os.meta |
243 |
device.umount() |
244 |
|
245 |
#Shrink
|
246 |
size = device.shrink() |
247 |
session['shrinked'] = True |
248 |
update_background_title(session) |
249 |
|
250 |
metadata.update(device.meta) |
251 |
metadata['DESCRIPTION'] = wizard['ImageDescription'] |
252 |
|
253 |
#MD5
|
254 |
md5 = MD5(out) |
255 |
session['checksum'] = md5.compute(snapshot, size)
|
256 |
|
257 |
#Metadata
|
258 |
metastring = '\n'.join(
|
259 |
['%s=%s' % (key, value) for (key, value) in metadata.items()]) |
260 |
metastring += '\n'
|
261 |
|
262 |
out.output() |
263 |
try:
|
264 |
out.output("Uploading image to pithos:")
|
265 |
kamaki = Kamaki(wizard['account'], wizard['token'], out) |
266 |
|
267 |
name = "%s-%s.diskdump" % (wizard['ImageName'], |
268 |
time.strftime("%Y%m%d%H%M"))
|
269 |
pithos_file = ""
|
270 |
with open(snapshot, 'rb') as f: |
271 |
pithos_file = kamaki.upload(f, size, name, |
272 |
"(1/4) Calculating block hashes",
|
273 |
"(2/4) Uploading missing blocks")
|
274 |
|
275 |
out.output("(3/4) Uploading metadata file ...", False) |
276 |
kamaki.upload(StringIO.StringIO(metastring), size=len(metastring),
|
277 |
remote_path="%s.%s" % (name, 'meta')) |
278 |
out.success('done')
|
279 |
out.output("(4/4) Uploading md5sum file ...", False) |
280 |
md5sumstr = '%s %s\n' % (session['checksum'], name) |
281 |
kamaki.upload(StringIO.StringIO(md5sumstr), size=len(md5sumstr),
|
282 |
remote_path="%s.%s" % (name, 'md5sum')) |
283 |
out.success('done')
|
284 |
out.output() |
285 |
|
286 |
out.output('Registering image with ~okeanos ...', False) |
287 |
kamaki.register(wizard['ImageName'], pithos_file, metadata)
|
288 |
out.success('done')
|
289 |
out.output() |
290 |
|
291 |
except ClientError as e: |
292 |
raise FatalError("Pithos client: %d %s" % (e.status, e.message)) |
293 |
finally:
|
294 |
out.remove(with_progress) |
295 |
|
296 |
msg = "The image was successfully uploaded and registered with " \
|
297 |
"~okeanos. Would you like to keep a local copy of the image?"
|
298 |
if not d.yesno(msg, width=PAGE_WIDTH): |
299 |
extract_image(session) |
300 |
|
301 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
|