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

import getopt
import logging
import sys
import Bcfg2.Server.Core
import Bcfg2.Logger
import 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)
    avail_opts = {'-C <configfile>': 'Set alternate bcfg2.conf location'}
    # 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':
        print ("Usage: bcfg2-admin [OPTIONS] MODE [ARGS]\n\n"
               "Available options are:")
        for (opt, arg) in list(avail_opts.items()):
            print(("   %-15s   " % opt + arg))
        print ("\nAvailable modes are:")
        if len(args) > 1:
            args = [args[1], args[0]]
        else:
            for mode in modes:
                try:
                    print(("   %-15s   %s" %
                           (mode, mode_import(mode).__shorthelp__)))
                except ImportError:
                    continue
            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])