Beispiel #1
0
    def slc_init(self):
        erlang_cookie = self.spawn_args.get('erlang_cookie', None)
        if not erlang_cookie:
            cookie_path = self.spawn_args.get('erlang_cookie_path', None)
            erlang_cookie = read_cookie(cookie_path)

        rabbitmq_node = self.spawn_args.get('rabbitmq_node',
                                            'rabbit@localhost')
        node_name = twotp.node.buildNodeName(rabbitmq_node)
        twotp_process = twotp.node.Process(node_name, erlang_cookie)
        self.rabbitmq = RabbitMQControlService(twotp_process, node_name)

        self.interval = float(
            self.spawn_args.get('interval_seconds', DEFAULT_INTERVAL_SECONDS))
        self.sensor_id = self.spawn_args.get('sensor_id', DEFAULT_SENSOR_ID)
        self.loop = LoopingCall(self._wrapped_do_poll)

        # a dict of sets of (subscriber,op) tuples
        self.watched_queues = {}
Beispiel #2
0
    def slc_init(self):
        erlang_cookie = self.spawn_args.get("erlang_cookie", None)
        if not erlang_cookie:
            cookie_path = self.spawn_args.get("erlang_cookie_path", None)
            erlang_cookie = read_cookie(cookie_path)

        rabbitmq_node = self.spawn_args.get("rabbitmq_node", "rabbit@localhost")
        node_name = twotp.node.buildNodeName(rabbitmq_node)
        twotp_process = twotp.node.Process(node_name, erlang_cookie)
        self.rabbitmq = RabbitMQControlService(twotp_process, node_name)

        self.interval = float(self.spawn_args.get("interval_seconds", DEFAULT_INTERVAL_SECONDS))
        self.sensor_id = self.spawn_args.get("sensor_id", DEFAULT_SENSOR_ID)
        self.loop = LoopingCall(self._wrapped_do_poll)

        # a dict of sets of (subscriber,op) tuples
        self.watched_queues = {}
Beispiel #3
0
class QueueStatService(ServiceProcess):
    """Queue stat subscription service

    Only works on a RabbitMQ server running *on localhost*
    """

    declare = ServiceProcess.service_declare(name='queuestat',
                                             version='0.1.0',
                                             dependencies=[])

    def slc_init(self):
        erlang_cookie = self.spawn_args.get('erlang_cookie', None)
        if not erlang_cookie:
            cookie_path = self.spawn_args.get('erlang_cookie_path', None)
            erlang_cookie = read_cookie(cookie_path)

        rabbitmq_node = self.spawn_args.get('rabbitmq_node',
                                            'rabbit@localhost')
        node_name = twotp.node.buildNodeName(rabbitmq_node)
        twotp_process = twotp.node.Process(node_name, erlang_cookie)
        self.rabbitmq = RabbitMQControlService(twotp_process, node_name)

        self.interval = float(
            self.spawn_args.get('interval_seconds', DEFAULT_INTERVAL_SECONDS))
        self.sensor_id = self.spawn_args.get('sensor_id', DEFAULT_SENSOR_ID)
        self.loop = LoopingCall(self._wrapped_do_poll)

        # a dict of sets of (subscriber,op) tuples
        self.watched_queues = {}

    def plc_terminate(self):
        log.debug('Shutdown triggered')
        self.rabbitmq.shutdown()

    def op_watch_queue(self, content, headers, msg):
        """Start watching a queue for updates. If queue is already being
        watched by this subscriber, this operation does nothing.
        """
        queue_name = content.get('queue_name')
        subscriber_name = content.get('subscriber_name')
        subscriber_op = content.get('subscriber_op')

        if not (queue_name and subscriber_name and subscriber_op):
            log.warn("Got invalid watch request: %s" % content)
            return

        sub_tuple = (subscriber_name, subscriber_op)

        queue_subs = self.watched_queues.get(queue_name, None)
        if queue_subs is None:
            queue_subs = set()
            self.watched_queues[queue_name] = queue_subs
        queue_subs.add(sub_tuple)

        if not self.loop.running:
            log.debug('starting LoopingCall, to poll queues')
            self.loop.start(self.interval)

    def op_unwatch_queue(self, content, headers, msg):
        """Stop watching a queue. If queue is not being watched by subscriber,
        this operation does nothing.
        """
        queue_name = content.get('queue_name')
        subscriber_name = content.get('subscriber_name')
        subscriber_op = content.get('subscriber_op')

        if not (queue_name and subscriber_name and subscriber_op):
            log.warn("Got invalid unwatch request: %s" % content)
            return

        sub_tuple = (subscriber_name, subscriber_op)

        queue_subs = self.watched_queues.get(queue_name, None)
        if queue_subs:
            queue_subs.discard(sub_tuple)
            if not queue_subs:
                del self.watched_queues[queue_name]

        if not self.watched_queues and self.loop.running:
            log.debug('No queues are being watched, disabling LoopingCall')
            self.loop.stop()

    @defer.inlineCallbacks
    def _wrapped_do_poll(self):
        try:
            yield self._do_poll()
        except Exception, e:
            log.error("Error in RabbitMQ poll: %s", str(e), exc_info=True)
Beispiel #4
0
class QueueStatService(ServiceProcess):
    """Queue stat subscription service

    Only works on a RabbitMQ server running *on localhost*
    """

    declare = ServiceProcess.service_declare(name="queuestat", version="0.1.0", dependencies=[])

    def slc_init(self):
        erlang_cookie = self.spawn_args.get("erlang_cookie", None)
        if not erlang_cookie:
            cookie_path = self.spawn_args.get("erlang_cookie_path", None)
            erlang_cookie = read_cookie(cookie_path)

        rabbitmq_node = self.spawn_args.get("rabbitmq_node", "rabbit@localhost")
        node_name = twotp.node.buildNodeName(rabbitmq_node)
        twotp_process = twotp.node.Process(node_name, erlang_cookie)
        self.rabbitmq = RabbitMQControlService(twotp_process, node_name)

        self.interval = float(self.spawn_args.get("interval_seconds", DEFAULT_INTERVAL_SECONDS))
        self.sensor_id = self.spawn_args.get("sensor_id", DEFAULT_SENSOR_ID)
        self.loop = LoopingCall(self._wrapped_do_poll)

        # a dict of sets of (subscriber,op) tuples
        self.watched_queues = {}

    def plc_terminate(self):
        log.debug("Shutdown triggered")
        self.rabbitmq.shutdown()

    def op_watch_queue(self, content, headers, msg):
        """Start watching a queue for updates. If queue is already being
        watched by this subscriber, this operation does nothing.
        """
        queue_name = content.get("queue_name")
        subscriber_name = content.get("subscriber_name")
        subscriber_op = content.get("subscriber_op")

        if not (queue_name and subscriber_name and subscriber_op):
            log.warn("Got invalid watch request: %s" % content)
            return

        sub_tuple = (subscriber_name, subscriber_op)

        queue_subs = self.watched_queues.get(queue_name, None)
        if queue_subs is None:
            queue_subs = set()
            self.watched_queues[queue_name] = queue_subs
        queue_subs.add(sub_tuple)

        if not self.loop.running:
            log.debug("starting LoopingCall, to poll queues")
            self.loop.start(self.interval)

    def op_unwatch_queue(self, content, headers, msg):
        """Stop watching a queue. If queue is not being watched by subscriber,
        this operation does nothing.
        """
        queue_name = content.get("queue_name")
        subscriber_name = content.get("subscriber_name")
        subscriber_op = content.get("subscriber_op")

        if not (queue_name and subscriber_name and subscriber_op):
            log.warn("Got invalid unwatch request: %s" % content)
            return

        sub_tuple = (subscriber_name, subscriber_op)

        queue_subs = self.watched_queues.get(queue_name, None)
        if queue_subs:
            queue_subs.discard(sub_tuple)
            if not queue_subs:
                del self.watched_queues[queue_name]

        if not self.watched_queues and self.loop.running:
            log.debug("No queues are being watched, disabling LoopingCall")
            self.loop.stop()

    @defer.inlineCallbacks
    def _wrapped_do_poll(self):
        try:
            yield self._do_poll()
        except Exception, e:
            log.error("Error in RabbitMQ poll: %s", str(e), exc_info=True)
Beispiel #5
0
class TestRabbitMQControlService(unittest.TestCase):
    """Test the RabbitMQControlService.

    Requires the defaults of a fresh RabbitMQ instance 
    to run all tests with success.

    To run these tests, type:

    $ trial tests/test_service.py

    From the root level dir of `txrabbitmq`.
    """ 

    def setUp(self):
        cookie = readCookie()
        node = os.environ.get("TXRABBITMQ_NODENAME") or "rabbit@localhost"
        nodeName = buildNodeName(node)
        self.process = Process(nodeName, cookie)
        self.service = RabbitMQControlService(self.process, nodeName)

    def tearDown(self):
        for epmd in self.process.oneShotEpmds.values():
            epmd.closeConnections()

    @inlineCallbacks
    def test_list_users(self):
        users = yield self.service.list_users()
        self.failUnless(users["count"] > 0)
        self.failUnless(users["command"] == "list_users")
        self.failUnless("guest" in users["result"])

    @inlineCallbacks
    def test_list_vhosts(self):
        vhosts = yield self.service.list_vhosts()
        self.failUnless(vhosts["count"] > 0)
        self.failUnless(vhosts["command"] == "list_vhosts")
        self.failUnless("/" in vhosts["result"])

    @inlineCallbacks
    def test_add_changepassword_delete_user(self):
        """New user test.
        Add a new user, change new user's password, delete new user.
        """
        add_user = yield self.service.add_user("test_temp_user", "test_temp_password")
        self.failUnless(add_user["command"] == "add_user")
        self.failUnless(add_user["username"] == "test_temp_user")
        self.failUnless(add_user["result"] == "ok")

        change_password = yield self.service.change_password("test_temp_user", "new_temp_password")
        self.failUnless(change_password["command"] == "change_password")
        self.failUnless(change_password["username"] == "test_temp_user")
        self.failUnless(change_password["result"] == "ok")

        delete_user = yield self.service.delete_user("test_temp_user")
        self.failUnless(delete_user["command"] == "delete_user")
        self.failUnless(delete_user["username"] == "test_temp_user")
        self.failUnless(delete_user["result"] == "ok")

    @inlineCallbacks
    def test_add_delete_vhost(self):
        """New vhost test.
        Add a new vhost, delete new vhost.
        """
        add_vhost = yield self.service.add_vhost("test_vhost_path")
        self.failUnless(add_vhost["command"] == "add_vhost")
        self.failUnless(add_vhost["vhostpath"] == "test_vhost_path")
        self.failUnless(add_vhost["result"] == "ok")
        delete_vhost = yield self.service.delete_vhost("test_vhost_path")
        self.failUnless(delete_vhost["command"] == "delete_vhost")
        self.failUnless(delete_vhost["vhostpath"] == "test_vhost_path")
        self.failUnless(delete_vhost["result"] == "ok")

    @inlineCallbacks
    def test_set_clear_permission(self):
        """Test setting and clearing permissions for a user.
    Create a test user, set permissions, test permissions, clear permission, delete test user.
    """
        _test_username = "******"
        add_user = yield self.service.add_user(_test_username, "test_temp_password")
        set_permissions = yield self.service.set_permissions(_test_username, ".*", ".*", ".*")
        self.failUnless(set_permissions["result"].text == "ok")

        list_vhost_permissions = yield self.service.list_vhost_permissions()
        permissions_result = list_vhost_permissions["result"]
        self.failUnless(permissions_result[_test_username] == ['/', '.*', '.*', '.*'])

        clear_permissions = yield self.service.clear_permissions(_test_username)
        self.failUnless(clear_permissions["result"].text == "ok")
        delete_user = yield self.service.delete_user(_test_username)

    @inlineCallbacks
    def test_list_vhost_permissions(self):
        """Test list all vhost permissions"""
        list_vhost_permissions = yield self.service.list_vhost_permissions()
        self.failUnless(list_vhost_permissions["command"] == "list_vhost_permissions")
        result = list_vhost_permissions["result"]
        self.failUnless(result['guest'] == ['/', '.*', '.*', '.*'])

    @inlineCallbacks
    def test_list_user_permissions(self):
        """Test list all users permissions"""
        list_user_permissions = yield self.service.list_user_permissions()
        self.failUnless(list_user_permissions["command"] == "list_user_permissions")
        result = list_user_permissions["result"]
        self.failUnless(result['/'] == ['guest', '.*', '.*', '.*'])
 
    @inlineCallbacks
    def test_list_queues(self):
        #Create dynamic queue here for better test case?
        list_queues = yield self.service.list_queues()
        self.failUnless(list_queues["vhostpath"] == "/")
        self.failUnless(list_queues["command"] == "list_queues")

    @inlineCallbacks
    def test_list_exchanges(self):
        broker_default_exchanges = ['amq.rabbitmq.log', 'amq.match', 'amq.headers', 'amq.topic', 'amq.direct', 'amq.fanout']
        list_exchanges = yield self.service.list_exchanges()
        self.failUnless(list_exchanges["vhostpath"] == "/")
        self.failUnless(list_exchanges["command"] == "list_exchanges")
        result = list_exchanges["result"]
        existing_default_exchanges = []
        for e in result:
            if e[0].startswith("amq."):
                existing_default_exchanges.append(e[0])
        self.failUnless(existing_default_exchanges == broker_default_exchanges)

    @inlineCallbacks
    def test_list_bindings(self):
        #Create dynamic binding here for better test case?
        list_bindings = yield self.service.list_bindings()
        self.failUnless(list_bindings["vhostpath"] == "/")
        self.failUnless(list_bindings["command"] == "list_bindings")

    @inlineCallbacks
    def test_list_connections(self):
        #Create dynamic connection here for better test case?
        list_connections = yield self.service.list_connections()
        self.failUnless(list_connections["command"] == "list_connections")
Beispiel #6
0
 def setUp(self):
     cookie = readCookie()
     node = os.environ.get("TXRABBITMQ_NODENAME") or "rabbit@localhost"
     nodeName = buildNodeName(node)
     self.process = Process(nodeName, cookie)
     self.service = RabbitMQControlService(self.process, nodeName)