root / image_creator / dialog_menu.py @ fa65eda1
History | View | Annotate | Download (25 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 os |
37 |
import textwrap |
38 |
import StringIO |
39 |
|
40 |
from image_creator import __version__ as version |
41 |
from image_creator.util import MD5 |
42 |
from image_creator.output.dialog import GaugeOutput, InfoBoxOutput |
43 |
from image_creator.kamaki_wrapper import Kamaki, ClientError |
44 |
from image_creator.help import get_help_file |
45 |
from image_creator.dialog_util import SMALL_WIDTH, WIDTH, \ |
46 |
update_background_title, confirm_reset, confirm_exit, Reset, \ |
47 |
extract_image, extract_metadata_string |
48 |
|
49 |
CONFIGURATION_TASKS = [ |
50 |
("Partition table manipulation", ["FixPartitionTable"], |
51 |
["linux", "windows"]), |
52 |
("File system resize",
|
53 |
["FilesystemResizeUnmounted", "FilesystemResizeMounted"], |
54 |
["linux", "windows"]), |
55 |
("Swap partition configuration", ["AddSwap"], ["linux"]), |
56 |
("SSH keys removal", ["DeleteSSHKeys"], ["linux"]), |
57 |
("Temporal RDP disabling", ["DisableRemoteDesktopConnections"], |
58 |
["windows"]),
|
59 |
("SELinux relabeling at next boot", ["SELinuxAutorelabel"], ["linux"]), |
60 |
("Hostname/Computer Name assignment", ["AssignHostname"], |
61 |
["windows", "linux"]), |
62 |
("Password change", ["ChangePassword"], ["windows", "linux"]), |
63 |
("File injection", ["EnforcePersonality"], ["windows", "linux"]) |
64 |
] |
65 |
|
66 |
|
67 |
class MetadataMonitor(object): |
68 |
"""Monitors image metadata chages"""
|
69 |
def __init__(self, session, meta): |
70 |
self.session = session
|
71 |
self.meta = meta
|
72 |
|
73 |
def __enter__(self): |
74 |
self.old = {}
|
75 |
for (k, v) in self.meta.items(): |
76 |
self.old[k] = v
|
77 |
|
78 |
def __exit__(self, type, value, traceback): |
79 |
d = self.session['dialog'] |
80 |
|
81 |
altered = {} |
82 |
added = {} |
83 |
|
84 |
for (k, v) in self.meta.items(): |
85 |
if k not in self.old: |
86 |
added[k] = v |
87 |
elif self.old[k] != v: |
88 |
altered[k] = v |
89 |
|
90 |
if not (len(added) or len(altered)): |
91 |
return
|
92 |
|
93 |
msg = "The last action has changed some image properties:\n\n"
|
94 |
if len(added): |
95 |
msg += "New image properties:\n"
|
96 |
for (k, v) in added.items(): |
97 |
msg += ' %s: "%s"\n' % (k, v)
|
98 |
msg += "\n"
|
99 |
if len(altered): |
100 |
msg += "Updated image properties:\n"
|
101 |
for (k, v) in altered.items(): |
102 |
msg += ' %s: "%s" -> "%s"\n' % (k, self.old[k], v) |
103 |
msg += "\n"
|
104 |
|
105 |
self.session['metadata'].update(added) |
106 |
self.session['metadata'].update(altered) |
107 |
d.msgbox(msg, title="Image Property Changes", width=SMALL_WIDTH)
|
108 |
|
109 |
|
110 |
def upload_image(session): |
111 |
"""Upload the image to pithos+"""
|
112 |
d = session["dialog"]
|
113 |
image = session['image']
|
114 |
meta = session['metadata']
|
115 |
size = image.size |
116 |
|
117 |
if "account" not in session: |
118 |
d.msgbox("You need to provide your ~okeanos credentials before you "
|
119 |
"can upload images to pithos+", width=SMALL_WIDTH)
|
120 |
return False |
121 |
|
122 |
while 1: |
123 |
if 'upload' in session: |
124 |
init = session['upload']
|
125 |
elif 'OS' in meta: |
126 |
init = "%s.diskdump" % meta['OS'] |
127 |
else:
|
128 |
init = ""
|
129 |
(code, answer) = d.inputbox("Please provide a filename:", init=init,
|
130 |
width=WIDTH) |
131 |
|
132 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
133 |
return False |
134 |
|
135 |
filename = answer.strip() |
136 |
if len(filename) == 0: |
137 |
d.msgbox("Filename cannot be empty", width=SMALL_WIDTH)
|
138 |
continue
|
139 |
session['upload'] = filename
|
140 |
break
|
141 |
|
142 |
gauge = GaugeOutput(d, "Image Upload", "Uploading...") |
143 |
try:
|
144 |
out = image.out |
145 |
out.add(gauge) |
146 |
try:
|
147 |
if 'checksum' not in session: |
148 |
md5 = MD5(out) |
149 |
session['checksum'] = md5.compute(image.device, size)
|
150 |
|
151 |
kamaki = Kamaki(session['account'], out)
|
152 |
try:
|
153 |
# Upload image file
|
154 |
with open(image.device, 'rb') as f: |
155 |
session["pithos_uri"] = \
|
156 |
kamaki.upload(f, size, filename, |
157 |
"Calculating block hashes",
|
158 |
"Uploading missing blocks")
|
159 |
# Upload metadata file
|
160 |
out.output("Uploading metadata file...")
|
161 |
metastring = extract_metadata_string(session) |
162 |
kamaki.upload(StringIO.StringIO(metastring), |
163 |
size=len(metastring),
|
164 |
remote_path="%s.meta" % filename)
|
165 |
out.success("done")
|
166 |
|
167 |
# Upload md5sum file
|
168 |
out.output("Uploading md5sum file...")
|
169 |
md5str = "%s %s\n" % (session['checksum'], filename) |
170 |
kamaki.upload(StringIO.StringIO(md5str), size=len(md5str),
|
171 |
remote_path="%s.md5sum" % filename)
|
172 |
out.success("done")
|
173 |
|
174 |
except ClientError as e: |
175 |
d.msgbox("Error in pithos+ client: %s" % e.message,
|
176 |
title="Pithos+ Client Error", width=SMALL_WIDTH)
|
177 |
if 'pithos_uri' in session: |
178 |
del session['pithos_uri'] |
179 |
return False |
180 |
finally:
|
181 |
out.remove(gauge) |
182 |
finally:
|
183 |
gauge.cleanup() |
184 |
|
185 |
d.msgbox("Image file `%s' was successfully uploaded to pithos+" % filename,
|
186 |
width=SMALL_WIDTH) |
187 |
|
188 |
return True |
189 |
|
190 |
|
191 |
def register_image(session): |
192 |
"""Register image with cyclades"""
|
193 |
d = session["dialog"]
|
194 |
|
195 |
is_public = False
|
196 |
|
197 |
if "account" not in session: |
198 |
d.msgbox("You need to provide your ~okeanos credentians before you "
|
199 |
"can register an images with cyclades", width=SMALL_WIDTH)
|
200 |
return False |
201 |
|
202 |
if "pithos_uri" not in session: |
203 |
d.msgbox("You need to upload the image to pithos+ before you can "
|
204 |
"register it with cyclades", width=SMALL_WIDTH)
|
205 |
return False |
206 |
|
207 |
while 1: |
208 |
(code, answer) = d.inputbox("Please provide a registration name:",
|
209 |
width=WIDTH) |
210 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
211 |
return False |
212 |
|
213 |
name = answer.strip() |
214 |
if len(name) == 0: |
215 |
d.msgbox("Registration name cannot be empty", width=SMALL_WIDTH)
|
216 |
continue
|
217 |
|
218 |
ret = d.yesno("Make the image public?\\nA public image is accessible "
|
219 |
"by every user of the service.", defaultno=1, |
220 |
width=WIDTH) |
221 |
if ret not in (0, 1): |
222 |
continue
|
223 |
|
224 |
is_public = True if ret == 0 else False |
225 |
|
226 |
break
|
227 |
|
228 |
metadata = {} |
229 |
metadata.update(session['metadata'])
|
230 |
if 'task_metadata' in session: |
231 |
for key in session['task_metadata']: |
232 |
metadata[key] = 'yes'
|
233 |
|
234 |
img_type = "public" if is_public else "private" |
235 |
gauge = GaugeOutput(d, "Image Registration", "Registering image...") |
236 |
try:
|
237 |
out = session['image'].out
|
238 |
out.add(gauge) |
239 |
try:
|
240 |
out.output("Registering %s image with Cyclades..." % img_type)
|
241 |
try:
|
242 |
kamaki = Kamaki(session['account'], out)
|
243 |
kamaki.register(name, session['pithos_uri'], metadata,
|
244 |
is_public) |
245 |
out.success('done')
|
246 |
except ClientError as e: |
247 |
d.msgbox("Error in pithos+ client: %s" % e.message)
|
248 |
return False |
249 |
finally:
|
250 |
out.remove(gauge) |
251 |
finally:
|
252 |
gauge.cleanup() |
253 |
|
254 |
d.msgbox("%s image `%s' was successfully registered with Cyclades as `%s'"
|
255 |
% (img_type.title(), session['upload'], name), width=SMALL_WIDTH)
|
256 |
return True |
257 |
|
258 |
|
259 |
def kamaki_menu(session): |
260 |
"""Show kamaki related actions"""
|
261 |
d = session['dialog']
|
262 |
default_item = "Account"
|
263 |
|
264 |
if 'account' not in session: |
265 |
token = Kamaki.get_token() |
266 |
if token:
|
267 |
session['account'] = Kamaki.get_account(token)
|
268 |
if not session['account']: |
269 |
del session['account'] |
270 |
Kamaki.save_token('') # The token was not valid. Remove it |
271 |
|
272 |
while 1: |
273 |
account = session["account"]['username'] if "account" in session else \ |
274 |
"<none>"
|
275 |
upload = session["upload"] if "upload" in session else "<none>" |
276 |
|
277 |
choices = [("Account", "Change your ~okeanos account: %s" % account), |
278 |
("Upload", "Upload image to pithos+"), |
279 |
("Register", "Register the image to cyclades: %s" % upload)] |
280 |
|
281 |
(code, choice) = d.menu( |
282 |
text="Choose one of the following or press <Back> to go back.",
|
283 |
width=WIDTH, choices=choices, cancel="Back", height=13, |
284 |
menu_height=5, default_item=default_item,
|
285 |
title="Image Registration Menu")
|
286 |
|
287 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
288 |
return False |
289 |
|
290 |
if choice == "Account": |
291 |
default_item = "Account"
|
292 |
(code, answer) = d.inputbox( |
293 |
"Please provide your ~okeanos authentication token:",
|
294 |
init=session["account"]['auth_token'] if "account" in session |
295 |
else '', width=WIDTH) |
296 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
297 |
continue
|
298 |
if len(answer) == 0 and "account" in session: |
299 |
del session["account"] |
300 |
else:
|
301 |
token = answer.strip() |
302 |
session['account'] = Kamaki.get_account(token)
|
303 |
if session['account'] is not None: |
304 |
Kamaki.save_token(token) |
305 |
default_item = "Upload"
|
306 |
else:
|
307 |
del session['account'] |
308 |
d.msgbox("The token you provided is not valid!",
|
309 |
width=SMALL_WIDTH) |
310 |
elif choice == "Upload": |
311 |
if upload_image(session):
|
312 |
default_item = "Register"
|
313 |
else:
|
314 |
default_item = "Upload"
|
315 |
elif choice == "Register": |
316 |
if register_image(session):
|
317 |
return True |
318 |
else:
|
319 |
default_item = "Register"
|
320 |
|
321 |
|
322 |
def add_property(session): |
323 |
"""Add a new property to the image"""
|
324 |
d = session['dialog']
|
325 |
|
326 |
while 1: |
327 |
(code, answer) = d.inputbox("Please provide a name for a new image"
|
328 |
" property:", width=WIDTH)
|
329 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
330 |
return False |
331 |
|
332 |
name = answer.strip() |
333 |
if len(name) == 0: |
334 |
d.msgbox("A property name cannot be empty", width=SMALL_WIDTH)
|
335 |
continue
|
336 |
|
337 |
break
|
338 |
|
339 |
while 1: |
340 |
(code, answer) = d.inputbox("Please provide a value for image "
|
341 |
"property %s" % name, width=WIDTH)
|
342 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
343 |
return False |
344 |
|
345 |
value = answer.strip() |
346 |
if len(value) == 0: |
347 |
d.msgbox("Value cannot be empty", width=SMALL_WIDTH)
|
348 |
continue
|
349 |
|
350 |
break
|
351 |
|
352 |
session['metadata'][name] = value
|
353 |
|
354 |
return True |
355 |
|
356 |
|
357 |
def modify_properties(session): |
358 |
"""Modify an existing image property"""
|
359 |
d = session['dialog']
|
360 |
|
361 |
while 1: |
362 |
choices = [] |
363 |
for (key, val) in session['metadata'].items(): |
364 |
choices.append((str(key), str(val))) |
365 |
|
366 |
(code, choice) = d.menu( |
367 |
"In this menu you can edit existing image properties or add new "
|
368 |
"ones. Be careful! Most properties have special meaning and "
|
369 |
"alter the image deployment behaviour. Press <HELP> to see more "
|
370 |
"information about image properties. Press <BACK> when done.",
|
371 |
height=18, width=WIDTH, choices=choices, menu_height=10, |
372 |
ok_label="Edit", extra_button=1, extra_label="Add", cancel="Back", |
373 |
help_button=1, title="Image Properties") |
374 |
|
375 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
376 |
return True |
377 |
# Edit button
|
378 |
elif code == d.DIALOG_OK:
|
379 |
(code, answer) = d.inputbox("Please provide a new value for the "
|
380 |
"image property with name `%s':" %
|
381 |
choice, |
382 |
init=session['metadata'][choice],
|
383 |
width=WIDTH) |
384 |
if code not in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
385 |
value = answer.strip() |
386 |
if len(value) == 0: |
387 |
d.msgbox("Value cannot be empty!")
|
388 |
continue
|
389 |
else:
|
390 |
session['metadata'][choice] = value
|
391 |
# ADD button
|
392 |
elif code == d.DIALOG_EXTRA:
|
393 |
add_property(session) |
394 |
elif code == 'help': |
395 |
help_file = get_help_file("image_properties")
|
396 |
assert os.path.exists(help_file)
|
397 |
d.textbox(help_file, title="Image Properties", width=70, height=40) |
398 |
|
399 |
|
400 |
def delete_properties(session): |
401 |
"""Delete an image property"""
|
402 |
d = session['dialog']
|
403 |
|
404 |
choices = [] |
405 |
for (key, val) in session['metadata'].items(): |
406 |
choices.append((key, "%s" % val, 0)) |
407 |
|
408 |
(code, to_delete) = d.checklist("Choose which properties to delete:",
|
409 |
choices=choices, width=WIDTH) |
410 |
|
411 |
# If the user exits with ESC or CANCEL, the returned tag list is empty.
|
412 |
for i in to_delete: |
413 |
del session['metadata'][i] |
414 |
|
415 |
cnt = len(to_delete)
|
416 |
if cnt > 0: |
417 |
d.msgbox("%d image properties were deleted." % cnt, width=SMALL_WIDTH)
|
418 |
return True |
419 |
else:
|
420 |
return False |
421 |
|
422 |
|
423 |
def exclude_tasks(session): |
424 |
"""Exclude specific tasks from running during image deployment"""
|
425 |
d = session['dialog']
|
426 |
|
427 |
index = 0
|
428 |
displayed_index = 1
|
429 |
choices = [] |
430 |
mapping = {} |
431 |
if 'excluded_tasks' not in session: |
432 |
session['excluded_tasks'] = []
|
433 |
|
434 |
if -1 in session['excluded_tasks']: |
435 |
if not d.yesno("Image deployment configuration is disabled. " |
436 |
"Do you wish to enable it?", width=SMALL_WIDTH):
|
437 |
session['excluded_tasks'].remove(-1) |
438 |
else:
|
439 |
return False |
440 |
|
441 |
for (msg, task, osfamily) in CONFIGURATION_TASKS: |
442 |
if session['metadata']['OSFAMILY'] in osfamily: |
443 |
checked = 1 if index in session['excluded_tasks'] else 0 |
444 |
choices.append((str(displayed_index), msg, checked))
|
445 |
mapping[displayed_index] = index |
446 |
displayed_index += 1
|
447 |
index += 1
|
448 |
|
449 |
while 1: |
450 |
(code, tags) = d.checklist( |
451 |
text="Please choose which configuration tasks you would like to "
|
452 |
"prevent from running during image deployment. "
|
453 |
"Press <No Config> to supress any configuration. "
|
454 |
"Press <Help> for more help on the image deployment "
|
455 |
"configuration tasks.",
|
456 |
choices=choices, height=19, list_height=8, width=WIDTH, |
457 |
help_button=1, extra_button=1, extra_label="No Config", |
458 |
title="Exclude Configuration Tasks")
|
459 |
|
460 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
461 |
return False |
462 |
elif code == d.DIALOG_HELP:
|
463 |
help_file = get_help_file("configuration_tasks")
|
464 |
assert os.path.exists(help_file)
|
465 |
d.textbox(help_file, title="Configuration Tasks",
|
466 |
width=70, height=40) |
467 |
# No Config button
|
468 |
elif code == d.DIALOG_EXTRA:
|
469 |
session['excluded_tasks'] = [-1] |
470 |
session['task_metadata'] = ["EXCLUDE_ALL_TASKS"] |
471 |
break
|
472 |
elif code == d.DIALOG_OK:
|
473 |
session['excluded_tasks'] = []
|
474 |
for tag in tags: |
475 |
session['excluded_tasks'].append(mapping[int(tag)]) |
476 |
|
477 |
exclude_metadata = [] |
478 |
for task in session['excluded_tasks']: |
479 |
exclude_metadata.extend(CONFIGURATION_TASKS[task][1])
|
480 |
|
481 |
session['task_metadata'] = map(lambda x: "EXCLUDE_TASK_%s" % x, |
482 |
exclude_metadata) |
483 |
break
|
484 |
|
485 |
return True |
486 |
|
487 |
|
488 |
def sysprep(session): |
489 |
"""Perform various system preperation tasks on the image"""
|
490 |
d = session['dialog']
|
491 |
image = session['image']
|
492 |
|
493 |
# Is the image already shrinked?
|
494 |
if 'shrinked' in session and session['shrinked']: |
495 |
msg = "It seems you have shrinked the image. Running system " \
|
496 |
"preparation tasks on a shrinked image is dangerous."
|
497 |
|
498 |
if d.yesno("%s\n\nDo you really want to continue?" % msg, |
499 |
width=SMALL_WIDTH, defaultno=1):
|
500 |
return
|
501 |
|
502 |
wrapper = textwrap.TextWrapper(width=WIDTH - 5)
|
503 |
|
504 |
help_title = "System Preperation Tasks"
|
505 |
sysprep_help = "%s\n%s\n\n" % (help_title, '=' * len(help_title)) |
506 |
|
507 |
|
508 |
syspreps = image.os.list_syspreps() |
509 |
|
510 |
if len(syspreps) == 0: |
511 |
d.msgbox("No system preparation task available to run!",
|
512 |
title="System Preperation", width=SMALL_WIDTH)
|
513 |
return
|
514 |
|
515 |
while 1: |
516 |
choices = [] |
517 |
index = 0
|
518 |
for sysprep in syspreps: |
519 |
name, descr = image.os.sysprep_info(sysprep) |
520 |
display_name = name.replace('-', ' ').capitalize() |
521 |
sysprep_help += "%s\n" % display_name
|
522 |
sysprep_help += "%s\n" % ('-' * len(display_name)) |
523 |
sysprep_help += "%s\n\n" % wrapper.fill(" ".join(descr.split())) |
524 |
enabled = 1 if sysprep.enabled else 0 |
525 |
choices.append((str(index + 1), display_name, enabled)) |
526 |
index += 1
|
527 |
|
528 |
(code, tags) = d.checklist( |
529 |
"Please choose which system preparation tasks you would like to "
|
530 |
"run on the image. Press <Help> to see details about the system "
|
531 |
"preparation tasks.", title="Run system preparation tasks", |
532 |
choices=choices, width=70, ok_label="Run", help_button=1) |
533 |
|
534 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
535 |
return False |
536 |
elif code == d.DIALOG_HELP:
|
537 |
d.scrollbox(sysprep_help, width=WIDTH) |
538 |
elif code == d.DIALOG_OK:
|
539 |
# Enable selected syspreps and disable the rest
|
540 |
for i in range(len(syspreps)): |
541 |
if str(i + 1) in tags: |
542 |
image.os.enable_sysprep(syspreps[i]) |
543 |
else:
|
544 |
image.os.disable_sysprep(syspreps[i]) |
545 |
|
546 |
if len([s for s in image.os.list_syspreps() if s.enabled]) == 0: |
547 |
d.msgbox("No system preperation task is selected!",
|
548 |
title="System Preperation", width=SMALL_WIDTH)
|
549 |
continue
|
550 |
|
551 |
infobox = InfoBoxOutput(d, "Image Configuration")
|
552 |
try:
|
553 |
image.out.add(infobox) |
554 |
try:
|
555 |
image.mount(readonly=False)
|
556 |
try:
|
557 |
err = "Unable to execute the system preparation " \
|
558 |
"tasks. Couldn't mount the media%s."
|
559 |
if not image.mounted: |
560 |
d.msgbox(err % "", title="System Preperation", |
561 |
width=SMALL_WIDTH) |
562 |
return
|
563 |
elif image.mounted_ro:
|
564 |
d.msgbox( |
565 |
err % " read-write",title="System Preperation", |
566 |
width=SMALL_WIDTH |
567 |
) |
568 |
return
|
569 |
|
570 |
# The checksum is invalid. We have mounted the image rw
|
571 |
if 'checksum' in session: |
572 |
del session['checksum'] |
573 |
|
574 |
# Monitor the metadata changes during syspreps
|
575 |
with MetadataMonitor(session, image.os.meta):
|
576 |
image.os.do_sysprep() |
577 |
infobox.finalize() |
578 |
|
579 |
finally:
|
580 |
image.umount() |
581 |
finally:
|
582 |
image.out.remove(infobox) |
583 |
finally:
|
584 |
infobox.cleanup() |
585 |
break
|
586 |
return True |
587 |
|
588 |
|
589 |
def shrink(session): |
590 |
"""Shrink the image"""
|
591 |
d = session['dialog']
|
592 |
image = session['image']
|
593 |
|
594 |
shrinked = 'shrinked' in session and session['shrinked'] |
595 |
|
596 |
if shrinked:
|
597 |
d.msgbox("The image is already shrinked!", title="Image Shrinking", |
598 |
width=SMALL_WIDTH) |
599 |
return True |
600 |
|
601 |
msg = "This operation will shrink the last partition of the image to " \
|
602 |
"reduce the total image size. If the last partition is a swap " \
|
603 |
"partition, then this partition is removed and the partition " \
|
604 |
"before that is shrinked. The removed swap partition will be " \
|
605 |
"recreated during image deployment."
|
606 |
|
607 |
if not d.yesno("%s\n\nDo you want to continue?" % msg, width=WIDTH, |
608 |
height=12, title="Image Shrinking"): |
609 |
with MetadataMonitor(session, image.meta):
|
610 |
infobox = InfoBoxOutput(d, "Image Shrinking", height=4) |
611 |
image.out.add(infobox) |
612 |
try:
|
613 |
image.shrink() |
614 |
infobox.finalize() |
615 |
finally:
|
616 |
image.out.remove(infobox) |
617 |
|
618 |
session['shrinked'] = True |
619 |
update_background_title(session) |
620 |
else:
|
621 |
return False |
622 |
|
623 |
return True |
624 |
|
625 |
|
626 |
def customization_menu(session): |
627 |
"""Show image customization menu"""
|
628 |
d = session['dialog']
|
629 |
|
630 |
choices = [("Sysprep", "Run various image preparation tasks"), |
631 |
("Shrink", "Shrink image"), |
632 |
("View/Modify", "View/Modify image properties"), |
633 |
("Delete", "Delete image properties"), |
634 |
("Exclude", "Exclude various deployment tasks from running")] |
635 |
|
636 |
default_item = 0
|
637 |
|
638 |
actions = {"Sysprep": sysprep,
|
639 |
"Shrink": shrink,
|
640 |
"View/Modify": modify_properties,
|
641 |
"Delete": delete_properties,
|
642 |
"Exclude": exclude_tasks}
|
643 |
while 1: |
644 |
(code, choice) = d.menu( |
645 |
text="Choose one of the following or press <Back> to exit.",
|
646 |
width=WIDTH, choices=choices, cancel="Back", height=13, |
647 |
menu_height=len(choices), default_item=choices[default_item][0], |
648 |
title="Image Customization Menu")
|
649 |
|
650 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
651 |
break
|
652 |
elif choice in actions: |
653 |
default_item = [entry[0] for entry in choices].index(choice) |
654 |
if actions[choice](session):
|
655 |
default_item = (default_item + 1) % len(choices) |
656 |
|
657 |
|
658 |
def main_menu(session): |
659 |
"""Show the main menu of the program"""
|
660 |
d = session['dialog']
|
661 |
|
662 |
update_background_title(session) |
663 |
|
664 |
choices = [("Customize", "Customize image & ~okeanos deployment options"), |
665 |
("Register", "Register image to ~okeanos"), |
666 |
("Extract", "Dump image to local file system"), |
667 |
("Reset", "Reset everything and start over again"), |
668 |
("Help", "Get help for using snf-image-creator")] |
669 |
|
670 |
default_item = "Customize"
|
671 |
|
672 |
actions = {"Customize": customization_menu, "Register": kamaki_menu, |
673 |
"Extract": extract_image}
|
674 |
while 1: |
675 |
(code, choice) = d.menu( |
676 |
text="Choose one of the following or press <Exit> to exit.",
|
677 |
width=WIDTH, choices=choices, cancel="Exit", height=13, |
678 |
default_item=default_item, menu_height=len(choices),
|
679 |
title="Image Creator for ~okeanos (snf-image-creator version %s)" %
|
680 |
version) |
681 |
|
682 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
683 |
if confirm_exit(d):
|
684 |
break
|
685 |
elif choice == "Reset": |
686 |
if confirm_reset(d):
|
687 |
d.infobox("Resetting snf-image-creator. Please wait...",
|
688 |
width=SMALL_WIDTH) |
689 |
raise Reset
|
690 |
elif choice == "Help": |
691 |
d.msgbox("For help, check the online documentation:\n\nhttp://www"
|
692 |
".synnefo.org/docs/snf-image-creator/latest/",
|
693 |
width=WIDTH, title="Help")
|
694 |
elif choice in actions: |
695 |
actions[choice](session) |
696 |
|
697 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
|