ud-mailgate: remove exception for münchen.debian.net
[mirror/userdir-ldap.git] / ud-replicated
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2014 Stephen Gran <sgran@debian.org>
4 #
5 # Run ud-replicate on a trigger
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 # THE SOFTWARE.
24
25 from dsa_mq.connection import Connection
26 from dsa_mq.config import Config
27
28 import json
29 import logging
30 import logging.handlers
31 import optparse
32 import os
33 import platform
34 import stat
35 import subprocess
36 import sys
37 import time
38
39 parser = optparse.OptionParser()
40 parser.add_option("-D", "--dryrun",
41                   action="store_true", default=False,
42                   help="Dry run mode")
43
44 parser.add_option("-d", "--debug",
45                   action="store_true", default=False,
46                   help="Enable debug output")
47
48 (options, args) = parser.parse_args()
49 options.section = 'dsa-udreplicate'
50 options.config = '/etc/dsa/pubsub.conf'
51 config = Config(options)
52 mq_conf  = {
53     'rabbit_userid': config.username,
54     'rabbit_password': config.password,
55     'rabbit_virtual_host': config.vhost,
56     'rabbit_hosts': ['pubsub02.debian.org', 'pubsub01.debian.org'],
57     'use_ssl': False
58 }
59
60 lvl = logging.INFO
61 if config.debug:
62     lvl = logging.DEBUG
63
64 FORMAT='%(asctime)s ud-replicated: %(levelname)s %(message)s'
65 SFORMAT='ud-replicated[%(process)s]: %(message)s'
66 logging.basicConfig(format=FORMAT, level=lvl)
67 LOG = logging.getLogger(__name__)
68 logsock = '/dev/log'
69 if os.path.exists('/var/run/log') and stat.S_ISSOCK(os.stat('/var/run/log').st_mode): # Kfreebsd randomly different
70     logsock = '/var/run/log'
71 syslog_handler = logging.handlers.SysLogHandler(address = logsock)
72 formatter = logging.Formatter(SFORMAT)
73 syslog_handler.setFormatter(formatter)
74 LOG.addHandler(syslog_handler)
75
76 last_run = 0
77
78 def do_replicate(message):
79     global last_run
80     last_update = int(time.time())
81     timestamp   = last_update
82     try:
83         message = json.loads(message)
84     except ValueError:
85         pass
86
87     if isinstance(message, dict):
88         timestamp = message.get('timestamp', last_update)
89         message   = message.get('message', 'update required')
90     LOG.debug("Got message at %s: %s" % (last_update, message))
91     if last_run > timestamp:
92         return
93
94     command = ['/usr/bin/ud-replicate']
95     if options.dryrun:
96         LOG.debug("Would have run %s" % command)
97     else:
98         old_term = os.environ.get('TERM')
99         os.environ['TERM'] = 'dumb'
100         try:
101             subprocess.check_call(command)
102         except:
103             LOG.error('%s failed:', ' '.join(command))
104         else:
105             LOG.debug('%s finished with ret: 0' % ' '.join(command))
106         finally:
107             if old_term is not None:
108                 os.environ['TERM'] = old_term
109     last_run = last_update
110
111 def main():
112     conn = Connection(conf=mq_conf)
113     conn.declare_topic_consumer(config.topic,
114                                 callback=do_replicate,
115                                 queue_name=config.queue,
116                                 exchange_name=config.exchange,
117                                 ack_on_error=False)
118
119     try:
120         conn.consume()
121     except KeyboardInterrupt:
122         pass
123     except Exception as e:
124         LOG.error(e)
125     finally:
126         conn.close()
127         sys.exit(0)
128
129 if __name__ == '__main__':
130     do_replicate(json.dumps(
131         {'timestamp': time.time(),
132         'message': 'startup complete'}))
133     main()