shovel.py 2.85 KB
Newer Older
1 2
import argparse
import subprocess
3
import requests
4 5
from requests.exceptions import HTTPError
import sys
6 7 8 9 10

parser=argparse.ArgumentParser(description='Shovels between RabbitMQ Clusters')
parser.add_argument('--src_host',action='store',dest='src_host')
parser.add_argument('--dest_host',action='store',dest='dest_host',default='127.0.0.1')
parser.add_argument('--src_user',action='store',dest='src_user')
11
parser.add_argument('--src_user_pass',action='store',dest='src_user_pass')
12
parser.add_argument('--dest_user',action='store',dest='dest_user')
13
parser.add_argument('--dest_user_pass',action='store',dest='dest_user_pass')
14 15 16

args=parser.parse_args()

17 18
src_uri='amqp://{}:{}@{}'.format(args.src_user,args.src_user_pass,args.src_host)
dest_uri='amqp://{}:{}@{}'.format(args.dest_user,args.dest_user_pass,args.dest_host)
19 20 21
port=15672

def list_vhosts():
22 23 24 25 26 27 28 29
    url='http://{}:{}/api/vhosts'.format(args.src_host,port)
    try:
        response=requests.get(url,auth=(args.src_user,args.src_user_pass))
        response.raise_for_status()
        vhosts=[v['name'] for v in response.json() if v['name'].startswith('/')]
    except Exception as ex:
        print "Failed to get vhosts: {}".format(ex)
        sys.exit(1)
30 31 32 33
    return vhosts

def list_queues():
    for vhost in list_vhosts():
34 35 36 37 38 39 40 41
        url='http://{}:{}/api/queues/{}'.format(args.src_host,port,vhost)
        try:
            response=requests.get(url,auth=(args.src_user,args.src_user_pass))
            response.raise_for_status()
            queues=[q['name'] for q in response.json()]
        except Exception as ex:
            print "Failed to get queues: {}".format(ex)
            sys.exit(1)
42
        return queues
43

44
def create_shovel(shovel,arg):
45 46 47 48 49 50
    cmd="/usr/sbin/rabbitmqctl set_parameter shovel {} '{}'".format(shovel,arg)
    try:
        subprocess.check_output(
                              cmd,stderr=subprocess.STDOUT,shell=True)
    except subprocess.CalledProcessError as ex:
        return ex.output
51 52 53

if __name__=='__main__':

54
    """
55
    command line arguments are expected to be in following format
56 57 58
    python shovel.py --src_host <src_host_IP> --src_user <src_rabbitmq_user> --src_user_pass <user_pass>  \
    --dest_host <dest_host_IP> --dest_user <dest_rabbitmq_user> --dest_user_pass <user_pass>
    """
khan committed
59
    output={}    
60 61 62 63 64 65
    for queue in list_queues():
        """ 
        Ignore queues celeryev and *.pidbox to shovel
        """
        q=queue.split('.')
        if (q[0]!='celeryev' and q[-1]!='pidbox'):
66
            args='{{"src-uri": "{}", "src-queue": "{}","dest-uri": "{}","dest-queue": "{}"}}'.format(src_uri,queue,dest_uri,queue)
khan committed
67
            print "Running shovel for queue:{}".format(queue)
68 69
            shovel_output=create_shovel(queue,args)
            if shovel_output is not None:
khan committed
70 71 72 73
               content=unicode(shovel_output,"utf-8")
               output[queue]=content
    for k,v in output.items():
          print k,v