kea-shell.in 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!@PYTHON@
  2. # Copyright (C) 2017 Internet Systems Consortium, Inc. ("ISC")
  3. #
  4. # This Source Code Form is subject to the terms of the Mozilla Public
  5. # License, v. 2.0. If a copy of the MPL was not distributed with this
  6. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. """
  8. Text client for Control Agent process
  9. """
  10. # First, let's import the right kea_connector.
  11. # We have two versions: one for python 2.x and another for python 3.x.
  12. # Sadly, there's no unified way to handle http connections. The recommended
  13. # way is to use Requests (http://docs.python-requests.org/en/master/), but
  14. # that's a stand alone package that requires separate installation. One of
  15. # the design requirements was to not require any additional packages, so
  16. # the code uses standard libraries available in python. Hence two versions.
  17. import sys
  18. import signal
  19. import argparse
  20. from kea_conn import CARequest # CAResponse
  21. if sys.version_info[0] == 2:
  22. # This is Python 2.x
  23. import kea_connector2 as kea_connector
  24. elif sys.version_info[0] == 3:
  25. # This is Python 3.x
  26. import kea_connector3 as kea_connector
  27. else:
  28. # This is... have no idea what it is.
  29. raise SystemExit("Unknown python version:" + str(sys.version_info[0]))
  30. def timeout_handler(signum, frame):
  31. """Connection timeoout handler"""
  32. del signum, frame
  33. print("Connection timeout")
  34. sys.exit(1)
  35. VERSION = "@PACKAGE_VERSION@"
  36. def shell_body():
  37. """
  38. Second step: Need to parse command line parameters. We will use
  39. argparse for that purpose. It does great job with having default
  40. values, taking care of the help and sanity checking input
  41. parameters.
  42. """
  43. parser = argparse.ArgumentParser(description='kea-shell is a simple text '
  44. 'client that uses REST interface to '
  45. 'connect to Kea Control Agent.')
  46. parser.add_argument('--host', type=str, default='127.0.0.1',
  47. help='hostname of the CA to connect to '
  48. '(defaul:; 127.0.0.1)')
  49. parser.add_argument('--port', type=int, default=8000,
  50. help='TCP port of the CA to connect to '
  51. '(default: 8000)')
  52. parser.add_argument('--timeout', type=int, default='10',
  53. help='Timeout (in seconds) when attempting to '
  54. 'connect to CA (default: 10)')
  55. parser.add_argument('command', type=str, nargs="?",
  56. default='list-commands',
  57. help='command to be executed. If not specified, '
  58. '"list-commands" is used')
  59. parser.add_argument('-v', action="store_true", help="Prints version")
  60. cmd_args = parser.parse_args()
  61. if cmd_args.v:
  62. print(VERSION)
  63. exit(0)
  64. # Ok, now it's time to put the parameters parsed into the structure to be
  65. # used by the connection.
  66. params = CARequest()
  67. params.command = cmd_args.command
  68. params.http_host = cmd_args.host
  69. params.http_port = cmd_args.port
  70. params.timeout = cmd_args.timeout
  71. params.version = VERSION
  72. params.generate_body()
  73. params.generate_headers()
  74. # Load command processor
  75. # @todo - command specific processing will be added as part of
  76. # future work (either #5138 or #5139, whichever is implemented
  77. # first)
  78. # Read parameters from stdin (they're optional for some commands)
  79. for line in sys.stdin:
  80. params.params += line
  81. # Set the timeout timer. If the connection takes too long,
  82. # it will send a signal to us.
  83. signal.signal(signal.SIGALRM, timeout_handler)
  84. signal.alarm(params.timeout)
  85. # Ok, everything is ready. Let's send the command and get a response.
  86. try:
  87. resp = kea_connector.send_to_control_agent(params)
  88. except Exception as exc:
  89. print("Failed to run: " + str(exc))
  90. sys.exit(1)
  91. resp.print_response()
  92. sys.exit(0)
  93. if __name__ == "__main__":
  94. shell_body()