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

import getopt, logging, sys
import Bcfg2.Server.Core, Bcfg2.Logger, Bcfg2.Options

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

import Bcfg2.Server.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)

if __name__ == '__main__':
    Bcfg2.Logger.setup_logging('bcfg2-admin', to_console=True, level=40)
    # Get config file path
    configfile = Bcfg2.Options.CFILE.default
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'hC:', ['help', 'configfile='])
    except getopt.GetoptError, msg:
        print msg
        raise SystemExit(1)


    # First get the options...
    for opt, arg in opts:
        if opt in ("-C", "--configfile"):
            configfile = arg

    modes = [x.lower() for x in Bcfg2.Server.Admin.__all__]
    modes.remove('mode')

    if len(args) < 1 or args[0] == 'help':
        sys.stdout.write("Usage: bcfg2-admin MODE [ARGS]\n\n"
               "Available modes are:")
        if len(args) > 1:
            args = [args[1], args[0]]
        else:
            for mode in modes:
                sys.stdout.write("\n   %-12s   %s" %
                                (mode, mode_import(mode).__shorthelp__))
            raise SystemExit(0)
    if args[0] in modes:
        modname = args[0].capitalize()
        try:
            mode_cls = mode_import(modname)
        except ImportError, e:
            log.error("Failed to load admin mod %s: %s" % (modname, e))
        mode = mode_cls(configfile)
        mode(args[1:])
    else:
        print "unknown mode %s" % args[0]