b10-cfgmgr.py.in 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. import bind10_config
  19. from isc.cc import SessionError
  20. import isc.util.process
  21. import signal
  22. import os
  23. from optparse import OptionParser
  24. import glob
  25. import os.path
  26. isc.util.process.rename()
  27. # Import some paths from our configuration
  28. DATA_PATH = bind10_config.DATA_PATH
  29. PLUGIN_PATHS = bind10_config.PLUGIN_PATHS
  30. PREFIX = bind10_config.PREFIX
  31. DEFAULT_CONFIG_FILE = "b10-config.db"
  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="config_file",
  39. help="Configuration database filename " +
  40. "(default=" + DEFAULT_CONFIG_FILE + ")",
  41. default=DEFAULT_CONFIG_FILE)
  42. (options, args) = parser.parse_args(args)
  43. if args:
  44. parser.error("No non-option arguments allowed")
  45. return options
  46. def signal_handler(signal, frame):
  47. global cm
  48. if cm:
  49. cm.running = False
  50. def load_plugins(path, cm):
  51. """Load all python files in the given path and treat them as plugins."""
  52. # Find the python files
  53. plugins = glob.glob(path + os.sep + '*.py')
  54. # Search this directory first, but leave the others there for the imports
  55. # of the modules
  56. sys.path.insert(0, path)
  57. try:
  58. for plugin in plugins:
  59. # Generate the name of the plugin
  60. filename = os.path.basename(plugin)
  61. name = filename[:-3]
  62. # Load it
  63. module = __import__(name)
  64. # Ask it to provide the spec and checking function
  65. (spec, check_func) = module.load()
  66. # And insert it into the manager
  67. cm.set_virtual_module(spec, check_func)
  68. finally:
  69. # Restore the search path
  70. sys.path = sys.path[1:]
  71. def main():
  72. options = parse_options()
  73. global cm
  74. try:
  75. cm = ConfigManager(options.data_path, options.config_file)
  76. signal.signal(signal.SIGINT, signal_handler)
  77. signal.signal(signal.SIGTERM, signal_handler)
  78. cm.read_config()
  79. for ppath in PLUGIN_PATHS:
  80. load_plugins(ppath, cm)
  81. cm.notify_boss()
  82. cm.run()
  83. except SessionError as se:
  84. print("[b10-cfgmgr] Error creating config manager, "
  85. "is the command channel daemon running?")
  86. return 1
  87. except KeyboardInterrupt as kie:
  88. print("[b10-cfgmgr] Interrupted, exiting")
  89. except ConfigManagerDataReadError as cmdre:
  90. print("[b10-cfgmgr] " + str(cmdre))
  91. return 2
  92. return 0
  93. if __name__ == "__main__":
  94. sys.exit(main())