summaryrefslogtreecommitdiffstats
path: root/src/sbin/bcfg2-lint
blob: cfb37a20611ff15bd2c73ec7a74d15e966663537 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python

"""This tool examines your Bcfg2 specifications for errors."""
__revision__ = '$Revision$'

import sys
import inspect
import logging
import ConfigParser
import Bcfg2.Logger
import Bcfg2.Options
import Bcfg2.Server.Core
import Bcfg2.Server.Lint

logger = logging.getLogger('bcfg2-lint')

class Parser(ConfigParser.ConfigParser):
    def get(self, section, option, default):
        """ Override ConfigParser.get: If the request option is not in
        the config file then return the value of default rather than
        raise an exception.  We still raise exceptions on missing
        sections.
        """
        try:
            return ConfigParser.ConfigParser.get(self, section, option)
        except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
            return default

def run_serverless_plugins(plugins, config=None, setup=None):
    logger.debug("Running serverless plugins")
    errors = (0, 0)
    for plugin_name, plugin in plugins.items():
        plugin_errors = run_plugin(plugin, plugin_name,
                                   setup=setup, config=config, files=files)
        errors = [errors[n] + plugin_errors[n]
                  for n in range(0, len(errors))]
    return errors

def run_server_plugins(plugins, config=None, setup=None):
    core = load_server(setup)
    logger.debug("Running server plugins")
    errors = (0, 0)
    for plugin_name, plugin in plugins.items():
        plugin_errors = run_plugin(plugin, plugin_name, args=[core],
                                   setup=setup, config=config, files=files)
        errors = [errors[n] + plugin_errors[n]
                  for n in range(0, len(errors))]
    return errors

def run_plugin(plugin, plugin_name, setup=None, args=None, config=None,
               files=None):
    logger.debug("  Running %s" % plugin_name)
    if args is None:
        args = []

    if config is not None and config.has_section(plugin_name):
        args.append(dict(config.items(plugin_name), **setup))
    else:
        args.append(setup)

    # older versions of python do not support mixing *-magic and
    # non-*-magic (e.g., "plugin(*args, files=files)", so we do this
    # all with *-magic
    kwargs = dict(files=files)
    
    try:
        return plugin(*args, **kwargs).Run()
    except Exception, err:
        logger.error("Failed to run plugin %s: %s" % (plugin, err))
        raise SystemExit(1)

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
    core.fam.handle_events_in_interval(4)
    return core

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),
        })
    setup = Bcfg2.Options.OptionParser(optinfo)
    setup.parse(sys.argv[1:])

    log_args = dict(to_syslog=False, to_console=logging.WARNING)
    if setup['verbose']:
        log_args['to_console'] = logging.DEBUG
    Bcfg2.Logger.setup_logging('bcfg2-info', **log_args)

    config = Parser()
    config.read(setup['config'])

    # get list of plugins to run
    if setup['args']:
        allplugins = setup['args']
    elif "bcfg2-repo-validate" in sys.argv[0]:
        allplugins = 'Duplicates,RequiredAttrs,Validate'.split(',')
    else:
        allplugins = config.get('main', 'plugins',
                                ",".join(Bcfg2.Server.Lint.__all__)).split(',')

    if setup['stdin']:
        files = [s.strip() for s in sys.stdin.readlines()]
    else:
        files = None

    # load plugins
    serverplugins = {}
    serverlessplugins = {}
    for plugin_name in allplugins:
        try:
            mod = getattr(__import__("Bcfg2.Server.Lint.%s" %
                                     (plugin_name)).Server.Lint, plugin_name)
        except ImportError:
            try:
                mod = __import__(plugin_name)
            except Exception, err:
                logger.error("Failed to load plugin %s: %s" % (plugin_name,
                                                               err))
                raise SystemExit(1)
        plugin = getattr(mod, plugin_name)
        if [c for c in inspect.getmro(plugin)
            if c == Bcfg2.Server.Lint.ServerPlugin]:
            serverplugins[plugin_name] = plugin
        else:
            serverlessplugins[plugin_name] = plugin

    # errors is a tuple of (errors, warnings)
    errors = run_serverless_plugins(serverlessplugins,
                                    config=config, setup=setup)

    if serverplugins:
        perrors = run_server_plugins(serverplugins, config=config, setup=setup)
        errors = [errors[n] + perrors[n] for n in range(0, len(errors))]

    if errors[0] or errors[1] or setup['verbose']:
        print "%d errors" % errors[0]
        print "%d warnings" % errors[1]
        
    if errors[0]:
        raise SystemExit(2)
    elif errors[1]:
        raise SystemExit(3)