bind-cfgd.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.cc.group_subscribe("Boss", "ConfigManager")
  16. self.config = ConfigData()
  17. self.db_filename = "/tmp/parkinglot.db"
  18. self.running = False
  19. def notify_boss(self):
  20. self.cc.group_sendmsg({"running": "configmanager"}, "Boss")
  21. def add_zone(self, zone_name):
  22. self.config.add_zone(zone_name, "todo")
  23. self.write_config()
  24. print("sending update zone add")
  25. self.cc.group_sendmsg({"zone_added": zone_name }, "ParkingLot")
  26. def remove_zone(self, zone_name):
  27. self.config.remove_zone(zone_name)
  28. print("sending update zone del")
  29. self.cc.group_sendmsg({"zone_deleted": zone_name }, "ParkingLot")
  30. def read_config(self):
  31. print("Reading config")
  32. try:
  33. file = open(self.db_filename, 'rb');
  34. self.config = pickle.load(file)
  35. file.close()
  36. except IOError as ioe:
  37. print("No config file found, starting with empty config")
  38. except EOFError as eofe:
  39. print("Config file empty, starting with empty config")
  40. def write_config(self):
  41. print("Writing config")
  42. try:
  43. file = open(self.db_filename, 'wb');
  44. pickle.dump(self.config, file)
  45. file.close()
  46. except IOError as ioe:
  47. print("Unable to write config file; configuration not stored")
  48. def handle_msg(self, msg):
  49. """return answer message"""
  50. answer = {}
  51. if "command" in msg:
  52. cmd = msg["command"]
  53. try:
  54. if cmd[0] == "zone" and cmd[1] == "add":
  55. self.add_zone(cmd[2])
  56. answer["result"] = [ 0 ]
  57. elif cmd[0] == "zone" and cmd[1] == "remove":
  58. try:
  59. self.remove_zone(cmd[2])
  60. answer["result"] = [ 0 ]
  61. except KeyError:
  62. # zone wasn't there, should we make
  63. # a separate exception for that?
  64. answer["result"] = [ 1, "Unknown zone" ]
  65. elif cmd[0] == "zone" and cmd[1] == "list":
  66. answer["result"] = list(self.config.zones.keys())
  67. elif cmd == "shutdown":
  68. print("[bind-cfgd] Received shutdown command")
  69. self.running = False
  70. else:
  71. print("[bind-cfgd] unknown command: " + str(cmd))
  72. answer["result"] = [ 1, "Unknown command: " + str(cmd) ]
  73. except IndexError as ie:
  74. print("[bind-cfgd] missing argument")
  75. answer["result"] = [ 1, "Missing argument in command" ]
  76. else:
  77. print("[bind-cfgd] unknown message: " + str(msg))
  78. answer["result"] = [ 1, "Unknown module: " + str(msg) ]
  79. return answer
  80. def run(self):
  81. self.running = True
  82. while (self.running):
  83. msg, env = self.cc.group_recvmsg(False)
  84. if msg:
  85. print("[bind-cfgd] received message: ")
  86. print(msg)
  87. answer = self.handle_msg(msg);
  88. print("[bind-cfgd] sending answer: ")
  89. print(answer)
  90. self.cc.group_reply(env, answer)
  91. print("[bind-cfgd] answer sent")
  92. else:
  93. self.running = False
  94. cm = None
  95. def signal_handler(signal, frame):
  96. global cm
  97. if cm:
  98. cm.running = False
  99. if __name__ == "__main__":
  100. try:
  101. cm = ConfigManager()
  102. signal.signal(signal.SIGINT, signal_handler)
  103. signal.signal(signal.SIGTERM, signal_handler)
  104. cm.read_config()
  105. # do loading here if necessary
  106. cm.notify_boss()
  107. cm.run()
  108. cm.write_config()
  109. except ISC.CC.SessionError as se:
  110. print("[bind-cfgd] Error creating config manager, "
  111. "is the command channel daemon running?")
  112. except KeyboardInterrupt as kie:
  113. print("Got ctrl-c, save config and exit")
  114. cm.write_config()