From: Nikos Skalkotos Date: Mon, 9 Apr 2012 12:09:20 +0000 (+0300) Subject: Add options for printing sysprep and data cleanup X-Git-Tag: v0.1~113 X-Git-Url: https://code.grnet.gr/git/snf-image-creator/commitdiff_plain/76d4a1c97a6b60dc7d50683454e4ecfb5271d5f8 Add options for printing sysprep and data cleanup Add --print-data-cleanup and --print sysprep input options. If enabled the program will output what data cleanup and syspep operations will and won't run a a selected input media --- diff --git a/image_creator/main.py b/image_creator/main.py index ab69b2d..336d795 100755 --- a/image_creator/main.py +++ b/image_creator/main.py @@ -78,6 +78,14 @@ def parse_options(input_args): default=None, action="callback", callback=check_writable_dir, help="dump image to FILE", metavar="FILE") + parser.add_option("--print-sysprep", dest="print_sysprep", default=False, + help="Print the enabled and disable sysprep actions for this image", + action="store_true") + + parser.add_option("--print-data-cleanup", dest="print_data_cleanup", + default=False, help="Print the enabled and disable data cleanup " + "operations actions for this source", action="store_true") + parser.add_option("-s", "--silent", dest="silent", default=False, help="silent mode, only output error", action="store_true") @@ -98,9 +106,6 @@ def parse_options(input_args): if options.register: options.upload = True - if options.outfile is None and not options.upload: - parser.error('either outfile (-o) or upload (-u) must be set.') - return options @@ -110,6 +115,11 @@ def image_creator(): if options.silent: util.silent = True + if options.outfile is None and not options.upload \ + and not options.print_sysprep and not options.print_data_cleanup: + FatalError("At least one of the following: `-o', `-u', " + "`--print-sysprep' `--print-data-cleanup' must be set") + output('snf-image-creator %s\n' % version) if os.geteuid() != 0: @@ -134,6 +144,17 @@ def image_creator(): output() + if options.print_sysprep: + image_os.print_sysprep() + output() + + if options.print_data_cleanup: + image_os.print_data_cleanup() + output() + + if options.outfile is None and not options.upload: + return 0 + if options.sysprep: image_os.sysprep() diff --git a/image_creator/os_type/__init__.py b/image_creator/os_type/__init__.py index 2760903..e9a8c3a 100644 --- a/image_creator/os_type/__init__.py +++ b/image_creator/os_type/__init__.py @@ -54,6 +54,23 @@ class OSBase(object): 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""" @@ -116,6 +133,30 @@ class OSBase(object): 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.""" @@ -144,28 +185,50 @@ class OSBase(object): task() output() - 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 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 :