a22557933709d453b9f59d603118a8664696d2d9
[snf-image-creator] / image_creator / main.py
1 #!/usr/bin/env python
2
3 # Copyright 2011 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 from image_creator import get_os_class
37 from image_creator import __version__ as version
38 from image_creator import FatalError
39 from image_creator.disk import Disk
40 from image_creator.util import get_command
41
42 import sys
43 import os
44 import optparse
45
46 dd = get_command('dd')
47
48
49 def check_writable_dir(option, opt_str, value, parser):
50     dirname = os.path.dirname(value)
51     name = os.path.basename(value)
52     if dirname and not os.path.isdir(dirname):
53         parser.error("`%s' is not an existing directory" % dirname)
54
55     if not name:
56         parser.error("`%s' is not a valid file name" % dirname)
57
58     setattr(parser.values, option.dest, value)
59
60
61 def parse_options(input_args):
62     usage = "Usage: %prog [options] <input_media>"
63     parser = optparse.OptionParser(version=version, usage=usage)
64
65     parser.add_option("-f", "--force", dest="force", default=False,
66         action="store_true", help="Overwrite output files if they exist")
67
68     parser.add_option("--no-cleanup", dest="cleanup", default=True,
69         help="Don't cleanup sensitive data before extracting the image",
70         action="store_false")
71
72     parser.add_option("--no-sysprep", dest="sysprep", default=True,
73         help="Don't perform system preperation before extracting the image",
74         action="store_false")
75
76     parser.add_option("--no-shrink", dest="shrink", default=True,
77         help="Don't shrink any partition before extracting the image",
78         action="store_false")
79
80     parser.add_option("-o", "--outfile", type="string", dest="outfile",
81         default=None, action="callback", callback=check_writable_dir,
82         help="Output image file",
83         metavar="FILE")
84
85     parser.add_option("-u", "--upload", dest="upload", default=False,
86         help="Upload image to a pithos repository using kamaki",
87         action="store_true")
88
89     parser.add_option("-r", "--register", dest="register", default=False,
90         help="Register image to okeanos using kamaki", action="store_true")
91
92     options, args = parser.parse_args(input_args)
93
94     if len(args) != 1:
95         parser.error('Wrong number of arguments')
96     options.source = args[0]
97     if not os.path.exists(options.source):
98         parser.error('input media is not accessible')
99
100     if options.register:
101         options.upload = True
102
103     if options.outfile is None and not options.upload:
104         parser.error('either outfile (-o) or upload (-u) must be set.')
105
106     return options
107
108
109 def image_creator():
110
111     options = parse_options(sys.argv[1:])
112
113     if os.geteuid() != 0:
114         raise FatalError("You must run %s as root" \
115                         % os.path.basename(sys.argv[0]))
116
117     if not options.force:
118         for extension in ('', '.meta'):
119             filename = "%s%s" % (options.outfile, extension)
120             if os.path.exists(filename):
121                 raise FatalError("Output file %s exists "
122                     "(use --force to overwrite it)." % filename)
123
124     disk = Disk(options.source)
125     try:
126         dev = disk.get_device()
127         dev.mount()
128         osclass = get_os_class(dev.distro, dev.ostype)
129         image_os = osclass(dev.root, dev.g)
130         metadata = image_os.get_metadata()
131
132         if options.sysprep:
133             image_os.sysprep()
134
135         if options.cleanup:
136             image_os.data_cleanup()
137
138         dev.umount()
139
140         size = options.shrink and dev.shrink() or dev.size()
141         metadata['size'] = str(size // 2 ** 20)
142
143         outfile = ""
144         if options.outfile is not None:
145             outfile = options.outfile
146             f = open('%s.%s' % (options.outfile, 'meta'), 'w')
147             try:
148                 for key in metadata.keys():
149                     f.write("%s=%s\n" % (key, metadata[key]))
150             finally:
151                 f.close()
152         else:
153             outfd, outfile = tmpfile.mkstemp()
154             os.close(outfd)
155
156         dd('if=%s' % dev.device,
157             'of=%s' % outfile,
158             'bs=4M', 'count=%d' % ((size + 1) // 2 ** 22))
159
160     finally:
161         disk.cleanup()
162
163     #The image is ready, lets call kamaki if necessary
164     if options.upload:
165         pass
166
167     if options.outfile is None:
168         os.unlink(outfile)
169
170     return 0
171
172 COLOR_BLACK = "\033[00m"
173 COLOR_RED = "\033[1;31m"
174
175
176 def main():
177     try:
178         ret = image_creator()
179         sys.exit(ret)
180     except FatalError as e:
181         print >> sys.stderr, "\n%sError: %s%s\n" % (COLOR_RED, e, COLOR_BLACK)
182         sys.exit(1)
183
184
185 if __name__ == '__main__':
186     main()
187
188 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :