b10-cfgmgr.py.in 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!@PYTHON@
  2. # Copyright (C) 2010 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. import sys; sys.path.append ('@@PYTHONPATH@@')
  17. from isc.config.cfgmgr import ConfigManager, ConfigManagerDataReadError
  18. from isc.cc import SessionError
  19. import isc.util.process
  20. import signal
  21. import os
  22. from optparse import OptionParser
  23. isc.util.process.rename()
  24. # If B10_FROM_SOURCE is set in the environment, we use data files
  25. # from a directory relative to that, otherwise we use the ones
  26. # installed on the system
  27. if "B10_FROM_SOURCE" in os.environ:
  28. DATA_PATH = os.environ["B10_FROM_SOURCE"]
  29. else:
  30. PREFIX = "@prefix@"
  31. DATA_PATH = "@localstatedir@/@PACKAGE@".replace("${prefix}", PREFIX)
  32. cm = None
  33. def parse_options(args=sys.argv[1:], Parser=OptionParser):
  34. parser = Parser()
  35. parser.add_option("-p", "--data-path", dest="data_path",
  36. help="Directory to search for configuration files " +
  37. "(default="+DATA_PATH+")", default=DATA_PATH)
  38. parser.add_option("-c", "--config-filename", dest="file_name",
  39. help="Configuration database filename " +
  40. "(default=b10-config.db)", default="b10-config.db")
  41. (options, args) = parser.parse_args(args)
  42. if args:
  43. parser.error("No non-option arguments allowed")
  44. return options
  45. def signal_handler(signal, frame):
  46. global cm
  47. if cm:
  48. cm.running = False
  49. def main():
  50. options = parse_options()
  51. global cm
  52. try:
  53. cm = ConfigManager(options.data_path, options.file_name)
  54. signal.signal(signal.SIGINT, signal_handler)
  55. signal.signal(signal.SIGTERM, signal_handler)
  56. cm.read_config()
  57. cm.notify_boss()
  58. cm.run()
  59. except SessionError as se:
  60. print("[b10-cfgmgr] Error creating config manager, "
  61. "is the command channel daemon running?")
  62. return 1
  63. except KeyboardInterrupt as kie:
  64. print("[b10-cfgmgr] Interrupted, exiting")
  65. except ConfigManagerDataReadError as cmdre:
  66. print("[b10-cfgmgr] " + str(cmdre))
  67. return 2
  68. return 0
  69. if __name__ == "__main__":
  70. sys.exit(main())