Ejemplo n.º 1
0
    def message_handler(self):
        store = P2pMessageStore()
        self.handler_status = 'idle'

        self._logger.debug('Starting message handler')

        while self.running:
            if not self.handler_locked:
                try:
                    if self.message_to_ack:
                        for queue, message in store.get_unhandled(
                                self.endpoint):
                            sid = self.message_to_ack.meta['server_id']
                            if message.name == self.message_to_ack.name and \
                                            message.body.get('server_id', sid) == sid:
                                self._logger.debug(
                                    'Going to handle_one_message. Thread: %s',
                                    threading.currentThread().getName())
                                try:
                                    self._handle_one_message(
                                        message, queue, store)
                                except:
                                    self.subhandler_exc_info = sys.exc_info()
                                    self._logger.debug(
                                        'Caught exception from _handle_one_message: thread: %s',
                                        threading.currentThread())

                                self._logger.debug(
                                    'Completed handle_one_message. Thread: %s',
                                    threading.currentThread().getName())
                                self.message_to_ack = None
                                self.result_msg = message
                                self.ack_event.set()
                                if self.special_case == 'HostInit' and \
                                    parse_bool(self.result_msg.body.get('base', {}).get('reboot_after_hostinit_phase')):
                                    self._logger.debug(
                                        'Hostinit case and reboot_after_hostinit_phase. Interrupting message handler'
                                    )
                                    return
                                if self.special_case == 'BeforeHostUp':
                                    self._logger.debug(
                                        'BeforeHostUp case. Interrupting message handler'
                                    )
                                    return
                                self._logger.debug(
                                    'Found a message and continue message handler'
                                )
                                break
                        time.sleep(0.1)
                        continue

                    for queue, message in store.get_unhandled(self.endpoint):
                        self._handle_one_message(message, queue, store)

                except (BaseException, Exception), e:
                    self._logger.exception(e)
            time.sleep(0.1)
Ejemplo n.º 2
0
 def test_rotate(self):
     store = P2pMessageStore()
     db = bus.db
     conn = db.get().get_connection()
     cur = conn.cursor()
     cur.execute('SELECT COUNT(*) FROM p2p_message')
     self.assertEqual(cur.fetchone()[0], 114)
     store.rotate()
     cur.execute('SELECT COUNT(*) FROM p2p_message')
     self.assertEqual(cur.fetchone()[0], 50)
Ejemplo n.º 3
0
    def message_handler(self):
        store = P2pMessageStore()
        self.handler_status = 'idle'

        self._logger.debug('Starting message handler')

        while self.running:
            if not self.handler_locked:
                try:
                    if self.message_to_ack:
                        for queue, message in store.get_unhandled(
                                self.endpoint):
                            sid = self.message_to_ack.meta['server_id']
                            if message.name == self.message_to_ack.name and \
                                            message.body.get('server_id', sid) == sid:
                                self._logger.debug(
                                    'Going to handle_one_message. Thread: %s',
                                    threading.currentThread().getName())
                                try:
                                    self._handle_one_message(
                                        message, queue, store)
                                except:
                                    self.subhandler_exc_info = sys.exc_info()
                                    self._logger.debug(
                                        'Caught exception from _handle_one_message: thread: %s',
                                        threading.currentThread())

                                self._logger.debug(
                                    'Completed handle_one_message. Thread: %s',
                                    threading.currentThread().getName())
                                self.message_to_ack = None
                                self.ack_event.set()
                                if self.return_on_ack:
                                    return
                                break
                        time.sleep(0.1)
                        continue

                    for queue, message in store.get_unhandled(self.endpoint):
                        self._handle_one_message(message, queue, store)

                except (BaseException, Exception), e:
                    self._logger.exception(e)
            time.sleep(0.1)
Ejemplo n.º 4
0
    def shutdown(self, force=False):
        self._logger.debug('entring shutdown _server: %s, running: %s',
                           self._server, self.running)
        self.running = False
        if not self._server:
            return

        self._logger.debug('Shutdown message consumer %s ...', self.endpoint)

        self._logger.debug("Shutdown HTTP server")
        self._server.shutdown()
        self._server.socket.shutdown(socket.SHUT_RDWR)
        self._server.socket.close()
        #self._server.server_close()
        self._server = None
        self._logger.debug("HTTP server terminated")

        self._logger.debug("Shutdown message handler")
        self.handler_locked = True
        if not force:
            t = 120
            self._logger.debug(
                'Waiting for message handler to complete it`s task. Timeout: %d seconds',
                t)
            wait_until(lambda: self.handler_status in ('idle', 'stopped'),
                       timeout=t,
                       error_text='Message consumer is busy',
                       logger=self._logger)

        if self.handing_message_id:
            store = P2pMessageStore()
            store.mark_as_handled(self.handing_message_id)

        if self._handler_thread:
            self._handler_thread.join()
            self._logger.debug("Message handler terminated")

        self._logger.debug('Message consumer %s terminated', self.endpoint)
Ejemplo n.º 5
0
    def _get_request_handler_class(self):
        class RequestHandler(BaseHTTPRequestHandler):
            consumer = None
            '''
            @cvar consumer: Message consumer instance
            @type consumer: P2pMessageConsumer
            '''
            def _msg_without_sensitive_data(self, message):
                msg_copy = P2pMessage(message.name, message.meta.copy(),
                                      deepcopy(message.body))
                msg_copy.id = message.id

                if 'platform_access_data' in msg_copy.body:
                    del msg_copy.body['platform_access_data']

                if 'global_variables' in msg_copy.body:
                    glob_vars = msg_copy.body.get('global_variables', []) or []
                    i = 0
                    for v in list(glob_vars):
                        if v.get('private'):
                            del glob_vars[i]
                            i -= 1
                        elif 'private' in v:
                            del glob_vars[i]['private']
                        i += 1

                if 'chef' in msg_copy.body:
                    try:
                        del msg_copy.body['chef']['validator_name']
                        del msg_copy.body['chef']['validator_key']
                    except (KeyError, TypeError):
                        pass
                return msg_copy

            def do_POST(self):
                logger = logging.getLogger(__name__)

                queue = os.path.basename(self.path)
                rawmsg = self.rfile.read(int(self.headers["Content-length"]))
                logger.debug("Received ingoing message in queue: '%s'", queue)

                try:
                    for f in self.consumer.filters['protocol']:
                        rawmsg = f(self.consumer, queue, rawmsg)
                        try:
                            if isinstance(rawmsg, str):
                                h = HTMLParser.HTMLParser()
                                rawmsg = h.unescape(rawmsg).encode('utf-8')
                        except:
                            logger.debug('Caught message parsing error',
                                         exc_info=sys.exc_info())

                except (BaseException, Exception), e:
                    err = 'Message consumer protocol filter raises exception: %s' % str(
                        e)
                    logger.exception(err)
                    self.send_response(201, 'Created')
                    return

                try:
                    #logger.debug("Decoding message: %s", rawmsg)
                    message = P2pMessage()

                    mime_type = self.headers.get('Content-Type',
                                                 'application/xml')
                    format = ('application/json'
                              in mime_type) and 'json' or 'xml'

                    if 'json' == format:
                        message.fromjson(rawmsg)
                    else:
                        message.fromxml(rawmsg)

                    msg_copy = self._msg_without_sensitive_data(message)
                    logger.debug('Decoding message: %s',
                                 msg_copy.tojson(indent=4))

                except (BaseException, Exception), e:
                    err = "Cannot decode message. error: %s; raw message: %s" % (
                        str(e), rawmsg)
                    logger.exception(err)
                    self.send_response(201, 'Created')
                    return

                logger.debug(
                    "Received message '%s' (message_id: %s, format: %s)",
                    message.name, message.id, format)
                #logger.info("Received ingoing message '%s' in queue %s", message.name, queue)

                try:
                    store = P2pMessageStore()
                    store.put_ingoing(message, queue, self.consumer.endpoint)
                    #self.consumer._not_empty.set()
                except (BaseException, Exception), e:
                    logger.exception(e)
                    self.send_response(500, str(e))
                    return
Ejemplo n.º 6
0
    def _get_request_handler_class(self):
        class RequestHandler(BaseHTTPRequestHandler):
            consumer = None
            '''
            @cvar consumer: Message consumer instance
            @type consumer: P2pMessageConsumer
            '''
            def do_POST(self):
                logger = logging.getLogger(__name__)

                queue = os.path.basename(self.path)
                rawmsg = self.rfile.read(int(self.headers["Content-length"]))
                logger.debug("Received ingoing message in queue: '%s'", queue)

                try:
                    for f in self.consumer.filters['protocol']:
                        rawmsg = f(self.consumer, queue, rawmsg)
                        try:
                            if isinstance(rawmsg, str):
                                h = HTMLParser.HTMLParser()
                                rawmsg = h.unescape(rawmsg).encode('utf-8')
                        except:
                            logger.debug('%s',
                                         sys.exc_info()[1],
                                         sys.exc_info()[2])

                except (BaseException, Exception), e:
                    err = 'Message consumer protocol filter raises exception: %s' % str(
                        e)
                    logger.info(
                        err
                    )  # Downshift level, cause HTTP scanners do a lot of flood
                    self.send_response(400, str(e))
                    return

                try:
                    logger.debug("Decoding message: %s", rawmsg)
                    message = P2pMessage()

                    mime_type = self.headers.get('Content-Type',
                                                 'application/xml')
                    format = ('application/json'
                              in mime_type) and 'json' or 'xml'
                    if 'json' == format:
                        message.fromjson(rawmsg)
                    else:
                        message.fromxml(rawmsg)

                except (BaseException, Exception), e:
                    err = "Cannot decode message. error: %s; raw message: %s" % (
                        str(e), rawmsg)
                    logger.exception(err)
                    self.send_response(400, err)
                    return

                logger.debug("Received message '%s' (message_id: %s)",
                             message.name, message.id)
                #logger.info("Received ingoing message '%s' in queue %s", message.name, queue)

                try:
                    store = P2pMessageStore()
                    store.put_ingoing(message, queue, self.consumer.endpoint)
                    #self.consumer._not_empty.set()
                except (BaseException, Exception), e:
                    logger.exception(e)
                    self.send_response(500, str(e))
                    return