2 # -*- coding: utf-8 -*-
4 # Copyright 2012 GRNET S.A. All rights reserved.
6 # Redistribution and use in source and binary forms, with or
7 # without modification, are permitted provided that the following
10 # 1. Redistributions of source code must retain the above
11 # copyright notice, this list of conditions and the following
14 # 2. Redistributions in binary form must reproduce the above
15 # copyright notice, this list of conditions and the following
16 # disclaimer in the documentation and/or other materials
17 # provided with the distribution.
19 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
20 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
23 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
32 # The views and conclusions contained in the software and
33 # documentation are those of the authors and should not be
34 # interpreted as representing official policies, either expressed
35 # or implied, of GRNET S.A.
37 """This module is the entrance point for the dialog-based version of the
38 snf-image-creator program. The main function will create a dialog where the
39 user is asked if he wants to use the program in expert or wizard mode.
51 from image_creator import __version__ as version
52 from image_creator.util import FatalError
53 from image_creator.output import Output
54 from image_creator.output.cli import SimpleOutput
55 from image_creator.output.dialog import GaugeOutput
56 from image_creator.output.composite import CompositeOutput
57 from image_creator.disk import Disk
58 from image_creator.dialog_wizard import start_wizard
59 from image_creator.dialog_menu import main_menu
60 from image_creator.dialog_util import SMALL_WIDTH, WIDTH, confirm_exit, \
61 Reset, update_background_title
64 def create_image(d, media, out, tmp):
65 """Create an image out of `media'"""
66 d.setBackgroundTitle('snf-image-creator')
68 gauge = GaugeOutput(d, "Initialization", "Initializing...")
70 disk = Disk(media, out, tmp)
72 def signal_handler(signum, frame):
76 signal.signal(signal.SIGINT, signal_handler)
77 signal.signal(signal.SIGTERM, signal_handler)
79 snapshot = disk.snapshot()
80 image = disk.get_image(snapshot)
82 out.output("Collecting image metadata ...")
84 for (key, value) in image.meta.items():
85 metadata[str(key)] = str(value)
87 for (key, value) in image.os.meta.items():
88 metadata[str(key)] = str(value)
94 # Make sure the signal handler does not call gauge.cleanup again
97 gauge.cleanup = type(GaugeOutput.cleanup)(dummy, gauge, GaugeOutput)
99 session = {"dialog": d,
102 "metadata": metadata}
104 msg = "snf-image-creator detected a %s system on the input media. " \
105 "Would you like to run a wizard to assist you through the " \
106 "image creation process?\n\nChoose <Wizard> to run the wizard," \
107 " <Expert> to run the snf-image-creator in expert mode or " \
108 "press ESC to quit the program." \
109 % (image.ostype if image.ostype == image.distro or
110 image.distro == "unknown" else "%s (%s)" %
111 (image.ostype, image.distro))
113 update_background_title(session)
116 code = d.yesno(msg, width=WIDTH, height=12, yes_label="Wizard",
118 if code == d.DIALOG_OK:
119 if start_wizard(session):
121 elif code == d.DIALOG_CANCEL:
128 d.infobox("Thank you for using snf-image-creator. Bye", width=53)
135 def select_file(d, media):
136 """Select a media file"""
140 default = os.getcwd() + os.sep
142 if media is not None:
143 if not os.path.exists(media):
144 d.msgbox("The file `%s' you choose does not exist." % media,
147 mode = os.stat(media).st_mode
148 if not stat.S_ISDIR(mode):
152 (code, media) = d.fselect(default, 10, 60, extra_button=1,
153 title="Please select an input media.",
154 extra_label="Bundle Host")
155 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
156 if confirm_exit(d, "You canceled the media selection dialog box."):
161 elif code == d.DIALOG_EXTRA:
167 def _dialog_form(self, text, height=20, width=60, form_height=15, fields=[],
169 """Display a form box.
171 fields is in the form: [(label1, item1, item_length1), ...]
174 cmd = ["--form", text, str(height), str(width), str(form_height)]
178 if len(field[0]) > label_len:
179 label_len = len(field[0])
181 input_len = width - label_len - 1
188 cmd.extend((label, str(line), str(1), item, str(line),
189 str(label_len + 1), str(input_len), str(item_len)))
192 code, output = self._perform(*(cmd,), **kwargs)
197 return (code, output.splitlines())
202 d = dialog.Dialog(dialog="dialog")
204 # Add extra button in dialog library
205 dialog._common_args_syntax["extra_button"] = \
206 lambda enable: dialog._simple_option("--extra-button", enable)
208 dialog._common_args_syntax["extra_label"] = \
209 lambda string: ("--extra-label", string)
211 # Allow yes-no label overwriting
212 dialog._common_args_syntax["yes_label"] = \
213 lambda string: ("--yes-label", string)
215 dialog._common_args_syntax["no_label"] = \
216 lambda string: ("--no-label", string)
218 # Monkey-patch pythondialog to include support for form dialog boxes
219 if not hasattr(dialog, 'form'):
220 d.form = types.MethodType(_dialog_form, d)
222 usage = "Usage: %prog [options] [<input_media>]"
223 parser = optparse.OptionParser(version=version, usage=usage)
224 parser.add_option("-l", "--logfile", type="string", dest="logfile",
225 default=None, help="log all messages to FILE",
227 parser.add_option("--tmpdir", type="string", dest="tmp", default=None,
228 help="create large temporary image files under DIR",
231 options, args = parser.parse_args(sys.argv[1:])
234 parser.error("Wrong number of arguments")
236 d.setBackgroundTitle('snf-image-creator')
239 if os.geteuid() != 0:
240 raise FatalError("You must run %s as root" %
241 parser.get_prog_name())
243 if options.tmp is not None and not os.path.isdir(options.tmp):
244 raise FatalError("The directory `%s' specified with --tmpdir is "
245 "not valid" % options.tmp)
248 if options.logfile is not None:
250 logfile = open(options.logfile, 'w')
253 "Unable to open logfile `%s' for writing. Reason: %s" %
254 (options.logfile, e.strerror))
256 media = select_file(d, args[0] if len(args) == 1 else None)
259 log = SimpleOutput(False, logfile) if logfile is not None \
263 out = CompositeOutput([log])
264 out.output("Starting %s v%s ..." %
265 (parser.get_prog_name(), version))
266 ret = create_image(d, media, out, options.tmp)
269 log.output("Resetting everything ...")
272 if logfile is not None:
274 except FatalError as e:
275 msg = textwrap.fill(str(e), width=WIDTH)
276 d.infobox(msg, width=WIDTH, title="Fatal Error")
279 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :