示例#1
0
def drain_queue(queue):
    """Strip a (declared) queue from all outstanding messages"""
    if not queue:
        return

    if not queue in queues.QUEUES:
        print "Queue %s not configured" % queue
        return

    print "Queue to be drained: %s" % queue

    if not get_user_confirmation():
        return

    client = AMQPClient()
    client.connect()

    tag = client.basic_consume(queue=queue, callback=callbacks.dummy_proc)

    print "Queue draining about to start, hit Ctrl+c when done"
    time.sleep(2)
    print "Queue draining starting"

    num_processed = 0
    while True:
        client.basic_wait()
        num_processed += 1
        sys.stderr.write("Ignored %d messages\r" % num_processed)

    client.basic_cancel(tag)
    client.close()
示例#2
0
def drain_queue(queue):
    """Strip a (declared) queue from all outstanding messages"""
    if not queue:
        return

    if not queue in queues.QUEUES:
        print "Queue %s not configured" % queue
        return

    print "Queue to be drained: %s" % queue

    if not get_user_confirmation():
        return

    client = AMQPClient()
    client.connect()

    tag = client.basic_consume(queue=queue, callback=callbacks.dummy_proc)

    print "Queue draining about to start, hit Ctrl+c when done"
    time.sleep(2)
    print "Queue draining starting"

    num_processed = 0
    while True:
        client.basic_wait()
        num_processed += 1
        sys.stderr.write("Ignored %d messages\r" % num_processed)

    client.basic_cancel(tag)
    client.close()
示例#3
0
def check_dispatcher_status(pid_file):
    """Check the status of a running snf-dispatcher process.

    Check the status of a running snf-dispatcher process, the PID of which is
    contained in the 'pid_file'. This function will send a 'status-check'
    message to the running snf-dispatcher, wait for dispatcher's response and
    pretty-print the results.

    """
    dispatcher_pid = pidlockfile.read_pid_from_pidfile(pid_file)
    if dispatcher_pid is None:
        sys.stdout.write("snf-dispatcher with PID file '%s' is not running."
                         " PID file does not exist\n" % pid_file)
        sys.exit(1)
    sys.stdout.write("snf-dispatcher (PID: %s): running\n" % dispatcher_pid)

    hostname = get_hostname()
    local_queue = "snf:check_tool:%s:%s" % (hostname, os.getpid())
    dispatcher_queue = queues.get_dispatcher_request_queue(
        hostname, dispatcher_pid)

    log_amqp.setLevel(logging.WARNING)
    try:
        client = AMQPClient(logger=log_amqp)
        client.connect()
        client.queue_declare(queue=local_queue, mirrored=False, exclusive=True)
        client.basic_consume(queue=local_queue,
                             callback=lambda x, y: 0,
                             no_ack=True)
        msg = json.dumps({"action": "status-check", "reply_to": local_queue})
        client.basic_publish("", dispatcher_queue, msg)
    except:
        sys.stdout.write("Error while connecting with AMQP\nError:\n")
        traceback.print_exc()
        sys.exit(1)

    sys.stdout.write("AMQP -> snf-dispatcher: ")
    msg = client.basic_wait(timeout=CHECK_TOOL_ACK_TIMEOUT)
    if msg is None:
        sys.stdout.write("fail\n")
        sys.stdout.write("ERROR: No reply from snf-dipatcher after '%s'"
                         " seconds.\n" % CHECK_TOOL_ACK_TIMEOUT)
        sys.exit(1)
    else:
        try:
            body = json.loads(msg["body"])
            assert (body["action"] == "status-check"), "Invalid action"
            assert (body["status"] == "started"), "Invalid status"
            sys.stdout.write("ok\n")
        except Exception as e:
            sys.stdout.write("Received invalid msg from snf-dispatcher:"
                             " msg: %s error: %s\n" % (msg, e))
            sys.exit(1)

    msg = client.basic_wait(timeout=CHECK_TOOL_REPORT_TIMEOUT)
    if msg is None:
        sys.stdout.write("fail\n")
        sys.stdout.write("ERROR: No status repot after '%s' seconds.\n" %
                         CHECK_TOOL_REPORT_TIMEOUT)
        sys.exit(1)

    sys.stdout.write("Backends:\n")
    status = json.loads(msg["body"])["status"]
    for backend, bstatus in sorted(status.items()):
        sys.stdout.write(" * %s: \n" % backend)
        sys.stdout.write("   snf-dispatcher -> ganeti: %s\n" % bstatus["RAPI"])
        sys.stdout.write("   snf-ganeti-eventd -> AMQP: %s\n" %
                         bstatus["eventd"])
    sys.exit(0)
示例#4
0
def main():
    parser = OptionParser()
    parser.add_option('-v', '--verbose', action='store_true', default=False,
                      dest='verbose', help='Enable verbose logging')
    parser.add_option('--host', default=BROKER_HOST, dest='host',
                      help='RabbitMQ host (default: %s)' % BROKER_HOST)
    parser.add_option('--port', default=BROKER_PORT, dest='port',
                      help='RabbitMQ port (default: %s)' % BROKER_PORT, type='int')
    parser.add_option('--user', default=BROKER_USER, dest='user',
                      help='RabbitMQ user (default: %s)' % BROKER_USER)
    parser.add_option('--password', default=BROKER_PASSWORD, dest='password',
                      help='RabbitMQ password (default: %s)' % BROKER_PASSWORD)
    parser.add_option('--vhost', default=BROKER_VHOST, dest='vhost',
                      help='RabbitMQ vhost (default: %s)' % BROKER_VHOST)
    parser.add_option('--queue', default=CONSUMER_QUEUE, dest='queue',
                      help='RabbitMQ queue (default: %s)' % CONSUMER_QUEUE)
    parser.add_option('--exchange', default=CONSUMER_EXCHANGE, dest='exchange',
                      help='RabbitMQ exchange (default: %s)' % CONSUMER_EXCHANGE)
    parser.add_option('--key', default=CONSUMER_KEY, dest='key',
                      help='RabbitMQ key (default: %s)' % CONSUMER_KEY)
    parser.add_option('--callback', default=None, dest='callback',
                      help='Callback function to consume messages')
    parser.add_option('--test', action='store_true', default=False,
                      dest='test', help='Produce a dummy message for testing')
    opts, args = parser.parse_args()

    DEBUG = False
    if opts.verbose:
        DEBUG = True
    logging.basicConfig(
        format='%(asctime)s [%(levelname)s] %(name)s %(message)s',
        datefmt='%Y-%m-%d %H:%M:%S',
        level=logging.DEBUG if DEBUG else logging.INFO)
    logger = logging.getLogger('dispatcher')

    host =  'amqp://%s:%s@%s:%s' % (opts.user, opts.password, opts.host, opts.port)
    queue = opts.queue
    key = opts.key
    exchange = opts.exchange
    
    client = AMQPClient(hosts=[host])
    client.connect()

    if opts.test:
        client.exchange_declare(exchange=exchange,
                                type='topic')
        client.basic_publish(exchange=exchange,
                             routing_key=key,
                             body= json.dumps({"test": "0123456789"}))
        client.close()
        sys.exit()

    callback = None
    if opts.callback:
        cb = opts.callback.rsplit('.', 1)
        if len(cb) == 2:
            __import__(cb[0])
            cb_module = sys.modules[cb[0]]
            callback = getattr(cb_module, cb[1])

    def handle_message(client, msg):
        logger.debug('%s', msg)
        if callback:
            callback(msg)
        client.basic_ack(msg)

    client.queue_declare(queue=queue)
    client.queue_bind(queue=queue,
                      exchange=exchange,
                      routing_key=key)

    client.basic_consume(queue=queue, callback=handle_message)

    try:
        while True:
            client.basic_wait()
    except KeyboardInterrupt:
        pass
    finally:
        client.close()
示例#5
0
def main():
    parser = OptionParser()
    parser.add_option('-v',
                      '--verbose',
                      action='store_true',
                      default=False,
                      dest='verbose',
                      help='Enable verbose logging')
    parser.add_option('--host',
                      default=BROKER_HOST,
                      dest='host',
                      help='RabbitMQ host (default: %s)' % BROKER_HOST)
    parser.add_option('--port',
                      default=BROKER_PORT,
                      dest='port',
                      help='RabbitMQ port (default: %s)' % BROKER_PORT,
                      type='int')
    parser.add_option('--user',
                      default=BROKER_USER,
                      dest='user',
                      help='RabbitMQ user (default: %s)' % BROKER_USER)
    parser.add_option('--password',
                      default=BROKER_PASSWORD,
                      dest='password',
                      help='RabbitMQ password (default: %s)' % BROKER_PASSWORD)
    parser.add_option('--vhost',
                      default=BROKER_VHOST,
                      dest='vhost',
                      help='RabbitMQ vhost (default: %s)' % BROKER_VHOST)
    parser.add_option('--queue',
                      default=CONSUMER_QUEUE,
                      dest='queue',
                      help='RabbitMQ queue (default: %s)' % CONSUMER_QUEUE)
    parser.add_option('--exchange',
                      default=CONSUMER_EXCHANGE,
                      dest='exchange',
                      help='RabbitMQ exchange (default: %s)' %
                      CONSUMER_EXCHANGE)
    parser.add_option('--key',
                      default=CONSUMER_KEY,
                      dest='key',
                      help='RabbitMQ key (default: %s)' % CONSUMER_KEY)
    parser.add_option('--callback',
                      default=None,
                      dest='callback',
                      help='Callback function to consume messages')
    parser.add_option('--test',
                      action='store_true',
                      default=False,
                      dest='test',
                      help='Produce a dummy message for testing')
    opts, args = parser.parse_args()

    DEBUG = False
    if opts.verbose:
        DEBUG = True
    logging.basicConfig(
        format='%(asctime)s [%(levelname)s] %(name)s %(message)s',
        datefmt='%Y-%m-%d %H:%M:%S',
        level=logging.DEBUG if DEBUG else logging.INFO)
    logger = logging.getLogger('dispatcher')

    host = 'amqp://%s:%s@%s:%s' % (opts.user, opts.password, opts.host,
                                   opts.port)
    queue = opts.queue
    key = opts.key
    exchange = opts.exchange

    client = AMQPClient(hosts=[host])
    client.connect()

    if opts.test:
        client.exchange_declare(exchange=exchange, type='topic')
        client.basic_publish(exchange=exchange,
                             routing_key=key,
                             body=json.dumps({"test": "0123456789"}))
        client.close()
        sys.exit()

    callback = None
    if opts.callback:
        cb = opts.callback.rsplit('.', 1)
        if len(cb) == 2:
            __import__(cb[0])
            cb_module = sys.modules[cb[0]]
            callback = getattr(cb_module, cb[1])

    def handle_message(client, msg):
        logger.debug('%s', msg)
        if callback:
            callback(msg)
        client.basic_ack(msg)

    client.queue_declare(queue=queue)
    client.queue_bind(queue=queue, exchange=exchange, routing_key=key)

    client.basic_consume(queue=queue, callback=handle_message)

    try:
        while True:
            client.basic_wait()
    except KeyboardInterrupt:
        pass
    finally:
        client.close()
示例#6
0
def check_dispatcher_status(pid_file):
    """Check the status of a running snf-dispatcher process.

    Check the status of a running snf-dispatcher process, the PID of which is
    contained in the 'pid_file'. This function will send a 'status-check'
    message to the running snf-dispatcher, wait for dispatcher's response and
    pretty-print the results.

    """
    dispatcher_pid = pidlockfile.read_pid_from_pidfile(pid_file)
    if dispatcher_pid is None:
        sys.stdout.write("snf-dispatcher with PID file '%s' is not running."
                         " PID file does not exist\n" % pid_file)
        sys.exit(1)
    sys.stdout.write("snf-dispatcher (PID: %s): running\n" % dispatcher_pid)

    hostname = get_hostname()
    local_queue = "snf:check_tool:%s:%s" % (hostname, os.getpid())
    dispatcher_queue = queues.get_dispatcher_request_queue(hostname,
                                                           dispatcher_pid)

    log_amqp.setLevel(logging.WARNING)
    try:
        client = AMQPClient(logger=log_amqp)
        client.connect()
        client.queue_declare(queue=local_queue, mirrored=False, exclusive=True)
        client.basic_consume(queue=local_queue, callback=lambda x, y: 0,
                             no_ack=True)
        msg = json.dumps({"action": "status-check", "reply_to": local_queue})
        client.basic_publish("", dispatcher_queue, msg)
    except:
        sys.stdout.write("Error while connecting with AMQP\nError:\n")
        traceback.print_exc()
        sys.exit(1)

    sys.stdout.write("AMQP -> snf-dispatcher: ")
    msg = client.basic_wait(timeout=CHECK_TOOL_ACK_TIMEOUT)
    if msg is None:
        sys.stdout.write("fail\n")
        sys.stdout.write("ERROR: No reply from snf-dipatcher after '%s'"
                         " seconds.\n" % CHECK_TOOL_ACK_TIMEOUT)
        sys.exit(1)
    else:
        try:
            body = json.loads(msg["body"])
            assert(body["action"] == "status-check"), "Invalid action"
            assert(body["status"] == "started"), "Invalid status"
            sys.stdout.write("ok\n")
        except Exception as e:
            sys.stdout.write("Received invalid msg from snf-dispatcher:"
                             " msg: %s error: %s\n" % (msg, e))
            sys.exit(1)

    msg = client.basic_wait(timeout=CHECK_TOOL_REPORT_TIMEOUT)
    if msg is None:
        sys.stdout.write("fail\n")
        sys.stdout.write("ERROR: No status repot after '%s' seconds.\n"
                         % CHECK_TOOL_REPORT_TIMEOUT)
        sys.exit(1)

    sys.stdout.write("Backends:\n")
    status = json.loads(msg["body"])["status"]
    for backend, bstatus in sorted(status.items()):
        sys.stdout.write(" * %s: \n" % backend)
        sys.stdout.write("   snf-dispatcher -> ganeti: %s\n" %
                         bstatus["RAPI"])
        sys.stdout.write("   snf-ganeti-eventd -> AMQP: %s\n" %
                         bstatus["eventd"])
    sys.exit(0)