X-Git-Url: https://code.grnet.gr/git/snf-image-creator/blobdiff_plain/3b2f6619501d07d02faf39adcd94d1ab20ffd0d0..f8cccab5464e07d05e2295e38669676b9f2aa543:/image_creator/os_type/__init__.py diff --git a/image_creator/os_type/__init__.py b/image_creator/os_type/__init__.py index 260cb08..5601a87 100644 --- a/image_creator/os_type/__init__.py +++ b/image_creator/os_type/__init__.py @@ -1,8 +1,57 @@ -#!/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 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] @@ -10,11 +59,98 @@ def add_prefix(target): return wrapper +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 _sysprep_change_status(self, name, status): + + 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) + + setattr(method.im_func, 'enabled', status) + + def enable_sysprep(self, name): + """Enable a system preperation operation""" + self._sysprep_change_status(name, True) + + def disable_sysprep(self, name): + """Disable a system preperation operation""" + self._sysprep_change_status(name, False) + + def print_syspreps(self): + """Print enabled and disabled system preperation 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 preperation 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 preperation 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): @@ -68,17 +204,20 @@ class OSBase(object): if has_ftype(f, ftype): action(full_path) - def get_metadata(self): - """Returnes some descriptive metadata of the OS.""" - meta = {} - 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) + def do_sysprep(self): + """Prepere system for image creation.""" + + self.out.output('Preparing system for image creation:') - return meta + tasks = self.list_syspreps() + enabled = filter(lambda x: x.enabled, tasks) - def data_cleanup(self): - """Cleanup sesitive data out of the OS image.""" - raise NotImplementedError + size = len(enabled) + cnt = 0 + for task in enabled: + cnt += 1 + self.out.output(('(%d/%d)' % (cnt, size)).ljust(7), False) + task() + self.out.output() # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :