Statistics
| Branch: | Tag: | Revision:

root / image_creator / dialog_main.py @ f5174d2c

History | View | Annotate | Download (8 kB)

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 dialog
37
import sys
38
import os
39
import stat
40
import textwrap
41
import signal
42
import optparse
43

    
44
from image_creator import __version__ as version
45
from image_creator.util import FatalError
46
from image_creator.output import Output
47
from image_creator.output.cli import SimpleOutput
48
from image_creator.output.dialog import GaugeOutput
49
from image_creator.output.composite import CompositeOutput
50
from image_creator.disk import Disk
51
from image_creator.dialog_wizard import wizard
52
from image_creator.dialog_menu import main_menu
53
from image_creator.dialog_util import SMALL_WIDTH, WIDTH, confirm_exit, \
54
    Reset, update_background_title
55

    
56

    
57
def create_image(d, media, out, tmp):
58

    
59
    d.setBackgroundTitle('snf-image-creator')
60

    
61
    gauge = GaugeOutput(d, "Initialization", "Initializing...")
62
    out.add(gauge)
63
    disk = Disk(media, out, tmp)
64

    
65
    def signal_handler(signum, frame):
66
        gauge.cleanup()
67
        disk.cleanup()
68

    
69
    signal.signal(signal.SIGINT, signal_handler)
70
    signal.signal(signal.SIGTERM, signal_handler)
71
    try:
72
        snapshot = disk.snapshot()
73
        image = disk.get_image(snapshot)
74

    
75
        out.output("Collecting image metadata...")
76
        metadata = {}
77
        for (key, value) in image.meta.items():
78
            metadata[str(key)] = str(value)
79

    
80
        for (key, value) in image.os.meta.items():
81
            metadata[str(key)] = str(value)
82

    
83
        out.success("done")
84
        gauge.cleanup()
85
        out.remove(gauge)
86

    
87
        # Make sure the signal handler does not call gauge.cleanup again
88
        def dummy(self):
89
            pass
90
        gauge.cleanup = type(GaugeOutput.cleanup)(dummy, gauge, GaugeOutput)
91

    
92
        session = {"dialog": d,
93
                   "disk": disk,
94
                   "image": image,
95
                   "metadata": metadata}
96

    
97
        msg = "snf-image-creator detected a %s system on the input media. " \
98
              "Would you like to run a wizard to assist you through the " \
99
              "image creation process?\n\nChoose <Wizard> to run the wizard," \
100
              " <Expert> to run the snf-image-creator in expert mode or " \
101
              "press ESC to quit the program." \
102
              % (image.ostype if image.ostype == image.distro else "%s (%s)" %
103
                 (image.ostype, image.distro))
104

    
105
        update_background_title(session)
106

    
107
        while True:
108
            code = d.yesno(msg, width=WIDTH, height=12, yes_label="Wizard",
109
                           no_label="Expert")
110
            if code == d.DIALOG_OK:
111
                if wizard(session):
112
                    break
113
            elif code == d.DIALOG_CANCEL:
114
                main_menu(session)
115
                break
116

    
117
            if confirm_exit(d):
118
                break
119

    
120
        d.infobox("Thank you for using snf-image-creator. Bye", width=53)
121
    finally:
122
        disk.cleanup()
123

    
124
    return 0
125

    
126

    
127
def select_file(d, media):
128

    
129
    if media == '/':
130
        return '/'
131

    
132
    default = os.getcwd() + os.sep
133
    while 1:
134
        if media is not None:
135
            if not os.path.exists(media):
136
                d.msgbox("The file `%s' you choose does not exist." % media,
137
                         width=SMALL_WIDTH)
138
            else:
139
                mode = os.stat(media).st_mode
140
                if not stat.S_ISDIR(mode):
141
                    break
142
                default = media
143

    
144
        (code, media) = d.fselect(default, 10, 60, extra_button=1,
145
                                  title="Please select an input media.",
146
                                  extra_label="Bundle Host")
147
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
148
            if confirm_exit(d, "You canceled the media selection dialog box."):
149
                sys.exit(0)
150
            else:
151
                media = None
152
                continue
153
        elif code == d.DIALOG_EXTRA:
154
            return '/'
155

    
156
    return media
157

    
158

    
159
def main():
160

    
161
    d = dialog.Dialog(dialog="dialog")
162

    
163
    # Add extra button in dialog library
164
    dialog._common_args_syntax["extra_button"] = \
165
        lambda enable: dialog._simple_option("--extra-button", enable)
166

    
167
    dialog._common_args_syntax["extra_label"] = \
168
        lambda string: ("--extra-label", string)
169

    
170
    # Allow yes-no label overwriting
171
    dialog._common_args_syntax["yes_label"] = \
172
        lambda string: ("--yes-label", string)
173

    
174
    dialog._common_args_syntax["no_label"] = \
175
        lambda string: ("--no-label", string)
176

    
177
    usage = "Usage: %prog [options] [<input_media>]"
178
    parser = optparse.OptionParser(version=version, usage=usage)
179
    parser.add_option("-l", "--logfile", type="string", dest="logfile",
180
                      default=None, help="log all messages to FILE",
181
                      metavar="FILE")
182
    parser.add_option("--tmpdir", type="string", dest="tmp", default=None,
183
                      help="create large temporary image files under DIR",
184
                      metavar="DIR")
185

    
186
    options, args = parser.parse_args(sys.argv[1:])
187

    
188
    if len(args) > 1:
189
        parser.error("Wrong number of arguments")
190

    
191
    d.setBackgroundTitle('snf-image-creator')
192

    
193
    try:
194
        if os.geteuid() != 0:
195
            raise FatalError("You must run %s as root" %
196
                             parser.get_prog_name())
197

    
198
        if options.tmp is not None and not os.path.isdir(options.tmp):
199
            raise FatalError("The directory `%s' specified with --tmpdir is "
200
                             "not valid" % options.tmp)
201

    
202
        logfile = None
203
        if options.logfile is not None:
204
            try:
205
                logfile = open(options.logfile, 'w')
206
            except IOError as e:
207
                raise FatalError(
208
                    "Unable to open logfile `%s' for writing. Reason: %s" %
209
                    (options.logfile, e.strerror))
210

    
211
        media = select_file(d, args[0] if len(args) == 1 else None)
212

    
213
        try:
214
            log = SimpleOutput(False, logfile) if logfile is not None \
215
                else Output()
216
            while 1:
217
                try:
218
                    out = CompositeOutput([log])
219
                    out.output("Starting %s v%s..." %
220
                               (parser.get_prog_name(), version))
221
                    ret = create_image(d, media, out, options.tmp)
222
                    sys.exit(ret)
223
                except Reset:
224
                    log.output("Resetting everything...")
225
                    continue
226
        finally:
227
            if logfile is not None:
228
                logfile.close()
229
    except FatalError as e:
230
        msg = textwrap.fill(str(e), width=WIDTH)
231
        d.infobox(msg, width=WIDTH, title="Fatal Error")
232
        sys.exit(1)
233

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