Beautify program's output.
[snf-image-creator] / image_creator / main.py
old mode 100644 (file)
new mode 100755 (executable)
index 574d1a2..e4f9a29
@@ -1,3 +1,5 @@
+#!/usr/bin/env python
+
 # Copyright 2011 GRNET S.A. All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or
 # or implied, of GRNET S.A.
 
 from image_creator import get_os_class
+from image_creator import __version__ as version
+from image_creator import FatalError
 from image_creator.disk import Disk
+from image_creator.util import get_command, error, progress_generator, success
+from clint.textui import puts, indent
+from sendfile import sendfile
+
 import sys
 import os
+import optparse
+
+dd = get_command('dd')
+
+
+def check_writable_dir(option, opt_str, value, parser):
+    dirname = os.path.dirname(value)
+    name = os.path.basename(value)
+    if dirname and not os.path.isdir(dirname):
+        parser.error("`%s' is not an existing directory" % dirname)
+
+    if not name:
+        parser.error("`%s' is not a valid file name" % dirname)
+
+    setattr(parser.values, option.dest, value)
+
+
+def parse_options(input_args):
+    usage = "Usage: %prog [options] <input_media>"
+    parser = optparse.OptionParser(version=version, usage=usage)
+
+    parser.add_option("-f", "--force", dest="force", default=False,
+        action="store_true", help="Overwrite output files if they exist")
+
+    parser.add_option("--no-cleanup", dest="cleanup", default=True,
+        help="Don't cleanup sensitive data before extracting the image",
+        action="store_false")
+
+    parser.add_option("--no-sysprep", dest="sysprep", default=True,
+        help="Don't perform system preperation before extracting the image",
+        action="store_false")
+
+    parser.add_option("--no-shrink", dest="shrink", default=True,
+        help="Don't shrink any partition before extracting the image",
+        action="store_false")
+
+    parser.add_option("-o", "--outfile", type="string", dest="outfile",
+        default=None, action="callback", callback=check_writable_dir,
+        help="Output image file",
+        metavar="FILE")
+
+    parser.add_option("-u", "--upload", dest="upload", default=False,
+        help="Upload image to a pithos repository using kamaki",
+        action="store_true")
+
+    parser.add_option("-r", "--register", dest="register", default=False,
+        help="Register image to okeanos using kamaki", action="store_true")
+
+    options, args = parser.parse_args(input_args)
+
+    if len(args) != 1:
+        parser.error('Wrong number of arguments')
+    options.source = args[0]
+    if not os.path.exists(options.source):
+        parser.error('input media is not accessible')
+
+    if options.register:
+        options.upload = True
+
+    if options.outfile is None and not options.upload:
+        parser.error('either outfile (-o) or upload (-u) must be set.')
+
+    return options
 
-def main():
-    if len(sys.argv) != 3:
-        sys.exit("Usage: %s <source> <output_file>" %
-                        os.path.basename(sys.argv[0]))
-    source = sys.argv[1]
-    dest = sys.argv[2]
 
-    disk = Disk(source)
+def extract_image(device, outfile, size):
+    blocksize = 4194304  # 4MB
+    progress_size = (size + 1048575) // 1048576  # in MB
+    progressbar = progress_generator("Dumping image file: ",
+                                                    progress_size)
+    source = open(device, "r")
+    try:
+        dest = open(outfile, "w")
+        try:
+            left = size
+            offset = 0
+            progressbar.next()
+            while left > 0:
+                length = min(left, blocksize)
+                sent = sendfile(dest.fileno(), source.fileno(), offset, length)
+                offset += sent
+                left -= sent
+                for i in range(4):
+                    progressbar.next()
+        finally:
+            dest.close()
+    finally:
+        source.close()
+
+    success('Image file %s was successfully created' % outfile)
+
+
+def image_creator():
+    puts('snf-image-creator %s\n' % version)
+    options = parse_options(sys.argv[1:])
+
+    if os.geteuid() != 0:
+        raise FatalError("You must run %s as root" \
+                        % os.path.basename(sys.argv[0]))
+
+    if not options.force:
+        for extension in ('', '.meta'):
+            filename = "%s%s" % (options.outfile, extension)
+            if os.path.exists(filename):
+                raise FatalError("Output file %s exists "
+                    "(use --force to overwrite it)." % filename)
+
+    disk = Disk(options.source)
     try:
         dev = disk.get_device()
+        dev.mount()
+
         osclass = get_os_class(dev.distro, dev.ostype)
         image_os = osclass(dev.root, dev.g)
         metadata = image_os.get_metadata()
-        for key, val in metadata.iteritems():
-            print "%s=%s" % (key,val)
+
+        if options.sysprep:
+            image_os.sysprep()
+
+        if options.cleanup:
+            image_os.data_cleanup()
+
+        dev.umount()
+
+        size = options.shrink and dev.shrink() or dev.size()
+        metadata['size'] = str(size // 2 ** 20)
+
+        if options.outfile is not None:
+            f = open('%s.%s' % (options.outfile, 'meta'), 'w')
+            try:
+                for key in metadata.keys():
+                    f.write("%s=%s\n" % (key, metadata[key]))
+            finally:
+                f.close()
+
+            extract_image(dev.device, options.outfile, size)
 
     finally:
+        puts('cleaning up...')
         disk.cleanup()
 
+    return 0
+
+
+def main():
+    try:
+        ret = image_creator()
+        sys.exit(ret)
+    except FatalError as e:
+        error(e)
+        sys.exit(1)
+
+
 if __name__ == '__main__':
     main()
 
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
-