bind-cfgd.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import ISC
  2. import pickle
  3. import signal
  4. class ConfigData:
  5. def __init__(self):
  6. self.zones = {}
  7. def add_zone(self, zone_name, zone_file):
  8. self.zones[zone_name] = zone_file
  9. def remove_zone(self, zone_name):
  10. del self.zones[zone_name]
  11. class ConfigManager:
  12. def __init__(self):
  13. self.cc = ISC.CC.Session()
  14. self.cc.group_subscribe("ConfigManager")
  15. self.config = ConfigData()
  16. self.running = False
  17. def notify_boss(self):
  18. self.cc.group_sendmsg({"running": "configmanager"}, "Boss")
  19. def add_zone(self, zone_name):
  20. self.config.add_zone(zone_name, "todo")
  21. print("sending update zone add")
  22. self.cc.group_sendmsg({"zone_added": zone_name }, "ParkingLot")
  23. def remove_zone(self, zone_name):
  24. self.config.remove_zone(zone_name)
  25. print("sending update zone del")
  26. self.cc.group_sendmsg({"zone_deleted": zone_name }, "ParkingLot")
  27. def read_config(self, filename):
  28. print("Reading config")
  29. try:
  30. file = open(filename, 'rb');
  31. self.config = pickle.load(file)
  32. except IOError as ioe:
  33. print("No config file found, starting with empty config")
  34. except EOFError as eofe:
  35. print("Config file empty, starting with empty config")
  36. def write_config(self, filename):
  37. print("Writing config")
  38. file = open(filename, 'wb');
  39. pickle.dump(self.config, file)
  40. def handle_msg(self, msg):
  41. """return answer message"""
  42. answer = {}
  43. try:
  44. cmd = msg["command"]
  45. if cmd:
  46. if cmd[0] == "zone" and cmd[1] == "add":
  47. self.add_zone(cmd[2])
  48. answer["result"] = [ 0 ]
  49. elif cmd[0] == "zone" and cmd[1] == "remove":
  50. self.remove_zone(cmd[2])
  51. answer["result"] = [ 0 ]
  52. elif cmd[0] == "zone" and cmd[1] == "list":
  53. answer["result"] = list(self.config.zones.keys())
  54. else:
  55. print("unknown command: " + str(cmd))
  56. answer["result"] = [ 1, "Unknown command: " + str(cmd) ]
  57. except KeyError as ke:
  58. print("unknown module: " + str(msg))
  59. answer["result"] = [ 1, "Unknown module: " + str(msg) ]
  60. except IndexError as ie:
  61. print("missing argument")
  62. answer["result"] = [ 1, "Missing argument in command" ]
  63. return answer
  64. def run(self):
  65. self.running = True
  66. while (self.running):
  67. msg, env = self.cc.group_recvmsg(False)
  68. if msg:
  69. print("received message: ")
  70. print(msg)
  71. answer = self.handle_msg(msg);
  72. print("sending answer: ")
  73. print(answer)
  74. self.cc.group_reply(env, answer)
  75. print("answer sent")
  76. else:
  77. self.running = False
  78. cm = None
  79. def signal_handler(signal, frame):
  80. global cm
  81. if cm:
  82. cm.running = False
  83. if __name__ == "__main__":
  84. print("Hello, BIND10 world!")
  85. db_file = "/tmp/parkinglot.db"
  86. try:
  87. cm = ConfigManager()
  88. signal.signal(signal.SIGINT, signal_handler)
  89. signal.signal(signal.SIGTERM, signal_handler)
  90. cm.read_config(db_file)
  91. # do loading here if necessary
  92. cm.notify_boss()
  93. cm.run()
  94. cm.write_config(db_file)
  95. except ISC.CC.SessionError as se:
  96. print("Error creating config manager, "
  97. "is the command channel daemon running?")
  98. except KeyboardInterrupt as kie:
  99. print("Got ctrl-c, save config and exit")
  100. cm.write_config(db_file)