bindctl-source.py.in 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.utils.process
  24. isc.utils.process.rename()
  25. __version__ = 'Bindctl'
  26. def prepare_config_commands(tool):
  27. '''Prepare fixed commands for local configuration editing'''
  28. module = ModuleInfo(name = CONFIG_MODULE_NAME, desc = "Configuration commands")
  29. cmd = CommandInfo(name = "show", desc = "Show configuration")
  30. param = ParamInfo(name = "identifier", type = "string", optional=True)
  31. cmd.add_param(param)
  32. module.add_command(cmd)
  33. cmd = CommandInfo(name = "add", desc = "Add entry to configuration list")
  34. param = ParamInfo(name = "identifier", type = "string", optional=True)
  35. cmd.add_param(param)
  36. param = ParamInfo(name = "value", type = "string", optional=False)
  37. cmd.add_param(param)
  38. module.add_command(cmd)
  39. cmd = CommandInfo(name = "remove", desc = "Remove entry from configuration list")
  40. param = ParamInfo(name = "identifier", type = "string", optional=True)
  41. cmd.add_param(param)
  42. param = ParamInfo(name = "value", type = "string", optional=False)
  43. cmd.add_param(param)
  44. module.add_command(cmd)
  45. cmd = CommandInfo(name = "set", desc = "Set a configuration value")
  46. param = ParamInfo(name = "identifier", type = "string", optional=True)
  47. cmd.add_param(param)
  48. param = ParamInfo(name = "value", type = "string", optional=False)
  49. cmd.add_param(param)
  50. module.add_command(cmd)
  51. cmd = CommandInfo(name = "unset", desc = "Unset a configuration value")
  52. param = ParamInfo(name = "identifier", type = "string", optional=False)
  53. cmd.add_param(param)
  54. module.add_command(cmd)
  55. cmd = CommandInfo(name = "diff", desc = "Show all local changes")
  56. module.add_command(cmd)
  57. cmd = CommandInfo(name = "revert", desc = "Revert all local changes")
  58. module.add_command(cmd)
  59. cmd = CommandInfo(name = "commit", desc = "Commit all local changes")
  60. module.add_command(cmd)
  61. cmd = CommandInfo(name = "go", desc = "Go to a specific configuration part")
  62. param = ParamInfo(name = "identifier", type="string", optional=False)
  63. cmd.add_param(param)
  64. module.add_command(cmd)
  65. tool.add_module_info(module)
  66. def check_port(option, opt_str, value, parser):
  67. if (value < 0) or (value > 65535):
  68. raise OptionValueError('%s requires a port number (0-65535)' % opt_str)
  69. parser.values.port = value
  70. def check_addr(option, opt_str, value, parser):
  71. ipstr = value
  72. ip_family = socket.AF_INET
  73. if (ipstr.find(':') != -1):
  74. ip_family = socket.AF_INET6
  75. try:
  76. socket.inet_pton(ip_family, ipstr)
  77. except:
  78. raise OptionValueError("%s invalid ip address" % ipstr)
  79. parser.values.addr = value
  80. def set_bindctl_options(parser):
  81. parser.add_option('-p', '--port', dest = 'port', type = 'int',
  82. action = 'callback', callback=check_port,
  83. default = '8080', help = 'port for cmdctl of bind10')
  84. parser.add_option('-a', '--address', dest = 'addr', type = 'string',
  85. action = 'callback', callback=check_addr,
  86. default = '127.0.0.1', help = 'IP address for cmdctl of bind10')
  87. parser.add_option('-c', '--certificate-chain', dest = 'cert_chain',
  88. type = 'string', action = 'store',
  89. help = 'PEM formatted server certificate validation chain file')
  90. if __name__ == '__main__':
  91. try:
  92. parser = OptionParser(version = __version__)
  93. set_bindctl_options(parser)
  94. (options, args) = parser.parse_args()
  95. server_addr = options.addr + ':' + str(options.port)
  96. tool = BindCmdInterpreter(server_addr, pem_file=options.cert_chain)
  97. prepare_config_commands(tool)
  98. tool.run()
  99. except Exception as e:
  100. print(e, "\nFailed to connect with b10-cmdctl module, is it running?")