3 # Copyright (c) 2014 Stephen Gran <sgran@debian.org>
5 # Run ud-replicate on a trigger
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:
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
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
25 from dsa_mq.connection import Connection
26 from dsa_mq.config import Config
29 import logging.handlers
37 parser = optparse.OptionParser()
38 parser.add_option("-D", "--dryrun",
39 action="store_true", default=False,
42 parser.add_option("-d", "--debug",
43 action="store_true", default=False,
44 help="Enable debug output")
46 (options, args) = parser.parse_args()
47 options.section = 'dsa-udreplicate'
48 options.config = '/etc/dsa/pubsub.conf'
49 config = Config(options)
51 'rabbit_userid': config.username,
52 'rabbit_password': config.password,
53 'rabbit_virtual_host': config.vhost,
54 'rabbit_hosts': ['pubsub02.debian.org', 'pubsub01.debian.org'],
62 FORMAT='%(asctime)s ud-replicated: %(levelname)s %(message)s'
63 SFORMAT='ud-replicated[%(process)s]: %(message)s'
64 logging.basicConfig(format=FORMAT, level=lvl)
65 LOG = logging.getLogger(__name__)
66 syslog_handler = logging.handlers.SysLogHandler(address = '/dev/log')
67 formatter = logging.Formatter(SFORMAT)
68 syslog_handler.setFormatter(formatter)
69 LOG.addHandler(syslog_handler)
71 def do_replicate(message):
72 last_update = time.time()
73 LOG.debug("Got message at %s" % last_update)
75 command = ['/usr/bin/ud-replicate']
77 LOG.debug("Would have run %s" % command)
79 old_term = os.environ.get('TERM')
80 os.environ['TERM'] = 'dumb'
82 subprocess.check_call(command)
84 LOG.error('%s failed:', ' '.join(command))
86 LOG.debug('%s finished with ret: 0' % ' '.join(command))
88 os.environ['TERM'] = old_term
92 conn = Connection(conf=mq_conf)
93 conn.declare_topic_consumer(config.topic,
94 callback=do_replicate,
95 queue_name=config.queue,
96 exchange_name=config.exchange,
101 except KeyboardInterrupt:
103 except Exception as e:
109 if __name__ == '__main__':
110 do_replicate('startup complete')