Add new bundle_volume module
[snf-image-creator] / image_creator / os_type / __init__.py
index 03ca4a7..c0fa505 100644 (file)
 # interpreted as representing official policies, either expressed
 # or implied, of GRNET S.A.
 
-from image_creator.util import output
+from image_creator.util import FatalError
 
+import textwrap
 import re
 
 
+def os_cls(distro, osfamily):
+    module = None
+    classname = None
+    try:
+        module = __import__("image_creator.os_type.%s" % distro,
+                            fromlist=['image_creator.os_type'])
+        classname = distro.capitalize()
+    except ImportError:
+        module = __import__("image_creator.os_type.%s" % osfamily,
+                            fromlist=['image_creator.os_type'])
+        classname = osfamily.capitalize()
+
+    return getattr(module, classname)
+
+
 def add_prefix(target):
     def wrapper(self, *args):
         prefix = args[0]
         return map(lambda x: prefix + x, target(self, *args))
     return wrapper
 
-def exclude_task(func):
-    func.excluded = True
-    return func
+
+def sysprep(enabled=True):
+    def wrapper(func):
+        func.sysprep = True
+        func.enabled = enabled
+        return func
+    return wrapper
+
 
 class OSBase(object):
     """Basic operating system class"""
-    def __init__(self, rootdev, ghandler):
+
+    def __init__(self, rootdev, ghandler, output):
         self.root = rootdev
         self.g = ghandler
+        self.out = output
+
+        # Collect metadata about the OS
+        self.meta = {}
+        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['DESCRIPTION'] = self.g.inspect_get_product_name(self.root)
+
+    def _is_sysprep(self, obj):
+        return getattr(obj, 'sysprep', False) and callable(obj)
+
+    def list_syspreps(self):
+
+        objs = [getattr(self, name) for name in dir(self)
+                if not name.startswith('_')]
+
+        return [x for x in objs if self._is_sysprep(x)]
+
+    def sysprep_info(self, obj):
+        assert self._is_sysprep(obj), "Object is not a sysprep"
+
+        return (obj.__name__.replace('_', '-'), textwrap.dedent(obj.__doc__))
+
+    def get_sysprep_by_name(self, name):
+        """Returns the sysprep object with the given name"""
+        error_msg = "Syprep operation %s does not exist for %s" % \
+                    (name, self.__class__.__name__)
+
+        method_name = name.replace('-', '_')
+        method = None
+        try:
+            method = getattr(self, method_name)
+        except AttributeError:
+            raise FatalError(error_msg)
+
+        if not self._is_sysprep(method):
+            raise FatalError(error_msg)
+
+        return method
+
+    def enable_sysprep(self, obj):
+        """Enable a system preparation operation"""
+        setattr(obj.im_func, 'enabled', True)
+
+    def disable_sysprep(self, obj):
+        """Disable a system preparation operation"""
+        setattr(obj.im_func, 'enabled', False)
+
+    def print_syspreps(self):
+        """Print enabled and disabled system preparation operations."""
+
+        syspreps = self.list_syspreps()
+        enabled = filter(lambda x: x.enabled, syspreps)
+        disabled = filter(lambda x: not x.enabled, syspreps)
+
+        wrapper = textwrap.TextWrapper()
+        wrapper.subsequent_indent = '\t'
+        wrapper.initial_indent = '\t'
+        wrapper.width = 72
+
+        self.out.output("Enabled system preparation operations:")
+        if len(enabled) == 0:
+            self.out.output("(none)")
+        else:
+            for sysprep in enabled:
+                name = sysprep.__name__.replace('_', '-')
+                descr = wrapper.fill(textwrap.dedent(sysprep.__doc__))
+                self.out.output('    %s:\n%s\n' % (name, descr))
+
+        self.out.output("Disabled system preparation operations:")
+        if len(disabled) == 0:
+            self.out.output("(none)")
+        else:
+            for sysprep in disabled:
+                name = sysprep.__name__.replace('_', '-')
+                descr = wrapper.fill(textwrap.dedent(sysprep.__doc__))
+                self.out.output('    %s:\n%s\n' % (name, descr))
 
     @add_prefix
     def ls(self, directory):
@@ -104,66 +204,20 @@ class OSBase(object):
             if has_ftype(f, ftype):
                 action(full_path)
 
-    def get_metadata(self):
-        """Returns some descriptive metadata about the OS."""
-        meta = {}
-        meta['ROOT_PARTITION'] = "%d" % self.g.part_to_partnum(self.root)
-        meta['OSFAMILY'] = self.g.inspect_get_type(self.root)
-        meta['OS'] = self.g.inspect_get_distro(self.root)
-        meta['DESCRIPTION'] = self.g.inspect_get_product_name(self.root)
-
-        return meta
-
-    def data_cleanup(self):
-        """Cleanup sensitive data out of the OS image."""
-
-        output('Cleaning up sensitive data out of the OS image:')
-
-        tasks, _ = self.list_data_cleanup()
-        size = len(tasks)
-        cnt = 0
-        for task in tasks:
-            cnt += 1
-            output(('(%d/%d)' % (cnt, size)).ljust(7), False)
-            task()
-        output()
-
-    def sysprep(self):
+    def do_sysprep(self):
         """Prepere system for image creation."""
 
-        output('Preparing system for image creation:')
+        self.out.output('Preparing system for image creation:')
+
+        tasks = self.list_syspreps()
+        enabled = filter(lambda x: x.enabled, tasks)
 
-        tasks, _ = self.list_sysprep()
-        size = len(tasks)
+        size = len(enabled)
         cnt = 0
-        for task in tasks:
+        for task in enabled:
             cnt += 1
-            output(('(%d/%d)' % (cnt, size)).ljust(7), False)
+            self.out.output(('(%d/%d)' % (cnt, size)).ljust(7), False)
             task()
-        output()
-
-    def list_sysprep(self):
-        """List all sysprep actions"""
-
-        is_sysprep = lambda x: x.startswith('sysprep_') and \
-                                                    callable(getattr(self, x))
-        tasks = [getattr(self, x) for x in dir(self) if is_sysprep(x)]
-
-        included = [t for t in tasks if not getattr(t, "excluded", False)]
-        excluded = [t for t in tasks if getattr(t, "excluded", False)]
-
-        return included, excluded
-
-    def list_data_cleanup(self):
-        """List all data_cleanup actions"""
-
-        is_cleanup = lambda x: x.startswith('data_cleanup_') and \
-                                                    callable(getattr(self, x))
-        tasks = [getattr(self, x) for x in dir(self) if is_cleanup(x)]
-
-        included = [t for t in tasks if not getattr(t, "excluded", False)]
-        excluded = [t for t in tasks if getattr(t, "excluded", False)]
-
-        return included, excluded
+        self.out.output()
 
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :