1 # -*- coding: utf-8 -*-
3 # Copyright 2012 GRNET S.A. All rights reserved.
5 # Redistribution and use in source and binary forms, with or
6 # without modification, are permitted provided that the following
9 # 1. Redistributions of source code must retain the above
10 # copyright notice, this list of conditions and the following
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.
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.
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.
36 """This module implements the "expert" mode of the dialog-based version of
44 from image_creator import __version__ as version
45 from image_creator.util import MD5, FatalError
46 from image_creator.output.dialog import GaugeOutput, InfoBoxOutput
47 from image_creator.kamaki_wrapper import Kamaki, ClientError
48 from image_creator.help import get_help_file
49 from image_creator.dialog_util import SMALL_WIDTH, WIDTH, \
50 update_background_title, confirm_reset, confirm_exit, Reset, \
51 extract_image, extract_metadata_string
53 CONFIGURATION_TASKS = [
54 ("Partition table manipulation", ["FixPartitionTable"],
55 ["linux", "windows"]),
56 ("File system resize",
57 ["FilesystemResizeUnmounted", "FilesystemResizeMounted"],
58 ["linux", "windows"]),
59 ("Swap partition configuration", ["AddSwap"], ["linux"]),
60 ("SSH keys removal", ["DeleteSSHKeys"], ["linux"]),
61 ("Temporal RDP disabling", ["DisableRemoteDesktopConnections"],
63 ("SELinux relabeling at next boot", ["SELinuxAutorelabel"], ["linux"]),
64 ("Hostname/Computer Name assignment", ["AssignHostname"],
65 ["windows", "linux"]),
66 ("Password change", ["ChangePassword"], ["windows", "linux"]),
67 ("File injection", ["EnforcePersonality"], ["windows", "linux"])
71 class MetadataMonitor(object):
72 """Monitors image metadata chages"""
73 def __init__(self, session, meta):
74 self.session = session
79 for (k, v) in self.meta.items():
82 def __exit__(self, type, value, traceback):
83 d = self.session['dialog']
88 for (k, v) in self.meta.items():
91 elif self.old[k] != v:
94 if not (len(added) or len(altered)):
97 msg = "The last action has changed some image properties:\n\n"
99 msg += "New image properties:\n"
100 for (k, v) in added.items():
101 msg += ' %s: "%s"\n' % (k, v)
104 msg += "Updated image properties:\n"
105 for (k, v) in altered.items():
106 msg += ' %s: "%s" -> "%s"\n' % (k, self.old[k], v)
109 self.session['metadata'].update(added)
110 self.session['metadata'].update(altered)
111 d.msgbox(msg, title="Image Property Changes", width=SMALL_WIDTH)
114 def upload_image(session):
115 """Upload the image to pithos+"""
116 d = session["dialog"]
117 image = session['image']
118 meta = session['metadata']
121 if "account" not in session:
122 d.msgbox("You need to provide your ~okeanos credentials before you "
123 "can upload images to pithos+", width=SMALL_WIDTH)
127 if 'upload' in session:
128 init = session['upload']
130 init = "%s.diskdump" % meta['OS']
133 (code, answer) = d.inputbox("Please provide a filename:", init=init,
136 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
139 filename = answer.strip()
140 if len(filename) == 0:
141 d.msgbox("Filename cannot be empty", width=SMALL_WIDTH)
144 kamaki = Kamaki(session['account'], None)
146 for f in (filename, "%s.md5sum" % filename, "%s.meta" % filename):
147 if kamaki.object_exists(f):
150 if len(overwrite) > 0:
151 if d.yesno("The following pithos object(s) already exist(s):\n"
152 "%s\nDo you want to overwrite them?" %
153 "\n".join(overwrite), width=WIDTH, defaultno=1):
156 session['upload'] = filename
159 gauge = GaugeOutput(d, "Image Upload", "Uploading...")
165 if 'checksum' not in session:
167 session['checksum'] = md5.compute(image.device, size)
171 with open(image.device, 'rb') as f:
172 session["pithos_uri"] = \
173 kamaki.upload(f, size, filename,
174 "Calculating block hashes",
175 "Uploading missing blocks")
177 out.output("Uploading md5sum file...")
178 md5str = "%s %s\n" % (session['checksum'], filename)
179 kamaki.upload(StringIO.StringIO(md5str), size=len(md5str),
180 remote_path="%s.md5sum" % filename)
183 except ClientError as e:
184 d.msgbox("Error in pithos+ client: %s" % e.message,
185 title="Pithos+ Client Error", width=SMALL_WIDTH)
186 if 'pithos_uri' in session:
187 del session['pithos_uri']
194 d.msgbox("Image file `%s' was successfully uploaded to pithos+" % filename,
200 def register_image(session):
201 """Register image with cyclades"""
202 d = session["dialog"]
206 if "account" not in session:
207 d.msgbox("You need to provide your ~okeanos credentians before you "
208 "can register an images with cyclades", width=SMALL_WIDTH)
211 if "pithos_uri" not in session:
212 d.msgbox("You need to upload the image to pithos+ before you can "
213 "register it with cyclades", width=SMALL_WIDTH)
217 (code, answer) = d.inputbox("Please provide a registration name:",
219 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
222 name = answer.strip()
224 d.msgbox("Registration name cannot be empty", width=SMALL_WIDTH)
227 ret = d.yesno("Make the image public?\\nA public image is accessible "
228 "by every user of the service.", defaultno=1,
230 if ret not in (0, 1):
233 is_public = True if ret == 0 else False
238 metadata.update(session['metadata'])
239 if 'task_metadata' in session:
240 for key in session['task_metadata']:
241 metadata[key] = 'yes'
243 img_type = "public" if is_public else "private"
244 gauge = GaugeOutput(d, "Image Registration", "Registering image...")
246 out = session['image'].out
250 out.output("Registering %s image with Cyclades..." % img_type)
251 kamaki = Kamaki(session['account'], out)
252 kamaki.register(name, session['pithos_uri'], metadata,
255 # Upload metadata file
256 out.output("Uploading metadata file...")
257 metastring = extract_metadata_string(session)
258 kamaki.upload(StringIO.StringIO(metastring),
259 size=len(metastring),
260 remote_path="%s.meta" % session['upload'])
263 out.output("Sharing metadata and md5sum files...")
264 kamaki.share("%s.meta" % session['upload'])
265 kamaki.share("%s.md5sum" % session['upload'])
267 except ClientError as e:
268 d.msgbox("Error in pithos+ client: %s" % e.message)
275 d.msgbox("%s image `%s' was successfully registered with Cyclades as `%s'"
276 % (img_type.title(), session['upload'], name), width=SMALL_WIDTH)
280 def kamaki_menu(session):
281 """Show kamaki related actions"""
282 d = session['dialog']
283 default_item = "Account"
285 if 'account' not in session:
286 token = Kamaki.get_token()
288 session['account'] = Kamaki.get_account(token)
289 if not session['account']:
290 del session['account']
291 Kamaki.save_token('') # The token was not valid. Remove it
294 account = session["account"]['username'] if "account" in session else \
296 upload = session["upload"] if "upload" in session else "<none>"
298 choices = [("Account", "Change your ~okeanos account: %s" % account),
299 ("Upload", "Upload image to pithos+"),
300 ("Register", "Register the image to cyclades: %s" % upload)]
302 (code, choice) = d.menu(
303 text="Choose one of the following or press <Back> to go back.",
304 width=WIDTH, choices=choices, cancel="Back", height=13,
305 menu_height=5, default_item=default_item,
306 title="Image Registration Menu")
308 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
311 if choice == "Account":
312 default_item = "Account"
313 (code, answer) = d.inputbox(
314 "Please provide your ~okeanos authentication token:",
315 init=session["account"]['auth_token'] if "account" in session
316 else '', width=WIDTH)
317 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
319 if len(answer) == 0 and "account" in session:
320 del session["account"]
322 token = answer.strip()
323 session['account'] = Kamaki.get_account(token)
324 if session['account'] is not None:
325 Kamaki.save_token(token)
326 default_item = "Upload"
328 del session['account']
329 d.msgbox("The token you provided is not valid!",
331 elif choice == "Upload":
332 if upload_image(session):
333 default_item = "Register"
335 default_item = "Upload"
336 elif choice == "Register":
337 if register_image(session):
340 default_item = "Register"
343 def add_property(session):
344 """Add a new property to the image"""
345 d = session['dialog']
348 (code, answer) = d.inputbox("Please provide a name for a new image"
349 " property:", width=WIDTH)
350 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
353 name = answer.strip()
355 d.msgbox("A property name cannot be empty", width=SMALL_WIDTH)
361 (code, answer) = d.inputbox("Please provide a value for image "
362 "property %s" % name, width=WIDTH)
363 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
366 value = answer.strip()
368 d.msgbox("Value cannot be empty", width=SMALL_WIDTH)
373 session['metadata'][name] = value
378 def modify_properties(session):
379 """Modify an existing image property"""
380 d = session['dialog']
384 for (key, val) in session['metadata'].items():
385 choices.append((str(key), str(val)))
387 (code, choice) = d.menu(
388 "In this menu you can edit existing image properties or add new "
389 "ones. Be careful! Most properties have special meaning and "
390 "alter the image deployment behaviour. Press <HELP> to see more "
391 "information about image properties. Press <BACK> when done.",
392 height=18, width=WIDTH, choices=choices, menu_height=10,
393 ok_label="Edit", extra_button=1, extra_label="Add", cancel="Back",
394 help_button=1, title="Image Properties")
396 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
399 elif code == d.DIALOG_OK:
400 (code, answer) = d.inputbox("Please provide a new value for the "
401 "image property with name `%s':" %
403 init=session['metadata'][choice],
405 if code not in (d.DIALOG_CANCEL, d.DIALOG_ESC):
406 value = answer.strip()
408 d.msgbox("Value cannot be empty!")
411 session['metadata'][choice] = value
413 elif code == d.DIALOG_EXTRA:
414 add_property(session)
416 help_file = get_help_file("image_properties")
417 assert os.path.exists(help_file)
418 d.textbox(help_file, title="Image Properties", width=70, height=40)
421 def delete_properties(session):
422 """Delete an image property"""
423 d = session['dialog']
426 for (key, val) in session['metadata'].items():
427 choices.append((key, "%s" % val, 0))
429 (code, to_delete) = d.checklist("Choose which properties to delete:",
430 choices=choices, width=WIDTH)
432 # If the user exits with ESC or CANCEL, the returned tag list is empty.
434 del session['metadata'][i]
438 d.msgbox("%d image properties were deleted." % cnt, width=SMALL_WIDTH)
444 def exclude_tasks(session):
445 """Exclude specific tasks from running during image deployment"""
446 d = session['dialog']
452 if 'excluded_tasks' not in session:
453 session['excluded_tasks'] = []
455 if -1 in session['excluded_tasks']:
456 if not d.yesno("Image deployment configuration is disabled. "
457 "Do you wish to enable it?", width=SMALL_WIDTH):
458 session['excluded_tasks'].remove(-1)
462 for (msg, task, osfamily) in CONFIGURATION_TASKS:
463 if session['metadata']['OSFAMILY'] in osfamily:
464 checked = 1 if index in session['excluded_tasks'] else 0
465 choices.append((str(displayed_index), msg, checked))
466 mapping[displayed_index] = index
471 (code, tags) = d.checklist(
472 text="Please choose which configuration tasks you would like to "
473 "prevent from running during image deployment. "
474 "Press <No Config> to supress any configuration. "
475 "Press <Help> for more help on the image deployment "
476 "configuration tasks.",
477 choices=choices, height=19, list_height=8, width=WIDTH,
478 help_button=1, extra_button=1, extra_label="No Config",
479 title="Exclude Configuration Tasks")
481 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
483 elif code == d.DIALOG_HELP:
484 help_file = get_help_file("configuration_tasks")
485 assert os.path.exists(help_file)
486 d.textbox(help_file, title="Configuration Tasks",
489 elif code == d.DIALOG_EXTRA:
490 session['excluded_tasks'] = [-1]
491 session['task_metadata'] = ["EXCLUDE_ALL_TASKS"]
493 elif code == d.DIALOG_OK:
494 session['excluded_tasks'] = []
496 session['excluded_tasks'].append(mapping[int(tag)])
498 exclude_metadata = []
499 for task in session['excluded_tasks']:
500 exclude_metadata.extend(CONFIGURATION_TASKS[task][1])
502 session['task_metadata'] = map(lambda x: "EXCLUDE_TASK_%s" % x,
509 def sysprep(session):
510 """Perform various system preperation tasks on the image"""
511 d = session['dialog']
512 image = session['image']
514 # Is the image already shrinked?
515 if 'shrinked' in session and session['shrinked']:
516 msg = "It seems you have shrinked the image. Running system " \
517 "preparation tasks on a shrinked image is dangerous."
519 if d.yesno("%s\n\nDo you really want to continue?" % msg,
520 width=SMALL_WIDTH, defaultno=1):
523 wrapper = textwrap.TextWrapper(width=WIDTH - 5)
525 help_title = "System Preperation Tasks"
526 sysprep_help = "%s\n%s\n\n" % (help_title, '=' * len(help_title))
528 syspreps = image.os.list_syspreps()
530 if len(syspreps) == 0:
531 d.msgbox("No system preparation task available to run!",
532 title="System Preperation", width=SMALL_WIDTH)
538 for sysprep in syspreps:
539 name, descr = image.os.sysprep_info(sysprep)
540 display_name = name.replace('-', ' ').capitalize()
541 sysprep_help += "%s\n" % display_name
542 sysprep_help += "%s\n" % ('-' * len(display_name))
543 sysprep_help += "%s\n\n" % wrapper.fill(" ".join(descr.split()))
544 enabled = 1 if sysprep.enabled else 0
545 choices.append((str(index + 1), display_name, enabled))
548 (code, tags) = d.checklist(
549 "Please choose which system preparation tasks you would like to "
550 "run on the image. Press <Help> to see details about the system "
551 "preparation tasks.", title="Run system preparation tasks",
552 choices=choices, width=70, ok_label="Run", help_button=1)
554 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
556 elif code == d.DIALOG_HELP:
557 d.scrollbox(sysprep_help, width=WIDTH)
558 elif code == d.DIALOG_OK:
559 # Enable selected syspreps and disable the rest
560 for i in range(len(syspreps)):
561 if str(i + 1) in tags:
562 image.os.enable_sysprep(syspreps[i])
564 image.os.disable_sysprep(syspreps[i])
566 if len([s for s in image.os.list_syspreps() if s.enabled]) == 0:
567 d.msgbox("No system preperation task is selected!",
568 title="System Preperation", width=SMALL_WIDTH)
571 infobox = InfoBoxOutput(d, "Image Configuration")
573 image.out.add(infobox)
575 # The checksum is invalid. We have mounted the image rw
576 if 'checksum' in session:
577 del session['checksum']
579 # Monitor the metadata changes during syspreps
580 with MetadataMonitor(session, image.os.meta):
582 image.os.do_sysprep()
584 except FatalError as e:
585 title = "System Preparation"
586 d.msgbox("System Preparation failed: %s" % e,
587 title=title, width=SMALL_WIDTH)
589 image.out.remove(infobox)
597 """Shrink the image"""
598 d = session['dialog']
599 image = session['image']
601 shrinked = 'shrinked' in session and session['shrinked']
604 d.msgbox("The image is already shrinked!", title="Image Shrinking",
608 msg = "This operation will shrink the last partition of the image to " \
609 "reduce the total image size. If the last partition is a swap " \
610 "partition, then this partition is removed and the partition " \
611 "before that is shrinked. The removed swap partition will be " \
612 "recreated during image deployment."
614 if not d.yesno("%s\n\nDo you want to continue?" % msg, width=WIDTH,
615 height=12, title="Image Shrinking"):
616 with MetadataMonitor(session, image.meta):
617 infobox = InfoBoxOutput(d, "Image Shrinking", height=4)
618 image.out.add(infobox)
623 image.out.remove(infobox)
625 session['shrinked'] = True
626 update_background_title(session)
633 def customization_menu(session):
634 """Show image customization menu"""
635 d = session['dialog']
637 choices = [("Sysprep", "Run various image preparation tasks"),
638 ("Shrink", "Shrink image"),
639 ("View/Modify", "View/Modify image properties"),
640 ("Delete", "Delete image properties"),
641 ("Exclude", "Exclude various deployment tasks from running")]
645 actions = {"Sysprep": sysprep,
647 "View/Modify": modify_properties,
648 "Delete": delete_properties,
649 "Exclude": exclude_tasks}
651 (code, choice) = d.menu(
652 text="Choose one of the following or press <Back> to exit.",
653 width=WIDTH, choices=choices, cancel="Back", height=13,
654 menu_height=len(choices), default_item=choices[default_item][0],
655 title="Image Customization Menu")
657 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
659 elif choice in actions:
660 default_item = [entry[0] for entry in choices].index(choice)
661 if actions[choice](session):
662 default_item = (default_item + 1) % len(choices)
665 def main_menu(session):
666 """Show the main menu of the program"""
667 d = session['dialog']
669 update_background_title(session)
671 choices = [("Customize", "Customize image & ~okeanos deployment options"),
672 ("Register", "Register image to ~okeanos"),
673 ("Extract", "Dump image to local file system"),
674 ("Reset", "Reset everything and start over again"),
675 ("Help", "Get help for using snf-image-creator")]
677 default_item = "Customize"
679 actions = {"Customize": customization_menu, "Register": kamaki_menu,
680 "Extract": extract_image}
682 (code, choice) = d.menu(
683 text="Choose one of the following or press <Exit> to exit.",
684 width=WIDTH, choices=choices, cancel="Exit", height=13,
685 default_item=default_item, menu_height=len(choices),
686 title="Image Creator for ~okeanos (snf-image-creator version %s)" %
689 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
692 elif choice == "Reset":
694 d.infobox("Resetting snf-image-creator. Please wait...",
697 elif choice == "Help":
698 d.msgbox("For help, check the online documentation:\n\nhttp://www"
699 ".synnefo.org/docs/snf-image-creator/latest/",
700 width=WIDTH, title="Help")
701 elif choice in actions:
702 actions[choice](session)
704 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :