3 # Copyright 2012 GRNET S.A. All rights reserved.
5 # Redistribution and use in source and binary forms, with or
6 # without modification, are permitted provided that the following
9 # 1. Redistributions of source code must retain the above
10 # copyright notice, this list of conditions and the following
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.
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.
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.
43 from image_creator import __version__ as version
44 from image_creator.util import FatalError
45 from image_creator.output import Output
46 from image_creator.output.cli import SimpleOutput
47 from image_creator.output.dialog import GaugeOutput
48 from image_creator.output.composite import CompositeOutput
49 from image_creator.disk import Disk
50 from image_creator.os_type import os_cls
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
57 def image_creator(d, media, out):
59 d.setBackgroundTitle('snf-image-creator')
61 gauge = GaugeOutput(d, "Initialization", "Initializing...")
63 disk = Disk(media, out)
65 def signal_handler(signum, frame):
69 signal.signal(signal.SIGINT, signal_handler)
70 signal.signal(signal.SIGTERM, signal_handler)
72 snapshot = disk.snapshot()
73 dev = disk.get_device(snapshot)
76 for (key, value) in dev.meta.items():
77 metadata[str(key)] = str(value)
79 dev.mount(readonly=True)
80 out.output("Collecting image metadata...")
81 cls = os_cls(dev.distro, dev.ostype)
82 image_os = cls(dev.root, dev.g, out)
85 for (key, value) in image_os.meta.items():
86 metadata[str(key)] = str(value)
92 # Make sure the signal handler does not call gauge.cleanup again
95 gauge.cleanup = type(GaugeOutput.cleanup)(dummy, gauge, GaugeOutput)
97 session = {"dialog": d,
101 "image_os": image_os,
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 % (dev.ostype if dev.ostype == dev.distro else "%s (%s)" %
110 (dev.ostype, dev.distro))
112 update_background_title(session)
115 code = d.yesno(msg, width=WIDTH, height=12, yes_label="Wizard",
117 if code == d.DIALOG_OK:
120 elif code == d.DIALOG_CANCEL:
127 d.infobox("Thank you for using snf-image-creator. Bye", width=53)
134 def select_file(d, media):
137 if media is not None:
138 if not os.path.exists(media):
139 d.msgbox("The file `%s' you choose does not exist." % media,
144 (code, media) = d.fselect(root, 10, 50,
145 title="Please select input media")
146 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
147 if confirm_exit(d, "You canceled the media selection dialog box."):
158 d = dialog.Dialog(dialog="dialog")
160 # Add extra button in dialog library
161 dialog._common_args_syntax["extra_button"] = \
162 lambda enable: dialog._simple_option("--extra-button", enable)
164 dialog._common_args_syntax["extra_label"] = \
165 lambda string: ("--extra-label", string)
167 # Allow yes-no label overwriting
168 dialog._common_args_syntax["yes_label"] = \
169 lambda string: ("--yes-label", string)
171 dialog._common_args_syntax["no_label"] = \
172 lambda string: ("--no-label", string)
174 usage = "Usage: %prog [options] [<input_media>]"
175 parser = optparse.OptionParser(version=version, usage=usage)
176 parser.add_option("-l", "--logfile", type="string", dest="logfile",
177 default=None, help="log all messages to FILE",
180 options, args = parser.parse_args(sys.argv[1:])
183 parser.error("Wrong number of arguments")
185 d.setBackgroundTitle('snf-image-creator')
188 if os.geteuid() != 0:
189 raise FatalError("You must run %s as root" %
190 parser.get_prog_name())
192 media = select_file(d, args[0] if len(args) == 1 else None)
195 if options.logfile is not None:
197 logfile = open(options.logfile, 'w')
200 "Unable to open logfile `%s' for writing. Reason: %s" %
201 (options.logfile, e.strerror))
203 log = SimpleOutput(False, logfile) if logfile is not None \
207 out = CompositeOutput([log])
208 out.output("Starting %s v%s..." %
209 (parser.get_prog_name(), version))
210 ret = image_creator(d, media, out)
213 log.output("Resetting everything...")
216 if logfile is not None:
218 except FatalError as e:
219 msg = textwrap.fill(str(e), width=WIDTH)
220 d.infobox(msg, width=WIDTH, title="Fatal Error")
223 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :