Convert check_guestfs_version into an Image method
authorNikos Skalkotos <skalkoto@grnet.gr>
Sat, 3 Aug 2013 13:05:20 +0000 (16:05 +0300)
committerNikos Skalkotos <skalkoto@grnet.gr>
Sat, 3 Aug 2013 13:05:20 +0000 (16:05 +0300)
It used to be a function in util module

image_creator/image.py
image_creator/os_type/windows.py
image_creator/util.py

index 8a111a1..686fad6 100644 (file)
@@ -33,7 +33,7 @@
 # interpreted as representing official policies, either expressed
 # or implied, of GRNET S.A.
 
-from image_creator.util import FatalError, check_guestfs_version
+from image_creator.util import FatalError
 from image_creator.gpt import GPTPartitionTable
 from image_creator.os_type import os_cls
 
@@ -61,6 +61,25 @@ class Image(object):
 
         self.g = guestfs.GuestFS()
         self.guestfs_enabled = False
+        self.guestfs_version = self.g.version()
+
+    def check_guestfs_version(self, major, minor, release):
+        """Checks if the version of the used libguestfs is smaller, equal or
+        greater than the one specified by the major, minor and release triplet
+
+        Returns:
+            < 0 if the installed version is smaller than the specified one
+            = 0 if they are equal
+            > 0 if the installed one is greater than the specified one
+        """
+
+        for (a, b) in (self.guestfs_version['major'], major), \
+                (self.guestfs_version['minor'], minor), \
+                (self.guestfs_version['release'], release):
+            if a != b:
+                return a - b
+
+        return 0
 
     def enable(self):
         """Enable a newly created Image instance"""
@@ -96,7 +115,7 @@ class Image(object):
         # Before version 1.18.4 the behaviour of kill_subprocess was different
         # and you need to reset the guestfs handler to relaunch a previously
         # shut down qemu backend
-        if check_guestfs_version(self.g, 1, 18, 4) < 0:
+        if self.check_guestfs_version(1, 18, 4) < 0:
             self.g = guestfs.GuestFS()
 
         self.g.add_drive_opts(self.device, readonly=0, format="raw")
@@ -106,7 +125,7 @@ class Image(object):
         # file descriptors. This can cause problems especially if the parent
         # process has opened pipes. Since the recovery process is an optional
         # feature of libguestfs, it's better to disable it.
-        if check_guestfs_version(self.g, 1, 17, 14) >= 0:
+        if self.check_guestfs_version(1, 17, 14) >= 0:
             self.out.output("Enabling recovery proc")
             self.g.set_recovery_proc(1)
         else:
@@ -126,7 +145,7 @@ class Image(object):
         # self.progressbar.success('done')
         # self.progressbar = None
 
-        if check_guestfs_version(self.g, 1, 18, 4) < 0:
+        if self.check_guestfs_version(1, 18, 4) < 0:
             self.g.inspect_os()  # some calls need this
 
         self.out.success('done')
@@ -142,7 +161,7 @@ class Image(object):
         self.g.sync()
         # guestfs_shutdown which is the prefered way to shutdown the backend
         # process was introduced in version 1.19.16
-        if check_guestfs_version(self.g, 1, 19, 16) >= 0:
+        if self.check_guestfs_version(1, 19, 16) >= 0:
             self.g.shutdown()
         else:
             self.g.kill_subprocess()
index 690a569..4f427d6 100644 (file)
@@ -37,8 +37,7 @@
 Windows OSs."""
 
 from image_creator.os_type import OSBase, sysprep, add_sysprep_param
-from image_creator.util import FatalError, check_guestfs_version, \
-    get_kvm_binary
+from image_creator.util import FatalError, get_kvm_binary
 from image_creator.winexe import WinEXE, WinexeTimeout
 
 import hivex
@@ -116,15 +115,19 @@ class Windows(OSBase):
     def __init__(self, image, **kargs):
         super(Windows, self).__init__(image, **kargs)
 
-        # This commit was added in libguestfs 1.17.18 and is critical because
-        # Microsoft Sysprep removes this key:
+        # The commit with the following message was added in
+        # libguestfs 1.17.18:
         #
         # When a Windows guest doesn't have a HKLM\SYSTEM\MountedDevices node,
         # inspection fails.  However inspection should not completely fail just
         # because we cannot get the drive letter mapping from a guest.
-        if check_guestfs_version(self.image.g, 1, 17, 18) < 0:
+        #
+        # Since Microsoft Sysprep removes the aforementioned key, image
+        # creation for windows can only be supported if the installed guestfs
+        # version is 1.17.18 or higher
+        if self.image.check_guestfs_version(1, 17, 18) < 0:
             raise FatalError(
-                'For windows support libguestfs 1.17.18 or above is needed')
+                'For windows support libguestfs 1.17.18 or above is required')
 
         device = self.image.g.part_to_dev(self.root)
 
index e5fab41..4e1b706 100644 (file)
@@ -108,26 +108,6 @@ def free_space(dirname):
     return stat.f_bavail * stat.f_frsize
 
 
-def check_guestfs_version(ghandler, major, minor, release):
-    """Checks if the version of the used libguestfs is smaller, equal or
-    greater than the one specified by the major, minor and release triplet
-
-    Returns:
-        < 0 if the installed version is smaller than the specified one
-        = 0 if they are equal
-        > 0 if the installed one is greater than the specified one
-    """
-
-    ver = ghandler.version()
-
-    for (a, b) in (ver['major'], major), (ver['minor'], minor), \
-            (ver['release'], release):
-        if a != b:
-            return a - b
-
-    return 0
-
-
 class MD5:
     """Represents MD5 computations"""
     def __init__(self, output):