26e1be510e431f0957d072947c71037c5cb81817
[snf-image-creator] / image_creator / main.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright 2012 GRNET S.A. All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or
7 # without modification, are permitted provided that the following
8 # conditions are met:
9 #
10 #   1. Redistributions of source code must retain the above
11 #      copyright notice, this list of conditions and the following
12 #      disclaimer.
13 #
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.
18 #
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.
31 #
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.
36
37 """This module is the entrance point for the non-interactive version of the
38 snf-image-creator program.
39 """
40
41 from image_creator import __version__ as version
42 from image_creator.disk import Disk
43 from image_creator.util import FatalError, MD5
44 from image_creator.output.cli import SilentOutput, SimpleOutput, \
45     OutputWthProgress
46 from image_creator.kamaki_wrapper import Kamaki, ClientError
47 import sys
48 import os
49 import optparse
50 import StringIO
51 import signal
52 import json
53 import textwrap
54
55
56 def check_writable_dir(option, opt_str, value, parser):
57     dirname = os.path.dirname(value)
58     name = os.path.basename(value)
59     if dirname and not os.path.isdir(dirname):
60         raise FatalError("`%s' is not an existing directory" % dirname)
61
62     if not name:
63         raise FatalError("`%s' is not a valid file name" % dirname)
64
65     setattr(parser.values, option.dest, value)
66
67
68 def parse_options(input_args):
69     usage = "Usage: %prog [options] <input_media>"
70     parser = optparse.OptionParser(version=version, usage=usage)
71
72     parser.add_option("-o", "--outfile", type="string", dest="outfile",
73                       default=None, action="callback",
74                       callback=check_writable_dir, help="dump image to FILE",
75                       metavar="FILE")
76
77     parser.add_option("-f", "--force", dest="force", default=False,
78                       action="store_true",
79                       help="overwrite output files if they exist")
80
81     parser.add_option("-s", "--silent", dest="silent", default=False,
82                       help="output only errors",
83                       action="store_true")
84
85     parser.add_option("-u", "--upload", dest="upload", type="string",
86                       default=False,
87                       help="upload the image to the cloud with name FILENAME",
88                       metavar="FILENAME")
89
90     parser.add_option("-r", "--register", dest="register", type="string",
91                       default=False,
92                       help="register the image with a cloud as IMAGENAME",
93                       metavar="IMAGENAME")
94
95     parser.add_option("-m", "--metadata", dest="metadata", default=[],
96                       help="add custom KEY=VALUE metadata to the image",
97                       action="append", metavar="KEY=VALUE")
98
99     parser.add_option("-t", "--token", dest="token", type="string",
100                       default=None, help="use this authentication token when "
101                       "uploading/registering images")
102
103     parser.add_option("-a", "--authentication-url", dest="url", type="string",
104                       default=None, help="use this authentication URL when "
105                       "uploading/registering images")
106
107     parser.add_option("-c", "--cloud", dest="cloud", type="string",
108                       default=None, help="use this saved cloud account to "
109                       "authenticate against a cloud when "
110                       "uploading/registering images")
111
112     parser.add_option("--print-syspreps", dest="print_syspreps", default=False,
113                       help="print the enabled and disabled system preparation "
114                       "operations for this input media", action="store_true")
115
116     parser.add_option("--enable-sysprep", dest="enabled_syspreps", default=[],
117                       help="run SYSPREP operation on the input media",
118                       action="append", metavar="SYSPREP")
119
120     parser.add_option("--disable-sysprep", dest="disabled_syspreps",
121                       help="prevent SYSPREP operation from running on the "
122                       "input media", default=[], action="append",
123                       metavar="SYSPREP")
124
125     parser.add_option("--print-sysprep-params", dest="print_sysprep_params",
126                       default=False, help="print the needed sysprep parameters"
127                       " for this input media", action="store_true")
128
129     parser.add_option("--sysprep-param", dest="sysprep_params", default=[],
130                       help="Add KEY=VALUE system preparation parameter",
131                       action="append")
132
133     parser.add_option("--no-sysprep", dest="sysprep", default=True,
134                       help="don't perform any system preparation operation",
135                       action="store_false")
136
137     parser.add_option("--no-shrink", dest="shrink", default=True,
138                       help="don't shrink any partition", action="store_false")
139
140     parser.add_option("--public", dest="public", default=False,
141                       help="register image with the cloud as public",
142                       action="store_true")
143
144     parser.add_option("--allow-unsupported", dest="allow_unsupported",
145                       help="Proceed with the image creation even if the media "
146                       "is not supported", default=False, action="store_true")
147
148     parser.add_option("--tmpdir", dest="tmp", type="string", default=None,
149                       help="create large temporary image files under DIR",
150                       metavar="DIR")
151
152     options, args = parser.parse_args(input_args)
153
154     if len(args) != 1:
155         parser.error('Wrong number of arguments')
156
157     options.source = args[0]
158     if not os.path.exists(options.source):
159         raise FatalError("Input media `%s' is not accessible" % options.source)
160
161     if options.register and not options.upload:
162         raise FatalError("You also need to set -u when -r option is set")
163
164     if options.upload and (options.token is None or options.url is None) and \
165             options.cloud is None:
166
167         err = "You need to either specify an authentication URL and token " \
168               "pair or an available cloud name."
169
170         raise FatalError("Image uploading cannot be performed. %s" % err)
171
172     if options.tmp is not None and not os.path.isdir(options.tmp):
173         raise FatalError("The directory `%s' specified with --tmpdir is not "
174                          "valid" % options.tmp)
175
176     meta = {}
177     for m in options.metadata:
178         try:
179             key, value = m.split('=', 1)
180         except ValueError:
181             raise FatalError("Metadata option: `%s' is not in KEY=VALUE "
182                              "format." % m)
183         meta[key] = value
184     options.metadata = meta
185
186     sysprep_params = {}
187     for p in options.sysprep_params:
188         try:
189             key, value = p.split('=', 1)
190         except ValueError:
191             raise FatalError("Sysprep parameter optiont: `%s' is not in "
192                              "KEY=VALUE format." % p)
193         sysprep_params[key] = value
194     options.sysprep_params = sysprep_params
195
196     return options
197
198
199 def image_creator():
200     options = parse_options(sys.argv[1:])
201
202     if options.outfile is None and not options.upload and not \
203             options.print_syspreps and not options.print_sysprep_params:
204         raise FatalError("At least one of `-o', `-u', `--print-syspreps' or "
205                          "`--print-sysprep-params' must be set")
206
207     if options.silent:
208         out = SilentOutput()
209     else:
210         out = OutputWthProgress(True) if sys.stderr.isatty() else \
211             SimpleOutput(False)
212
213     title = 'snf-image-creator %s' % version
214     out.output(title)
215     out.output('=' * len(title))
216
217     if os.geteuid() != 0:
218         raise FatalError("You must run %s as root"
219                          % os.path.basename(sys.argv[0]))
220
221     if not options.force and options.outfile is not None:
222         for extension in ('', '.meta', '.md5sum'):
223             filename = "%s%s" % (options.outfile, extension)
224             if os.path.exists(filename):
225                 raise FatalError("Output file `%s' exists "
226                                  "(use --force to overwrite it)." % filename)
227
228     # Check if the authentication info is valid. The earlier the better
229     if options.token is not None and options.url is not None:
230         try:
231             account = Kamaki.create_account(options.url, options.token)
232             if account is None:
233                 raise FatalError("The authentication token and/or URL you "
234                                  "provided is not valid!")
235             else:
236                 kamaki = Kamaki(account, out)
237         except ClientError as e:
238             raise FatalError("Astakos client: %d %s" % (e.status, e.message))
239     elif options.cloud:
240         avail_clouds = Kamaki.get_clouds()
241         if options.cloud not in avail_clouds.keys():
242             raise FatalError(
243                 "Cloud: `%s' does not exist.\n\nAvailable clouds:\n\n\t%s\n"
244                 % (options.cloud, "\n\t".join(avail_clouds.keys())))
245         try:
246             account = Kamaki.get_account(options.cloud)
247             if account is None:
248                 raise FatalError(
249                     "Cloud: `$s' exists but is not valid!" % options.cloud)
250             else:
251                 kamaki = Kamaki(account, out)
252         except ClientError as e:
253             raise FatalError("Astakos client: %d %s" % (e.status, e.message))
254
255     if options.upload and not options.force:
256         if kamaki.object_exists(options.upload):
257             raise FatalError("Remote storage service object: `%s' exists "
258                              "(use --force to overwrite it)." % options.upload)
259         if kamaki.object_exists("%s.md5sum" % options.upload):
260             raise FatalError("Remote storage service object: `%s.md5sum' "
261                              "exists (use --force to overwrite it)." %
262                              options.upload)
263
264     if options.register and not options.force:
265         if kamaki.object_exists("%s.meta" % options.upload):
266             raise FatalError("Remote storage service object `%s.meta' exists "
267                              "(use --force to overwrite it)." % options.upload)
268
269     disk = Disk(options.source, out, options.tmp)
270
271     def signal_handler(signum, frame):
272         disk.cleanup()
273
274     signal.signal(signal.SIGINT, signal_handler)
275     signal.signal(signal.SIGTERM, signal_handler)
276     try:
277         snapshot = disk.snapshot()
278
279         image = disk.get_image(snapshot, sysprep_params=options.sysprep_params)
280
281         if image.is_unsupported() and not options.allow_unsupported:
282             raise FatalError(
283                 "The media seems to be unsupported.\n\n" +
284                 textwrap.fill("To create an image from an unsupported media, "
285                               "you'll need to use the`--allow-unsupported' "
286                               "command line option. Using this is highly "
287                               "discouraged, since the resulting image will "
288                               "not be cleared out of sensitive data and will "
289                               "not get customized during the deployment."))
290
291         for sysprep in options.disabled_syspreps:
292             image.os.disable_sysprep(image.os.get_sysprep_by_name(sysprep))
293
294         for sysprep in options.enabled_syspreps:
295             image.os.enable_sysprep(image.os.get_sysprep_by_name(sysprep))
296
297         if options.print_syspreps:
298             image.os.print_syspreps()
299             out.output()
300
301         if options.print_sysprep_params:
302             image.os.print_sysprep_params()
303             out.output()
304
305         if options.outfile is None and not options.upload:
306             return 0
307
308         if options.sysprep:
309             image.os.do_sysprep()
310
311         metadata = image.os.meta
312
313         size = options.shrink and image.shrink() or image.size
314         metadata.update(image.meta)
315
316         if image.is_unsupported():
317             metadata['EXCLUDE_ALL_TASKS'] = "yes"
318
319         # Add command line metadata to the collected ones...
320         metadata.update(options.metadata)
321
322         md5 = MD5(out)
323         checksum = md5.compute(image.device, size)
324
325         metastring = unicode(json.dumps(
326             {'properties': metadata,
327              'disk-format': 'diskdump'}, ensure_ascii=False))
328
329         if options.outfile is not None:
330             image.dump(options.outfile)
331
332             out.output('Dumping metadata file ...', False)
333             with open('%s.%s' % (options.outfile, 'meta'), 'w') as f:
334                 f.write(metastring)
335             out.success('done')
336
337             out.output('Dumping md5sum file ...', False)
338             with open('%s.%s' % (options.outfile, 'md5sum'), 'w') as f:
339                 f.write('%s %s\n' % (checksum,
340                                      os.path.basename(options.outfile)))
341             out.success('done')
342
343         # Destroy the image instance. We only need the snapshot from now on
344         disk.destroy_image(image)
345
346         out.output()
347         try:
348             uploaded_obj = ""
349             if options.upload:
350                 out.output("Uploading image to the storage service:")
351                 with open(snapshot, 'rb') as f:
352                     uploaded_obj = kamaki.upload(
353                         f, size, options.upload,
354                         "(1/3)  Calculating block hashes",
355                         "(2/3)  Uploading missing blocks")
356                 out.output("(3/3)  Uploading md5sum file ...", False)
357                 md5sumstr = '%s %s\n' % (checksum,
358                                          os.path.basename(options.upload))
359                 kamaki.upload(StringIO.StringIO(md5sumstr),
360                               size=len(md5sumstr),
361                               remote_path="%s.%s" % (options.upload, 'md5sum'))
362                 out.success('done')
363                 out.output()
364
365             if options.register:
366                 img_type = 'public' if options.public else 'private'
367                 out.output('Registering %s image with the compute service ...'
368                            % img_type, False)
369                 result = kamaki.register(options.register, uploaded_obj,
370                                          metadata, options.public)
371                 out.success('done')
372                 out.output("Uploading metadata file ...", False)
373                 metastring = unicode(json.dumps(result, ensure_ascii=False))
374                 kamaki.upload(StringIO.StringIO(metastring),
375                               size=len(metastring),
376                               remote_path="%s.%s" % (options.upload, 'meta'))
377                 out.success('done')
378                 if options.public:
379                     out.output("Sharing md5sum file ...", False)
380                     kamaki.share("%s.md5sum" % options.upload)
381                     out.success('done')
382                     out.output("Sharing metadata file ...", False)
383                     kamaki.share("%s.meta" % options.upload)
384                     out.success('done')
385
386                 out.output()
387         except ClientError as e:
388             raise FatalError("Service client: %d %s" % (e.status, e.message))
389
390     finally:
391         out.output('cleaning up ...')
392         disk.cleanup()
393
394     out.success("snf-image-creator exited without errors")
395
396     return 0
397
398
399 def main():
400     try:
401         ret = image_creator()
402         sys.exit(ret)
403     except FatalError as e:
404         colored = sys.stderr.isatty()
405         SimpleOutput(colored).error(e)
406         sys.exit(1)
407
408 if __name__ == '__main__':
409     main()
410
411 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :