root / image_creator / dialog_main.py @ 74585c56
History | View | Annotate | Download (28.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 sys |
38 |
import os |
39 |
import textwrap |
40 |
import signal |
41 |
import StringIO |
42 |
|
43 |
from image_creator import __version__ as version |
44 |
from image_creator.util import FatalError, MD5 |
45 |
from image_creator.output.dialog import GaugeOutput, InfoBoxOutput |
46 |
from image_creator.disk import Disk |
47 |
from image_creator.os_type import os_cls |
48 |
from image_creator.kamaki_wrapper import Kamaki, ClientError |
49 |
from image_creator.help import get_help_file |
50 |
|
51 |
MSGBOX_WIDTH = 60
|
52 |
YESNO_WIDTH = 50
|
53 |
MENU_WIDTH = 70
|
54 |
INPUTBOX_WIDTH = 70
|
55 |
CHECKBOX_WIDTH = 70
|
56 |
HELP_WIDTH = 70
|
57 |
INFOBOX_WIDTH = 70
|
58 |
|
59 |
CONFIGURATION_TASKS = [ |
60 |
("Partition table manipulation", ["FixPartitionTable"], |
61 |
["linux", "windows"]), |
62 |
("File system resize",
|
63 |
["FilesystemResizeUnmounted", "FilesystemResizeMounted"], |
64 |
["linux", "windows"]), |
65 |
("Swap partition configuration", ["AddSwap"], ["linux"]), |
66 |
("SSH keys removal", ["DeleteSSHKeys"], ["linux"]), |
67 |
("Temporal RDP disabling", ["DisableRemoteDesktopConnections"], ["windows"]), |
68 |
("SELinux relabeling at next boot", ["SELinuxAutorelabel"], |
69 |
["linux"]),
|
70 |
("Hostname/Computer Name assignment", ["AssignHostname"], |
71 |
["windows", "linux"]), |
72 |
("Password change", ["ChangePassword"], ["windows", "linux"]), |
73 |
("File injection", ["EnforcePersonality"], ["windows", "linux"]) |
74 |
] |
75 |
|
76 |
|
77 |
class Reset(Exception): |
78 |
pass
|
79 |
|
80 |
|
81 |
class metadata_monitor(object): |
82 |
def __init__(self, session, meta): |
83 |
self.session = session
|
84 |
self.meta = meta
|
85 |
|
86 |
def __enter__(self): |
87 |
self.old = {}
|
88 |
for (k, v) in self.meta.items(): |
89 |
self.old[k] = v
|
90 |
|
91 |
def __exit__(self, type, value, traceback): |
92 |
d = self.session['dialog'] |
93 |
|
94 |
altered = {} |
95 |
added = {} |
96 |
|
97 |
for (k, v) in self.meta.items(): |
98 |
if k not in self.old: |
99 |
added[k] = v |
100 |
elif self.old[k] != v: |
101 |
altered[k] = v |
102 |
|
103 |
if not (len(added) or len(altered)): |
104 |
return
|
105 |
|
106 |
msg = "The last action has changed some image properties:\n\n"
|
107 |
if len(added): |
108 |
msg += "New image properties:\n"
|
109 |
for (k, v) in added.items(): |
110 |
msg += ' %s: "%s"\n' % (k, v)
|
111 |
msg += "\n"
|
112 |
if len(altered): |
113 |
msg += "Updated image properties:\n"
|
114 |
for (k, v) in altered.items(): |
115 |
msg += ' %s: "%s" -> "%s"\n' % (k, self.old[k], v) |
116 |
msg += "\n"
|
117 |
|
118 |
self.session['metadata'].update(added) |
119 |
self.session['metadata'].update(altered) |
120 |
d.msgbox(msg, title="Image Property Changes", width=MSGBOX_WIDTH)
|
121 |
|
122 |
|
123 |
def confirm_exit(d, msg=''): |
124 |
return not d.yesno("%s Do you want to exit?" % msg, width=YESNO_WIDTH) |
125 |
|
126 |
|
127 |
def confirm_reset(d): |
128 |
return not d.yesno("Are you sure you want to reset everything?", |
129 |
width=YESNO_WIDTH, defaultno=1)
|
130 |
|
131 |
|
132 |
def update_background_title(session): |
133 |
d = session['dialog']
|
134 |
dev = session['device']
|
135 |
|
136 |
MB = 2 ** 20 |
137 |
|
138 |
size = (dev.meta['SIZE'] + MB - 1) // MB |
139 |
shrinked = 'shrinked' in session and session['shrinked'] == True |
140 |
postfix = " (shrinked)" if shrinked else '' |
141 |
|
142 |
title = "OS: %s, Distro: %s, Size: %dMB%s" % \
|
143 |
(dev.ostype, dev.distro, size, postfix) |
144 |
|
145 |
d.setBackgroundTitle(title) |
146 |
|
147 |
|
148 |
def extract_image(session): |
149 |
d = session['dialog']
|
150 |
dir = os.getcwd() |
151 |
while 1: |
152 |
if dir and dir[-1] != os.sep: |
153 |
dir = dir + os.sep
|
154 |
|
155 |
(code, path) = d.fselect(dir, 10, 50, title="Save image as...") |
156 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
157 |
return False |
158 |
|
159 |
if os.path.isdir(path):
|
160 |
dir = path |
161 |
continue
|
162 |
|
163 |
if os.path.isdir("%s.meta" % path): |
164 |
d.msgbox("Can't overwrite directory `%s.meta'" % path,
|
165 |
width=MSGBOX_WIDTH) |
166 |
continue
|
167 |
|
168 |
if os.path.isdir("%s.md5sum" % path): |
169 |
d.msgbox("Can't overwrite directory `%s.md5sum'" % path,
|
170 |
width=MSGBOX_WIDTH) |
171 |
continue
|
172 |
|
173 |
basedir = os.path.dirname(path) |
174 |
name = os.path.basename(path) |
175 |
if not os.path.exists(basedir): |
176 |
d.msgbox("Directory `%s' does not exist" % basedir,
|
177 |
width=MSGBOX_WIDTH) |
178 |
continue
|
179 |
|
180 |
dir = basedir |
181 |
if len(name) == 0: |
182 |
continue
|
183 |
|
184 |
files = ["%s%s" % (path, ext) for ext in ('', '.meta', '.md5sum')] |
185 |
overwrite = filter(os.path.exists, files)
|
186 |
|
187 |
if len(overwrite) > 0: |
188 |
if d.yesno("The following file(s) exist:\n" |
189 |
"%s\nDo you want to overwrite them?" %
|
190 |
"\n".join(overwrite), width=YESNO_WIDTH):
|
191 |
continue
|
192 |
|
193 |
out = GaugeOutput(d, "Image Extraction", "Extracting image...") |
194 |
try:
|
195 |
dev = session['device']
|
196 |
if "checksum" not in session: |
197 |
size = dev.meta['SIZE']
|
198 |
md5 = MD5(out) |
199 |
session['checksum'] = md5.compute(session['snapshot'], size) |
200 |
|
201 |
# Extract image file
|
202 |
dev.out = out |
203 |
dev.dump(path) |
204 |
|
205 |
# Extract metadata file
|
206 |
out.output("Extracting metadata file...")
|
207 |
metastring = '\n'.join(
|
208 |
['%s=%s' % (k, v) for (k, v) in session['metadata'].items()]) |
209 |
metastring += '\n'
|
210 |
with open('%s.meta' % path, 'w') as f: |
211 |
f.write(metastring) |
212 |
out.success('done')
|
213 |
|
214 |
# Extract md5sum file
|
215 |
out.output("Extracting md5sum file...")
|
216 |
md5str = "%s %s\n" % (session['checksum'], name) |
217 |
with open('%s.md5sum' % path, 'w') as f: |
218 |
f.write(md5str) |
219 |
out.success("done")
|
220 |
|
221 |
finally:
|
222 |
out.cleanup() |
223 |
d.msgbox("Image file `%s' was successfully extracted!" % path,
|
224 |
width=MSGBOX_WIDTH) |
225 |
break
|
226 |
|
227 |
return True |
228 |
|
229 |
|
230 |
def upload_image(session): |
231 |
d = session["dialog"]
|
232 |
size = session['device'].meta['SIZE'] |
233 |
|
234 |
if "account" not in session: |
235 |
d.msgbox("You need to provide your ~okeanos login username before you "
|
236 |
"can upload images to pithos+", width=MSGBOX_WIDTH)
|
237 |
return False |
238 |
|
239 |
if "token" not in session: |
240 |
d.msgbox("You need to provide your ~okeanos account authentication "
|
241 |
"token before you can upload images to pithos+",
|
242 |
width=MSGBOX_WIDTH) |
243 |
return False |
244 |
|
245 |
while 1: |
246 |
init = session["upload"] if "upload" in session else '' |
247 |
(code, answer) = d.inputbox("Please provide a filename:", init=init,
|
248 |
width=INPUTBOX_WIDTH) |
249 |
|
250 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
251 |
return False |
252 |
|
253 |
filename = answer.strip() |
254 |
if len(filename) == 0: |
255 |
d.msgbox("Filename cannot be empty", width=MSGBOX_WIDTH)
|
256 |
continue
|
257 |
|
258 |
break
|
259 |
|
260 |
out = GaugeOutput(d, "Image Upload", "Uploading...") |
261 |
if 'checksum' not in session: |
262 |
md5 = MD5(out) |
263 |
session['checksum'] = md5.compute(session['snapshot'], size) |
264 |
try:
|
265 |
kamaki = Kamaki(session['account'], session['token'], out) |
266 |
try:
|
267 |
# Upload image file
|
268 |
with open(session['snapshot'], 'rb') as f: |
269 |
session["upload"] = kamaki.upload(f, size, filename,
|
270 |
"Calculating block hashes",
|
271 |
"Uploading missing blocks")
|
272 |
# Upload metadata file
|
273 |
out.output("Uploading metadata file...")
|
274 |
metastring = '\n'.join(
|
275 |
['%s=%s' % (k, v) for (k, v) in session['metadata'].items()]) |
276 |
metastring += '\n'
|
277 |
kamaki.upload(StringIO.StringIO(metastring), size=len(metastring),
|
278 |
remote_path="%s.meta" % filename)
|
279 |
out.success("done")
|
280 |
|
281 |
# Upload md5sum file
|
282 |
out.output("Uploading md5sum file...")
|
283 |
md5str = "%s %s\n" % (session['checksum'], filename) |
284 |
kamaki.upload(StringIO.StringIO(md5str), size=len(md5str),
|
285 |
remote_path="%s.md5sum" % filename)
|
286 |
out.success("done")
|
287 |
|
288 |
except ClientError as e: |
289 |
d.msgbox("Error in pithos+ client: %s" % e.message,
|
290 |
title="Pithos+ Client Error", width=MSGBOX_WIDTH)
|
291 |
if 'upload' in session: |
292 |
del session['upload'] |
293 |
return False |
294 |
finally:
|
295 |
out.cleanup() |
296 |
|
297 |
d.msgbox("Image file `%s' was successfully uploaded to pithos+" % filename,
|
298 |
width=MSGBOX_WIDTH) |
299 |
|
300 |
return True |
301 |
|
302 |
|
303 |
def register_image(session): |
304 |
d = session["dialog"]
|
305 |
|
306 |
if "account" not in session: |
307 |
d.msgbox("You need to provide your ~okeanos login username before you "
|
308 |
"can register an images to cyclades",
|
309 |
width=MSGBOX_WIDTH) |
310 |
return False |
311 |
|
312 |
if "token" not in session: |
313 |
d.msgbox("You need to provide your ~okeanos account authentication "
|
314 |
"token before you can register an images to cyclades",
|
315 |
width=MSGBOX_WIDTH) |
316 |
return False |
317 |
|
318 |
if "upload" not in session: |
319 |
d.msgbox("You need to have an image uploaded to pithos+ before you "
|
320 |
"can register it to cyclades",
|
321 |
width=MSGBOX_WIDTH) |
322 |
return False |
323 |
|
324 |
while 1: |
325 |
(code, answer) = d.inputbox("Please provide a registration name:"
|
326 |
" be registered:", width=INPUTBOX_WIDTH)
|
327 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
328 |
return False |
329 |
|
330 |
name = answer.strip() |
331 |
if len(name) == 0: |
332 |
d.msgbox("Registration name cannot be empty", width=MSGBOX_WIDTH)
|
333 |
continue
|
334 |
break
|
335 |
|
336 |
out = GaugeOutput(d, "Image Registration", "Registrating image...") |
337 |
try:
|
338 |
out.output("Registring image to cyclades...")
|
339 |
try:
|
340 |
kamaki = Kamaki(session['account'], session['token'], out) |
341 |
kamaki.register(name, session['upload'], session['metadata']) |
342 |
out.success('done')
|
343 |
except ClientError as e: |
344 |
d.msgbox("Error in pithos+ client: %s" % e.message)
|
345 |
return False |
346 |
finally:
|
347 |
out.cleanup() |
348 |
|
349 |
d.msgbox("Image `%s' was successfully registered to cyclades as `%s'" %
|
350 |
(session['upload'], name), width=MSGBOX_WIDTH)
|
351 |
return True |
352 |
|
353 |
|
354 |
def kamaki_menu(session): |
355 |
d = session['dialog']
|
356 |
default_item = "Account"
|
357 |
while 1: |
358 |
account = session["account"] if "account" in session else "<none>" |
359 |
token = session["token"] if "token" in session else "<none>" |
360 |
upload = session["upload"] if "upload" in session else "<none>" |
361 |
|
362 |
choices = [("Account", "Change your ~okeanos username: %s" % account), |
363 |
("Token", "Change your ~okeanos token: %s" % token), |
364 |
("Upload", "Upload image to pithos+"), |
365 |
("Register", "Register image to cyclades: %s" % upload)] |
366 |
|
367 |
(code, choice) = d.menu( |
368 |
text="Choose one of the following or press <Back> to go back.",
|
369 |
width=MENU_WIDTH, choices=choices, cancel="Back",
|
370 |
default_item=default_item, title="Image Registration Menu")
|
371 |
|
372 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
373 |
return False |
374 |
|
375 |
if choice == "Account": |
376 |
default_item = "Account"
|
377 |
(code, answer) = d.inputbox( |
378 |
"Please provide your ~okeanos account e-mail address:",
|
379 |
init=session["account"] if "account" in session else '', |
380 |
width=70)
|
381 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
382 |
continue
|
383 |
if len(answer) == 0 and "account" in session: |
384 |
del session["account"] |
385 |
else:
|
386 |
session["account"] = answer.strip()
|
387 |
default_item = "Token"
|
388 |
elif choice == "Token": |
389 |
default_item = "Token"
|
390 |
(code, answer) = d.inputbox( |
391 |
"Please provide your ~okeanos account authetication token:",
|
392 |
init=session["token"] if "token" in session else '', |
393 |
width=70)
|
394 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
395 |
continue
|
396 |
if len(answer) == 0 and "token" in session: |
397 |
del session["token"] |
398 |
else:
|
399 |
session["token"] = answer.strip()
|
400 |
default_item = "Upload"
|
401 |
elif choice == "Upload": |
402 |
if upload_image(session):
|
403 |
default_item = "Register"
|
404 |
else:
|
405 |
default_item = "Upload"
|
406 |
elif choice == "Register": |
407 |
if register_image(session):
|
408 |
return True |
409 |
else:
|
410 |
default_item = "Register"
|
411 |
|
412 |
|
413 |
def add_property(session): |
414 |
d = session['dialog']
|
415 |
|
416 |
while 1: |
417 |
(code, answer) = d.inputbox("Please provide a name for a new image"
|
418 |
" property:", width=INPUTBOX_WIDTH)
|
419 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
420 |
return False |
421 |
|
422 |
name = answer.strip() |
423 |
if len(name) == 0: |
424 |
d.msgbox("A property name cannot be empty", width=MSGBOX_WIDTH)
|
425 |
continue
|
426 |
|
427 |
break
|
428 |
|
429 |
while 1: |
430 |
(code, answer) = d.inputbox("Please provide a value for image "
|
431 |
"property %s" % name, width=INPUTBOX_WIDTH)
|
432 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
433 |
return False |
434 |
|
435 |
value = answer.strip() |
436 |
if len(value) == 0: |
437 |
d.msgbox("Value cannot be empty", width=MSGBOX_WIDTH)
|
438 |
continue
|
439 |
|
440 |
break
|
441 |
|
442 |
session['metadata'][name] = value
|
443 |
|
444 |
return True |
445 |
|
446 |
|
447 |
def modify_properties(session): |
448 |
d = session['dialog']
|
449 |
|
450 |
while 1: |
451 |
choices = [] |
452 |
for (key, val) in session['metadata'].items(): |
453 |
choices.append((str(key), str(val))) |
454 |
|
455 |
(code, choice) = d.menu( |
456 |
"In this menu you can edit existing image properties or add new "
|
457 |
"ones. Be careful! Most properties have special meaning and "
|
458 |
"alter the image deployment behaviour. Press <HELP> to see more "
|
459 |
"information about image properties. Press <BACK> when done.",
|
460 |
height=18, width=MENU_WIDTH, choices=choices, menu_height=10, |
461 |
ok_label="Edit", extra_button=1, extra_label="Add", cancel="Back", |
462 |
help_button=1, title="Image Properties") |
463 |
|
464 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
465 |
break
|
466 |
# Edit button
|
467 |
elif code == d.DIALOG_OK:
|
468 |
(code, answer) = d.inputbox("Please provide a new value for "
|
469 |
"the image property with name `%s':" % choice,
|
470 |
init=session['metadata'][choice], width=INPUTBOX_WIDTH)
|
471 |
if code not in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
472 |
value = answer.strip() |
473 |
if len(value) == 0: |
474 |
d.msgbox("Value cannot be empty!")
|
475 |
continue
|
476 |
else:
|
477 |
session['metadata'][choice] = value
|
478 |
# ADD button
|
479 |
elif code == d.DIALOG_EXTRA:
|
480 |
add_property(session) |
481 |
|
482 |
|
483 |
def delete_properties(session): |
484 |
d = session['dialog']
|
485 |
|
486 |
choices = [] |
487 |
for (key, val) in session['metadata'].items(): |
488 |
choices.append((key, "%s" % val, 0)) |
489 |
|
490 |
(code, to_delete) = d.checklist("Choose which properties to delete:",
|
491 |
choices=choices, width=CHECKBOX_WIDTH) |
492 |
|
493 |
# If the user exits with ESC or CANCEL, the returned tag list is empty.
|
494 |
for i in to_delete: |
495 |
del session['metadata'][i] |
496 |
|
497 |
cnt = len(to_delete)
|
498 |
if cnt > 0: |
499 |
d.msgbox("%d image properties were deleted." % cnt, width=MSGBOX_WIDTH)
|
500 |
|
501 |
|
502 |
def exclude_tasks(session): |
503 |
d = session['dialog']
|
504 |
|
505 |
index = 0
|
506 |
displayed_index = 1
|
507 |
choices = [] |
508 |
mapping = {} |
509 |
if 'excluded_tasks' not in session: |
510 |
session['excluded_tasks'] = []
|
511 |
|
512 |
if -1 in session['excluded_tasks']: |
513 |
if not d.yesno("Image deployment configuration is disabled. " |
514 |
"Do you wish to enable it?", width=YESNO_WIDTH):
|
515 |
session['excluded_tasks'].remove(-1) |
516 |
else:
|
517 |
return
|
518 |
|
519 |
for (msg, task, osfamily) in CONFIGURATION_TASKS: |
520 |
if session['metadata']['OSFAMILY'] in osfamily: |
521 |
checked = 1 if index in session['excluded_tasks'] else 0 |
522 |
choices.append((str(displayed_index), msg, checked))
|
523 |
mapping[displayed_index] = index |
524 |
displayed_index += 1
|
525 |
index += 1
|
526 |
|
527 |
while 1: |
528 |
(code, tags) = d.checklist( |
529 |
text="Please choose which configuration tasks you would like to "
|
530 |
"prevent from running during image deployment. "
|
531 |
"Press <No Config> to supress any configuration. "
|
532 |
"Press <Help> for more help on the image deployment "
|
533 |
"configuration tasks.",
|
534 |
choices=choices, height=19, list_height=8, width=CHECKBOX_WIDTH, |
535 |
help_button=1, extra_button=1, extra_label="No Config", |
536 |
title="Exclude Configuration Tasks")
|
537 |
|
538 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
539 |
break
|
540 |
elif code == d.DIALOG_HELP:
|
541 |
help_file = get_help_file("configuration_tasks")
|
542 |
assert os.path.exists(help_file)
|
543 |
d.textbox(help_file, title="Configuration Tasks",
|
544 |
width=70, height=40) |
545 |
# No Config button
|
546 |
elif code == d.DIALOG_EXTRA:
|
547 |
session['excluded_tasks'] = [-1] |
548 |
session['task_metadata'] = ["EXCLUDE_ALL_TASKS"] |
549 |
break
|
550 |
elif code == d.DIALOG_OK:
|
551 |
session['excluded_tasks'] = []
|
552 |
for tag in tags: |
553 |
session['excluded_tasks'].append(mapping[int(tag)]) |
554 |
|
555 |
exclude_metadata = [] |
556 |
for task in session['excluded_tasks']: |
557 |
exclude_metadata.extend(CONFIGURATION_TASKS[task][1])
|
558 |
|
559 |
session['task_metadata'] = \
|
560 |
map(lambda x: "EXCLUDE_TASK_%s" % x, exclude_metadata) |
561 |
break
|
562 |
|
563 |
|
564 |
def sysprep(session): |
565 |
d = session['dialog']
|
566 |
image_os = session['image_os']
|
567 |
|
568 |
# Is the image already shrinked?
|
569 |
if 'shrinked' in session and session['shrinked'] == True: |
570 |
msg = "It seems you have shrinked the image. Running system " \
|
571 |
"preparation tasks on a shrinked image is dangerous."
|
572 |
|
573 |
if d.yesno("%s\n\nDo you really want to continue?" % msg, |
574 |
width=YESNO_WIDTH, defaultno=1):
|
575 |
return
|
576 |
|
577 |
wrapper = textwrap.TextWrapper(width=65)
|
578 |
|
579 |
help_title = "System Preperation Tasks"
|
580 |
sysprep_help = "%s\n%s\n\n" % (help_title, '=' * len(help_title)) |
581 |
|
582 |
if 'exec_syspreps' not in session: |
583 |
session['exec_syspreps'] = []
|
584 |
|
585 |
all_syspreps = image_os.list_syspreps() |
586 |
# Only give the user the choice between syspreps that have not ran yet
|
587 |
syspreps = [s for s in all_syspreps if s not in session['exec_syspreps']] |
588 |
|
589 |
if len(syspreps) == 0: |
590 |
d.msgbox("No system preparation task left to run!", width=MSGBOX_WIDTH)
|
591 |
return
|
592 |
|
593 |
while 1: |
594 |
choices = [] |
595 |
index = 0
|
596 |
for sysprep in syspreps: |
597 |
name, descr = image_os.sysprep_info(sysprep) |
598 |
display_name = name.replace('-', ' ').capitalize() |
599 |
sysprep_help += "%s\n" % display_name
|
600 |
sysprep_help += "%s\n" % ('-' * len(display_name)) |
601 |
sysprep_help += "%s\n\n" % wrapper.fill(" ".join(descr.split())) |
602 |
enabled = 1 if sysprep.enabled else 0 |
603 |
choices.append((str(index + 1), display_name, enabled)) |
604 |
index += 1
|
605 |
|
606 |
(code, tags) = d.checklist( |
607 |
"Please choose which system preperation tasks you would like to "
|
608 |
"run on the image. Press <Help> to see details about the system "
|
609 |
"preperation tasks.", title="Run system preperation tasks", |
610 |
choices=choices, width=70, ok_label="Run", help_button=1) |
611 |
|
612 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
613 |
break
|
614 |
elif code == d.DIALOG_HELP:
|
615 |
d.scrollbox(sysprep_help, width=HELP_WIDTH) |
616 |
elif code == d.DIALOG_OK:
|
617 |
# Enable selected syspreps and disable the rest
|
618 |
for i in range(len(syspreps)): |
619 |
if str(i + 1) in tags: |
620 |
image_os.enable_sysprep(syspreps[i]) |
621 |
session['exec_syspreps'].append(syspreps[i])
|
622 |
else:
|
623 |
image_os.disable_sysprep(syspreps[i]) |
624 |
|
625 |
out = InfoBoxOutput(d, "Image Configuration")
|
626 |
try:
|
627 |
dev = session['device']
|
628 |
dev.out = out |
629 |
dev.mount(readonly=False)
|
630 |
try:
|
631 |
# The checksum is invalid. We have mounted the image rw
|
632 |
if 'checksum' in session: |
633 |
del session['checksum'] |
634 |
|
635 |
# Monitor the metadata changes during syspreps
|
636 |
with metadata_monitor(session, image_os.meta):
|
637 |
image_os.out = out |
638 |
image_os.do_sysprep() |
639 |
image_os.out.finalize() |
640 |
|
641 |
# Disable syspreps that have ran
|
642 |
for sysprep in session['exec_syspreps']: |
643 |
image_os.disable_sysprep(sysprep) |
644 |
|
645 |
finally:
|
646 |
dev.umount() |
647 |
finally:
|
648 |
out.cleanup() |
649 |
break
|
650 |
|
651 |
|
652 |
def shrink(session): |
653 |
d = session['dialog']
|
654 |
dev = session['device']
|
655 |
|
656 |
shrinked = 'shrinked' in session and session['shrinked'] == True |
657 |
|
658 |
if shrinked:
|
659 |
d.msgbox("You have already shrinked your image!")
|
660 |
return
|
661 |
|
662 |
msg = "This operation will shrink the last partition of the image to " \
|
663 |
"reduce the total image size. If the last partition is a swap " \
|
664 |
"partition, then this partition is removed and the partition " \
|
665 |
"before that is shrinked. The removed swap partition will be " \
|
666 |
"recreated during image deployment."
|
667 |
|
668 |
if not d.yesno("%s\n\nDo you want to continue?" % msg, width=70, |
669 |
height=12, title="Image Shrinking"): |
670 |
|
671 |
with metadata_monitor(session, dev.meta):
|
672 |
dev.out = InfoBoxOutput(d, "Image Shrinking", height=3) |
673 |
dev.shrink() |
674 |
dev.out.finalize() |
675 |
|
676 |
session['shrinked'] = True |
677 |
update_background_title(session) |
678 |
|
679 |
|
680 |
def customization_menu(session): |
681 |
d = session['dialog']
|
682 |
|
683 |
choices = [("Sysprep", "Run various image preperation tasks"), |
684 |
("Shrink", "Shrink image"), |
685 |
("View/Modify", "View/Modify image properties"), |
686 |
("Delete", "Delete image properties"), |
687 |
("Exclude", "Exclude various deployment tasks from running")] |
688 |
|
689 |
default_item = "Sysprep"
|
690 |
|
691 |
actions = {"Sysprep": sysprep,
|
692 |
"Shrink": shrink,
|
693 |
"View/Modify": modify_properties,
|
694 |
"Delete": delete_properties,
|
695 |
"Exclude": exclude_tasks}
|
696 |
while 1: |
697 |
(code, choice) = d.menu( |
698 |
text="Choose one of the following or press <Back> to exit.",
|
699 |
width=MENU_WIDTH, choices=choices, cancel="Back", height=13, |
700 |
menu_height=len(choices), default_item=default_item,
|
701 |
title="Image Customization Menu")
|
702 |
|
703 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
704 |
break
|
705 |
elif choice in actions: |
706 |
default_item = choice |
707 |
actions[choice](session) |
708 |
|
709 |
|
710 |
def main_menu(session): |
711 |
d = session['dialog']
|
712 |
dev = session['device']
|
713 |
|
714 |
update_background_title(session) |
715 |
|
716 |
choices = [("Customize", "Customize image & ~okeanos deployment options"), |
717 |
("Register", "Register image to ~okeanos"), |
718 |
("Extract", "Dump image to local file system"), |
719 |
("Reset", "Reset everything and start over again"), |
720 |
("Help", "Get help for using snf-image-creator")] |
721 |
|
722 |
default_item = "Customize"
|
723 |
|
724 |
actions = {"Customize": customization_menu, "Register": kamaki_menu, |
725 |
"Extract": extract_image}
|
726 |
while 1: |
727 |
(code, choice) = d.menu( |
728 |
text="Choose one of the following or press <Exit> to exit.",
|
729 |
width=MENU_WIDTH, choices=choices, cancel="Exit", height=13, |
730 |
default_item=default_item, menu_height=len(choices),
|
731 |
title="Image Creator for ~okeanos (snf-image-creator version %s)" %
|
732 |
version) |
733 |
|
734 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
735 |
if confirm_exit(d):
|
736 |
break
|
737 |
elif choice == "Reset": |
738 |
if confirm_reset(d):
|
739 |
d.infobox("Resetting snf-image-creator. Please wait...",
|
740 |
width=INFOBOX_WIDTH) |
741 |
raise Reset
|
742 |
elif choice in actions: |
743 |
actions[choice](session) |
744 |
|
745 |
|
746 |
def select_file(d, media): |
747 |
root = os.sep |
748 |
while 1: |
749 |
if media is not None: |
750 |
if not os.path.exists(media): |
751 |
d.msgbox("The file you choose does not exist",
|
752 |
width=MSGBOX_WIDTH) |
753 |
else:
|
754 |
break
|
755 |
|
756 |
(code, media) = d.fselect(root, 10, 50, |
757 |
title="Please select input media")
|
758 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
759 |
if confirm_exit(d, "You canceled the media selection dialog box."): |
760 |
sys.exit(0)
|
761 |
else:
|
762 |
media = None
|
763 |
continue
|
764 |
|
765 |
return media
|
766 |
|
767 |
|
768 |
def image_creator(d): |
769 |
basename = os.path.basename(sys.argv[0])
|
770 |
usage = "Usage: %s [input_media]" % basename
|
771 |
if len(sys.argv) > 2: |
772 |
sys.stderr.write("%s\n" % usage)
|
773 |
return 1 |
774 |
|
775 |
d.setBackgroundTitle('snf-image-creator')
|
776 |
|
777 |
if os.geteuid() != 0: |
778 |
raise FatalError("You must run %s as root" % basename) |
779 |
|
780 |
media = select_file(d, sys.argv[1] if len(sys.argv) == 2 else None) |
781 |
|
782 |
out = GaugeOutput(d, "Initialization", "Initializing...") |
783 |
disk = Disk(media, out) |
784 |
|
785 |
def signal_handler(signum, frame): |
786 |
out.cleanup() |
787 |
disk.cleanup() |
788 |
|
789 |
signal.signal(signal.SIGINT, signal_handler) |
790 |
try:
|
791 |
snapshot = disk.snapshot() |
792 |
dev = disk.get_device(snapshot) |
793 |
|
794 |
out.output("Collecting image metadata...")
|
795 |
|
796 |
metadata = {} |
797 |
for (key, value) in dev.meta.items(): |
798 |
metadata[str(key)] = str(value) |
799 |
|
800 |
dev.mount(readonly=True)
|
801 |
cls = os_cls(dev.distro, dev.ostype) |
802 |
image_os = cls(dev.root, dev.g, out) |
803 |
dev.umount() |
804 |
|
805 |
for (key, value) in image_os.meta.items(): |
806 |
metadata[str(key)] = str(value) |
807 |
|
808 |
out.success("done")
|
809 |
out.cleanup() |
810 |
|
811 |
# Make sure the signal handler does not call out.cleanup again
|
812 |
def dummy(self): |
813 |
pass
|
814 |
out.cleanup = type(GaugeOutput.cleanup)(dummy, out, GaugeOutput)
|
815 |
|
816 |
session = {"dialog": d,
|
817 |
"disk": disk,
|
818 |
"snapshot": snapshot,
|
819 |
"device": dev,
|
820 |
"image_os": image_os,
|
821 |
"metadata": metadata}
|
822 |
|
823 |
main_menu(session) |
824 |
d.infobox("Thank you for using snf-image-creator. Bye", width=53) |
825 |
finally:
|
826 |
disk.cleanup() |
827 |
|
828 |
return 0 |
829 |
|
830 |
|
831 |
def main(): |
832 |
|
833 |
d = dialog.Dialog(dialog="dialog")
|
834 |
|
835 |
# Add extra button in dialog library
|
836 |
dialog._common_args_syntax["extra_button"] = \
|
837 |
lambda enable: dialog._simple_option("--extra-button", enable) |
838 |
|
839 |
dialog._common_args_syntax["extra_label"] = \
|
840 |
lambda string: ("--extra-label", string) |
841 |
|
842 |
while 1: |
843 |
try:
|
844 |
try:
|
845 |
ret = image_creator(d) |
846 |
sys.exit(ret) |
847 |
except FatalError as e: |
848 |
msg = textwrap.fill(str(e), width=70) |
849 |
d.infobox(msg, width=INFOBOX_WIDTH, title="Fatal Error")
|
850 |
sys.exit(1)
|
851 |
except Reset:
|
852 |
continue
|
853 |
|
854 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
|