Add {enable, disable}_guestfs methods in image cls
[snf-image-creator] / image_creator / os_type / __init__.py
index ddda45e..19e63fc 100644 (file)
@@ -62,6 +62,7 @@ def os_cls(distro, osfamily):
 
 
 def add_prefix(target):
+    """Decorator that adds a prefix to the result of a function"""
     def wrapper(self, *args):
         prefix = args[0]
         return [prefix + path for path in target(self, *args)]
@@ -88,7 +89,7 @@ def sysprep(message, enabled=True, **kwargs):
     return wrapper
 
 
-def add_sysprep_param(name, default, description, validator=lambda x: True):
+def add_sysprep_param(name, type, default, descr, validate=lambda x: True):
     """Decorator for __init__ that adds the definition for a system preparation
     parameter in an instance of a os_type class
     """
@@ -97,7 +98,9 @@ def add_sysprep_param(name, default, description, validator=lambda x: True):
         def inner(self, *args, **kwargs):
             init(self, *args, **kwargs)
             self.needed_sysprep_params[name] = \
-                self.SysprepParam(default, description, validator)
+                self.SysprepParam(type, default, descr, validate)
+            if default is not None:
+                self.sysprep_params[name] = default
         return inner
     return wrapper
 
@@ -118,13 +121,13 @@ def del_sysprep_param(name):
 class OSBase(object):
     """Basic operating system class"""
 
-    SysprepParam = namedtuple('SysprepParam', 'default description validator')
+    SysprepParam = namedtuple('SysprepParam',
+                              ['type', 'default', 'description', 'validate'])
 
     def __init__(self, image, **kargs):
         self.image = image
 
         self.root = image.root
-        self.g = image.g
         self.out = image.out
 
         self.needed_sysprep_params = {}
@@ -134,6 +137,13 @@ class OSBase(object):
         self.meta = {}
         self.mounted = False
 
+        # Many guestfs compilations don't support scrub
+        self._scrub_support = True
+        try:
+            self.image.g.available(['scrub'])
+        except RuntimeError:
+            self._scrub_support = False
+
     def collect_metadata(self):
         """Collect metadata about the OS"""
         try:
@@ -277,7 +287,7 @@ class OSBase(object):
         """Umount all mounted filesystems."""
 
         self.out.output("Umounting the media ...", False)
-        self.g.umount_all()
+        self.image.g.umount_all()
         self.mounted = False
         self.out.success('done')
 
@@ -288,12 +298,12 @@ class OSBase(object):
     @add_prefix
     def _ls(self, directory):
         """List the name of all files under a directory"""
-        return self.g.ls(directory)
+        return self.image.g.ls(directory)
 
     @add_prefix
     def _find(self, directory):
         """List the name of all files recursively under a directory"""
-        return self.g.find(directory)
+        return self.image.g.find(directory)
 
     def _foreach_file(self, directory, action, **kargs):
         """Perform an action recursively on all files under a directory.
@@ -322,7 +332,7 @@ class OSBase(object):
         ftype = None if 'ftype' not in kargs else kargs['ftype']
         has_ftype = lambda x, y: y is None and True or x['ftyp'] == y
 
-        for f in self.g.readdir(directory):
+        for f in self.image.g.readdir(directory):
             if f['name'] in ('.', '..'):
                 continue
 
@@ -339,17 +349,20 @@ class OSBase(object):
 
     def _do_collect_metadata(self):
         """helper method for collect_metadata"""
-        self.meta['ROOT_PARTITION'] = "%d" % self.g.part_to_partnum(self.root)
-        self.meta['OSFAMILY'] = self.g.inspect_get_type(self.root)
-        self.meta['OS'] = self.g.inspect_get_distro(self.root)
+        self.meta['ROOT_PARTITION'] = \
+            "%d" % self.image.g.part_to_partnum(self.root)
+        self.meta['OSFAMILY'] = self.image.g.inspect_get_type(self.root)
+        self.meta['OS'] = self.image.g.inspect_get_distro(self.root)
         if self.meta['OS'] == "unknown":
             self.meta['OS'] = self.meta['OSFAMILY']
-        self.meta['DESCRIPTION'] = self.g.inspect_get_product_name(self.root)
+        self.meta['DESCRIPTION'] = \
+            self.image.g.inspect_get_product_name(self.root)
 
     def _do_mount(self, readonly):
         """helper method for mount"""
         try:
-            self.g.mount_options('ro' if readonly else 'rw', self.root, '/')
+            self.image.g.mount_options(
+                'ro' if readonly else 'rw', self.root, '/')
         except RuntimeError as msg:
             self.out.warn("unable to mount the root partition: %s" % msg)
             return False