Add exclude_task decorator in os_type
[snf-image-creator] / image_creator / disk.py
index b84de1a..6b69aad 100644 (file)
@@ -31,9 +31,9 @@
 # interpreted as representing official policies, either expressed
 # or implied, of GRNET S.A.
 
-from image_creator.util import get_command, warn, progress_generator
+from image_creator.util import get_command
+from image_creator.util import warn, progress, success, output
 from image_creator import FatalError
-from clint.textui import indent, puts, colored
 
 import stat
 import os
@@ -43,6 +43,7 @@ import re
 import sys
 import guestfs
 import time
+from sendfile import sendfile
 
 
 class DiskError(Exception):
@@ -100,23 +101,23 @@ class Disk(object):
         the Disk instance.
         """
 
-        puts("Examining source media `%s'..." % self.source, False)
+        output("Examining source media `%s'..." % self.source, False)
         sourcedev = self.source
         mode = os.stat(self.source).st_mode
         if stat.S_ISDIR(mode):
-            puts(colored.green('looks like a directory'))
+            success('looks like a directory')
             return self._losetup(self._dir_to_disk())
         elif stat.S_ISREG(mode):
-            puts(colored.green('looks like an image file'))
+            success('looks like an image file')
             sourcedev = self._losetup(self.source)
         elif not stat.S_ISBLK(mode):
             raise ValueError("Invalid media source. Only block devices, "
                             "regular files and directories are supported.")
         else:
-            puts(colored.green('looks like a block device'))
+            success('looks like a block device')
 
         # Take a snapshot and return it to the user
-        puts("Snapshotting media source...", False)
+        output("Snapshotting media source...", False)
         size = blockdev('--getsize', sourcedev)
         cowfd, cow = tempfile.mkstemp()
         os.close(cowfd)
@@ -140,7 +141,7 @@ class Disk(object):
 
         finally:
             os.unlink(table)
-        puts(colored.green('done'))
+        success('done')
         new_device = DiskDevice("/dev/mapper/%s" % snapshot)
         self._devices.append(new_device)
         new_device.enable()
@@ -176,7 +177,7 @@ class DiskDevice(object):
 
     def enable(self):
         """Enable a newly created DiskDevice"""
-        self.progressbar = progress_generator("Launching helper VM: ")
+        self.progressbar = progress("Launching helper VM: ")
         self.progressbar.next()
         eh = self.g.set_event_callback(self.progress_callback,
                                                     guestfs.EVENT_PROGRESS)
@@ -187,7 +188,7 @@ class DiskDevice(object):
             self.progressbar.send(100)
             self.progressbar = None
 
-        puts('Inspecting Operating System...', False)
+        output('Inspecting Operating System...', False)
         roots = self.g.inspect_os()
         if len(roots) == 0:
             raise FatalError("No operating system found")
@@ -197,7 +198,7 @@ class DiskDevice(object):
         self.root = roots[0]
         self.ostype = self.g.inspect_get_type(self.root)
         self.distro = self.g.inspect_get_distro(self.root)
-        puts(colored.green('found a %s system' % self.distro))
+        success('found a %s system' % self.distro)
 
     def destroy(self):
         """Destroy this DiskDevice instance."""
@@ -220,6 +221,8 @@ class DiskDevice(object):
 
     def mount(self):
         """Mount all disk partitions in a correct order."""
+
+        output("Mounting image...", False)
         mps = self.g.inspect_get_mountpoints(self.root)
 
         # Sort the keys to mount the fs in a correct order.
@@ -236,7 +239,8 @@ class DiskDevice(object):
             try:
                 self.g.mount(dev, mp)
             except RuntimeError as msg:
-                print "%s (ignored)" % msg
+                warn("%s (ignored)" % msg)
+        success("done")
 
     def umount(self):
         """Umount all mounted filesystems."""
@@ -249,7 +253,7 @@ class DiskDevice(object):
         disk and then updating the partition table. The new disk size
         (in bytes) is returned.
         """
-        puts("Shrinking image (this may take a while)...", False)
+        output("Shrinking image (this may take a while)...", False)
 
         dev = self.g.part_to_dev(self.root)
         parttype = self.g.part_get_parttype(dev)
@@ -287,8 +291,8 @@ class DiskDevice(object):
         self.g.part_add(dev, 'p', start, end)
 
         new_size = (end + 1) * sector_size
-        puts(colored.green("new image size is %dMB\n" % (new_size // 2 ** 20)))
-
+        success("new image size is %dMB" %
+                            ((new_size + 2 ** 20 - 1) // 2 ** 20))
         return new_size
 
     def size(self):
@@ -300,6 +304,39 @@ class DiskDevice(object):
         dev = self.g.part_to_dev(self.root)
         last = self.g.part_list(dev)[-1]
 
-        return last['part_end']
+        return last['part_end'] + 1
+
+    def dump(self, outfile):
+        """Dumps the content of device into a file.
+
+        This method will only dump the actual payload, found by reading the
+        partition table. Empty space in the end of the device will be ignored.
+        """
+        blocksize = 2 ** 22  # 4MB
+        size = self.size()
+        progress_size = (size + 2 ** 20 - 1) // 2 ** 20  # in MB
+        progressbar = progress("Dumping image file: ", progress_size)
+
+        source = open(self.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((length + 2 ** 20 - 1) // 2 ** 20):
+                        progressbar.next()
+            finally:
+                dest.close()
+        finally:
+            source.close()
+
+        success('Image file %s was successfully created' % outfile)
 
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :