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

import getopt, difflib, logging, lxml.etree, os, popen2, re, socket, sys, ConfigParser
import Bcfg2.Server.Core, Bcfg2.Logging
from Bcfg2.Settings import settings

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.Logging.setup_logging('bcfg2-admin', to_console=True)

    configfile = settings.CONFIG_FILE

    try:
        opts, args = getopt.getopt(sys.argv[1:], 'hC:', ['help', 'configfile='])
    except getopt.GetoptError, msg:
        print msg
        raise SystemExit(1)


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

    # override default settings
    settings.read_config_file(configfile)

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

    if help or len(args) < 1 or args[0] == 'help':
        if len(args) > 1:
            args = [args[1], args[0]]
        else:
            for mod in [mode_import(mode) for mode in modes]:
                print mod.__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))
            raise SystemExit(0)
        mode = mode_cls()
        mode(args[1:])
    else:
        print "unknown mode %s" % args[0]