Пример #1
0
    def checkReplicationStatus(self, activeStatus, dbInfo, source, target,
                               previousUpdateNum, checkUpdateSeq):
        """
        adhog way to check the replication
        """

        logging.info("checking the replication status")
        if "Starting" in activeStatus["status"]:
            logging.info("Starting status: ok")
            return True
        elif "update" in activeStatus["status"]:
            updateNum = int(activeStatus["status"].split('#')[-1])
            self.setPreviousUpdateSequence(source, target, updateNum)

            if not checkUpdateSeq:
                logging.warning(
                    """Update sequence is not checked\n" /
                                   replication from %s to %s""" %
                    (replaceToSantizeURL(source), replaceToSantizeURL(target)))
                return True
            elif updateNum == dbInfo[
                    "update_seq"] or updateNum > previousUpdateNum:
                logging.info("update upto date: ok")
                return True
            else:
                return False
        return False
Пример #2
0
    def checkReplicationStatusCouch15(self, activeStatus, dbInfo, source,
                                      target, previousUpdateNum,
                                      checkUpdateSeq):
        """
        adhog way to check the replication
        """

        logging.info("checking the replication status")
        #if activeStatus["started_on"]:
        #   logging.info("Starting status: ok")
        #    return True
        if activeStatus["updated_on"]:
            updateNum = int(activeStatus["source_seq"])
            self.setPreviousUpdateSequence(source, target, updateNum)
            if not checkUpdateSeq:
                logging.warning(
                    "Need to check replication from %s to %s" %
                    (replaceToSantizeURL(source), replaceToSantizeURL(target)))
                return True
            elif updateNum == dbInfo[
                    "update_seq"] or updateNum > previousUpdateNum:
                logging.info("update upto date: ok")
                return True
            else:
                return False
        return False
Пример #3
0
 def checkReplicationStatus(self, activeStatus, dbInfo, source, target, 
                            previousUpdateNum, checkUpdateSeq):
     """
     adhog way to check the replication
     """
     
     logging.info("checking the replication status")
     if "Starting" in activeStatus["status"]:
         logging.info("Starting status: ok")
         return True
     elif "update" in activeStatus["status"]:
         updateNum = int(activeStatus["status"].split('#')[-1])
         self.setPreviousUpdateSequence(source, target, updateNum)
         
         if not checkUpdateSeq:
             logging.warning("""Update sequence is not checked\n" /
                                replication from %s to %s""" % (
                             replaceToSantizeURL(source), replaceToSantizeURL(target)))
             return True
         elif updateNum == dbInfo["update_seq"] or updateNum > previousUpdateNum:
             logging.info("update upto date: ok")
             return True
         else:
             return False
     return False
Пример #4
0
 def checkReplicationStatus(self, activeStatus, dbInfo, source, target, 
                                   checkUpdateSeq):
     """
     adhog way to check the replication
     """
     
     logging.info("checking the replication status")
     #if activeStatus["started_on"]:
     #   logging.info("Starting status: ok")
     #    return True
     previousUpdateNum = self.getPreviousUpdateSequence(source, target)
     
     if activeStatus["updated_on"]:
         updateNum = int(activeStatus["source_seq"])
         self.setPreviousUpdateSequence(source, target, updateNum)
         if not checkUpdateSeq:
             logging.warning("Need to check replication from %s to %s" % (
                             replaceToSantizeURL(source), replaceToSantizeURL(target)))
             return True
         elif updateNum == dbInfo["update_seq"] or updateNum > previousUpdateNum:
             logging.info("update upto date: ok")
             return True
         else:
             return False
     return False
Пример #5
0
 def _filterReplicationDocs(self, repDocs, source, target):
     filteredDocs = []
     for j in repDocs:
         if not j['id'].startswith('_'):
             if j['doc']['source'] == source and j['doc']['target'] == target:
                 doc = {}
                 doc["_id"]  = j['id']
                 doc["_rev"] = j['value']['rev']
                 if doc.has_key("_replication_state"):
                     doc["_replication_state"] = j["doc"]["_replication_state"]
                 else:
                     logging.error("""replication failed from %s to %s 
                                      couch server manually need to be restarted""" % (
                                      replaceToSantizeURL(source), 
                                      replaceToSantizeURL(target)))
                 filteredDocs.append(doc)
     return filteredDocs
Пример #6
0
 def _filterReplicationDocs(self, repDocs, source, target):
     filteredDocs = []
     for j in repDocs:
         if not j['id'].startswith('_'):
             if j['doc']['source'] == source and j['doc'][
                     'target'] == target:
                 doc = {}
                 doc["_id"] = j['id']
                 doc["_rev"] = j['value']['rev']
                 if doc.has_key("_replication_state"):
                     doc["_replication_state"] = j["doc"][
                         "_replication_state"]
                 else:
                     logging.error("""replication failed from %s to %s 
                                      couch server manually need to be restarted"""
                                   % (replaceToSantizeURL(source),
                                      replaceToSantizeURL(target)))
                 filteredDocs.append(doc)
     return filteredDocs
Пример #7
0
    def _buildResponse(self, args=[], kwargs={}):
        """
        Set the headers for the response appropriately and format the response
        data (e.g. serialise to XML, JSON, RSS/ATOM) or return the documentation
        if no method is specified.
        """
        args = list(args)
        if len(args) == 0 and len(kwargs) == 0:
            self.debug('returning REST documentation')
            types = []
            for m in dir(self.formatter):
                prop = dir(self.formatter.__getattribute__(m))
                if 'exposed' in prop:

                    types.append(m)

            return self.templatepage('RESTAPI', model = self.model,
                                 types = types,
                                 title = self.config.title,
                                 description = self.config.description)
        try:
            data, expires = self.methods['handler']['call'](request.method,
                                                            args, kwargs)
            return self._formatResponse(data, expires,
                                       kwargs.get('return_type', None))

        except HTTPError as h:
            # If something raises an HTTPError assume it's something that should
            # go to the client
            response.status = h[0]
            for kwarg in kwargs.keys():
                if isinstance(kwargs[kwarg], cgi.FieldStorage):
                    kwargs[kwarg] = 'FieldStorage class, not printed.'
            self.debug('call to %s with args: %s kwargs: %s resulted in %s' % (request.method, args, kwargs, h[1]))
            return self._formatResponse({'exception': h[0],
                                        'type': 'HTTPError',
                                        'message': h[1]}, expires=0,
                                        format=kwargs.get('return_type', None))
        except Exception as e:
            # If something raises a generic exception assume the details are private
            # and should not go to the client
            response.status = 500
            for kwarg in kwargs.keys():
                if isinstance(kwargs[kwarg], cgi.FieldStorage):
                    kwargs[kwarg] = 'FieldStorage class, not printed.'
            debugMsg = replaceToSantizeURL("""call to %s with args: %s kwargs: %s resulted in %s \n
                                              stack trace: %s""" % (request.method, args,
                                                     kwargs, str(e), traceback.format_exc()))
            self.debug(debugMsg)
            return self._formatResponse({'exception': 500,
                                        'type': e.__class__.__name__,
                                        'message': 'Server Error'}, expires=0,
                                        format=kwargs.get('return_type', None))
Пример #8
0
    def _buildResponse(self, args=[], kwargs={}):
        """
        Set the headers for the response appropriately and format the response
        data (e.g. serialise to XML, JSON, RSS/ATOM) or return the documentation
        if no method is specified.
        """
        args = list(args)
        if len(args) == 0 and len(kwargs) == 0:
            self.debug('returning REST documentation')
            types = []
            for m in dir(self.formatter):
                prop = dir(self.formatter.__getattribute__(m))
                if 'exposed' in prop:

                    types.append(m)

            return self.templatepage('RESTAPI',
                                     model=self.model,
                                     types=types,
                                     title=self.config.title,
                                     description=self.config.description)
        try:
            data, expires = self.methods['handler']['call'](request.method,
                                                            args, kwargs)
            return self._formatResponse(data, expires,
                                        kwargs.get('return_type', None))

        except HTTPError as h:
            # If something raises an HTTPError assume it's something that should
            # go to the client
            response.status = h[0]
            for kwarg in kwargs.keys():
                if isinstance(kwargs[kwarg], cgi.FieldStorage):
                    kwargs[kwarg] = 'FieldStorage class, not printed.'
            self.debug('call to %s with args: %s kwargs: %s resulted in %s' %
                       (request.method, args, kwargs, h[1]))
            return self._formatResponse(
                {
                    'exception': h[0],
                    'type': 'HTTPError',
                    'message': h[1]
                },
                expires=0,
                format=kwargs.get('return_type', None))
        except Exception as e:
            # If something raises a generic exception assume the details are private
            # and should not go to the client
            response.status = 500
            for kwarg in kwargs.keys():
                if isinstance(kwargs[kwarg], cgi.FieldStorage):
                    kwargs[kwarg] = 'FieldStorage class, not printed.'
            debugMsg = replaceToSantizeURL(
                """call to %s with args: %s kwargs: %s resulted in %s \n
                                              stack trace: %s""" %
                (request.method, args, kwargs, str(e), traceback.format_exc()))
            self.debug(debugMsg)
            return self._formatResponse(
                {
                    'exception': 500,
                    'type': e.__class__.__name__,
                    'message': 'Server Error'
                },
                expires=0,
                format=kwargs.get('return_type', None))
Пример #9
0
class RESTApi(WebAPI):
    """
    Don't subclass this, use the RESTModel as a base for your application code
    """
    __version__ = 1

    def __init__(self, config={}):

        self._set_formatter(config)

        self._set_model(config)

        self.__doc__ = self.model.__doc__

        WebAPI.__init__(self, config)
        self.methods.update({
            'handler': {
                'args': [],
                'call': self.model.handler,
                'version': 1
            }
        })

        # TODO: implement HEAD & TRACE
        self.supporttypes = self.formatter.supporttypes.keys()

    def _set_model(self, config):
        """
        Load the model object, and assign it to the RESTAPI
        """
        factory = WMFactory('webtools_factory')
        self.model = factory.loadObject(config.model.object, config)

    def _set_formatter(self, config):
        """
        Load the formatter object, and assign it to the RESTAPI
        """
        factory = WMFactory('webtools_factory')
        self.formatter = factory.loadObject(config.formatter.object, config)

    @expose
    def index(self, **kwargs):
        """
        Return the auto-generated documentation for the API
        """
        return self._buildResponse()

    @expose
    def default(self, *args, **kwargs):
        """
        Return either the documentation or run the appropriate method.
        """
        return self._buildResponse(args, kwargs)

    def _buildResponse(self, args=[], kwargs={}):
        """
        Set the headers for the response appropriately and format the response
        data (e.g. serialise to XML, JSON, RSS/ATOM) or return the documentation
        if no method is specified.
        """
        args = list(args)
        if len(args) == 0 and len(kwargs) == 0:
            self.debug('returning REST documentation')
            types = []
            for m in dir(self.formatter):
                prop = dir(self.formatter.__getattribute__(m))
                if 'exposed' in prop:

                    types.append(m)

            return self.templatepage('RESTAPI',
                                     model=self.model,
                                     types=types,
                                     title=self.config.title,
                                     description=self.config.description)
        try:
            data, expires = self.methods['handler']['call'](request.method,
                                                            args, kwargs)
            return self._formatResponse(data, expires,
                                        kwargs.get('return_type', None))

        except HTTPError, h:
            # If something raises an HTTPError assume it's something that should
            # go to the client
            response.status = h[0]
            for kwarg in kwargs.keys():
                if isinstance(kwargs[kwarg], cgi.FieldStorage):
                    kwargs[kwarg] = 'FieldStorage class, not printed.'
            self.debug('call to %s with args: %s kwargs: %s resulted in %s' %
                       (request.method, args, kwargs, h[1]))
            return self._formatResponse(
                {
                    'exception': h[0],
                    'type': 'HTTPError',
                    'message': h[1]
                },
                expires=0,
                format=kwargs.get('return_type', None))
        except Exception, e:
            # If something raises a generic exception assume the details are private
            # and should not go to the client
            response.status = 500
            for kwarg in kwargs.keys():
                if isinstance(kwargs[kwarg], cgi.FieldStorage):
                    kwargs[kwarg] = 'FieldStorage class, not printed.'
            debugMsg = replaceToSantizeURL(
                """call to %s with args: %s kwargs: %s resulted in %s \n
                                              stack trace: %s""" %
                (request.method, args, kwargs, str(e), traceback.format_exc()))
            self.debug(debugMsg)
            return self._formatResponse(
                {
                    'exception': 500,
                    'type': e.__class__.__name__,
                    'message': 'Server Error'
                },
                expires=0,
                format=kwargs.get('return_type', None))