bindctl.py 4.33 KB
Newer Older
Likun Zhang's avatar
Likun Zhang committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Copyright (C) 2009  Internet Systems Consortium.
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.


Likun Zhang's avatar
Likun Zhang committed
17
from moduleinfo  import *
18
from bindcmd import *
19
import isc
20
import pprint
21
from optparse import OptionParser, OptionValueError
Likun Zhang's avatar
Likun Zhang committed
22

23
__version__ = 'Bindctl'
Likun Zhang's avatar
Likun Zhang committed
24

25
def prepare_config_commands(tool):
26
    module = ModuleInfo(name = "config", desc = "Configuration commands")
27
    cmd = CommandInfo(name = "show", desc = "Show configuration")
28 29 30 31
    param = ParamInfo(name = "identifier", type = "string", optional=True)
    cmd.add_param(param)
    module.add_command(cmd)

32
    cmd = CommandInfo(name = "add", desc = "Add entry to configuration list")
33 34
    param = ParamInfo(name = "identifier", type = "string", optional=True)
    cmd.add_param(param)
35
    param = ParamInfo(name = "value", type = "string", optional=False)
36 37 38
    cmd.add_param(param)
    module.add_command(cmd)

39
    cmd = CommandInfo(name = "remove", desc = "Remove entry from configuration list")
40 41
    param = ParamInfo(name = "identifier", type = "string", optional=True)
    cmd.add_param(param)
42
    param = ParamInfo(name = "value", type = "string", optional=False)
43 44 45
    cmd.add_param(param)
    module.add_command(cmd)

46
    cmd = CommandInfo(name = "set", desc = "Set a configuration value")
47 48
    param = ParamInfo(name = "identifier", type = "string", optional=True)
    cmd.add_param(param)
49
    param = ParamInfo(name = "value", type = "string", optional=False)
50 51 52
    cmd.add_param(param)
    module.add_command(cmd)

53
    cmd = CommandInfo(name = "unset", desc = "Unset a configuration value")
54
    param = ParamInfo(name = "identifier", type = "string", optional=False)
55 56
    cmd.add_param(param)
    module.add_command(cmd)
57

58
    cmd = CommandInfo(name = "diff", desc = "Show all local changes")
59
    module.add_command(cmd)
60

61
    cmd = CommandInfo(name = "revert", desc = "Revert all local changes")
62 63
    module.add_command(cmd)

64
    cmd = CommandInfo(name = "commit", desc = "Commit all local changes")
65 66
    module.add_command(cmd)

67
    cmd = CommandInfo(name = "go", desc = "Go to a specific configuration part")
68 69 70 71
    param = ParamInfo(name = "identifier", type="string", optional=False)
    cmd.add_param(param)
    module.add_command(cmd)

72
    tool.add_module_info(module)
Likun Zhang's avatar
Likun Zhang committed
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
def check_port(option, opt_str, value, parser):
    if (value < 0) or (value > 65535):
        raise OptionValueError('%s requires a port number (0-65535)' % opt_str)
    parser.values.port = value

def check_addr(option, opt_str, value, parser):
    ipstr = value
    ip_family = socket.AF_INET
    if (ipstr.find(':') != -1):
        ip_family = socket.AF_INET6

    try:
        socket.inet_pton(ip_family, ipstr)
    except:
        raise OptionValueError("%s invalid ip address" % ipstr)

    parser.values.addr = value

def set_bindctl_options(parser):
    parser.add_option('-p', '--port', dest = 'port', type = 'int',
            action = 'callback', callback=check_port,
            default = '8080', help = 'port for cmdctl of bind10')

    parser.add_option('-a', '--address', dest = 'addr', type = 'string',
            action = 'callback', callback=check_addr,
            default = '127.0.0.1', help = 'IP address for cmdctl of bind10')


Likun Zhang's avatar
Likun Zhang committed
102
if __name__ == '__main__':
103 104 105 106 107 108 109 110 111 112
    try:
        parser = OptionParser(version = __version__)
        set_bindctl_options(parser)
        (options, args) = parser.parse_args()
        server_addr = options.addr + ':' + str(options.port)
        tool = BindCmdInterpreter(server_addr)
        prepare_config_commands(tool)
        tool.run()
    except Exception as e:
        print(e, "\nFailed to connect with b10-cmdctl module, is it running?")
113

Likun Zhang's avatar
Likun Zhang committed
114