ud-mailage: allow dashes in hostnames.
[mirror/userdir-ldap.git] / ud-replicated
old mode 100644 (file)
new mode 100755 (executable)
index ac83a4d..8bef0a5
@@ -25,6 +25,7 @@
 from dsa_mq.connection import Connection
 from dsa_mq.config import Config
 
+import json
 import logging
 import logging.handlers
 import optparse
@@ -63,14 +64,31 @@ FORMAT='%(asctime)s ud-replicated: %(levelname)s %(message)s'
 SFORMAT='ud-replicated[%(process)s]: %(message)s'
 logging.basicConfig(format=FORMAT, level=lvl)
 LOG = logging.getLogger(__name__)
-syslog_handler = logging.handlers.SysLogHandler(address = '/dev/log')
+logsock = '/dev/log'
+if os.path.exists('/var/run/log'): # Kfreebsd randomly different
+    logsock = '/var/run/log'
+syslog_handler = logging.handlers.SysLogHandler(address = logsock)
 formatter = logging.Formatter(SFORMAT)
 syslog_handler.setFormatter(formatter)
 LOG.addHandler(syslog_handler)
 
+last_run = 0
+
 def do_replicate(message):
-    last_update = time.time()
-    LOG.debug("Got message at %s" % last_update)
+    global last_run
+    last_update = int(time.time())
+    timestamp   = last_update
+    try:
+        message = json.loads(message)
+    except ValueError:
+        pass
+
+    if isinstance(message, dict):
+        timestamp = message.get('timestamp', last_update)
+        message   = message.get('message', 'update required')
+    LOG.debug("Got message at %s: %s" % (last_update, message))
+    if last_run > timestamp, last_update):
+        return
 
     command = ['/usr/bin/ud-replicate']
     if options.dryrun:
@@ -84,9 +102,9 @@ def do_replicate(message):
             LOG.error('%s failed:', ' '.join(command))
         else:
             LOG.debug('%s finished with ret: 0' % ' '.join(command))
-       finally:
+        finally:
             os.environ['TERM'] = old_term
-    LOG.debug(message)
+    last_run = last_update
 
 def main():
     conn = Connection(conf=mq_conf)
@@ -107,5 +125,7 @@ def main():
         sys.exit(0)
 
 if __name__ == '__main__':
-    do_replicate('startup complete')
+    do_replicate(json.dumps(
+        {'timestamp': time.time(),
+        'message': 'startup complete'}))
     main()