summaryrefslogtreecommitdiffstats
path: root/src/sbin/bcfg2-lint
diff options
context:
space:
mode:
Diffstat (limited to 'src/sbin/bcfg2-lint')
-rwxr-xr-xsrc/sbin/bcfg2-lint125
1 files changed, 54 insertions, 71 deletions
diff --git a/src/sbin/bcfg2-lint b/src/sbin/bcfg2-lint
index 2d371f4aa..423c63ba3 100755
--- a/src/sbin/bcfg2-lint
+++ b/src/sbin/bcfg2-lint
@@ -1,7 +1,6 @@
#!/usr/bin/env python
"""This tool examines your Bcfg2 specifications for errors."""
-__revision__ = '$Revision$'
import sys
import inspect
@@ -63,46 +62,36 @@ def get_errorhandler(config):
def load_server(setup):
""" load server """
core = Bcfg2.Server.Core.Core(setup['repo'], setup['plugins'],
- setup['password'], setup['encoding'])
- if setup['event debug']:
- core.fam.debug = True
+ setup['password'], setup['encoding'],
+ filemonitor=setup['filemonitor'],
+ setup=setup)
core.fam.handle_events_in_interval(4)
return core
+def load_plugin(module, obj_name=None):
+ parts = module.split(".")
+ if obj_name is None:
+ obj_name = parts[-1]
+
+ try:
+ mod = __import__(module)
+ except ImportError:
+ err = sys.exc_info()[1]
+ logger.error("Failed to load plugin %s: %s" % (obj_name, err))
+ raise
+
+ for p in parts[1:]:
+ mod = getattr(mod, p)
+ return getattr(mod, obj_name)
+
if __name__ == '__main__':
- optinfo = {
- 'configfile': Bcfg2.Options.CFILE,
- 'help': Bcfg2.Options.HELP,
- 'verbose': Bcfg2.Options.VERBOSE,
- }
- optinfo.update({
- 'event debug': Bcfg2.Options.DEBUG,
- 'encoding': Bcfg2.Options.ENCODING,
- # Server options
- 'repo': Bcfg2.Options.SERVER_REPOSITORY,
- 'plugins': Bcfg2.Options.SERVER_PLUGINS,
- 'mconnect': Bcfg2.Options.SERVER_MCONNECT,
- 'filemonitor': Bcfg2.Options.SERVER_FILEMONITOR,
- 'location': Bcfg2.Options.SERVER_LOCATION,
- 'static': Bcfg2.Options.SERVER_STATIC,
- 'key': Bcfg2.Options.SERVER_KEY,
- 'cert': Bcfg2.Options.SERVER_CERT,
- 'ca': Bcfg2.Options.SERVER_CA,
- 'password': Bcfg2.Options.SERVER_PASSWORD,
- 'protocol': Bcfg2.Options.SERVER_PROTOCOL,
- # More options
- 'logging': Bcfg2.Options.LOGGING_FILE_PATH,
- 'stdin': Bcfg2.Options.FILES_ON_STDIN,
- 'schema': Bcfg2.Options.SCHEMA_PATH,
- 'config': Bcfg2.Options.Option('Specify bcfg2-lint configuration file',
- '/etc/bcfg2-lint.conf',
- cmd='--lint-config',
- odesc='<conffile>',
- long_arg=True),
- 'showerrors': Bcfg2.Options.Option('Show error handling', False,
- cmd='--list-errors',
- long_arg=True),
- })
+ optinfo = dict(config=Bcfg2.Options.LINT_CONFIG,
+ showerrors=Bcfg2.Options.LINT_SHOW_ERRORS,
+ stdin=Bcfg2.Options.LINT_FILES_ON_STDIN,
+ schema=Bcfg2.Options.SCHEMA_PATH,
+ plugins=Bcfg2.Options.SERVER_PLUGINS)
+ optinfo.update(Bcfg2.Options.CLI_COMMON_OPTIONS)
+ optinfo.update(Bcfg2.Options.SERVER_COMMON_OPTIONS)
setup = Bcfg2.Options.OptionParser(optinfo)
setup.parse(sys.argv[1:])
@@ -115,53 +104,38 @@ if __name__ == '__main__':
config.read(setup['configfile'])
config.read(setup['config'])
- if setup['showerrors']:
- if config.has_section("errors"):
- econf = dict(config.items("errors"))
- else:
- econf = dict()
-
- print("%-35s %-35s" % ("Error name", "Handler (Default)"))
- for err, default in Bcfg2.Server.Lint.ErrorHandler._errors.items():
- if err in econf and econf[err] != default:
- handler = "%s (%s)" % (econf[err], default)
- else:
- handler = default
- print("%-35s %-35s" % (err, handler))
- raise SystemExit(0)
-
# get list of plugins to run
if setup['args']:
- allplugins = setup['args']
+ plugin_list = setup['args']
elif "bcfg2-repo-validate" in sys.argv[0]:
- allplugins = 'Duplicates,RequiredAttrs,Validate'.split(',')
+ plugin_list = 'Duplicates,RequiredAttrs,Validate'.split(',')
else:
try:
- allplugins = config.get('lint', 'plugins').split(',')
+ plugin_list = config.get('lint', 'plugins').split(',')
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
- allplugins = Bcfg2.Server.Lint.__all__
+ plugin_list = Bcfg2.Server.Lint.__all__
if setup['stdin']:
files = [s.strip() for s in sys.stdin.readlines()]
else:
files = None
- # load plugins
- serverplugins = {}
- serverlessplugins = {}
- for plugin_name in allplugins:
+ # load plugins specified in the config first
+ allplugins = dict()
+ for plugin in plugin_list:
+ allplugins[plugin] = load_plugin("Bcfg2.Server.Lint." + plugin)
+
+ # load lint plugins bundled with bcfg2-server plugins
+ for plugin in setup['plugins']:
try:
- mod = getattr(__import__("Bcfg2.Server.Lint.%s" %
- (plugin_name)).Server.Lint, plugin_name)
- except ImportError:
- try:
- mod = __import__(plugin_name)
- except Exception:
- err = sys.exc_info()[1]
- logger.error("Failed to load plugin %s: %s" % (plugin_name,
- err))
- raise SystemExit(1)
- plugin = getattr(mod, plugin_name)
+ allplugins[plugin] = load_plugin("Bcfg2.Server.Plugins." + plugin,
+ obj_name=plugin + "Lint")
+ except AttributeError:
+ pass
+
+ serverplugins = dict()
+ serverlessplugins = dict()
+ for plugin_name, plugin in allplugins.items():
if [c for c in inspect.getmro(plugin)
if c == Bcfg2.Server.Lint.ServerPlugin]:
serverplugins[plugin_name] = plugin
@@ -170,6 +144,15 @@ if __name__ == '__main__':
errorhandler = get_errorhandler(config)
+ if setup['showerrors']:
+ for plugin in serverplugins.values() + serverlessplugins.values():
+ errorhandler.RegisterErrors(getattr(plugin, 'Errors')())
+
+ print("%-35s %-35s" % ("Error name", "Handler"))
+ for err, handler in errorhandler._handlers.items():
+ print("%-35s %-35s" % (err, handler.__name__))
+ raise SystemExit(0)
+
run_serverless_plugins(serverlessplugins,
errorhandler=errorhandler,
config=config, setup=setup)