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.
41 from image_creator import __version__ as version
42 from image_creator.util import MD5
43 from image_creator.output.dialog import GaugeOutput, InfoBoxOutput
44 from image_creator.kamaki_wrapper import Kamaki, ClientError
45 from image_creator.help import get_help_file
46 from image_creator.dialog_util import SMALL_WIDTH, WIDTH, \
47 update_background_title, confirm_reset, confirm_exit, Reset, \
48 extract_image, extract_metadata_string
50 CONFIGURATION_TASKS = [
51 ("Partition table manipulation", ["FixPartitionTable"],
52 ["linux", "windows"]),
53 ("File system resize",
54 ["FilesystemResizeUnmounted", "FilesystemResizeMounted"],
55 ["linux", "windows"]),
56 ("Swap partition configuration", ["AddSwap"], ["linux"]),
57 ("SSH keys removal", ["DeleteSSHKeys"], ["linux"]),
58 ("Temporal RDP disabling", ["DisableRemoteDesktopConnections"],
60 ("SELinux relabeling at next boot", ["SELinuxAutorelabel"], ["linux"]),
61 ("Hostname/Computer Name assignment", ["AssignHostname"],
62 ["windows", "linux"]),
63 ("Password change", ["ChangePassword"], ["windows", "linux"]),
64 ("File injection", ["EnforcePersonality"], ["windows", "linux"])
68 class metadata_monitor(object):
69 def __init__(self, session, meta):
70 self.session = session
75 for (k, v) in self.meta.items():
78 def __exit__(self, type, value, traceback):
79 d = self.session['dialog']
84 for (k, v) in self.meta.items():
87 elif self.old[k] != v:
90 if not (len(added) or len(altered)):
93 msg = "The last action has changed some image properties:\n\n"
95 msg += "New image properties:\n"
96 for (k, v) in added.items():
97 msg += ' %s: "%s"\n' % (k, v)
100 msg += "Updated image properties:\n"
101 for (k, v) in altered.items():
102 msg += ' %s: "%s" -> "%s"\n' % (k, self.old[k], v)
105 self.session['metadata'].update(added)
106 self.session['metadata'].update(altered)
107 d.msgbox(msg, title="Image Property Changes", width=SMALL_WIDTH)
110 def upload_image(session):
111 d = session["dialog"]
112 dev = session['device']
115 if "account" not in session:
116 d.msgbox("You need to provide your ~okeanos account before you "
117 "can upload images to pithos+", width=SMALL_WIDTH)
121 init = session["upload"] if "upload" in session else ''
122 (code, answer) = d.inputbox("Please provide a filename:", init=init,
125 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
128 filename = answer.strip()
129 if len(filename) == 0:
130 d.msgbox("Filename cannot be empty", width=SMALL_WIDTH)
132 session['upload'] = filename
135 gauge = GaugeOutput(d, "Image Upload", "Uploading...")
140 if 'checksum' not in session:
142 session['checksum'] = md5.compute(session['snapshot'], size)
144 kamaki = Kamaki(session['account'], out)
147 with open(session['snapshot'], 'rb') as f:
148 session["pithos_uri"] = \
149 kamaki.upload(f, size, filename,
150 "Calculating block hashes",
151 "Uploading missing blocks")
152 # Upload metadata file
153 out.output("Uploading metadata file...")
154 metastring = extract_metadata_string(session)
155 kamaki.upload(StringIO.StringIO(metastring),
156 size=len(metastring),
157 remote_path="%s.meta" % filename)
161 out.output("Uploading md5sum file...")
162 md5str = "%s %s\n" % (session['checksum'], filename)
163 kamaki.upload(StringIO.StringIO(md5str), size=len(md5str),
164 remote_path="%s.md5sum" % filename)
167 except ClientError as e:
168 d.msgbox("Error in pithos+ client: %s" % e.message,
169 title="Pithos+ Client Error", width=SMALL_WIDTH)
170 if 'pithos_uri' in session:
171 del session['pithos_uri']
178 d.msgbox("Image file `%s' was successfully uploaded to pithos+" % filename,
184 def register_image(session):
185 d = session["dialog"]
186 dev = session['device']
190 if "account" not in session:
191 d.msgbox("You need to provide your ~okeanos credentians before you "
192 "can register an images to cyclades",
196 if "pithos_uri" not in session:
197 d.msgbox("You need to upload the image to pithos+ before you can "
198 "register it to cyclades", width=SMALL_WIDTH)
202 (code, answer) = d.inputbox("Please provide a registration name:",
204 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
207 name = answer.strip()
209 d.msgbox("Registration name cannot be empty", width=SMALL_WIDTH)
212 ret = d.yesno("Make the image public?\\nA public image is accessible"
213 "by every user of the service.", defaultno=1,
215 if ret not in (0, 1):
218 is_public = True if ret == 0 else False
223 metadata.update(session['metadata'])
224 if 'task_metadata' in session:
225 for key in session['task_metadata']:
226 metadata[key] = 'yes'
228 img_type = "public" if is_public else "private"
229 gauge = GaugeOutput(d, "Image Registration", "Registering image...")
234 out.output("Registering %s image with Cyclades..." % img_type)
236 kamaki = Kamaki(session['account'], out)
237 kamaki.register(name, session['pithos_uri'], metadata,
240 except ClientError as e:
241 d.msgbox("Error in pithos+ client: %s" % e.message)
248 d.msgbox("%s image `%s' was successfully registered with Cyclades as `%s'"
249 % (img_type.title(), session['upload'], name), width=SMALL_WIDTH)
253 def kamaki_menu(session):
254 d = session['dialog']
255 default_item = "Account"
257 if 'account' not in session:
258 token = Kamaki.get_token()
260 session['account'] = Kamaki.get_account(token)
261 if not session['account']:
262 del session['account']
263 Kamaki.save_token('') # The token was not valid. Remove it
266 account = session["account"]['username'] if "account" in session else \
268 upload = session["upload"] if "upload" in session else "<none>"
270 choices = [("Account", "Change your ~okeanos account: %s" % account),
271 ("Upload", "Upload image to pithos+"),
272 ("Register", "Register the image to cyclades: %s" % upload)]
274 (code, choice) = d.menu(
275 text="Choose one of the following or press <Back> to go back.",
276 width=WIDTH, choices=choices, cancel="Back", height=13,
277 menu_height=5, default_item=default_item,
278 title="Image Registration Menu")
280 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
283 if choice == "Account":
284 default_item = "Account"
285 (code, answer) = d.inputbox(
286 "Please provide your ~okeanos token:",
287 init=session["account"]['auth_token'] if "account" in session
288 else '', width=WIDTH)
289 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
291 if len(answer) == 0 and "account" in session:
292 del session["account"]
294 token = answer.strip()
295 session['account'] = Kamaki.get_account(token)
296 if session['account'] is not None:
297 Kamaki.save_token(token)
298 default_item = "Upload"
300 del session['account']
301 d.msgbox("The token you provided is not valid!",
303 elif choice == "Upload":
304 if upload_image(session):
305 default_item = "Register"
307 default_item = "Upload"
308 elif choice == "Register":
309 if register_image(session):
312 default_item = "Register"
315 def add_property(session):
316 d = session['dialog']
319 (code, answer) = d.inputbox("Please provide a name for a new image"
320 " property:", width=WIDTH)
321 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
324 name = answer.strip()
326 d.msgbox("A property name cannot be empty", width=SMALL_WIDTH)
332 (code, answer) = d.inputbox("Please provide a value for image "
333 "property %s" % name, width=WIDTH)
334 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
337 value = answer.strip()
339 d.msgbox("Value cannot be empty", width=SMALL_WIDTH)
344 session['metadata'][name] = value
349 def modify_properties(session):
350 d = session['dialog']
354 for (key, val) in session['metadata'].items():
355 choices.append((str(key), str(val)))
357 (code, choice) = d.menu(
358 "In this menu you can edit existing image properties or add new "
359 "ones. Be careful! Most properties have special meaning and "
360 "alter the image deployment behaviour. Press <HELP> to see more "
361 "information about image properties. Press <BACK> when done.",
362 height=18, width=WIDTH, choices=choices, menu_height=10,
363 ok_label="Edit", extra_button=1, extra_label="Add", cancel="Back",
364 help_button=1, title="Image Properties")
366 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
369 elif code == d.DIALOG_OK:
370 (code, answer) = d.inputbox("Please provide a new value for the "
371 "image property with name `%s':" %
373 init=session['metadata'][choice],
375 if code not in (d.DIALOG_CANCEL, d.DIALOG_ESC):
376 value = answer.strip()
378 d.msgbox("Value cannot be empty!")
381 session['metadata'][choice] = value
383 elif code == d.DIALOG_EXTRA:
384 add_property(session)
386 help_file = get_help_file("image_properties")
387 assert os.path.exists(help_file)
388 d.textbox(help_file, title="Image Properties", width=70, height=40)
391 def delete_properties(session):
392 d = session['dialog']
395 for (key, val) in session['metadata'].items():
396 choices.append((key, "%s" % val, 0))
398 (code, to_delete) = d.checklist("Choose which properties to delete:",
399 choices=choices, width=WIDTH)
401 # If the user exits with ESC or CANCEL, the returned tag list is empty.
403 del session['metadata'][i]
407 d.msgbox("%d image properties were deleted." % cnt, width=SMALL_WIDTH)
413 def exclude_tasks(session):
414 d = session['dialog']
420 if 'excluded_tasks' not in session:
421 session['excluded_tasks'] = []
423 if -1 in session['excluded_tasks']:
424 if not d.yesno("Image deployment configuration is disabled. "
425 "Do you wish to enable it?", width=SMALL_WIDTH):
426 session['excluded_tasks'].remove(-1)
430 for (msg, task, osfamily) in CONFIGURATION_TASKS:
431 if session['metadata']['OSFAMILY'] in osfamily:
432 checked = 1 if index in session['excluded_tasks'] else 0
433 choices.append((str(displayed_index), msg, checked))
434 mapping[displayed_index] = index
439 (code, tags) = d.checklist(
440 text="Please choose which configuration tasks you would like to "
441 "prevent from running during image deployment. "
442 "Press <No Config> to supress any configuration. "
443 "Press <Help> for more help on the image deployment "
444 "configuration tasks.",
445 choices=choices, height=19, list_height=8, width=WIDTH,
446 help_button=1, extra_button=1, extra_label="No Config",
447 title="Exclude Configuration Tasks")
449 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
451 elif code == d.DIALOG_HELP:
452 help_file = get_help_file("configuration_tasks")
453 assert os.path.exists(help_file)
454 d.textbox(help_file, title="Configuration Tasks",
457 elif code == d.DIALOG_EXTRA:
458 session['excluded_tasks'] = [-1]
459 session['task_metadata'] = ["EXCLUDE_ALL_TASKS"]
461 elif code == d.DIALOG_OK:
462 session['excluded_tasks'] = []
464 session['excluded_tasks'].append(mapping[int(tag)])
466 exclude_metadata = []
467 for task in session['excluded_tasks']:
468 exclude_metadata.extend(CONFIGURATION_TASKS[task][1])
470 session['task_metadata'] = map(lambda x: "EXCLUDE_TASK_%s" % x,
477 def sysprep(session):
478 d = session['dialog']
479 image_os = session['image_os']
481 # Is the image already shrinked?
482 if 'shrinked' in session and session['shrinked']:
483 msg = "It seems you have shrinked the image. Running system " \
484 "preparation tasks on a shrinked image is dangerous."
486 if d.yesno("%s\n\nDo you really want to continue?" % msg,
487 width=SMALL_WIDTH, defaultno=1):
490 wrapper = textwrap.TextWrapper(width=WIDTH - 5)
492 help_title = "System Preperation Tasks"
493 sysprep_help = "%s\n%s\n\n" % (help_title, '=' * len(help_title))
495 if 'exec_syspreps' not in session:
496 session['exec_syspreps'] = []
498 all_syspreps = image_os.list_syspreps()
499 # Only give the user the choice between syspreps that have not ran yet
500 syspreps = [s for s in all_syspreps if s not in session['exec_syspreps']]
502 if len(syspreps) == 0:
503 d.msgbox("No system preparation task available to run!",
504 title="System Preperation", width=SMALL_WIDTH)
510 for sysprep in syspreps:
511 name, descr = image_os.sysprep_info(sysprep)
512 display_name = name.replace('-', ' ').capitalize()
513 sysprep_help += "%s\n" % display_name
514 sysprep_help += "%s\n" % ('-' * len(display_name))
515 sysprep_help += "%s\n\n" % wrapper.fill(" ".join(descr.split()))
516 enabled = 1 if sysprep.enabled else 0
517 choices.append((str(index + 1), display_name, enabled))
520 (code, tags) = d.checklist(
521 "Please choose which system preparation tasks you would like to "
522 "run on the image. Press <Help> to see details about the system "
523 "preparation tasks.", title="Run system preparation tasks",
524 choices=choices, width=70, ok_label="Run", help_button=1)
526 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
528 elif code == d.DIALOG_HELP:
529 d.scrollbox(sysprep_help, width=WIDTH)
530 elif code == d.DIALOG_OK:
531 # Enable selected syspreps and disable the rest
532 for i in range(len(syspreps)):
533 if str(i + 1) in tags:
534 image_os.enable_sysprep(syspreps[i])
535 session['exec_syspreps'].append(syspreps[i])
537 image_os.disable_sysprep(syspreps[i])
539 infobox = InfoBoxOutput(d, "Image Configuration")
541 dev = session['device']
544 dev.mount(readonly=False)
546 # The checksum is invalid. We have mounted the image rw
547 if 'checksum' in session:
548 del session['checksum']
550 # Monitor the metadata changes during syspreps
551 with metadata_monitor(session, image_os.meta):
552 image_os.do_sysprep()
555 # Disable syspreps that have ran
556 for sysprep in session['exec_syspreps']:
557 image_os.disable_sysprep(sysprep)
561 dev.out.remove(infobox)
569 d = session['dialog']
570 dev = session['device']
572 shrinked = 'shrinked' in session and session['shrinked']
575 d.msgbox("The image is already shrinked!", title="Image Shrinking",
579 msg = "This operation will shrink the last partition of the image to " \
580 "reduce the total image size. If the last partition is a swap " \
581 "partition, then this partition is removed and the partition " \
582 "before that is shrinked. The removed swap partition will be " \
583 "recreated during image deployment."
585 if not d.yesno("%s\n\nDo you want to continue?" % msg, width=WIDTH,
586 height=12, title="Image Shrinking"):
587 with metadata_monitor(session, dev.meta):
588 infobox = InfoBoxOutput(d, "Image Shrinking", height=4)
594 dev.out.remove(infobox)
596 session['shrinked'] = True
597 update_background_title(session)
604 def customization_menu(session):
605 d = session['dialog']
607 choices = [("Sysprep", "Run various image preparation tasks"),
608 ("Shrink", "Shrink image"),
609 ("View/Modify", "View/Modify image properties"),
610 ("Delete", "Delete image properties"),
611 ("Exclude", "Exclude various deployment tasks from running")]
615 actions = {"Sysprep": sysprep,
617 "View/Modify": modify_properties,
618 "Delete": delete_properties,
619 "Exclude": exclude_tasks}
621 (code, choice) = d.menu(
622 text="Choose one of the following or press <Back> to exit.",
623 width=WIDTH, choices=choices, cancel="Back", height=13,
624 menu_height=len(choices), default_item=choices[default_item][0],
625 title="Image Customization Menu")
627 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
629 elif choice in actions:
630 default_item = [entry[0] for entry in choices].index(choice)
631 if actions[choice](session):
632 default_item = (default_item + 1) % len(choices)
635 def main_menu(session):
636 d = session['dialog']
637 dev = session['device']
639 update_background_title(session)
641 choices = [("Customize", "Customize image & ~okeanos deployment options"),
642 ("Register", "Register image to ~okeanos"),
643 ("Extract", "Dump image to local file system"),
644 ("Reset", "Reset everything and start over again"),
645 ("Help", "Get help for using snf-image-creator")]
647 default_item = "Customize"
649 actions = {"Customize": customization_menu, "Register": kamaki_menu,
650 "Extract": extract_image}
652 (code, choice) = d.menu(
653 text="Choose one of the following or press <Exit> to exit.",
654 width=WIDTH, choices=choices, cancel="Exit", height=13,
655 default_item=default_item, menu_height=len(choices),
656 title="Image Creator for ~okeanos (snf-image-creator version %s)" %
659 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
662 elif choice == "Reset":
664 d.infobox("Resetting snf-image-creator. Please wait...",
667 elif choice in actions:
668 actions[choice](session)
670 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :