~cprov/+junk/adt-proxy-service

« back to all changes in this revision

Viewing changes to adt-proxy-service.py

  • Committer: Celso Providelo
  • Date: 2015-03-02 22:47:39 UTC
  • Revision ID: celso.providelo@canonical.com-20150302224739-7ccrbgpwdsmnw7q9
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
from __future__ import print_function
 
4
 
 
5
import sys
 
6
 
 
7
import argparse
 
8
import configparser
 
9
 
 
10
import kombu
 
11
from kombu.log import get_logger
 
12
from kombu.pools import producers
 
13
from kombu.utils.debug import setup_logging
 
14
 
 
15
 
 
16
logger = get_logger(__name__)
 
17
 
 
18
 
 
19
def main():
 
20
    setup_logging(loglevel='DEBUG', loggers=[''])
 
21
 
 
22
    parser = argparse.ArgumentParser(
 
23
        description='ADT proxy service ...')
 
24
    parser.add_argument('-c', '--conf', default='.adt-service.conf',
 
25
                        help='Configuration file path')
 
26
    parser.add_argument('-t', '--tag', default='i386.nova',
 
27
                        help='Target worker tag (<arch>.<platform>)')
 
28
    parser.add_argument('-r', '--requests', default=1, type=int,
 
29
                        help='Number of requests')
 
30
 
 
31
    args = parser.parse_args()
 
32
    routing_key = args.tag
 
33
 
 
34
    # Load configuration options.
 
35
    config = configparser.ConfigParser()
 
36
    config.read(args.conf)
 
37
    amqp_uris = config.get('amqp', 'uris').split()
 
38
 
 
39
    adt_exchange = kombu.Exchange("adt.exchange", type="topic")
 
40
    queue = kombu.Queue(
 
41
        'adt.requests.{}'.format(routing_key),
 
42
        adt_exchange, routing_key=routing_key)
 
43
 
 
44
    # Always declare exchange and destination queue, even if there are no
 
45
    # available works the request is preserved.
 
46
    declare = [adt_exchange, queue]
 
47
 
 
48
    # Bogus script based producer ...
 
49
    arch, platform = routing_key.split('.')
 
50
    payload = {
 
51
        'arch': arch,
 
52
        'platform': platform,
 
53
    }
 
54
 
 
55
    with kombu.Connection(amqp_uris) as conn:
 
56
        with producers[conn].acquire(block=True) as producer:
 
57
 
 
58
            def errback(exc, interval):
 
59
                logger.error('Error: %r', exc, exc_info=1)
 
60
                logger.info('Retry in %s seconds.', interval)
 
61
 
 
62
            publish = conn.ensure(
 
63
                producer, producer.publish,
 
64
                errback=errback, max_retries=3)
 
65
 
 
66
            for i in range(args.requests):
 
67
                payload['index'] = i
 
68
                publish(
 
69
                    payload,
 
70
                    exchange=adt_exchange,
 
71
                    routing_key=routing_key,
 
72
                    declare=declare)
 
73
 
 
74
 
 
75
if __name__ == '__main__':
 
76
    main()