root / image_creator / dialog_wizard.py @ 023e1217
History | View | Annotate | Download (9.5 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 |
|
179 |
name = WizardInputPage("ImageName", "Please provide a name for the image:", |
180 |
title="Image Name", init=session['device'].distro) |
181 |
descr = WizardInputPage("ImageDescription",
|
182 |
"Please provide a description for the image:",
|
183 |
title="Image Description", empty=True, |
184 |
init=session['metadata']['DESCRIPTION'] if |
185 |
'DESCRIPTION' in session['metadata'] else '') |
186 |
account = WizardInputPage("account",
|
187 |
"Please provide your ~okeanos account e-mail:",
|
188 |
title="~okeanos account information",
|
189 |
init=Kamaki.get_account()) |
190 |
token = WizardInputPage("token",
|
191 |
"Please provide your ~okeanos account token:",
|
192 |
title="~okeanos account token",
|
193 |
init=Kamaki.get_token()) |
194 |
|
195 |
msg = "All necessary information has been gathered. Confirm and Proceed."
|
196 |
proceed = WizardYesNoPage(msg, title="Confirmation")
|
197 |
|
198 |
w = Wizard(session) |
199 |
|
200 |
w.add_page(name) |
201 |
w.add_page(descr) |
202 |
w.add_page(account) |
203 |
w.add_page(token) |
204 |
w.add_page(proceed) |
205 |
|
206 |
if w.run():
|
207 |
create_image(session) |
208 |
else:
|
209 |
return False |
210 |
|
211 |
return True |
212 |
|
213 |
|
214 |
def create_image(session): |
215 |
d = session['dialog']
|
216 |
disk = session['disk']
|
217 |
device = session['device']
|
218 |
snapshot = session['snapshot']
|
219 |
image_os = session['image_os']
|
220 |
wizard = session['wizard']
|
221 |
|
222 |
with_progress = OutputWthProgress(True)
|
223 |
out = disk.out |
224 |
out.add(with_progress) |
225 |
try:
|
226 |
out.clear() |
227 |
|
228 |
#Sysprep
|
229 |
device.mount(False)
|
230 |
image_os.do_sysprep() |
231 |
metadata = image_os.meta |
232 |
device.umount() |
233 |
|
234 |
#Shrink
|
235 |
size = device.shrink() |
236 |
session['shrinked'] = True |
237 |
update_background_title(session) |
238 |
|
239 |
metadata.update(device.meta) |
240 |
metadata['DESCRIPTION'] = wizard['ImageDescription'] |
241 |
|
242 |
#MD5
|
243 |
md5 = MD5(out) |
244 |
session['checksum'] = md5.compute(snapshot, size)
|
245 |
|
246 |
#Metadata
|
247 |
metastring = '\n'.join(
|
248 |
['%s=%s' % (key, value) for (key, value) in metadata.items()]) |
249 |
metastring += '\n'
|
250 |
|
251 |
out.output() |
252 |
try:
|
253 |
out.output("Uploading image to pithos:")
|
254 |
kamaki = Kamaki(wizard['account'], wizard['token'], out) |
255 |
|
256 |
name = "%s-%s.diskdump" % (wizard['ImageName'], |
257 |
time.strftime("%Y%m%d%H%M"))
|
258 |
pithos_file = ""
|
259 |
with open(snapshot, 'rb') as f: |
260 |
pithos_file = kamaki.upload(f, size, name, |
261 |
"(1/4) Calculating block hashes",
|
262 |
"(2/4) Uploading missing blocks")
|
263 |
|
264 |
out.output("(3/4) Uploading metadata file...", False) |
265 |
kamaki.upload(StringIO.StringIO(metastring), size=len(metastring),
|
266 |
remote_path="%s.%s" % (name, 'meta')) |
267 |
out.success('done')
|
268 |
out.output("(4/4) Uploading md5sum file...", False) |
269 |
md5sumstr = '%s %s\n' % (session['checksum'], name) |
270 |
kamaki.upload(StringIO.StringIO(md5sumstr), size=len(md5sumstr),
|
271 |
remote_path="%s.%s" % (name, 'md5sum')) |
272 |
out.success('done')
|
273 |
out.output() |
274 |
|
275 |
out.output('Registering image with ~okeanos...', False) |
276 |
kamaki.register(wizard['ImageName'], pithos_file, metadata)
|
277 |
out.success('done')
|
278 |
out.output() |
279 |
|
280 |
except ClientError as e: |
281 |
raise FatalError("Pithos client: %d %s" % (e.status, e.message)) |
282 |
finally:
|
283 |
out.remove(with_progress) |
284 |
|
285 |
msg = "The image was successfully uploaded and registered with " \
|
286 |
"~okeanos. Would you like to keep a local copy of the image?"
|
287 |
if not d.yesno(msg, width=PAGE_WIDTH): |
288 |
extract_image(session) |
289 |
|
290 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
|