Add a wrapper module for kamaki
[snf-image-creator] / image_creator / main.py
index 97b558c..f2aba56 100755 (executable)
 
 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
-
+from image_creator.util import get_command, error, success, output, FatalError
+from image_creator import util
 import sys
 import os
 import optparse
@@ -66,26 +64,33 @@ def parse_options(input_args):
     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",
-        action="store_false")
-
     parser.add_option("--no-sysprep", dest="sysprep", default=True,
-        help="don't perform system preperation",
-        action="store_false")
+        help="don't perform system preperation", action="store_false")
 
     parser.add_option("--no-shrink", dest="shrink", default=True,
-        help="don't shrink any partition",
-        action="store_false")
+        help="don't shrink any partition", action="store_false")
 
     parser.add_option("-o", "--outfile", type="string", dest="outfile",
         default=None, action="callback", callback=check_writable_dir,
-        help="dump image to FILE",
-        metavar="FILE")
+        help="dump image to FILE", metavar="FILE")
+
+    parser.add_option("--enable-sysprep", dest="enabled_syspreps", default=[],
+        help="Run SYSPREP operation on the input media",
+        action="append", metavar="SYSPREP")
+
+    parser.add_option("--disable-sysprep", dest="disabled_syspreps",
+        help="Prevent SYSPREP operation from running on the input media",
+        default=[], action="append", metavar="SYSPREP")
+
+    parser.add_option("--print-sysprep", dest="print_sysprep", default=False,
+        help="Print the enabled and disabled sysprep operations for this "
+        "input media", action="store_true")
+
+    parser.add_option("-s", "--silent", dest="silent", default=False,
+        help="silent mode, only output errors", action="store_true")
 
     parser.add_option("-u", "--upload", dest="upload", default=False,
-        help="upload the image to pithos",
-        action="store_true")
+        help="upload the image to pithos", action="store_true")
 
     parser.add_option("-r", "--register", dest="register", default=False,
         help="register the image to ~okeanos", action="store_true")
@@ -101,16 +106,22 @@ def parse_options(input_args):
     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 image_creator():
-    puts('snf-image-creator %s\n' % version)
     options = parse_options(sys.argv[1:])
 
+    if options.silent:
+        util.silent = True
+
+    if options.outfile is None and not options.upload \
+                                            and not options.print_sysprep:
+        raise FatalError("At least one of `-o', `-u' or" \
+                            "`--print-sysprep' must be set")
+
+    output('snf-image-creator %s\n' % version)
+
     if os.geteuid() != 0:
         raise FatalError("You must run %s as root" \
                         % os.path.basename(sys.argv[0]))
@@ -131,18 +142,28 @@ def image_creator():
         image_os = osclass(dev.root, dev.g)
         metadata = image_os.get_metadata()
 
-        puts()
+        output()
 
-        if options.sysprep:
-            image_os.sysprep()
+        for sysprep in options.disabled_syspreps:
+            image_os.disable_sysprep(sysprep)
+
+        for sysprep in options.enabled_syspreps:
+            image_os.enable_sysprep(sysprep)
 
-        if options.cleanup:
-            image_os.data_cleanup()
+        if options.print_sysprep:
+            image_os.print_syspreps()
+            output()
+
+        if options.outfile is None and not options.upload:
+            return 0
+
+        if options.sysprep:
+            image_os.do_sysprep()
 
         dev.umount()
 
         size = options.shrink and dev.shrink() or dev.size()
-        metadata['size'] = str(size // 2 ** 20)
+        metadata['SIZE'] = str(size // 2 ** 20)
 
         if options.outfile is not None:
             f = open('%s.%s' % (options.outfile, 'meta'), 'w')
@@ -154,7 +175,7 @@ def image_creator():
 
             dev.dump(options.outfile)
     finally:
-        puts('cleaning up...')
+        output('cleaning up...')
         disk.cleanup()
 
     return 0