Statistics
| Branch: | Tag: | Revision:

root / image_creator / dialog_menu.py @ 121f3bc0

History | View | Annotate | Download (25.3 kB)

1
# -*- coding: utf-8 -*-
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
"""This module implements the "expert" mode of the dialog-based version of
37
snf-image-creator.
38
"""
39

    
40
import os
41
import textwrap
42
import StringIO
43

    
44
from image_creator import __version__ as version
45
from image_creator.util import MD5, FatalError
46
from image_creator.output.dialog import GaugeOutput, InfoBoxOutput
47
from image_creator.kamaki_wrapper import Kamaki, ClientError
48
from image_creator.help import get_help_file
49
from image_creator.dialog_util import SMALL_WIDTH, WIDTH, \
50
    update_background_title, confirm_reset, confirm_exit, Reset, \
51
    extract_image, extract_metadata_string
52

    
53
CONFIGURATION_TASKS = [
54
    ("Partition table manipulation", ["FixPartitionTable"],
55
        ["linux", "windows"]),
56
    ("File system resize",
57
        ["FilesystemResizeUnmounted", "FilesystemResizeMounted"],
58
        ["linux", "windows"]),
59
    ("Swap partition configuration", ["AddSwap"], ["linux"]),
60
    ("SSH keys removal", ["DeleteSSHKeys"], ["linux"]),
61
    ("Temporal RDP disabling", ["DisableRemoteDesktopConnections"],
62
        ["windows"]),
63
    ("SELinux relabeling at next boot", ["SELinuxAutorelabel"], ["linux"]),
64
    ("Hostname/Computer Name assignment", ["AssignHostname"],
65
        ["windows", "linux"]),
66
    ("Password change", ["ChangePassword"], ["windows", "linux"]),
67
    ("File injection", ["EnforcePersonality"], ["windows", "linux"])
68
]
69

    
70

    
71
class MetadataMonitor(object):
72
    """Monitors image metadata chages"""
73
    def __init__(self, session, meta):
74
        self.session = session
75
        self.meta = meta
76

    
77
    def __enter__(self):
78
        self.old = {}
79
        for (k, v) in self.meta.items():
80
            self.old[k] = v
81

    
82
    def __exit__(self, type, value, traceback):
83
        d = self.session['dialog']
84

    
85
        altered = {}
86
        added = {}
87

    
88
        for (k, v) in self.meta.items():
89
            if k not in self.old:
90
                added[k] = v
91
            elif self.old[k] != v:
92
                altered[k] = v
93

    
94
        if not (len(added) or len(altered)):
95
            return
96

    
97
        msg = "The last action has changed some image properties:\n\n"
98
        if len(added):
99
            msg += "New image properties:\n"
100
            for (k, v) in added.items():
101
                msg += '    %s: "%s"\n' % (k, v)
102
            msg += "\n"
103
        if len(altered):
104
            msg += "Updated image properties:\n"
105
            for (k, v) in altered.items():
106
                msg += '    %s: "%s" -> "%s"\n' % (k, self.old[k], v)
107
            msg += "\n"
108

    
109
        self.session['metadata'].update(added)
110
        self.session['metadata'].update(altered)
111
        d.msgbox(msg, title="Image Property Changes", width=SMALL_WIDTH)
112

    
113

    
114
def upload_image(session):
115
    """Upload the image to pithos+"""
116
    d = session["dialog"]
117
    image = session['image']
118
    meta = session['metadata']
119
    size = image.size
120

    
121
    if "account" not in session:
122
        d.msgbox("You need to provide your ~okeanos credentials before you "
123
                 "can upload images to pithos+", width=SMALL_WIDTH)
124
        return False
125

    
126
    while 1:
127
        if 'upload' in session:
128
            init = session['upload']
129
        elif 'OS' in meta:
130
            init = "%s.diskdump" % meta['OS']
131
        else:
132
            init = ""
133
        (code, answer) = d.inputbox("Please provide a filename:", init=init,
134
                                    width=WIDTH)
135

    
136
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
137
            return False
138

    
139
        filename = answer.strip()
140
        if len(filename) == 0:
141
            d.msgbox("Filename cannot be empty", width=SMALL_WIDTH)
142
            continue
143

    
144
        kamaki = Kamaki(session['account'], None)
145
        overwrite = []
146
        for f in (filename, "%s.md5sum" % filename, "%s.meta" % filename):
147
            if kamaki.object_exists(f):
148
                overwrite.append(f)
149

    
150
        if len(overwrite) > 0:
151
            if d.yesno("The following pithos object(s) already exist(s):\n"
152
                       "%s\nDo you want to overwrite them?" %
153
                       "\n".join(overwrite), width=WIDTH, defaultno=1):
154
                continue
155

    
156
        session['upload'] = filename
157
        break
158

    
159
    gauge = GaugeOutput(d, "Image Upload", "Uploading...")
160
    try:
161
        out = image.out
162
        out.add(gauge)
163
        kamaki.out = out
164
        try:
165
            if 'checksum' not in session:
166
                md5 = MD5(out)
167
                session['checksum'] = md5.compute(image.device, size)
168

    
169
            try:
170
                # Upload image file
171
                with open(image.device, 'rb') as f:
172
                    session["pithos_uri"] = \
173
                        kamaki.upload(f, size, filename,
174
                                      "Calculating block hashes",
175
                                      "Uploading missing blocks")
176
                # Upload md5sum file
177
                out.output("Uploading md5sum file...")
178
                md5str = "%s %s\n" % (session['checksum'], filename)
179
                kamaki.upload(StringIO.StringIO(md5str), size=len(md5str),
180
                              remote_path="%s.md5sum" % filename)
181
                out.success("done")
182

    
183
            except ClientError as e:
184
                d.msgbox("Error in pithos+ client: %s" % e.message,
185
                         title="Pithos+ Client Error", width=SMALL_WIDTH)
186
                if 'pithos_uri' in session:
187
                    del session['pithos_uri']
188
                return False
189
        finally:
190
            out.remove(gauge)
191
    finally:
192
        gauge.cleanup()
193

    
194
    d.msgbox("Image file `%s' was successfully uploaded to pithos+" % filename,
195
             width=SMALL_WIDTH)
196

    
197
    return True
198

    
199

    
200
def register_image(session):
201
    """Register image with cyclades"""
202
    d = session["dialog"]
203

    
204
    is_public = False
205

    
206
    if "account" not in session:
207
        d.msgbox("You need to provide your ~okeanos credentians before you "
208
                 "can register an images with cyclades", width=SMALL_WIDTH)
209
        return False
210

    
211
    if "pithos_uri" not in session:
212
        d.msgbox("You need to upload the image to pithos+ before you can "
213
                 "register it with cyclades", width=SMALL_WIDTH)
214
        return False
215

    
216
    while 1:
217
        (code, answer) = d.inputbox("Please provide a registration name:",
218
                                    width=WIDTH)
219
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
220
            return False
221

    
222
        name = answer.strip()
223
        if len(name) == 0:
224
            d.msgbox("Registration name cannot be empty", width=SMALL_WIDTH)
225
            continue
226

    
227
        ret = d.yesno("Make the image public?\\nA public image is accessible "
228
                      "by every user of the service.", defaultno=1,
229
                      width=WIDTH)
230
        if ret not in (0, 1):
231
            continue
232

    
233
        is_public = True if ret == 0 else False
234

    
235
        break
236

    
237
    metadata = {}
238
    metadata.update(session['metadata'])
239
    if 'task_metadata' in session:
240
        for key in session['task_metadata']:
241
            metadata[key] = 'yes'
242

    
243
    img_type = "public" if is_public else "private"
244
    gauge = GaugeOutput(d, "Image Registration", "Registering image...")
245
    try:
246
        out = session['image'].out
247
        out.add(gauge)
248
        try:
249
            try:
250
                out.output("Registering %s image with Cyclades..." % img_type)
251
                kamaki = Kamaki(session['account'], out)
252
                kamaki.register(name, session['pithos_uri'], metadata,
253
                                is_public)
254
                out.success('done')
255
                # Upload metadata file
256
                out.output("Uploading metadata file...")
257
                metastring = extract_metadata_string(session)
258
                kamaki.upload(StringIO.StringIO(metastring),
259
                              size=len(metastring),
260
                              remote_path="%s.meta" % session['upload'])
261
                out.success("done")
262
                if is_public:
263
                    out.output("Sharing metadata and md5sum files...")
264
                    kamaki.share("%s.meta" % session['upload'])
265
                    kamaki.share("%s.md5sum" % session['upload'])
266
                    out.success('done')
267
            except ClientError as e:
268
                d.msgbox("Error in pithos+ client: %s" % e.message)
269
                return False
270
        finally:
271
            out.remove(gauge)
272
    finally:
273
        gauge.cleanup()
274

    
275
    d.msgbox("%s image `%s' was successfully registered with Cyclades as `%s'"
276
             % (img_type.title(), session['upload'], name), width=SMALL_WIDTH)
277
    return True
278

    
279

    
280
def kamaki_menu(session):
281
    """Show kamaki related actions"""
282
    d = session['dialog']
283
    default_item = "Account"
284

    
285
    if 'account' not in session:
286
        token = Kamaki.get_token()
287
        if token:
288
            session['account'] = Kamaki.get_account(token)
289
            if not session['account']:
290
                del session['account']
291
                Kamaki.save_token('')  # The token was not valid. Remove it
292

    
293
    while 1:
294
        account = session["account"]['username'] if "account" in session else \
295
            "<none>"
296
        upload = session["upload"] if "upload" in session else "<none>"
297

    
298
        choices = [("Account", "Change your ~okeanos account: %s" % account),
299
                   ("Upload", "Upload image to pithos+"),
300
                   ("Register", "Register the image to cyclades: %s" % upload)]
301

    
302
        (code, choice) = d.menu(
303
            text="Choose one of the following or press <Back> to go back.",
304
            width=WIDTH, choices=choices, cancel="Back", height=13,
305
            menu_height=5, default_item=default_item,
306
            title="Image Registration Menu")
307

    
308
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
309
            return False
310

    
311
        if choice == "Account":
312
            default_item = "Account"
313
            (code, answer) = d.inputbox(
314
                "Please provide your ~okeanos authentication token:",
315
                init=session["account"]['auth_token'] if "account" in session
316
                else '', width=WIDTH)
317
            if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
318
                continue
319
            if len(answer) == 0 and "account" in session:
320
                del session["account"]
321
            else:
322
                token = answer.strip()
323
                session['account'] = Kamaki.get_account(token)
324
                if session['account'] is not None:
325
                    Kamaki.save_token(token)
326
                    default_item = "Upload"
327
                else:
328
                    del session['account']
329
                    d.msgbox("The token you provided is not valid!",
330
                             width=SMALL_WIDTH)
331
        elif choice == "Upload":
332
            if upload_image(session):
333
                default_item = "Register"
334
            else:
335
                default_item = "Upload"
336
        elif choice == "Register":
337
            if register_image(session):
338
                return True
339
            else:
340
                default_item = "Register"
341

    
342

    
343
def add_property(session):
344
    """Add a new property to the image"""
345
    d = session['dialog']
346

    
347
    while 1:
348
        (code, answer) = d.inputbox("Please provide a name for a new image"
349
                                    " property:", width=WIDTH)
350
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
351
            return False
352

    
353
        name = answer.strip()
354
        if len(name) == 0:
355
            d.msgbox("A property name cannot be empty", width=SMALL_WIDTH)
356
            continue
357

    
358
        break
359

    
360
    while 1:
361
        (code, answer) = d.inputbox("Please provide a value for image "
362
                                    "property %s" % name, width=WIDTH)
363
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
364
            return False
365

    
366
        value = answer.strip()
367
        if len(value) == 0:
368
            d.msgbox("Value cannot be empty", width=SMALL_WIDTH)
369
            continue
370

    
371
        break
372

    
373
    session['metadata'][name] = value
374

    
375
    return True
376

    
377

    
378
def modify_properties(session):
379
    """Modify an existing image property"""
380
    d = session['dialog']
381

    
382
    while 1:
383
        choices = []
384
        for (key, val) in session['metadata'].items():
385
            choices.append((str(key), str(val)))
386

    
387
        (code, choice) = d.menu(
388
            "In this menu you can edit existing image properties or add new "
389
            "ones. Be careful! Most properties have special meaning and "
390
            "alter the image deployment behaviour. Press <HELP> to see more "
391
            "information about image properties. Press <BACK> when done.",
392
            height=18, width=WIDTH, choices=choices, menu_height=10,
393
            ok_label="Edit", extra_button=1, extra_label="Add", cancel="Back",
394
            help_button=1, title="Image Properties")
395

    
396
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
397
            return True
398
        # Edit button
399
        elif code == d.DIALOG_OK:
400
            (code, answer) = d.inputbox("Please provide a new value for the "
401
                                        "image property with name `%s':" %
402
                                        choice,
403
                                        init=session['metadata'][choice],
404
                                        width=WIDTH)
405
            if code not in (d.DIALOG_CANCEL, d.DIALOG_ESC):
406
                value = answer.strip()
407
                if len(value) == 0:
408
                    d.msgbox("Value cannot be empty!")
409
                    continue
410
                else:
411
                    session['metadata'][choice] = value
412
        # ADD button
413
        elif code == d.DIALOG_EXTRA:
414
            add_property(session)
415
        elif code == 'help':
416
            help_file = get_help_file("image_properties")
417
            assert os.path.exists(help_file)
418
            d.textbox(help_file, title="Image Properties", width=70, height=40)
419

    
420

    
421
def delete_properties(session):
422
    """Delete an image property"""
423
    d = session['dialog']
424

    
425
    choices = []
426
    for (key, val) in session['metadata'].items():
427
        choices.append((key, "%s" % val, 0))
428

    
429
    (code, to_delete) = d.checklist("Choose which properties to delete:",
430
                                    choices=choices, width=WIDTH)
431

    
432
    # If the user exits with ESC or CANCEL, the returned tag list is empty.
433
    for i in to_delete:
434
        del session['metadata'][i]
435

    
436
    cnt = len(to_delete)
437
    if cnt > 0:
438
        d.msgbox("%d image properties were deleted." % cnt, width=SMALL_WIDTH)
439
        return True
440
    else:
441
        return False
442

    
443

    
444
def exclude_tasks(session):
445
    """Exclude specific tasks from running during image deployment"""
446
    d = session['dialog']
447

    
448
    index = 0
449
    displayed_index = 1
450
    choices = []
451
    mapping = {}
452
    if 'excluded_tasks' not in session:
453
        session['excluded_tasks'] = []
454

    
455
    if -1 in session['excluded_tasks']:
456
        if not d.yesno("Image deployment configuration is disabled. "
457
                       "Do you wish to enable it?", width=SMALL_WIDTH):
458
            session['excluded_tasks'].remove(-1)
459
        else:
460
            return False
461

    
462
    for (msg, task, osfamily) in CONFIGURATION_TASKS:
463
        if session['metadata']['OSFAMILY'] in osfamily:
464
            checked = 1 if index in session['excluded_tasks'] else 0
465
            choices.append((str(displayed_index), msg, checked))
466
            mapping[displayed_index] = index
467
            displayed_index += 1
468
        index += 1
469

    
470
    while 1:
471
        (code, tags) = d.checklist(
472
            text="Please choose which configuration tasks you would like to "
473
                 "prevent from running during image deployment. "
474
                 "Press <No Config> to supress any configuration. "
475
                 "Press <Help> for more help on the image deployment "
476
                 "configuration tasks.",
477
            choices=choices, height=19, list_height=8, width=WIDTH,
478
            help_button=1, extra_button=1, extra_label="No Config",
479
            title="Exclude Configuration Tasks")
480

    
481
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
482
            return False
483
        elif code == d.DIALOG_HELP:
484
            help_file = get_help_file("configuration_tasks")
485
            assert os.path.exists(help_file)
486
            d.textbox(help_file, title="Configuration Tasks",
487
                      width=70, height=40)
488
        # No Config button
489
        elif code == d.DIALOG_EXTRA:
490
            session['excluded_tasks'] = [-1]
491
            session['task_metadata'] = ["EXCLUDE_ALL_TASKS"]
492
            break
493
        elif code == d.DIALOG_OK:
494
            session['excluded_tasks'] = []
495
            for tag in tags:
496
                session['excluded_tasks'].append(mapping[int(tag)])
497

    
498
            exclude_metadata = []
499
            for task in session['excluded_tasks']:
500
                exclude_metadata.extend(CONFIGURATION_TASKS[task][1])
501

    
502
            session['task_metadata'] = map(lambda x: "EXCLUDE_TASK_%s" % x,
503
                                           exclude_metadata)
504
            break
505

    
506
    return True
507

    
508

    
509
def sysprep(session):
510
    """Perform various system preperation tasks on the image"""
511
    d = session['dialog']
512
    image = session['image']
513

    
514
    # Is the image already shrinked?
515
    if 'shrinked' in session and session['shrinked']:
516
        msg = "It seems you have shrinked the image. Running system " \
517
              "preparation tasks on a shrinked image is dangerous."
518

    
519
        if d.yesno("%s\n\nDo you really want to continue?" % msg,
520
                   width=SMALL_WIDTH, defaultno=1):
521
            return
522

    
523
    wrapper = textwrap.TextWrapper(width=WIDTH - 5)
524

    
525
    help_title = "System Preperation Tasks"
526
    sysprep_help = "%s\n%s\n\n" % (help_title, '=' * len(help_title))
527

    
528
    syspreps = image.os.list_syspreps()
529

    
530
    if len(syspreps) == 0:
531
        d.msgbox("No system preparation task available to run!",
532
                 title="System Preperation", width=SMALL_WIDTH)
533
        return
534

    
535
    while 1:
536
        choices = []
537
        index = 0
538
        for sysprep in syspreps:
539
            name, descr = image.os.sysprep_info(sysprep)
540
            display_name = name.replace('-', ' ').capitalize()
541
            sysprep_help += "%s\n" % display_name
542
            sysprep_help += "%s\n" % ('-' * len(display_name))
543
            sysprep_help += "%s\n\n" % wrapper.fill(" ".join(descr.split()))
544
            enabled = 1 if sysprep.enabled else 0
545
            choices.append((str(index + 1), display_name, enabled))
546
            index += 1
547

    
548
        (code, tags) = d.checklist(
549
            "Please choose which system preparation tasks you would like to "
550
            "run on the image. Press <Help> to see details about the system "
551
            "preparation tasks.", title="Run system preparation tasks",
552
            choices=choices, width=70, ok_label="Run", help_button=1)
553

    
554
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
555
            return False
556
        elif code == d.DIALOG_HELP:
557
            d.scrollbox(sysprep_help, width=WIDTH)
558
        elif code == d.DIALOG_OK:
559
            # Enable selected syspreps and disable the rest
560
            for i in range(len(syspreps)):
561
                if str(i + 1) in tags:
562
                    image.os.enable_sysprep(syspreps[i])
563
                else:
564
                    image.os.disable_sysprep(syspreps[i])
565

    
566
            if len([s for s in image.os.list_syspreps() if s.enabled]) == 0:
567
                d.msgbox("No system preperation task is selected!",
568
                         title="System Preperation", width=SMALL_WIDTH)
569
                continue
570

    
571
            infobox = InfoBoxOutput(d, "Image Configuration")
572
            try:
573
                image.out.add(infobox)
574
                try:
575
                    # The checksum is invalid. We have mounted the image rw
576
                    if 'checksum' in session:
577
                        del session['checksum']
578

    
579
                    # Monitor the metadata changes during syspreps
580
                    with MetadataMonitor(session, image.os.meta):
581
                        try:
582
                            image.os.do_sysprep()
583
                            infobox.finalize()
584
                        except FatalError as e:
585
                            title = "System Preparation"
586
                            d.msgbox("System Preparation failed: %s" % e,
587
                                     title=title, width=SMALL_WIDTH)
588
                finally:
589
                    image.out.remove(infobox)
590
            finally:
591
                infobox.cleanup()
592
            break
593
    return True
594

    
595

    
596
def shrink(session):
597
    """Shrink the image"""
598
    d = session['dialog']
599
    image = session['image']
600

    
601
    shrinked = 'shrinked' in session and session['shrinked']
602

    
603
    if shrinked:
604
        d.msgbox("The image is already shrinked!", title="Image Shrinking",
605
                 width=SMALL_WIDTH)
606
        return True
607

    
608
    msg = "This operation will shrink the last partition of the image to " \
609
          "reduce the total image size. If the last partition is a swap " \
610
          "partition, then this partition is removed and the partition " \
611
          "before that is shrinked. The removed swap partition will be " \
612
          "recreated during image deployment."
613

    
614
    if not d.yesno("%s\n\nDo you want to continue?" % msg, width=WIDTH,
615
                   height=12, title="Image Shrinking"):
616
        with MetadataMonitor(session, image.meta):
617
            infobox = InfoBoxOutput(d, "Image Shrinking", height=4)
618
            image.out.add(infobox)
619
            try:
620
                image.shrink()
621
                infobox.finalize()
622
            finally:
623
                image.out.remove(infobox)
624

    
625
        session['shrinked'] = True
626
        update_background_title(session)
627
    else:
628
        return False
629

    
630
    return True
631

    
632

    
633
def customization_menu(session):
634
    """Show image customization menu"""
635
    d = session['dialog']
636

    
637
    choices = [("Sysprep", "Run various image preparation tasks"),
638
               ("Shrink", "Shrink image"),
639
               ("View/Modify", "View/Modify image properties"),
640
               ("Delete", "Delete image properties"),
641
               ("Exclude", "Exclude various deployment tasks from running")]
642

    
643
    default_item = 0
644

    
645
    actions = {"Sysprep": sysprep,
646
               "Shrink": shrink,
647
               "View/Modify": modify_properties,
648
               "Delete": delete_properties,
649
               "Exclude": exclude_tasks}
650
    while 1:
651
        (code, choice) = d.menu(
652
            text="Choose one of the following or press <Back> to exit.",
653
            width=WIDTH, choices=choices, cancel="Back", height=13,
654
            menu_height=len(choices), default_item=choices[default_item][0],
655
            title="Image Customization Menu")
656

    
657
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
658
            break
659
        elif choice in actions:
660
            default_item = [entry[0] for entry in choices].index(choice)
661
            if actions[choice](session):
662
                default_item = (default_item + 1) % len(choices)
663

    
664

    
665
def main_menu(session):
666
    """Show the main menu of the program"""
667
    d = session['dialog']
668

    
669
    update_background_title(session)
670

    
671
    choices = [("Customize", "Customize image & ~okeanos deployment options"),
672
               ("Register", "Register image to ~okeanos"),
673
               ("Extract", "Dump image to local file system"),
674
               ("Reset", "Reset everything and start over again"),
675
               ("Help", "Get help for using snf-image-creator")]
676

    
677
    default_item = "Customize"
678

    
679
    actions = {"Customize": customization_menu, "Register": kamaki_menu,
680
               "Extract": extract_image}
681
    while 1:
682
        (code, choice) = d.menu(
683
            text="Choose one of the following or press <Exit> to exit.",
684
            width=WIDTH, choices=choices, cancel="Exit", height=13,
685
            default_item=default_item, menu_height=len(choices),
686
            title="Image Creator for ~okeanos (snf-image-creator version %s)" %
687
                  version)
688

    
689
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
690
            if confirm_exit(d):
691
                break
692
        elif choice == "Reset":
693
            if confirm_reset(d):
694
                d.infobox("Resetting snf-image-creator. Please wait...",
695
                          width=SMALL_WIDTH)
696
                raise Reset
697
        elif choice == "Help":
698
            d.msgbox("For help, check the online documentation:\n\nhttp://www"
699
                     ".synnefo.org/docs/snf-image-creator/latest/",
700
                     width=WIDTH, title="Help")
701
        elif choice in actions:
702
            actions[choice](session)
703

    
704
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :