Monkey patch the source to work with gevent
[snf-image-creator] / image_creator / dialog_main.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 gevent.monkey  # Monkey-patch everything for gevent early on
37 gevent.monkey.patch_all()
38
39 import dialog
40 import sys
41 import os
42 import textwrap
43 import signal
44 import optparse
45
46 from image_creator import __version__ as version
47 from image_creator.util import FatalError
48 from image_creator.output import Output
49 from image_creator.output.cli import SimpleOutput
50 from image_creator.output.dialog import GaugeOutput
51 from image_creator.output.composite import CompositeOutput
52 from image_creator.disk import Disk
53 from image_creator.os_type import os_cls
54 from image_creator.dialog_wizard import wizard
55 from image_creator.dialog_menu import main_menu
56 from image_creator.dialog_util import SMALL_WIDTH, WIDTH, confirm_exit, \
57     Reset, update_background_title
58
59
60 def image_creator(d, media, out):
61
62     d.setBackgroundTitle('snf-image-creator')
63
64     gauge = GaugeOutput(d, "Initialization", "Initializing...")
65     out.add(gauge)
66     disk = Disk(media, out)
67
68     def signal_handler(signum, frame):
69         gauge.cleanup()
70         disk.cleanup()
71
72     signal.signal(signal.SIGINT, signal_handler)
73     signal.signal(signal.SIGTERM, signal_handler)
74     try:
75         snapshot = disk.snapshot()
76         dev = disk.get_device(snapshot)
77
78         metadata = {}
79         for (key, value) in dev.meta.items():
80             metadata[str(key)] = str(value)
81
82         dev.mount(readonly=True)
83         out.output("Collecting image metadata...")
84         cls = os_cls(dev.distro, dev.ostype)
85         image_os = cls(dev.root, dev.g, out)
86         dev.umount()
87
88         for (key, value) in image_os.meta.items():
89             metadata[str(key)] = str(value)
90
91         out.success("done")
92         gauge.cleanup()
93         out.remove(gauge)
94
95         # Make sure the signal handler does not call gauge.cleanup again
96         def dummy(self):
97             pass
98         gauge.cleanup = type(GaugeOutput.cleanup)(dummy, gauge, GaugeOutput)
99
100         session = {"dialog": d,
101                    "disk": disk,
102                    "snapshot": snapshot,
103                    "device": dev,
104                    "image_os": image_os,
105                    "metadata": metadata}
106
107         msg = "snf-image-creator detected a %s system on the input media. " \
108               "Would you like to run a wizard to assist you through the " \
109               "image creation process?\n\nChoose <Wizard> to run the wizard," \
110               " <Expert> to run the snf-image-creator in expert mode or " \
111               "press ESC to quit the program." \
112               % (dev.ostype if dev.ostype == dev.distro else "%s (%s)" %
113                  (dev.ostype, dev.distro))
114
115         update_background_title(session)
116
117         while True:
118             code = d.yesno(msg, width=WIDTH, height=12, yes_label="Wizard",
119                            no_label="Expert")
120             if code == d.DIALOG_OK:
121                 if wizard(session):
122                     break
123             elif code == d.DIALOG_CANCEL:
124                 main_menu(session)
125                 break
126
127             if confirm_exit(d):
128                 break
129
130         d.infobox("Thank you for using snf-image-creator. Bye", width=53)
131     finally:
132         disk.cleanup()
133
134     return 0
135
136
137 def select_file(d, media):
138     root = os.sep
139     while 1:
140         if media is not None:
141             if not os.path.exists(media):
142                 d.msgbox("The file `%s' you choose does not exist." % media,
143                          width=SMALL_WIDTH)
144             else:
145                 break
146
147         (code, media) = d.fselect(root, 10, 50,
148                                   title="Please select input media")
149         if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
150             if confirm_exit(d, "You canceled the media selection dialog box."):
151                 sys.exit(0)
152             else:
153                 media = None
154                 continue
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
183     options, args = parser.parse_args(sys.argv[1:])
184
185     if len(args) > 1:
186         parser.error("Wrong number of arguments")
187
188     d.setBackgroundTitle('snf-image-creator')
189
190     try:
191         if os.geteuid() != 0:
192             raise FatalError("You must run %s as root" %
193                              parser.get_prog_name())
194
195         media = select_file(d, args[0] if len(args) == 1 else None)
196
197         logfile = None
198         if options.logfile is not None:
199             try:
200                 logfile = open(options.logfile, 'w')
201             except IOError as e:
202                 raise FatalError(
203                     "Unable to open logfile `%s' for writing. Reason: %s" %
204                     (options.logfile, e.strerror))
205         try:
206             log = SimpleOutput(False, logfile) if logfile is not None \
207                 else Output()
208             while 1:
209                 try:
210                     out = CompositeOutput([log])
211                     out.output("Starting %s v%s..." %
212                                (parser.get_prog_name(), version))
213                     ret = image_creator(d, media, out)
214                     sys.exit(ret)
215                 except Reset:
216                     log.output("Resetting everything...")
217                     continue
218         finally:
219             if logfile is not None:
220                 logfile.close()
221     except FatalError as e:
222         msg = textwrap.fill(str(e), width=WIDTH)
223         d.infobox(msg, width=WIDTH, title="Fatal Error")
224         sys.exit(1)
225
226 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :