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)]
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
"""
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
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 = {}
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:
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)
"""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')
@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.
* 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
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
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