~cprov/+junk/adt-proxy-service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python

from __future__ import print_function

import sys

import argparse
import configparser

import kombu
from kombu.log import get_logger
from kombu.pools import producers
from kombu.utils.debug import setup_logging


logger = get_logger(__name__)


def main():
    setup_logging(loglevel='DEBUG', loggers=[''])

    parser = argparse.ArgumentParser(
        description='ADT proxy service ...')
    parser.add_argument('-c', '--conf', default='.adt-service.conf',
                        help='Configuration file path')
    parser.add_argument('-t', '--tag', default='i386.nova',
                        help='Target worker tag (<arch>.<platform>)')
    parser.add_argument('-r', '--requests', default=1, type=int,
                        help='Number of requests')

    args = parser.parse_args()
    routing_key = args.tag

    # Load configuration options.
    config = configparser.ConfigParser()
    config.read(args.conf)
    amqp_uris = config.get('amqp', 'uris').split()

    adt_exchange = kombu.Exchange("adt.exchange", type="topic")
    queue = kombu.Queue(
        'adt.requests.{}'.format(routing_key),
        adt_exchange, routing_key=routing_key)

    # Always declare exchange and destination queue, even if there are no
    # available works the request is preserved.
    declare = [adt_exchange, queue]

    # Bogus script based producer ...
    arch, platform = routing_key.split('.')
    payload = {
        'arch': arch,
        'platform': platform,
    }

    with kombu.Connection(amqp_uris) as conn:
        with producers[conn].acquire(block=True) as producer:

            def errback(exc, interval):
                logger.error('Error: %r', exc, exc_info=1)
                logger.info('Retry in %s seconds.', interval)

            publish = conn.ensure(
                producer, producer.publish,
                errback=errback, max_retries=3)

            for i in range(args.requests):
                payload['index'] = i
                publish(
                    payload,
                    exchange=adt_exchange,
                    routing_key=routing_key,
                    declare=declare)


if __name__ == '__main__':
    main()