Support private images
[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 account before you "
117                  "can upload images to pithos+", width=SMALL_WIDTH)
118         return False
119
120     while 1:
121         init = session["upload"] if "upload" in session else ''
122         (code, answer) = d.inputbox("Please provide a filename:", init=init,
123                                     width=WIDTH)
124
125         if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
126             return False
127
128         filename = answer.strip()
129         if len(filename) == 0:
130             d.msgbox("Filename cannot be empty", width=SMALL_WIDTH)
131             continue
132         session['upload'] = filename
133         break
134
135     gauge = GaugeOutput(d, "Image Upload", "Uploading...")
136     try:
137         out = dev.out
138         out.add(gauge)
139         try:
140             if 'checksum' not in session:
141                 md5 = MD5(out)
142                 session['checksum'] = md5.compute(session['snapshot'], size)
143
144             kamaki = Kamaki(session['account'], out)
145             try:
146                 # Upload image file
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)
158                 out.success("done")
159
160                 # Upload md5sum file
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)
165                 out.success("done")
166
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']
172                 return False
173         finally:
174             out.remove(gauge)
175     finally:
176         gauge.cleanup()
177
178     d.msgbox("Image file `%s' was successfully uploaded to pithos+" % filename,
179              width=SMALL_WIDTH)
180
181     return True
182
183
184 def register_image(session):
185     d = session["dialog"]
186     dev = session['device']
187
188     is_public = False
189
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",
193                  width=SMALL_WIDTH)
194         return False
195
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)
199         return False
200
201     while 1:
202         (code, answer) = d.inputbox("Please provide a registration name:",
203                                     width=WIDTH)
204         if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
205             return False
206
207         name = answer.strip()
208         if len(name) == 0:
209             d.msgbox("Registration name cannot be empty", width=SMALL_WIDTH)
210             continue
211
212         ret = d.yesno("Make the image public?\\nA public image is accessible"
213                       "by every user of the service.", defaultno=1,
214                       width=WIDTH)
215         if ret not in (0, 1):
216             continue
217
218         is_public = True if ret == 0 else False
219
220         break
221
222     metadata = {}
223     metadata.update(session['metadata'])
224     if 'task_metadata' in session:
225         for key in session['task_metadata']:
226             metadata[key] = 'yes'
227
228     img_type = "public" if is_public else "private"
229     gauge = GaugeOutput(d, "Image Registration", "Registering image...")
230     try:
231         out = dev.out
232         out.add(gauge)
233         try:
234             out.output("Registering %s image with Cyclades..." % img_type)
235             try:
236                 kamaki = Kamaki(session['account'], out)
237                 kamaki.register(name, session['pithos_uri'], metadata,
238                                 is_public)
239                 out.success('done')
240             except ClientError as e:
241                 d.msgbox("Error in pithos+ client: %s" % e.message)
242                 return False
243         finally:
244             out.remove(gauge)
245     finally:
246         gauge.cleanup()
247
248     d.msgbox("%s image `%s' was successfully registered with Cyclades as `%s'"
249              % (img_type.title(), session['upload'], name), width=SMALL_WIDTH)
250     return True
251
252
253 def kamaki_menu(session):
254     d = session['dialog']
255     default_item = "Account"
256
257     if 'account' not in session:
258         token = Kamaki.get_token()
259         if 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
264
265     while 1:
266         account = session["account"]['username'] if "account" in session else \
267             "<none>"
268         upload = session["upload"] if "upload" in session else "<none>"
269
270         choices = [("Account", "Change your ~okeanos account: %s" % account),
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 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):
290                 continue
291             if len(answer) == 0 and "account" in session:
292                     del session["account"]
293             else:
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"
299                 else:
300                     del session['account']
301                     d.msgbox("The token you provided is not valid!",
302                              width=SMALL_WIDTH)
303         elif choice == "Upload":
304             if upload_image(session):
305                 default_item = "Register"
306             else:
307                 default_item = "Upload"
308         elif choice == "Register":
309             if register_image(session):
310                 return True
311             else:
312                 default_item = "Register"
313
314
315 def add_property(session):
316     d = session['dialog']
317
318     while 1:
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):
322             return False
323
324         name = answer.strip()
325         if len(name) == 0:
326             d.msgbox("A property name cannot be empty", width=SMALL_WIDTH)
327             continue
328
329         break
330
331     while 1:
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):
335             return False
336
337         value = answer.strip()
338         if len(value) == 0:
339             d.msgbox("Value cannot be empty", width=SMALL_WIDTH)
340             continue
341
342         break
343
344     session['metadata'][name] = value
345
346     return True
347
348
349 def modify_properties(session):
350     d = session['dialog']
351
352     while 1:
353         choices = []
354         for (key, val) in session['metadata'].items():
355             choices.append((str(key), str(val)))
356
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")
365
366         if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
367             return True
368         # Edit button
369         elif code == d.DIALOG_OK:
370             (code, answer) = d.inputbox("Please provide a new value for the "
371                                         "image property with name `%s':" %
372                                         choice,
373                                         init=session['metadata'][choice],
374                                         width=WIDTH)
375             if code not in (d.DIALOG_CANCEL, d.DIALOG_ESC):
376                 value = answer.strip()
377                 if len(value) == 0:
378                     d.msgbox("Value cannot be empty!")
379                     continue
380                 else:
381                     session['metadata'][choice] = value
382         # ADD button
383         elif code == d.DIALOG_EXTRA:
384             add_property(session)
385         elif code == 'help':
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)
389
390
391 def delete_properties(session):
392     d = session['dialog']
393
394     choices = []
395     for (key, val) in session['metadata'].items():
396         choices.append((key, "%s" % val, 0))
397
398     (code, to_delete) = d.checklist("Choose which properties to delete:",
399                                     choices=choices, width=WIDTH)
400
401     # If the user exits with ESC or CANCEL, the returned tag list is empty.
402     for i in to_delete:
403         del session['metadata'][i]
404
405     cnt = len(to_delete)
406     if cnt > 0:
407         d.msgbox("%d image properties were deleted." % cnt, width=SMALL_WIDTH)
408         return True
409     else:
410         return False
411
412
413 def exclude_tasks(session):
414     d = session['dialog']
415
416     index = 0
417     displayed_index = 1
418     choices = []
419     mapping = {}
420     if 'excluded_tasks' not in session:
421         session['excluded_tasks'] = []
422
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)
427         else:
428             return False
429
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
435             displayed_index += 1
436         index += 1
437
438     while 1:
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")
448
449         if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
450             return False
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",
455                       width=70, height=40)
456         # No Config button
457         elif code == d.DIALOG_EXTRA:
458             session['excluded_tasks'] = [-1]
459             session['task_metadata'] = ["EXCLUDE_ALL_TASKS"]
460             break
461         elif code == d.DIALOG_OK:
462             session['excluded_tasks'] = []
463             for tag in tags:
464                 session['excluded_tasks'].append(mapping[int(tag)])
465
466             exclude_metadata = []
467             for task in session['excluded_tasks']:
468                 exclude_metadata.extend(CONFIGURATION_TASKS[task][1])
469
470             session['task_metadata'] = map(lambda x: "EXCLUDE_TASK_%s" % x,
471                                            exclude_metadata)
472             break
473
474     return True
475
476
477 def sysprep(session):
478     d = session['dialog']
479     image_os = session['image_os']
480
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."
485
486         if d.yesno("%s\n\nDo you really want to continue?" % msg,
487                    width=SMALL_WIDTH, defaultno=1):
488             return
489
490     wrapper = textwrap.TextWrapper(width=WIDTH - 5)
491
492     help_title = "System Preperation Tasks"
493     sysprep_help = "%s\n%s\n\n" % (help_title, '=' * len(help_title))
494
495     if 'exec_syspreps' not in session:
496         session['exec_syspreps'] = []
497
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']]
501
502     if len(syspreps) == 0:
503         d.msgbox("No system preparation task available to run!",
504                  title="System Preperation", width=SMALL_WIDTH)
505         return
506
507     while 1:
508         choices = []
509         index = 0
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))
518             index += 1
519
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)
525
526         if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
527             return False
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])
536                 else:
537                     image_os.disable_sysprep(syspreps[i])
538
539             infobox = InfoBoxOutput(d, "Image Configuration")
540             try:
541                 dev = session['device']
542                 dev.out.add(infobox)
543                 try:
544                     dev.mount(readonly=False)
545                     try:
546                         # The checksum is invalid. We have mounted the image rw
547                         if 'checksum' in session:
548                             del session['checksum']
549
550                         # Monitor the metadata changes during syspreps
551                         with metadata_monitor(session, image_os.meta):
552                             image_os.do_sysprep()
553                             infobox.finalize()
554
555                         # Disable syspreps that have ran
556                         for sysprep in session['exec_syspreps']:
557                             image_os.disable_sysprep(sysprep)
558                     finally:
559                         dev.umount()
560                 finally:
561                     dev.out.remove(infobox)
562             finally:
563                 infobox.cleanup()
564             break
565     return True
566
567
568 def shrink(session):
569     d = session['dialog']
570     dev = session['device']
571
572     shrinked = 'shrinked' in session and session['shrinked']
573
574     if shrinked:
575         d.msgbox("The image is already shrinked!", title="Image Shrinking",
576                  width=SMALL_WIDTH)
577         return True
578
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."
584
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)
589             dev.out.add(infobox)
590             try:
591                 dev.shrink()
592                 infobox.finalize()
593             finally:
594                 dev.out.remove(infobox)
595
596         session['shrinked'] = True
597         update_background_title(session)
598     else:
599         return False
600
601     return True
602
603
604 def customization_menu(session):
605     d = session['dialog']
606
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")]
612
613     default_item = 0
614
615     actions = {"Sysprep": sysprep,
616                "Shrink": shrink,
617                "View/Modify": modify_properties,
618                "Delete": delete_properties,
619                "Exclude": exclude_tasks}
620     while 1:
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")
626
627         if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
628             break
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)
633
634
635 def main_menu(session):
636     d = session['dialog']
637     dev = session['device']
638
639     update_background_title(session)
640
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")]
646
647     default_item = "Customize"
648
649     actions = {"Customize": customization_menu, "Register": kamaki_menu,
650                "Extract": extract_image}
651     while 1:
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)" %
657                   version)
658
659         if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
660             if confirm_exit(d):
661                 break
662         elif choice == "Reset":
663             if confirm_reset(d):
664                 d.infobox("Resetting snf-image-creator. Please wait...",
665                           width=SMALL_WIDTH)
666                 raise Reset
667         elif choice in actions:
668             actions[choice](session)
669
670 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :