Add options for printing sysprep and data cleanup
[snf-image-creator] / image_creator / os_type / __init__.py
index c5e38eb..e9a8c3a 100644 (file)
@@ -1,4 +1,37 @@
-#!/usr/bin/env python
+# Copyright 2012 GRNET S.A. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or
+# without modification, are permitted provided that the following
+# conditions are met:
+#
+#   1. Redistributions of source code must retain the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer.
+#
+#   2. Redistributions in binary form must reproduce the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer in the documentation and/or other materials
+#      provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# The views and conclusions contained in the software and
+# documentation are those of the authors and should not be
+# interpreted as representing official policies, either expressed
+# or implied, of GRNET S.A.
+
+from image_creator.util import output
 
 import re
 
@@ -10,12 +43,34 @@ def add_prefix(target):
     return wrapper
 
 
+def exclude_task(func):
+    func.excluded = True
+    return func
+
+
 class OSBase(object):
     """Basic operating system class"""
     def __init__(self, rootdev, ghandler):
         self.root = rootdev
         self.g = ghandler
 
+        self.sysprep_regexp = re.compile('^sysprep_')
+        self.data_cleanup_regexp = re.compile('^data_cleanup_')
+
+    def _print_task(self, task):
+        name = task.__name__
+
+        if self.sysprep_regexp.match(name):
+            name = self.sysprep_regexp.sub("", name)
+        elif self.data_cleanup_regexp.match(name):
+            name = self.data_cleanup_regexp.sub("", name)
+        else:
+            raise FatalError("%s is not a task" % name)
+
+        name = name.replace('_', '-')
+
+        output("  %s:\n    %s" % (name, task.__doc__))
+
     @add_prefix
     def ls(self, directory):
         """List the name of all files under a directory"""
@@ -74,16 +129,106 @@ class OSBase(object):
         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)
+        meta['DESCRIPTION'] = self.g.inspect_get_product_name(self.root)
 
         return meta
 
+    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
+
     def data_cleanup(self):
         """Cleanup sensitive data out of the OS image."""
-        raise NotImplementedError
+
+        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):
         """Prepere system for image creation."""
-        raise NotImplementedError
+
+        output('Preparing system for image creation:')
+
+        tasks, _ = self.list_sysprep()
+        size = len(tasks)
+        cnt = 0
+        for task in tasks:
+            cnt += 1
+            output(('(%d/%d)' % (cnt, size)).ljust(7), False)
+            task()
+        output()
+
+    def print_task(self, task):
+        name = task.__name__
+
+        if self.sysprep_regexp.match(name):
+            name = self.sysprep_regexp.sub("", name)
+        elif self.data_cleanup_regexp.match(name):
+            name = self.data_cleanup_regexp.sub("", name)
+        else:
+            raise FatalError("%s is not a task" % name)
+
+        name = name.replace('_', '-')
+
+        output("  %s:\n    %s" % (name, task.__doc__))
+
+    def print_data_cleanup(self):
+        included, excluded = self.list_data_cleanup()
+
+        output("Included data cleanup operations:")
+        if len(included) == 0:
+            ouput("(none)")
+        else:
+            for task in included:
+                self._print_task(task)
+        output("Ommited data cleanup operations:")
+        if len(excluded) == 0:
+            ouput("(none)")
+        else:
+            for task in excluded:
+                self._print_task(task)
+
+    def print_sysprep(self):
+        included, excluded = self.list_sysprep()
+
+        output("Included sysprep operations:")
+        if len(included) == 0:
+            ouput("(none)")
+        else:
+            for task in included:
+                self._print_task(task)
+        output("Ommited sysprep operations:")
+        if len(excluded) == 0:
+            output("(none)")
+        else:
+            for task in excluded:
+                self._print_task(task)
 
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :