Statistics
| Branch: | Tag: | Revision:

root / image_creator / dialog_menu.py @ 8e58e699

History | View | Annotate | Download (29.1 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
import json
44

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

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

    
71

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

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

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

    
86
        altered = {}
87
        added = {}
88

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

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

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

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

    
114

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

    
122
    if "account" not in session:
123
        d.msgbox("You need to select a valid cloud before you can upload "
124
                 "images to pithos+", width=SMALL_WIDTH)
125
        return False
126

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

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

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

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

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

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

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

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

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

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

    
198
    return True
199

    
200

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

    
205
    is_public = False
206

    
207
    if "account" not in session:
208
        d.msgbox("You need to select a valid cloud before you "
209
                 "can register an images with cyclades", width=SMALL_WIDTH)
210
        return False
211

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

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

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

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

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

    
236
        break
237

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

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

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

    
280

    
281
def modify_clouds(session):
282
    """Modify existing cloud accounts"""
283
    d = session['dialog']
284

    
285
    while 1:
286
        clouds = Kamaki.get_clouds()
287
        if not len(clouds):
288
            if not add_cloud(session):
289
                break
290
            continue
291

    
292
        choices = []
293
        for (name, cloud) in clouds.items():
294
            descr = cloud['description'] if 'description' in cloud else ''
295
            choices.append((name, descr))
296

    
297
        (code, choice) = d.menu(
298
            "In this menu you can edit existing cloud accounts or add new "
299
            " ones. Press <Edit> to edit an existing account or <Add> to add "
300
            " a new one. Press <Back> or hit <ESC> when done.", height=18,
301
            width=WIDTH, choices=choices, menu_height=10, ok_label="Edit",
302
            extra_button=1, extra_label="Add", cancel="Back", help_button=1,
303
            title="Clouds")
304

    
305
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
306
            return True
307
        elif code == d.DIALOG_OK:  # Edit button
308
            edit_cloud(session, choice)
309
        elif code == d.DIALOG_EXTRA:  # Add button
310
            add_cloud(session)
311

    
312

    
313
def delete_clouds(session):
314
    """Delete existing cloud accounts"""
315
    d = session['dialog']
316

    
317
    choices = []
318
    for (name, cloud) in Kamaki.get_clouds().items():
319
        descr = cloud['description'] if 'description' in cloud else ''
320
        choices.append((name, descr, 0))
321

    
322
    if len(choices) == 0:
323
        d.msgbox("No available clouds to delete!", width=SMALL_WIDTH)
324
        return True
325

    
326
    (code, to_delete) = d.checklist("Choose which cloud accounts to delete:",
327
                                    choices=choices, width=WIDTH)
328

    
329
    if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
330
        return False
331

    
332
    if not len(to_delete):
333
        d.msgbox("Nothing selected!", width=SMALL_WIDTH)
334
        return False
335

    
336
    if not d.yesno("Are you sure you want to remove the selected cloud "
337
                   "accounts?", width=WIDTH, defaultno=1):
338
        for i in to_delete:
339
            Kamaki.remove_cloud(i)
340
            if 'cloud' in session and session['cloud'] == i:
341
                del session['cloud']
342
                if 'account' in session:
343
                    del session['account']
344
    else:
345
        return False
346

    
347
    d.msgbox("%d cloud accounts were deleted." % len(to_delete),
348
             width=SMALL_WIDTH)
349
    return True
350

    
351

    
352
def kamaki_menu(session):
353
    """Show kamaki related actions"""
354
    d = session['dialog']
355
    default_item = "Cloud"
356

    
357
    if 'cloud' not in session:
358
        cloud = Kamaki.get_default_cloud_name()
359
        if cloud:
360
            session['cloud'] = cloud
361
            session['account'] = Kamaki.get_account(cloud)
362
            if not session['account']:
363
                del session['account']
364
        else:
365
            default_item = "Add/Edit"
366

    
367
    while 1:
368
        cloud = session["cloud"] if "cloud" in session else "<none>"
369
        if 'account' not in session and 'cloud' in session:
370
            cloud += " <invalid>"
371

    
372
        upload = session["upload"] if "upload" in session else "<none>"
373

    
374
        choices = [("Add/Edit", "Add/Edit cloud accounts"),
375
                   ("Delete", "Delete existing cloud accounts"),
376
                   ("Cloud", "Select cloud account to use: %s" % cloud),
377
                   ("Upload", "Upload image to pithos+"),
378
                   ("Register", "Register the image to cyclades: %s" % upload)]
379

    
380
        (code, choice) = d.menu(
381
            text="Choose one of the following or press <Back> to go back.",
382
            width=WIDTH, choices=choices, cancel="Back", height=13,
383
            menu_height=5, default_item=default_item,
384
            title="Image Registration Menu")
385

    
386
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
387
            return False
388

    
389
        if choice == "Add/Edit":
390
            if modify_clouds(session):
391
                default_item = "Cloud"
392
        elif choice == "Delete":
393
            if delete_clouds(session):
394
                if len(Kamaki.get_clouds()):
395
                    default_item = "Cloud"
396
                else:
397
                    default_time = "Add/Edit"
398
            else:
399
                default_time = "Delete"
400
        elif choice == "Cloud":
401
            default_item = "Cloud"
402
            clouds = Kamaki.get_clouds()
403
            if not len(clouds):
404
                d.msgbox("No clouds available. Please add a new cloud!",
405
                         width=SMALL_WIDTH)
406
                default_item = "Add/Edit"
407
                continue
408

    
409
            if 'cloud' not in session:
410
                session['cloud'] = clouds.keys()[0]
411

    
412
            choices = []
413
            for name, info in clouds.items():
414
                default = 1 if session['cloud'] == name else 0
415
                descr = info['description'] if 'description' in info else ""
416
                choices.append((name, descr, default))
417

    
418
            (code, answer) = d.radiolist("Please select a cloud:",
419
                                         width=WIDTH, choices=choices)
420
            if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
421
                continue
422
            else:
423
                session['account'] = Kamaki.get_account(answer)
424

    
425
                if session['account'] is None:  # invalid account
426
                    if not d.yesno("The cloud %s' is not valid! Would you "
427
                                   "like to edit it?" % answer, width=WIDTH):
428
                        if edit_cloud(session, answer):
429
                            session['account'] = Kamaki.get_account(answer)
430
                            Kamaki.set_default_cloud(answer)
431

    
432
                if session['account'] is not None:
433
                    session['cloud'] = answer
434
                    Kamaki.set_default_cloud(answer)
435
                    default_item = "Upload"
436
                else:
437
                    del session['account']
438
                    del session['cloud']
439
        elif choice == "Upload":
440
            if upload_image(session):
441
                default_item = "Register"
442
            else:
443
                default_item = "Upload"
444
        elif choice == "Register":
445
            if register_image(session):
446
                return True
447
            else:
448
                default_item = "Register"
449

    
450

    
451
def add_property(session):
452
    """Add a new property to the image"""
453
    d = session['dialog']
454

    
455
    while 1:
456
        (code, answer) = d.inputbox("Please provide a name for a new image"
457
                                    " property:", width=WIDTH)
458
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
459
            return False
460

    
461
        name = answer.strip()
462
        if len(name) == 0:
463
            d.msgbox("A property name cannot be empty", width=SMALL_WIDTH)
464
            continue
465

    
466
        break
467

    
468
    while 1:
469
        (code, answer) = d.inputbox("Please provide a value for image "
470
                                    "property %s" % name, width=WIDTH)
471
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
472
            return False
473

    
474
        value = answer.strip()
475
        if len(value) == 0:
476
            d.msgbox("Value cannot be empty", width=SMALL_WIDTH)
477
            continue
478

    
479
        break
480

    
481
    session['metadata'][name] = value
482

    
483
    return True
484

    
485

    
486
def modify_properties(session):
487
    """Modify an existing image property"""
488
    d = session['dialog']
489

    
490
    while 1:
491
        choices = []
492
        for (key, val) in session['metadata'].items():
493
            choices.append((str(key), str(val)))
494

    
495
        (code, choice) = d.menu(
496
            "In this menu you can edit existing image properties or add new "
497
            "ones. Be careful! Most properties have special meaning and "
498
            "alter the image deployment behaviour. Press <HELP> to see more "
499
            "information about image properties. Press <BACK> when done.",
500
            height=18, width=WIDTH, choices=choices, menu_height=10,
501
            ok_label="Edit", extra_button=1, extra_label="Add", cancel="Back",
502
            help_button=1, title="Image Properties")
503

    
504
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
505
            return True
506
        # Edit button
507
        elif code == d.DIALOG_OK:
508
            (code, answer) = d.inputbox("Please provide a new value for the "
509
                                        "image property with name `%s':" %
510
                                        choice,
511
                                        init=session['metadata'][choice],
512
                                        width=WIDTH)
513
            if code not in (d.DIALOG_CANCEL, d.DIALOG_ESC):
514
                value = answer.strip()
515
                if len(value) == 0:
516
                    d.msgbox("Value cannot be empty!")
517
                    continue
518
                else:
519
                    session['metadata'][choice] = value
520
        # ADD button
521
        elif code == d.DIALOG_EXTRA:
522
            add_property(session)
523
        elif code == 'help':
524
            help_file = get_help_file("image_properties")
525
            assert os.path.exists(help_file)
526
            d.textbox(help_file, title="Image Properties", width=70, height=40)
527

    
528

    
529
def delete_properties(session):
530
    """Delete an image property"""
531
    d = session['dialog']
532

    
533
    choices = []
534
    for (key, val) in session['metadata'].items():
535
        choices.append((key, "%s" % val, 0))
536

    
537
    (code, to_delete) = d.checklist("Choose which properties to delete:",
538
                                    choices=choices, width=WIDTH)
539

    
540
    # If the user exits with ESC or CANCEL, the returned tag list is empty.
541
    for i in to_delete:
542
        del session['metadata'][i]
543

    
544
    cnt = len(to_delete)
545
    if cnt > 0:
546
        d.msgbox("%d image properties were deleted." % cnt, width=SMALL_WIDTH)
547
        return True
548
    else:
549
        return False
550

    
551

    
552
def exclude_tasks(session):
553
    """Exclude specific tasks from running during image deployment"""
554
    d = session['dialog']
555

    
556
    index = 0
557
    displayed_index = 1
558
    choices = []
559
    mapping = {}
560
    if 'excluded_tasks' not in session:
561
        session['excluded_tasks'] = []
562

    
563
    if -1 in session['excluded_tasks']:
564
        if not d.yesno("Image deployment configuration is disabled. "
565
                       "Do you wish to enable it?", width=SMALL_WIDTH):
566
            session['excluded_tasks'].remove(-1)
567
        else:
568
            return False
569

    
570
    for (msg, task, osfamily) in CONFIGURATION_TASKS:
571
        if session['metadata']['OSFAMILY'] in osfamily:
572
            checked = 1 if index in session['excluded_tasks'] else 0
573
            choices.append((str(displayed_index), msg, checked))
574
            mapping[displayed_index] = index
575
            displayed_index += 1
576
        index += 1
577

    
578
    while 1:
579
        (code, tags) = d.checklist(
580
            text="Please choose which configuration tasks you would like to "
581
                 "prevent from running during image deployment. "
582
                 "Press <No Config> to supress any configuration. "
583
                 "Press <Help> for more help on the image deployment "
584
                 "configuration tasks.",
585
            choices=choices, height=19, list_height=8, width=WIDTH,
586
            help_button=1, extra_button=1, extra_label="No Config",
587
            title="Exclude Configuration Tasks")
588

    
589
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
590
            return False
591
        elif code == d.DIALOG_HELP:
592
            help_file = get_help_file("configuration_tasks")
593
            assert os.path.exists(help_file)
594
            d.textbox(help_file, title="Configuration Tasks",
595
                      width=70, height=40)
596
        # No Config button
597
        elif code == d.DIALOG_EXTRA:
598
            session['excluded_tasks'] = [-1]
599
            session['task_metadata'] = ["EXCLUDE_ALL_TASKS"]
600
            break
601
        elif code == d.DIALOG_OK:
602
            session['excluded_tasks'] = []
603
            for tag in tags:
604
                session['excluded_tasks'].append(mapping[int(tag)])
605

    
606
            exclude_metadata = []
607
            for task in session['excluded_tasks']:
608
                exclude_metadata.extend(CONFIGURATION_TASKS[task][1])
609

    
610
            session['task_metadata'] = map(lambda x: "EXCLUDE_TASK_%s" % x,
611
                                           exclude_metadata)
612
            break
613

    
614
    return True
615

    
616

    
617
def sysprep(session):
618
    """Perform various system preperation tasks on the image"""
619
    d = session['dialog']
620
    image = session['image']
621

    
622
    # Is the image already shrinked?
623
    if 'shrinked' in session and session['shrinked']:
624
        msg = "It seems you have shrinked the image. Running system " \
625
              "preparation tasks on a shrinked image is dangerous."
626

    
627
        if d.yesno("%s\n\nDo you really want to continue?" % msg,
628
                   width=SMALL_WIDTH, defaultno=1):
629
            return
630

    
631
    wrapper = textwrap.TextWrapper(width=WIDTH - 5)
632

    
633
    help_title = "System Preperation Tasks"
634
    sysprep_help = "%s\n%s\n\n" % (help_title, '=' * len(help_title))
635

    
636
    syspreps = image.os.list_syspreps()
637

    
638
    if len(syspreps) == 0:
639
        d.msgbox("No system preparation task available to run!",
640
                 title="System Preperation", width=SMALL_WIDTH)
641
        return
642

    
643
    while 1:
644
        choices = []
645
        index = 0
646
        for sysprep in syspreps:
647
            name, descr = image.os.sysprep_info(sysprep)
648
            display_name = name.replace('-', ' ').capitalize()
649
            sysprep_help += "%s\n" % display_name
650
            sysprep_help += "%s\n" % ('-' * len(display_name))
651
            sysprep_help += "%s\n\n" % wrapper.fill(" ".join(descr.split()))
652
            enabled = 1 if sysprep.enabled else 0
653
            choices.append((str(index + 1), display_name, enabled))
654
            index += 1
655

    
656
        (code, tags) = d.checklist(
657
            "Please choose which system preparation tasks you would like to "
658
            "run on the image. Press <Help> to see details about the system "
659
            "preparation tasks.", title="Run system preparation tasks",
660
            choices=choices, width=70, ok_label="Run", help_button=1)
661

    
662
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
663
            return False
664
        elif code == d.DIALOG_HELP:
665
            d.scrollbox(sysprep_help, width=WIDTH)
666
        elif code == d.DIALOG_OK:
667
            # Enable selected syspreps and disable the rest
668
            for i in range(len(syspreps)):
669
                if str(i + 1) in tags:
670
                    image.os.enable_sysprep(syspreps[i])
671
                else:
672
                    image.os.disable_sysprep(syspreps[i])
673

    
674
            if len([s for s in image.os.list_syspreps() if s.enabled]) == 0:
675
                d.msgbox("No system preperation task is selected!",
676
                         title="System Preperation", width=SMALL_WIDTH)
677
                continue
678

    
679
            infobox = InfoBoxOutput(d, "Image Configuration")
680
            try:
681
                image.out.add(infobox)
682
                try:
683
                    # The checksum is invalid. We have mounted the image rw
684
                    if 'checksum' in session:
685
                        del session['checksum']
686

    
687
                    # Monitor the metadata changes during syspreps
688
                    with MetadataMonitor(session, image.os.meta):
689
                        try:
690
                            image.os.do_sysprep()
691
                            infobox.finalize()
692
                        except FatalError as e:
693
                            title = "System Preparation"
694
                            d.msgbox("System Preparation failed: %s" % e,
695
                                     title=title, width=SMALL_WIDTH)
696
                finally:
697
                    image.out.remove(infobox)
698
            finally:
699
                infobox.cleanup()
700
            break
701
    return True
702

    
703

    
704
def shrink(session):
705
    """Shrink the image"""
706
    d = session['dialog']
707
    image = session['image']
708

    
709
    shrinked = 'shrinked' in session and session['shrinked']
710

    
711
    if shrinked:
712
        d.msgbox("The image is already shrinked!", title="Image Shrinking",
713
                 width=SMALL_WIDTH)
714
        return True
715

    
716
    msg = "This operation will shrink the last partition of the image to " \
717
          "reduce the total image size. If the last partition is a swap " \
718
          "partition, then this partition is removed and the partition " \
719
          "before that is shrinked. The removed swap partition will be " \
720
          "recreated during image deployment."
721

    
722
    if not d.yesno("%s\n\nDo you want to continue?" % msg, width=WIDTH,
723
                   height=12, title="Image Shrinking"):
724
        with MetadataMonitor(session, image.meta):
725
            infobox = InfoBoxOutput(d, "Image Shrinking", height=4)
726
            image.out.add(infobox)
727
            try:
728
                image.shrink()
729
                infobox.finalize()
730
            finally:
731
                image.out.remove(infobox)
732

    
733
        session['shrinked'] = True
734
        update_background_title(session)
735
    else:
736
        return False
737

    
738
    return True
739

    
740

    
741
def customization_menu(session):
742
    """Show image customization menu"""
743
    d = session['dialog']
744

    
745
    choices = [("Sysprep", "Run various image preparation tasks"),
746
               ("Shrink", "Shrink image"),
747
               ("View/Modify", "View/Modify image properties"),
748
               ("Delete", "Delete image properties"),
749
               ("Exclude", "Exclude various deployment tasks from running")]
750

    
751
    default_item = 0
752

    
753
    actions = {"Sysprep": sysprep,
754
               "Shrink": shrink,
755
               "View/Modify": modify_properties,
756
               "Delete": delete_properties,
757
               "Exclude": exclude_tasks}
758
    while 1:
759
        (code, choice) = d.menu(
760
            text="Choose one of the following or press <Back> to exit.",
761
            width=WIDTH, choices=choices, cancel="Back", height=13,
762
            menu_height=len(choices), default_item=choices[default_item][0],
763
            title="Image Customization Menu")
764

    
765
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
766
            break
767
        elif choice in actions:
768
            default_item = [entry[0] for entry in choices].index(choice)
769
            if actions[choice](session):
770
                default_item = (default_item + 1) % len(choices)
771

    
772

    
773
def main_menu(session):
774
    """Show the main menu of the program"""
775
    d = session['dialog']
776

    
777
    update_background_title(session)
778

    
779
    choices = [("Customize", "Customize image & cloud deployment options"),
780
               ("Register", "Register image to a cloud"),
781
               ("Extract", "Dump image to local file system"),
782
               ("Reset", "Reset everything and start over again"),
783
               ("Help", "Get help for using snf-image-creator")]
784

    
785
    default_item = "Customize"
786

    
787
    actions = {"Customize": customization_menu, "Register": kamaki_menu,
788
               "Extract": extract_image}
789
    while 1:
790
        (code, choice) = d.menu(
791
            text="Choose one of the following or press <Exit> to exit.",
792
            width=WIDTH, choices=choices, cancel="Exit", height=13,
793
            default_item=default_item, menu_height=len(choices),
794
            title="Image Creator for ~okeanos (snf-image-creator version %s)" %
795
                  version)
796

    
797
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
798
            if confirm_exit(d):
799
                break
800
        elif choice == "Reset":
801
            if confirm_reset(d):
802
                d.infobox("Resetting snf-image-creator. Please wait...",
803
                          width=SMALL_WIDTH)
804
                raise Reset
805
        elif choice == "Help":
806
            d.msgbox("For help, check the online documentation:\n\nhttp://www"
807
                     ".synnefo.org/docs/snf-image-creator/latest/",
808
                     width=WIDTH, title="Help")
809
        elif choice in actions:
810
            actions[choice](session)
811

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