X-Git-Url: https://code.grnet.gr/git/snf-image-creator/blobdiff_plain/47505e5305367404db5722e1c670d61286490b76..5a33a51fcf0d67d7ed42f81082066b05fa64072f:/image_creator/os_type/__init__.py diff --git a/image_creator/os_type/__init__.py b/image_creator/os_type/__init__.py index ddda45e..c8c1620 100644 --- a/image_creator/os_type/__init__.py +++ b/image_creator/os_type/__init__.py @@ -62,6 +62,7 @@ def os_cls(distro, osfamily): def add_prefix(target): + """Decorator that adds a prefix to the result of a function""" def wrapper(self, *args): prefix = args[0] return [prefix + path for path in target(self, *args)] @@ -88,7 +89,7 @@ def sysprep(message, enabled=True, **kwargs): return wrapper -def add_sysprep_param(name, default, description, validator=lambda x: True): +def add_sysprep_param(name, type, default, descr, validate=lambda x: True): """Decorator for __init__ that adds the definition for a system preparation parameter in an instance of a os_type class """ @@ -97,7 +98,9 @@ def add_sysprep_param(name, default, description, validator=lambda x: True): def inner(self, *args, **kwargs): init(self, *args, **kwargs) self.needed_sysprep_params[name] = \ - self.SysprepParam(default, description, validator) + self.SysprepParam(type, default, descr, validate) + if default is not None: + self.sysprep_params[name] = default return inner return wrapper @@ -118,13 +121,13 @@ def del_sysprep_param(name): class OSBase(object): """Basic operating system class""" - SysprepParam = namedtuple('SysprepParam', 'default description validator') + SysprepParam = namedtuple('SysprepParam', + ['type', 'default', 'description', 'validate']) def __init__(self, image, **kargs): self.image = image self.root = image.root - self.g = image.g self.out = image.out self.needed_sysprep_params = {} @@ -134,6 +137,29 @@ class OSBase(object): self.meta = {} self.mounted = False + # Many guestfs compilations don't support scrub + self._scrub_support = True + try: + self.image.g.available(['scrub']) + except RuntimeError: + self._scrub_support = False + + def inspect(self): + """Inspect the media to if it is supported""" + + if self.image.is_unsupported(): + return + + self.out.output('Running OS inspection:') + try: + if not self.mount(readonly=True): + raise FatalError("Unable to mount the media read-only") + self._do_inspect() + finally: + self.umount() + + self.out.output() + def collect_metadata(self): """Collect metadata about the OS""" try: @@ -237,12 +263,17 @@ class OSBase(object): def do_sysprep(self): """Prepare system for image creation.""" + self.out.output('Preparing system for image creation:') + + if self.image.is_unsupported(): + self.out.warn( + "System preparation is disabled for unsupported media") + return + try: if not self.mount(readonly=False): raise FatalError("Unable to mount the media read-write") - self.out.output('Preparing system for image creation:') - enabled = [task for task in self.list_syspreps() if task.enabled] size = len(enabled) @@ -277,7 +308,7 @@ class OSBase(object): """Umount all mounted filesystems.""" self.out.output("Umounting the media ...", False) - self.g.umount_all() + self.image.g.umount_all() self.mounted = False self.out.success('done') @@ -288,12 +319,12 @@ class OSBase(object): @add_prefix def _ls(self, directory): """List the name of all files under a directory""" - return self.g.ls(directory) + return self.image.g.ls(directory) @add_prefix def _find(self, directory): """List the name of all files recursively under a directory""" - return self.g.find(directory) + return self.image.g.find(directory) def _foreach_file(self, directory, action, **kargs): """Perform an action recursively on all files under a directory. @@ -310,6 +341,10 @@ class OSBase(object): * exclude: Exclude all files that follow this pattern. """ + if not self.image.g.is_dir(directory): + self.out.warn("Directory: `%s' does not exist!" % directory) + return + maxdepth = None if 'maxdepth' not in kargs else kargs['maxdepth'] if maxdepth == 0: return @@ -322,7 +357,7 @@ class OSBase(object): ftype = None if 'ftype' not in kargs else kargs['ftype'] has_ftype = lambda x, y: y is None and True or x['ftyp'] == y - for f in self.g.readdir(directory): + for f in self.image.g.readdir(directory): if f['name'] in ('.', '..'): continue @@ -337,19 +372,32 @@ class OSBase(object): if has_ftype(f, ftype): action(full_path) + def _do_inspect(self): + """helper method for inspect""" + pass + def _do_collect_metadata(self): """helper method for collect_metadata""" - 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) + + try: + self.meta['ROOT_PARTITION'] = \ + "%d" % self.image.g.part_to_partnum(self.root) + except RuntimeError: + self.out.warn("Unable to identify the partition number from root " + "partition: %s" % self.root) + + self.meta['OSFAMILY'] = self.image.g.inspect_get_type(self.root) + self.meta['OS'] = self.image.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) + self.meta['DESCRIPTION'] = \ + self.image.g.inspect_get_product_name(self.root) def _do_mount(self, readonly): """helper method for mount""" try: - self.g.mount_options('ro' if readonly else 'rw', self.root, '/') + self.image.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