summaryrefslogtreecommitdiffstats
path: root/src/sbin/bcfg2-admin
blob: 24e9eaac48b87240b776e10abc94e28153c72558 (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
#!/usr/bin/env python
"""bcfg2-admin is a script that helps to administrate a Bcfg2 deployment."""

import sys
import logging
import Bcfg2.Logger
import Bcfg2.Options
import Bcfg2.Server.Admin
# Compatibility import
from Bcfg2.Bcfg2Py3k import StringIO

log = logging.getLogger('bcfg2-admin')


def mode_import(modename):
    """Load Bcfg2.Server.Admin.<mode>."""
    modname = modename.capitalize()
    mod = getattr(__import__("Bcfg2.Server.Admin.%s" %
                             (modname)).Server.Admin, modname)
    return getattr(mod, modname)


def get_modes():
    """Get all available modes, except for the base mode."""
    return [x.lower() for x in Bcfg2.Server.Admin.__all__ if x != 'mode']


def create_description():
    """Create the description string from the list of modes."""
    modes = get_modes()
    description = StringIO()
    description.write("Available modes are:\n\n")
    for mode in modes:
        try:
            description.write(("   %-15s   %s\n" %
                               (mode, mode_import(mode).__shorthelp__)))
        except (ImportError, SystemExit):
            pass
    return description.getvalue()


def main():
    optinfo = dict()
    optinfo.update(Bcfg2.Options.CLI_COMMON_OPTIONS)
    optinfo.update(Bcfg2.Options.SERVER_COMMON_OPTIONS)
    setup = Bcfg2.Options.OptionParser(optinfo)
    # override default help message to include description of all modes
    setup.hm = "Usage:\n\n%s\n%s" % (setup.buildHelpMessage(),
                                     create_description())
    setup.parse(sys.argv[1:])

    if setup['debug']:
        level = logging.DEBUG
    elif setup['verbose']:
        level = logging.INFO
    else:
        level = logging.WARNING
    Bcfg2.Logger.setup_logging('bcfg2-admin', to_syslog=setup['syslog'],
                               level=level)

    # Provide help if requested or no args were specified
    if (not setup['args'] or len(setup['args']) < 1 or
        setup['args'][0] == 'help' or setup['help']):
        if len(setup['args']) > 1:
            # Get help for a specific mode by passing it the help argument
            setup['args'] = [setup['args'][1], setup['args'][0]]
        else:
            # Print short help for all modes
            print(setup.hm)
            raise SystemExit(0)

    if setup['args'][0] in get_modes():
        modname = setup['args'][0].capitalize()
        if len(setup['args']) > 1 and setup['args'][1] == 'help':
            print(mode_import(modname).__longhelp__)
            raise SystemExit(0)
        try:
            mode_cls = mode_import(modname)
        except ImportError:
            e = sys.exc_info()[1]
            log.error("Failed to load admin mode %s: %s" % (modname, e))
            raise SystemExit(1)
        mode = mode_cls(setup)
        mode(setup['args'][1:])
        if hasattr(mode, 'bcore'):
            mode.bcore.shutdown()
    else:
        log.error("Error: Unknown mode '%s'\n" % setup['args'][0])
        print(create_description())
        raise SystemExit(1)

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        raise SystemExit(1)