b10-cfgmgr.py.in 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. import glob
  24. import os.path
  25. isc.util.process.rename()
  26. # If B10_FROM_SOURCE is set in the environment, we use data files
  27. # from a directory relative to the value of that variable, or, if defined,
  28. # relative to the value of B10_FROM_SOURCE_LOCALSTATEDIR. Otherwise
  29. # we use the ones installed on the system.
  30. # B10_FROM_SOURCE_LOCALSTATEDIR is specifically intended to be used for
  31. # tests where we want to use variuos types of configuration within the test
  32. # environment. (We may want to make it even more generic so that the path is
  33. # passed from the boss process)
  34. if "B10_FROM_SOURCE" in os.environ:
  35. if "B10_FROM_SOURCE_LOCALSTATEDIR" in os.environ:
  36. DATA_PATH = os.environ["B10_FROM_SOURCE_LOCALSTATEDIR"]
  37. else:
  38. DATA_PATH = os.environ["B10_FROM_SOURCE"]
  39. PLUGIN_PATH = [DATA_PATH + '/src/bin/cfgmgr/plugins']
  40. else:
  41. PREFIX = "@prefix@"
  42. DATA_PATH = "@localstatedir@/@PACKAGE@".replace("${prefix}", PREFIX)
  43. PLUGIN_PATHS = ["@prefix@/share/@PACKAGE@/config_plugins"]
  44. DEFAULT_CONFIG_FILE = "b10-config.db"
  45. cm = None
  46. def parse_options(args=sys.argv[1:], Parser=OptionParser):
  47. parser = Parser()
  48. parser.add_option("-p", "--data-path", dest="data_path",
  49. help="Directory to search for configuration files " +
  50. "(default=" + DATA_PATH + ")", default=DATA_PATH)
  51. parser.add_option("-c", "--config-filename", dest="config_file",
  52. help="Configuration database filename " +
  53. "(default=" + DEFAULT_CONFIG_FILE + ")",
  54. default=DEFAULT_CONFIG_FILE)
  55. (options, args) = parser.parse_args(args)
  56. if args:
  57. parser.error("No non-option arguments allowed")
  58. return options
  59. def signal_handler(signal, frame):
  60. global cm
  61. if cm:
  62. cm.running = False
  63. def load_plugins(path, cm):
  64. """Load all python files in the given path and treat them as plugins."""
  65. # Find the python files
  66. plugins = glob.glob(path + os.sep + '*.py')
  67. # Search this directory first, but leave the others there for the imports
  68. # of the modules
  69. sys.path.insert(0, path)
  70. try:
  71. for plugin in plugins:
  72. # Generate the name of the plugin
  73. filename = os.path.basename(plugin)
  74. name = filename[:-3]
  75. # Load it
  76. module = __import__(name)
  77. # Ask it to provide the spec and checking function
  78. (spec, check_func) = module.load()
  79. # And insert it into the manager
  80. cm.set_virtual_module(spec, check_func)
  81. finally:
  82. # Restore the search path
  83. sys.path = sys.path[1:]
  84. def main():
  85. options = parse_options()
  86. global cm
  87. try:
  88. cm = ConfigManager(options.data_path, options.config_file)
  89. signal.signal(signal.SIGINT, signal_handler)
  90. signal.signal(signal.SIGTERM, signal_handler)
  91. cm.read_config()
  92. for ppath in PLUGIN_PATHS:
  93. load_plugins(ppath, cm)
  94. cm.notify_boss()
  95. cm.run()
  96. except SessionError as se:
  97. print("[b10-cfgmgr] Error creating config manager, "
  98. "is the command channel daemon running?")
  99. return 1
  100. except KeyboardInterrupt as kie:
  101. print("[b10-cfgmgr] Interrupted, exiting")
  102. except ConfigManagerDataReadError as cmdre:
  103. print("[b10-cfgmgr] " + str(cmdre))
  104. return 2
  105. return 0
  106. if __name__ == "__main__":
  107. sys.exit(main())