bindctl.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Copyright (C) 2009 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. from moduleinfo import *
  16. from bindcmd import *
  17. import isc
  18. import pprint
  19. from optparse import OptionParser, OptionValueError
  20. __version__ = 'Bindctl'
  21. def prepare_config_commands(tool):
  22. module = ModuleInfo(name = "config", desc = "Configuration commands")
  23. cmd = CommandInfo(name = "show", desc = "Show configuration")
  24. param = ParamInfo(name = "identifier", type = "string", optional=True)
  25. cmd.add_param(param)
  26. module.add_command(cmd)
  27. cmd = CommandInfo(name = "add", desc = "Add entry to configuration list")
  28. param = ParamInfo(name = "identifier", type = "string", optional=True)
  29. cmd.add_param(param)
  30. param = ParamInfo(name = "value", type = "string", optional=False)
  31. cmd.add_param(param)
  32. module.add_command(cmd)
  33. cmd = CommandInfo(name = "remove", desc = "Remove entry from 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 = "set", desc = "Set a configuration value")
  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 = "unset", desc = "Unset a configuration value")
  46. param = ParamInfo(name = "identifier", type = "string", optional=False)
  47. cmd.add_param(param)
  48. module.add_command(cmd)
  49. cmd = CommandInfo(name = "diff", desc = "Show all local changes")
  50. module.add_command(cmd)
  51. cmd = CommandInfo(name = "revert", desc = "Revert all local changes")
  52. module.add_command(cmd)
  53. cmd = CommandInfo(name = "commit", desc = "Commit all local changes")
  54. module.add_command(cmd)
  55. cmd = CommandInfo(name = "go", desc = "Go to a specific configuration part")
  56. param = ParamInfo(name = "identifier", type="string", optional=False)
  57. cmd.add_param(param)
  58. module.add_command(cmd)
  59. tool.add_module_info(module)
  60. def check_port(option, opt_str, value, parser):
  61. if (value < 0) or (value > 65535):
  62. raise OptionValueError('%s requires a port number (0-65535)' % opt_str)
  63. parser.values.port = value
  64. def check_addr(option, opt_str, value, parser):
  65. ipstr = value
  66. ip_family = socket.AF_INET
  67. if (ipstr.find(':') != -1):
  68. ip_family = socket.AF_INET6
  69. try:
  70. socket.inet_pton(ip_family, ipstr)
  71. except:
  72. raise OptionValueError("%s invalid ip address" % ipstr)
  73. parser.values.addr = value
  74. def set_bindctl_options(parser):
  75. parser.add_option('-p', '--port', dest = 'port', type = 'int',
  76. action = 'callback', callback=check_port,
  77. default = '8080', help = 'port for cmdctl of bind10')
  78. parser.add_option('-a', '--address', dest = 'addr', type = 'string',
  79. action = 'callback', callback=check_addr,
  80. default = '127.0.0.1', help = 'IP address for cmdctl of bind10')
  81. if __name__ == '__main__':
  82. try:
  83. parser = OptionParser(version = __version__)
  84. set_bindctl_options(parser)
  85. (options, args) = parser.parse_args()
  86. server_addr = options.addr + ':' + str(options.port)
  87. tool = BindCmdInterpreter(server_addr)
  88. prepare_config_commands(tool)
  89. tool.run()
  90. except Exception as e:
  91. print(e, "\nFailed to connect with b10-cmdctl module, is it running?")