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