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
45 from image_creator import __version__ as version
46 from image_creator.util import MD5, FatalError
47 from image_creator.output.dialog import GaugeOutput, InfoBoxOutput
48 from image_creator.kamaki_wrapper import Kamaki, ClientError
49 from image_creator.help import get_help_file
50 from image_creator.dialog_util import SMALL_WIDTH, WIDTH, \
51 update_background_title, confirm_reset, confirm_exit, Reset, \
52 extract_image, extract_metadata_string, add_cloud, edit_cloud
54 CONFIGURATION_TASKS = [
55 ("Partition table manipulation", ["FixPartitionTable"],
56 ["linux", "windows"]),
57 ("File system resize",
58 ["FilesystemResizeUnmounted", "FilesystemResizeMounted"],
59 ["linux", "windows"]),
60 ("Swap partition configuration", ["AddSwap"], ["linux"]),
61 ("SSH keys removal", ["DeleteSSHKeys"], ["linux"]),
62 ("Temporal RDP disabling", ["DisableRemoteDesktopConnections"],
64 ("SELinux relabeling at next boot", ["SELinuxAutorelabel"], ["linux"]),
65 ("Hostname/Computer Name assignment", ["AssignHostname"],
66 ["windows", "linux"]),
67 ("Password change", ["ChangePassword"], ["windows", "linux"]),
68 ("File injection", ["EnforcePersonality"], ["windows", "linux"])
72 class MetadataMonitor(object):
73 """Monitors image metadata chages"""
74 def __init__(self, session, meta):
75 self.session = session
80 for (k, v) in self.meta.items():
83 def __exit__(self, type, value, traceback):
84 d = self.session['dialog']
89 for (k, v) in self.meta.items():
92 elif self.old[k] != v:
95 if not (len(added) or len(altered)):
98 msg = "The last action has changed some image properties:\n\n"
100 msg += "New image properties:\n"
101 for (k, v) in added.items():
102 msg += ' %s: "%s"\n' % (k, v)
105 msg += "Updated image properties:\n"
106 for (k, v) in altered.items():
107 msg += ' %s: "%s" -> "%s"\n' % (k, self.old[k], v)
110 self.session['metadata'].update(added)
111 self.session['metadata'].update(altered)
112 d.msgbox(msg, title="Image Property Changes", width=SMALL_WIDTH)
115 def upload_image(session):
116 """Upload the image to pithos+"""
117 d = session["dialog"]
118 image = session['image']
119 meta = session['metadata']
122 if "account" not in session:
123 d.msgbox("You need to select a valid cloud before you can upload "
124 "images to pithos+", width=SMALL_WIDTH)
128 if 'upload' in session:
129 init = session['upload']
131 init = "%s.diskdump" % meta['OS']
134 (code, answer) = d.inputbox("Please provide a filename:", init=init,
137 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
140 filename = answer.strip()
141 if len(filename) == 0:
142 d.msgbox("Filename cannot be empty", width=SMALL_WIDTH)
145 kamaki = Kamaki(session['account'], None)
147 for f in (filename, "%s.md5sum" % filename, "%s.meta" % filename):
148 if kamaki.object_exists(f):
151 if len(overwrite) > 0:
152 if d.yesno("The following pithos object(s) already exist(s):\n"
153 "%s\nDo you want to overwrite them?" %
154 "\n".join(overwrite), width=WIDTH, defaultno=1):
157 session['upload'] = filename
160 gauge = GaugeOutput(d, "Image Upload", "Uploading...")
166 if 'checksum' not in session:
168 session['checksum'] = md5.compute(image.device, size)
172 with open(image.device, 'rb') as f:
173 session["pithos_uri"] = \
174 kamaki.upload(f, size, filename,
175 "Calculating block hashes",
176 "Uploading missing blocks")
178 out.output("Uploading md5sum file...")
179 md5str = "%s %s\n" % (session['checksum'], filename)
180 kamaki.upload(StringIO.StringIO(md5str), size=len(md5str),
181 remote_path="%s.md5sum" % filename)
184 except ClientError as e:
185 d.msgbox("Error in pithos+ client: %s" % e.message,
186 title="Pithos+ Client Error", width=SMALL_WIDTH)
187 if 'pithos_uri' in session:
188 del session['pithos_uri']
195 d.msgbox("Image file `%s' was successfully uploaded to pithos+" % filename,
201 def register_image(session):
202 """Register image with cyclades"""
203 d = session["dialog"]
207 if "account" not in session:
208 d.msgbox("You need to select a valid cloud before you "
209 "can register an images with cyclades", width=SMALL_WIDTH)
212 if "pithos_uri" not in session:
213 d.msgbox("You need to upload the image to pithos+ before you can "
214 "register it with cyclades", width=SMALL_WIDTH)
218 (code, answer) = d.inputbox("Please provide a registration name:",
220 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
223 name = answer.strip()
225 d.msgbox("Registration name cannot be empty", width=SMALL_WIDTH)
228 ret = d.yesno("Make the image public?\\nA public image is accessible "
229 "by every user of the service.", defaultno=1,
231 if ret not in (0, 1):
234 is_public = True if ret == 0 else False
239 metadata.update(session['metadata'])
240 if 'task_metadata' in session:
241 for key in session['task_metadata']:
242 metadata[key] = 'yes'
244 img_type = "public" if is_public else "private"
245 gauge = GaugeOutput(d, "Image Registration", "Registering image...")
247 out = session['image'].out
251 out.output("Registering %s image with Cyclades..." % img_type)
252 kamaki = Kamaki(session['account'], out)
253 result = kamaki.register(name, session['pithos_uri'], metadata,
256 # Upload metadata file
257 out.output("Uploading metadata file...")
258 metastring = unicode(json.dumps(result, ensure_ascii=False))
259 kamaki.upload(StringIO.StringIO(metastring),
260 size=len(metastring),
261 remote_path="%s.meta" % session['upload'])
264 out.output("Sharing metadata and md5sum files...")
265 kamaki.share("%s.meta" % session['upload'])
266 kamaki.share("%s.md5sum" % session['upload'])
268 except ClientError as e:
269 d.msgbox("Error in pithos+ client: %s" % e.message)
276 d.msgbox("%s image `%s' was successfully registered with Cyclades as `%s'"
277 % (img_type.title(), session['upload'], name), width=SMALL_WIDTH)
281 def modify_clouds(session):
282 """Modify existing cloud accounts"""
283 d = session['dialog']
286 clouds = Kamaki.get_clouds()
288 if not add_cloud(session):
293 for (name, cloud) in clouds.items():
294 descr = cloud['description'] if 'description' in cloud else ''
295 choices.append((name, descr))
297 (code, choice) = d.menu(
298 "In this menu you can edit existing cloud accounts or add new "
299 " ones. Press <Edit> to edit an existing account or <Add> to add "
300 " a new one. Press <Back> or hit <ESC> when done.", height=18,
301 width=WIDTH, choices=choices, menu_height=10, ok_label="Edit",
302 extra_button=1, extra_label="Add", cancel="Back", help_button=1,
305 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
307 elif code == d.DIALOG_OK: # Edit button
308 edit_cloud(session, choice)
309 elif code == d.DIALOG_EXTRA: # Add button
313 def delete_clouds(session):
314 """Delete existing cloud accounts"""
315 d = session['dialog']
318 for (name, cloud) in Kamaki.get_clouds().items():
319 descr = cloud['description'] if 'description' in cloud else ''
320 choices.append((name, descr, 0))
322 if len(choices) == 0:
323 d.msgbox("No available clouds to delete!", width=SMALL_WIDTH)
326 (code, to_delete) = d.checklist("Choose which cloud accounts to delete:",
327 choices=choices, width=WIDTH)
329 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
332 if not len(to_delete):
333 d.msgbox("Nothing selected!", width=SMALL_WIDTH)
336 if not d.yesno("Are you sure you want to remove the selected cloud "
337 "accounts?", width=WIDTH, defaultno=1):
339 Kamaki.remove_cloud(i)
340 if 'cloud' in session and session['cloud'] == i:
342 if 'account' in session:
343 del session['account']
347 d.msgbox("%d cloud accounts were deleted." % len(to_delete),
352 def kamaki_menu(session):
353 """Show kamaki related actions"""
354 d = session['dialog']
355 default_item = "Cloud"
357 if 'cloud' not in session:
358 cloud = Kamaki.get_default_cloud_name()
360 session['cloud'] = cloud
361 session['account'] = Kamaki.get_account(cloud)
362 if not session['account']:
363 del session['account']
365 default_item = "Add/Edit"
368 cloud = session["cloud"] if "cloud" in session else "<none>"
369 if 'account' not in session and 'cloud' in session:
370 cloud += " <invalid>"
372 upload = session["upload"] if "upload" in session else "<none>"
374 choices = [("Add/Edit", "Add/Edit cloud accounts"),
375 ("Delete", "Delete existing cloud accounts"),
376 ("Cloud", "Select cloud account to use: %s" % cloud),
377 ("Upload", "Upload image to pithos+"),
378 ("Register", "Register the image to cyclades: %s" % upload)]
380 (code, choice) = d.menu(
381 text="Choose one of the following or press <Back> to go back.",
382 width=WIDTH, choices=choices, cancel="Back", height=13,
383 menu_height=5, default_item=default_item,
384 title="Image Registration Menu")
386 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
389 if choice == "Add/Edit":
390 if modify_clouds(session):
391 default_item = "Cloud"
392 elif choice == "Delete":
393 if delete_clouds(session):
394 if len(Kamaki.get_clouds()):
395 default_item = "Cloud"
397 default_time = "Add/Edit"
399 default_time = "Delete"
400 elif choice == "Cloud":
401 default_item = "Cloud"
402 clouds = Kamaki.get_clouds()
404 d.msgbox("No clouds available. Please add a new cloud!",
406 default_item = "Add/Edit"
409 if 'cloud' not in session:
410 session['cloud'] = clouds.keys()[0]
413 for name, info in clouds.items():
414 default = 1 if session['cloud'] == name else 0
415 descr = info['description'] if 'description' in info else ""
416 choices.append((name, descr, default))
418 (code, answer) = d.radiolist("Please select a cloud:",
419 width=WIDTH, choices=choices)
420 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
423 session['account'] = Kamaki.get_account(answer)
425 if session['account'] is None: # invalid account
426 if not d.yesno("The cloud %s' is not valid! Would you "
427 "like to edit it?" % answer, width=WIDTH):
428 if edit_cloud(session, answer):
429 session['account'] = Kamaki.get_account(answer)
430 Kamaki.set_default_cloud(answer)
432 if session['account'] is not None:
433 session['cloud'] = answer
434 Kamaki.set_default_cloud(answer)
435 default_item = "Upload"
437 del session['account']
439 elif choice == "Upload":
440 if upload_image(session):
441 default_item = "Register"
443 default_item = "Upload"
444 elif choice == "Register":
445 if register_image(session):
448 default_item = "Register"
451 def add_property(session):
452 """Add a new property to the image"""
453 d = session['dialog']
456 (code, answer) = d.inputbox("Please provide a name for a new image"
457 " property:", width=WIDTH)
458 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
461 name = answer.strip()
463 d.msgbox("A property name cannot be empty", width=SMALL_WIDTH)
469 (code, answer) = d.inputbox("Please provide a value for image "
470 "property %s" % name, width=WIDTH)
471 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
474 value = answer.strip()
476 d.msgbox("Value cannot be empty", width=SMALL_WIDTH)
481 session['metadata'][name] = value
486 def modify_properties(session):
487 """Modify an existing image property"""
488 d = session['dialog']
492 for (key, val) in session['metadata'].items():
493 choices.append((str(key), str(val)))
495 (code, choice) = d.menu(
496 "In this menu you can edit existing image properties or add new "
497 "ones. Be careful! Most properties have special meaning and "
498 "alter the image deployment behaviour. Press <HELP> to see more "
499 "information about image properties. Press <BACK> when done.",
500 height=18, width=WIDTH, choices=choices, menu_height=10,
501 ok_label="Edit", extra_button=1, extra_label="Add", cancel="Back",
502 help_button=1, title="Image Properties")
504 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
507 elif code == d.DIALOG_OK:
508 (code, answer) = d.inputbox("Please provide a new value for the "
509 "image property with name `%s':" %
511 init=session['metadata'][choice],
513 if code not in (d.DIALOG_CANCEL, d.DIALOG_ESC):
514 value = answer.strip()
516 d.msgbox("Value cannot be empty!")
519 session['metadata'][choice] = value
521 elif code == d.DIALOG_EXTRA:
522 add_property(session)
524 help_file = get_help_file("image_properties")
525 assert os.path.exists(help_file)
526 d.textbox(help_file, title="Image Properties", width=70, height=40)
529 def delete_properties(session):
530 """Delete an image property"""
531 d = session['dialog']
534 for (key, val) in session['metadata'].items():
535 choices.append((key, "%s" % val, 0))
537 (code, to_delete) = d.checklist("Choose which properties to delete:",
538 choices=choices, width=WIDTH)
540 # If the user exits with ESC or CANCEL, the returned tag list is empty.
542 del session['metadata'][i]
546 d.msgbox("%d image properties were deleted." % cnt, width=SMALL_WIDTH)
552 def exclude_tasks(session):
553 """Exclude specific tasks from running during image deployment"""
554 d = session['dialog']
560 if 'excluded_tasks' not in session:
561 session['excluded_tasks'] = []
563 if -1 in session['excluded_tasks']:
564 if not d.yesno("Image deployment configuration is disabled. "
565 "Do you wish to enable it?", width=SMALL_WIDTH):
566 session['excluded_tasks'].remove(-1)
570 for (msg, task, osfamily) in CONFIGURATION_TASKS:
571 if session['metadata']['OSFAMILY'] in osfamily:
572 checked = 1 if index in session['excluded_tasks'] else 0
573 choices.append((str(displayed_index), msg, checked))
574 mapping[displayed_index] = index
579 (code, tags) = d.checklist(
580 text="Please choose which configuration tasks you would like to "
581 "prevent from running during image deployment. "
582 "Press <No Config> to supress any configuration. "
583 "Press <Help> for more help on the image deployment "
584 "configuration tasks.",
585 choices=choices, height=19, list_height=8, width=WIDTH,
586 help_button=1, extra_button=1, extra_label="No Config",
587 title="Exclude Configuration Tasks")
589 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
591 elif code == d.DIALOG_HELP:
592 help_file = get_help_file("configuration_tasks")
593 assert os.path.exists(help_file)
594 d.textbox(help_file, title="Configuration Tasks",
597 elif code == d.DIALOG_EXTRA:
598 session['excluded_tasks'] = [-1]
599 session['task_metadata'] = ["EXCLUDE_ALL_TASKS"]
601 elif code == d.DIALOG_OK:
602 session['excluded_tasks'] = []
604 session['excluded_tasks'].append(mapping[int(tag)])
606 exclude_metadata = []
607 for task in session['excluded_tasks']:
608 exclude_metadata.extend(CONFIGURATION_TASKS[task][1])
610 session['task_metadata'] = map(lambda x: "EXCLUDE_TASK_%s" % x,
617 def sysprep(session):
618 """Perform various system preperation tasks on the image"""
619 d = session['dialog']
620 image = session['image']
622 # Is the image already shrinked?
623 if 'shrinked' in session and session['shrinked']:
624 msg = "It seems you have shrinked the image. Running system " \
625 "preparation tasks on a shrinked image is dangerous."
627 if d.yesno("%s\n\nDo you really want to continue?" % msg,
628 width=SMALL_WIDTH, defaultno=1):
631 wrapper = textwrap.TextWrapper(width=WIDTH - 5)
633 help_title = "System Preperation Tasks"
634 sysprep_help = "%s\n%s\n\n" % (help_title, '=' * len(help_title))
636 syspreps = image.os.list_syspreps()
638 if len(syspreps) == 0:
639 d.msgbox("No system preparation task available to run!",
640 title="System Preperation", width=SMALL_WIDTH)
646 for sysprep in syspreps:
647 name, descr = image.os.sysprep_info(sysprep)
648 display_name = name.replace('-', ' ').capitalize()
649 sysprep_help += "%s\n" % display_name
650 sysprep_help += "%s\n" % ('-' * len(display_name))
651 sysprep_help += "%s\n\n" % wrapper.fill(" ".join(descr.split()))
652 enabled = 1 if sysprep.enabled else 0
653 choices.append((str(index + 1), display_name, enabled))
656 (code, tags) = d.checklist(
657 "Please choose which system preparation tasks you would like to "
658 "run on the image. Press <Help> to see details about the system "
659 "preparation tasks.", title="Run system preparation tasks",
660 choices=choices, width=70, ok_label="Run", help_button=1)
662 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
664 elif code == d.DIALOG_HELP:
665 d.scrollbox(sysprep_help, width=WIDTH)
666 elif code == d.DIALOG_OK:
667 # Enable selected syspreps and disable the rest
668 for i in range(len(syspreps)):
669 if str(i + 1) in tags:
670 image.os.enable_sysprep(syspreps[i])
672 image.os.disable_sysprep(syspreps[i])
674 if len([s for s in image.os.list_syspreps() if s.enabled]) == 0:
675 d.msgbox("No system preperation task is selected!",
676 title="System Preperation", width=SMALL_WIDTH)
679 infobox = InfoBoxOutput(d, "Image Configuration")
681 image.out.add(infobox)
683 # The checksum is invalid. We have mounted the image rw
684 if 'checksum' in session:
685 del session['checksum']
687 # Monitor the metadata changes during syspreps
688 with MetadataMonitor(session, image.os.meta):
690 image.os.do_sysprep()
692 except FatalError as e:
693 title = "System Preparation"
694 d.msgbox("System Preparation failed: %s" % e,
695 title=title, width=SMALL_WIDTH)
697 image.out.remove(infobox)
705 """Shrink the image"""
706 d = session['dialog']
707 image = session['image']
709 shrinked = 'shrinked' in session and session['shrinked']
712 d.msgbox("The image is already shrinked!", title="Image Shrinking",
716 msg = "This operation will shrink the last partition of the image to " \
717 "reduce the total image size. If the last partition is a swap " \
718 "partition, then this partition is removed and the partition " \
719 "before that is shrinked. The removed swap partition will be " \
720 "recreated during image deployment."
722 if not d.yesno("%s\n\nDo you want to continue?" % msg, width=WIDTH,
723 height=12, title="Image Shrinking"):
724 with MetadataMonitor(session, image.meta):
725 infobox = InfoBoxOutput(d, "Image Shrinking", height=4)
726 image.out.add(infobox)
731 image.out.remove(infobox)
733 session['shrinked'] = True
734 update_background_title(session)
741 def customization_menu(session):
742 """Show image customization menu"""
743 d = session['dialog']
745 choices = [("Sysprep", "Run various image preparation tasks"),
746 ("Shrink", "Shrink image"),
747 ("View/Modify", "View/Modify image properties"),
748 ("Delete", "Delete image properties"),
749 ("Exclude", "Exclude various deployment tasks from running")]
753 actions = {"Sysprep": sysprep,
755 "View/Modify": modify_properties,
756 "Delete": delete_properties,
757 "Exclude": exclude_tasks}
759 (code, choice) = d.menu(
760 text="Choose one of the following or press <Back> to exit.",
761 width=WIDTH, choices=choices, cancel="Back", height=13,
762 menu_height=len(choices), default_item=choices[default_item][0],
763 title="Image Customization Menu")
765 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
767 elif choice in actions:
768 default_item = [entry[0] for entry in choices].index(choice)
769 if actions[choice](session):
770 default_item = (default_item + 1) % len(choices)
773 def main_menu(session):
774 """Show the main menu of the program"""
775 d = session['dialog']
777 update_background_title(session)
779 choices = [("Customize", "Customize image & cloud deployment options"),
780 ("Register", "Register image to a cloud"),
781 ("Extract", "Dump image to local file system"),
782 ("Reset", "Reset everything and start over again"),
783 ("Help", "Get help for using snf-image-creator")]
785 default_item = "Customize"
787 actions = {"Customize": customization_menu, "Register": kamaki_menu,
788 "Extract": extract_image}
790 (code, choice) = d.menu(
791 text="Choose one of the following or press <Exit> to exit.",
792 width=WIDTH, choices=choices, cancel="Exit", height=13,
793 default_item=default_item, menu_height=len(choices),
794 title="Image Creator for ~okeanos (snf-image-creator version %s)" %
797 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
800 elif choice == "Reset":
802 d.infobox("Resetting snf-image-creator. Please wait...",
805 elif choice == "Help":
806 d.msgbox("For help, check the online documentation:\n\nhttp://www"
807 ".synnefo.org/docs/snf-image-creator/latest/",
808 width=WIDTH, title="Help")
809 elif choice in actions:
810 actions[choice](session)
812 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :