b10-cfgmgr.py.in 3.7 KB

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