Add various Windows syspreps
[snf-image-creator] / image_creator / os_type / __init__.py
index ec4f20d..855022f 100644 (file)
@@ -41,6 +41,8 @@ from image_creator.util import FatalError
 
 import textwrap
 import re
+from collections import namedtuple
+from functools import wraps
 
 
 def os_cls(distro, osfamily):
@@ -66,23 +68,40 @@ def add_prefix(target):
     return wrapper
 
 
-def sysprep(enabled=True):
+def sysprep(message, enabled=True):
     """Decorator for system preparation tasks"""
-    def wrapper(func):
+    def wrapper1(func):
         func.sysprep = True
         func.enabled = enabled
         func.executed = False
-        return func
-    return wrapper
+
+        @wraps(func)
+        def wrapper2(self, print_message=True):
+            if print_message:
+                self.out.output(message)
+            return func(self)
+
+        return wrapper2
+
+    return wrapper1
 
 
 class OSBase(object):
     """Basic operating system class"""
 
-    def __init__(self, rootdev, ghandler, output):
-        self.root = rootdev
-        self.g = ghandler
-        self.out = output
+    SysprepParam = namedtuple('SysprepParam',
+                              'name description length validator')
+
+    def __init__(self, image, **kargs):
+        self.image = image
+
+        self.root = image.root
+        self.g = image.g
+        self.out = image.out
+
+        self.sysprep_params = \
+            kargs['sysprep_params'] if 'sysprep_params' in kargs else {}
+
         self.meta = {}
 
     def collect_metadata(self):
@@ -99,6 +118,12 @@ class OSBase(object):
 
         self.out.output()
 
+    def needed_sysprep_params(self):
+        """Returns a list of needed sysprep parameters. Each element in the
+        list is a SysprepParam object.
+        """
+        return []
+
     def list_syspreps(self):
         """Returns a list of sysprep objects"""
         objs = [getattr(self, name) for name in dir(self)
@@ -167,6 +192,23 @@ class OSBase(object):
                 descr = wrapper.fill(textwrap.dedent(sysprep.__doc__))
                 self.out.output('    %s:\n%s\n' % (name, descr))
 
+    def print_sysprep_params(self):
+        """Print the system preparation parameter the user may use"""
+
+        self.out.output("Needed system preparation parameters:")
+
+        params = self.needed_sysprep_params()
+
+        if len(params) == 0:
+            self.out.output("(none)")
+            return
+
+        for param in params:
+            self.out.output("\t%s (%s): %s" %
+                            (param.description, param.name,
+                             self.sysprep_params[param.name] if param.name in
+                             self.sysprep_params else "(none)"))
+
     def do_sysprep(self):
         """Prepare system for image creation."""