netcelery.py 1.31 KB
Newer Older
1
""" Celery module for libvirt RPC calls. """
Guba Sándor committed
2 3
from celery import Celery
from kombu import Queue, Exchange
4
from os import getenv
5 6 7 8 9 10 11 12 13 14 15 16 17 18

from argparse import ArgumentParser

parser = ArgumentParser()
parser.add_argument("-n", "--hostname", dest="hostname",
                    help="Define the full queue name with"
                    "with priority", metavar="hostname.queue.priority")
(args, unknwon_args) = parser.parse_known_args()
HOSTNAME = vars(args).pop("hostname")
if HOSTNAME is None:
    raise Exception("You must define hostname as -n <hostname> or "
                    "--hostname=<hostname>.\n"
                    "Hostname format must be hostname.module.priority.")

19 20
AMQP_URI = getenv('AMQP_URI')

21 22 23 24

def to_bool(value):
    return value.lower() in ("true", "yes", "y", "t")

25

26
lib_connection = None
27 28 29 30 31 32 33
native_ovs = False

if to_bool(getenv('LIBVIRT_KEEPALIVE', "False")):
    import libvirt
    lib_connection = libvirt.open(getenv('LIBVIRT_URI'))
if to_bool(getenv('NATIVE_OVS', "False")):
    native_ovs = True
34 35

celery = Celery('netcelery',
36 37
                broker=AMQP_URI,
                include=['netdriver'])
Guba Sándor committed
38 39

celery.conf.update(
40
    CELERY_RESULT_BACKEND='amqp',
Bach Dániel committed
41
    CELERY_TASK_RESULT_EXPIRES=300,
Guba Sándor committed
42
    CELERY_QUEUES=(
43 44
        Queue(HOSTNAME, Exchange(
            'netdriver', type='direct'), routing_key="netdriver"),
Guba Sándor committed
45 46
    )
)