bindctl-source.py.in 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!@PYTHON@
  2. # Copyright (C) 2009 Internet Systems Consortium.
  3. #
  4. # Permission to use, copy, modify, and distribute this software for any
  5. # purpose with or without fee is hereby granted, provided that the above
  6. # copyright notice and this permission notice appear in all copies.
  7. #
  8. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  9. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  10. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  11. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  13. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  14. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  15. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. """This is the main calling class for the bindctl configuration and
  17. command tool. It sets up a command interpreter and runs that."""
  18. import sys; sys.path.append ('@@PYTHONPATH@@')
  19. from bindctl.moduleinfo import *
  20. from bindctl.bindcmd import *
  21. import pprint
  22. from optparse import OptionParser, OptionValueError
  23. import isc.util.process
  24. isc.util.process.rename()
  25. # This is the version that gets displayed to the user.
  26. # The VERSION string consists of the module name, the module version
  27. # number, and the overall BIND 10 version number (set in configure.ac).
  28. VERSION = "bindctl 20101201 (BIND 10 @PACKAGE_VERSION@)"
  29. DEFAULT_IDENTIFIER_DESC = "The identifier specifies the config item. Child elements are separated with the '/' character. List indices can be specified with '[i]', where i is an integer specifying the index, starting with 0. Examples: 'Boss/start_auth', 'Recurse/listen_on[0]/address'. If no identifier is given, shows the item at the current location."
  30. def prepare_config_commands(tool):
  31. '''Prepare fixed commands for local configuration editing'''
  32. module = ModuleInfo(name = CONFIG_MODULE_NAME, desc = "Configuration commands")
  33. cmd = CommandInfo(name = "show", desc = "Show configuration")
  34. param = ParamInfo(name = "argument", type = "string", optional=True, desc = "If you specify the argument 'all' (before the identifier), recursively show all child elements for the given identifier")
  35. cmd.add_param(param)
  36. param = ParamInfo(name = "identifier", type = "string", optional=True, desc = DEFAULT_IDENTIFIER_DESC)
  37. cmd.add_param(param)
  38. module.add_command(cmd)
  39. cmd = CommandInfo(name = "show_json", desc = "Show full configuration in JSON format")
  40. param = ParamInfo(name = "identifier", type = "string", optional=True, desc = DEFAULT_IDENTIFIER_DESC)
  41. cmd.add_param(param)
  42. module.add_command(cmd)
  43. cmd = CommandInfo(name = "add", desc = "Add an entry to configuration list. If no value is given, a default value is added.")
  44. param = ParamInfo(name = "identifier", type = "string", optional=True, desc = DEFAULT_IDENTIFIER_DESC)
  45. cmd.add_param(param)
  46. param = ParamInfo(name = "value", type = "string", optional=True, desc = "Specifies a value to add to the list. It must be in correct JSON format and complete.")
  47. cmd.add_param(param)
  48. module.add_command(cmd)
  49. cmd = CommandInfo(name = "remove", desc = "Remove entry from configuration list")
  50. param = ParamInfo(name = "identifier", type = "string", optional=True, desc = DEFAULT_IDENTIFIER_DESC)
  51. cmd.add_param(param)
  52. param = ParamInfo(name = "value", type = "string", optional=True, desc = "Specifies a value to remove from the list. It must be in correct JSON format and complete.")
  53. cmd.add_param(param)
  54. module.add_command(cmd)
  55. cmd = CommandInfo(name = "set", desc = "Set a configuration value")
  56. param = ParamInfo(name = "identifier", type = "string", optional=True, desc = DEFAULT_IDENTIFIER_DESC)
  57. cmd.add_param(param)
  58. param = ParamInfo(name = "value", type = "string", optional=False, desc = "Specifies a value to set. It must be in correct JSON format and complete.")
  59. cmd.add_param(param)
  60. module.add_command(cmd)
  61. cmd = CommandInfo(name = "unset", desc = "Unset a configuration value (i.e. revert to the default, if any)")
  62. param = ParamInfo(name = "identifier", type = "string", optional=False, desc = DEFAULT_IDENTIFIER_DESC)
  63. cmd.add_param(param)
  64. module.add_command(cmd)
  65. cmd = CommandInfo(name = "diff", desc = "Show all local changes that have not been committed")
  66. module.add_command(cmd)
  67. cmd = CommandInfo(name = "revert", desc = "Revert all local changes")
  68. module.add_command(cmd)
  69. cmd = CommandInfo(name = "commit", desc = "Commit all local changes.")
  70. module.add_command(cmd)
  71. cmd = CommandInfo(name = "go", desc = "Go to a specific configuration part")
  72. param = ParamInfo(name = "identifier", type="string", optional=False, desc = DEFAULT_IDENTIFIER_DESC)
  73. cmd.add_param(param)
  74. module.add_command(cmd)
  75. tool.add_module_info(module)
  76. def check_port(option, opt_str, value, parser):
  77. if (value < 0) or (value > 65535):
  78. raise OptionValueError('%s requires a port number (0-65535)' % opt_str)
  79. parser.values.port = value
  80. def check_addr(option, opt_str, value, parser):
  81. ipstr = value
  82. ip_family = socket.AF_INET
  83. if (ipstr.find(':') != -1):
  84. ip_family = socket.AF_INET6
  85. try:
  86. socket.inet_pton(ip_family, ipstr)
  87. except:
  88. raise OptionValueError("%s invalid ip address" % ipstr)
  89. parser.values.addr = value
  90. def set_bindctl_options(parser):
  91. parser.add_option('-p', '--port', dest = 'port', type = 'int',
  92. action = 'callback', callback=check_port,
  93. default = '8080', help = 'port for cmdctl of bind10')
  94. parser.add_option('-a', '--address', dest = 'addr', type = 'string',
  95. action = 'callback', callback=check_addr,
  96. default = '127.0.0.1', help = 'IP address for cmdctl of bind10')
  97. parser.add_option('-c', '--certificate-chain', dest = 'cert_chain',
  98. type = 'string', action = 'store',
  99. help = 'PEM formatted server certificate validation chain file')
  100. if __name__ == '__main__':
  101. parser = OptionParser(version = VERSION)
  102. set_bindctl_options(parser)
  103. (options, args) = parser.parse_args()
  104. server_addr = options.addr + ':' + str(options.port)
  105. tool = BindCmdInterpreter(server_addr, pem_file=options.cert_chain)
  106. prepare_config_commands(tool)
  107. tool.run()