Monitor passworded users in FreeBSD
[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 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 start_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     """Create an image out of `media'"""
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 or
103                  image.distro == "unknown" else "%s (%s)" %
104                  (image.ostype, image.distro))
105
106         update_background_title(session)
107
108         while True:
109             code = d.yesno(msg, width=WIDTH, height=12, yes_label="Wizard",
110                            no_label="Expert")
111             if code == d.DIALOG_OK:
112                 if start_wizard(session):
113                     break
114             elif code == d.DIALOG_CANCEL:
115                 main_menu(session)
116                 break
117
118             if confirm_exit(d):
119                 break
120
121         d.infobox("Thank you for using snf-image-creator. Bye", width=53)
122     finally:
123         disk.cleanup()
124
125     return 0
126
127
128 def select_file(d, media):
129     """Select a media file"""
130     if media == '/':
131         return '/'
132
133     default = os.getcwd() + os.sep
134     while 1:
135         if media is not None:
136             if not os.path.exists(media):
137                 d.msgbox("The file `%s' you choose does not exist." % media,
138                          width=SMALL_WIDTH)
139             else:
140                 mode = os.stat(media).st_mode
141                 if not stat.S_ISDIR(mode):
142                     break
143                 default = media
144
145         (code, media) = d.fselect(default, 10, 60, extra_button=1,
146                                   title="Please select an input media.",
147                                   extra_label="Bundle Host")
148         if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
149             if confirm_exit(d, "You canceled the media selection dialog box."):
150                 sys.exit(0)
151             else:
152                 media = None
153                 continue
154         elif code == d.DIALOG_EXTRA:
155             return '/'
156
157     return media
158
159
160 def main():
161
162     d = dialog.Dialog(dialog="dialog")
163
164     # Add extra button in dialog library
165     dialog._common_args_syntax["extra_button"] = \
166         lambda enable: dialog._simple_option("--extra-button", enable)
167
168     dialog._common_args_syntax["extra_label"] = \
169         lambda string: ("--extra-label", string)
170
171     # Allow yes-no label overwriting
172     dialog._common_args_syntax["yes_label"] = \
173         lambda string: ("--yes-label", string)
174
175     dialog._common_args_syntax["no_label"] = \
176         lambda string: ("--no-label", string)
177
178     usage = "Usage: %prog [options] [<input_media>]"
179     parser = optparse.OptionParser(version=version, usage=usage)
180     parser.add_option("-l", "--logfile", type="string", dest="logfile",
181                       default=None, help="log all messages to FILE",
182                       metavar="FILE")
183     parser.add_option("--tmpdir", type="string", dest="tmp", default=None,
184                       help="create large temporary image files under DIR",
185                       metavar="DIR")
186
187     options, args = parser.parse_args(sys.argv[1:])
188
189     if len(args) > 1:
190         parser.error("Wrong number of arguments")
191
192     d.setBackgroundTitle('snf-image-creator')
193
194     try:
195         if os.geteuid() != 0:
196             raise FatalError("You must run %s as root" %
197                              parser.get_prog_name())
198
199         if options.tmp is not None and not os.path.isdir(options.tmp):
200             raise FatalError("The directory `%s' specified with --tmpdir is "
201                              "not valid" % options.tmp)
202
203         logfile = None
204         if options.logfile is not None:
205             try:
206                 logfile = open(options.logfile, 'w')
207             except IOError as e:
208                 raise FatalError(
209                     "Unable to open logfile `%s' for writing. Reason: %s" %
210                     (options.logfile, e.strerror))
211
212         media = select_file(d, args[0] if len(args) == 1 else None)
213
214         try:
215             log = SimpleOutput(False, logfile) if logfile is not None \
216                 else Output()
217             while 1:
218                 try:
219                     out = CompositeOutput([log])
220                     out.output("Starting %s v%s..." %
221                                (parser.get_prog_name(), version))
222                     ret = create_image(d, media, out, options.tmp)
223                     sys.exit(ret)
224                 except Reset:
225                     log.output("Resetting everything...")
226                     continue
227         finally:
228             if logfile is not None:
229                 logfile.close()
230     except FatalError as e:
231         msg = textwrap.fill(str(e), width=WIDTH)
232         d.infobox(msg, width=WIDTH, title="Fatal Error")
233         sys.exit(1)
234
235 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :