Add remove_swap_entry sysprep in linux
[snf-image-creator] / image_creator / main.py
old mode 100755 (executable)
new mode 100644 (file)
index d073946..a18d6c7
 # interpreted as representing official policies, either expressed
 # or implied, of GRNET S.A.
 
-from image_creator import get_os_class
 from image_creator import __version__ as version
 from image_creator import util
 from image_creator.disk import Disk
-from image_creator.util import get_command, error, success, output, FatalError
+from image_creator.util import get_command, error, success, output, \
+                                                    FatalError, progress, md5
+from image_creator.os_type import get_os_class
 from image_creator.kamaki_wrapper import Kamaki
 import sys
 import os
 import optparse
-
-dd = get_command('dd')
+import StringIO
 
 
 def check_writable_dir(option, opt_str, value, parser):
@@ -143,17 +143,19 @@ def image_creator():
 
     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")
+        raise FatalError("At least one of `-o', `-u' or `--print-sysprep' " \
+                                                                "must be set")
 
-    output('snf-image-creator %s\n' % version)
+    title = 'snf-image-creator %s' % version
+    output(title)
+    output('=' * len(title))
 
     if os.geteuid() != 0:
         raise FatalError("You must run %s as root" \
                         % os.path.basename(sys.argv[0]))
 
     if not options.force and options.outfile is not None:
-        for extension in ('', '.meta'):
+        for extension in ('', '.meta', '.md5sum'):
             filename = "%s%s" % (options.outfile, extension)
             if os.path.exists(filename):
                 raise FatalError("Output file %s exists "
@@ -169,7 +171,6 @@ def image_creator():
         osclass = get_os_class(dev.distro, dev.ostype)
         image_os = osclass(dev.root, dev.g)
         metadata = image_os.get_metadata()
-
         output()
 
         for sysprep in options.disabled_syspreps:
@@ -190,32 +191,67 @@ def image_creator():
 
         dev.umount()
 
-        size = options.shrink and dev.shrink() or dev.size()
-        metadata['SIZE'] = str(size // 2 ** 20)
+        size = options.shrink and dev.shrink() or dev.size
+        metadata.update(dev.meta)
 
-        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()
+        checksum = md5(snapshot, size)
+
+        metastring = '\n'.join(
+                ['%s=%s' % (key, value) for (key, value) in metadata.items()])
+        metastring += '\n'
 
+        if options.outfile is not None:
             dev.dump(options.outfile)
 
+            output('Dumping metadata file...', False)
+            with open('%s.%s' % (options.outfile, 'meta'), 'w') as f:
+                f.write(metastring)
+            success('done')
+
+            output('Dumping md5sum file...', False)
+            with open('%s.%s' % (options.outfile, 'md5sum'), 'w') as f:
+                f.write('%s %s\n' % (checksum, \
+                                            os.path.basename(options.outfile)))
+            success('done')
+
         # Destroy the device. We only need the snapshot from now on
         disk.destroy_device(dev)
 
+        output()
+
+        uploaded_obj = ""
         if options.upload:
-            output("Uploading image to pithos...", False)
+            output("Uploading image to pithos:")
             kamaki = Kamaki(options.account, options.token)
-            kamaki.upload(snapshot, size, options.upload)
-            output("done")
+            with open(snapshot) as f:
+                uploaded_obj = kamaki.upload(f, size, options.upload,
+                                "(1/4)  Calculating block hashes",
+                                "(2/4)  Uploading missing blocks")
+
+            output("(3/4)  Uploading metadata file...", False)
+            kamaki.upload(StringIO.StringIO(metastring), size=len(metastring),
+                                remote_path="%s.%s" % (options.upload, 'meta'))
+            success('done')
+            output("(4/4)  Uploading md5sum file...", False)
+            md5sumstr = '%s %s\n' % (
+                checksum, os.path.basename(options.upload))
+            kamaki.upload(StringIO.StringIO(md5sumstr), size=len(md5sumstr),
+                            remote_path="%s.%s" % (options.upload, 'md5sum'))
+            success('done')
+            output()
+
+        if options.register:
+            output('Registring image to ~okeanos...', False)
+            kamaki.register(options.register, uploaded_obj, metadata)
+            success('done')
+            output()
 
     finally:
         output('cleaning up...')
         disk.cleanup()
 
+    success("snf-image-creator exited without errors")
+
     return 0