Move mount/umount from image to os_type module
[snf-image-creator] / image_creator / os_type / __init__.py
index 8a39846..d093172 100644 (file)
 # interpreted as representing official policies, either expressed
 # or implied, of GRNET S.A.
 
-from image_creator.util import output, FatalError
+from image_creator.util import FatalError
 
 import textwrap
 import re
 
 
-def get_os_class(distro, osfamily):
+def os_cls(distro, osfamily):
+    """Given the distro name and the osfamily, return the appropriate class"""
     module = None
     classname = None
     try:
-        module = __import__("image_creator.os_type.%s"
-            % distro, fromlist=['image_creator.os_type'])
+        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'])
+        module = __import__("image_creator.os_type.%s" % osfamily,
+                            fromlist=['image_creator.os_type'])
         classname = osfamily.capitalize()
 
     return getattr(module, classname)
@@ -60,9 +61,11 @@ def add_prefix(target):
 
 
 def sysprep(enabled=True):
+    """Decorator for system preparation tasks"""
     def wrapper(func):
         func.sysprep = True
         func.enabled = enabled
+        func.executed = False
         return func
     return wrapper
 
@@ -70,15 +73,32 @@ def sysprep(enabled=True):
 class OSBase(object):
     """Basic operating system class"""
 
-    def __init__(self, rootdev, ghandler):
+    def __init__(self, rootdev, ghandler, output):
         self.root = rootdev
         self.g = ghandler
-
-        # Collect metadata about the OS
+        self.out = output
         self.meta = {}
+
+    def collect_metadata(self):
+        """Collect metadata about the OS"""
+
+        try:
+            if not self.mount(readonly=True):
+                raise FatalError("Unable to mount the media read-only")
+
+            self.out.output('Collecting image metadata ...', False)
+            self._do_collect_metadata()
+            self.out.success('done')
+        finally:
+            self.umount()
+
+    def _do_collect_metadata(self):
+
         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)
+        if self.meta['OS'] == "unknown":
+            self.meta['OS'] = self.meta['OSFAMILY']
         self.meta['DESCRIPTION'] = self.g.inspect_get_product_name(self.root)
 
     def _is_sysprep(self, obj):
@@ -86,18 +106,20 @@ class OSBase(object):
 
     def list_syspreps(self):
 
-        objs = [getattr(self, name) for name in dir(self) \
-            if not name.startswith('_')]
+        objs = [getattr(self, name) for name in dir(self)
+                if not name.startswith('_')]
 
-        enabled = [x for x in objs if self._is_sysprep(x) and x.enabled]
-        disabled = [x for x in objs if self._is_sysprep(x) and not x.enabled]
+        return [x for x in objs if self._is_sysprep(x) and x.executed is False]
 
-        return enabled, disabled
+    def sysprep_info(self, obj):
+        assert self._is_sysprep(obj), "Object is not a sysprep"
 
-    def _sysprep_change_status(self, name, status):
+        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__)
+                    (name, self.__class__.__name__)
 
         method_name = name.replace('-', '_')
         method = None
@@ -109,43 +131,45 @@ class OSBase(object):
         if not self._is_sysprep(method):
             raise FatalError(error_msg)
 
-        setattr(method.im_func, 'enabled', status)
+        return method
 
-    def enable_sysprep(self, name):
-        """Enable a system preperation operation"""
-        self._sysprep_change_status(name, True)
+    def enable_sysprep(self, obj):
+        """Enable a system preparation operation"""
+        setattr(obj.im_func, 'enabled', True)
 
-    def disable_sysprep(self, name):
-        """Disable a system preperation operation"""
-        self._sysprep_change_status(name, False)
+    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 preperation operations."""
+        """Print enabled and disabled system preparation operations."""
 
-        enabled, disabled = self.list_syspreps()
+        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
 
-        output("Enabled system preperation operations:")
+        self.out.output("Enabled system preparation operations:")
         if len(enabled) == 0:
-            output("(none)")
+            self.out.output("(none)")
         else:
             for sysprep in enabled:
                 name = sysprep.__name__.replace('_', '-')
                 descr = wrapper.fill(textwrap.dedent(sysprep.__doc__))
-                output('    %s:\n%s\n' % (name, descr))
+                self.out.output('    %s:\n%s\n' % (name, descr))
 
-        output("Disabled system preperation operations:")
+        self.out.output("Disabled system preparation operations:")
         if len(disabled) == 0:
-            output("(none)")
+            self.out.output("(none)")
         else:
             for sysprep in disabled:
                 name = sysprep.__name__.replace('_', '-')
                 descr = wrapper.fill(textwrap.dedent(sysprep.__doc__))
-                output('    %s:\n%s\n' % (name, descr))
+                self.out.output('    %s:\n%s\n' % (name, descr))
 
     @add_prefix
     def ls(self, directory):
@@ -202,15 +226,54 @@ class OSBase(object):
     def do_sysprep(self):
         """Prepere system for image creation."""
 
-        output('Preparing system for image creation:')
+        try:
+            if not self.mount(readonly=False):
+                raise FatalError("Unable to mount the media read-write")
+
+            self.out.output('Preparing system for image creation:')
+
+            tasks = self.list_syspreps()
+            enabled = filter(lambda x: x.enabled, tasks)
+
+            size = len(enabled)
+            cnt = 0
+            for task in enabled:
+                cnt += 1
+                self.out.output(('(%d/%d)' % (cnt, size)).ljust(7), False)
+                task()
+                setattr(task.im_func, 'executed', True)
+            self.out.output()
+        finally:
+            self.umount()
+
+    def _do_mount(self, readonly):
+        try:
+            self.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
+
+        return True
+
+    def mount(self, readonly=False):
+        """Mount image."""
+
+        if getattr(self, "mounted", False):
+            return True
+
+        mount_type = 'read-only' if readonly else 'read-write'
+        self.out.output("Mount the media %s ..." % mount_type, False)
+
+        if not self._do_mount(readonly):
+            return False
+
+        self.mounted = True
+        self.out.success('done')
+        return True
 
-        tasks, _ = self.list_syspreps()
-        size = len(tasks)
-        cnt = 0
-        for task in tasks:
-            cnt += 1
-            output(('(%d/%d)' % (cnt, size)).ljust(7), False)
-            task()
-        output()
+    def umount(self):
+        """Umount all mounted filesystems."""
+        self.g.umount_all()
+        self.mounted = False
 
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :