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