Beispiel #1
0
 def run(self):
     log.info("ti-server running at http://%s:%s" % (self.host, self.port))
     while True:
         try:
             HTTPServer(
                 self.host, self.port, self.app, threaded=False
             ).start()
         except Exception as e:
             log.error("exception occurred when request to ti server http"
                       "://%s:%s, error: %s" % (self.host, self.port, e))
             time.sleep(3)
Beispiel #2
0
 def get_provider_method(self, url):
     log.info("get request url mapping method, url: %s" % url)
     for item in self.url_mapping:
         provider = item["provider"]
         uri_list = item["uris"]
         for uri in uri_list:
             path = uri["uri"]
             p = re.compile(path)
             if p.match(url):
                 return provider, uri["method"]
     log.error("get provider of url: %s failed." % url)
     return None, None
Beispiel #3
0
 def import_object(class_path):
     module_str, sep, class_name = class_path.rpartition(".")
     log.info("rest_parser, module_str: %s" % module_str)
     try:
         __import__(module_str)
         log.info("rest_parser, module_str: %s" % module_str)
         return getattr(sys.modules[module_str], class_name)()
     except (ValueError, AttributeError) as e:
         log.error("failed to import class: %s in file: %s, "
                   "error: %s, trace: %s" %
                   (class_name, module_str, e, traceback.format_exc()))
         raise ImportError("Class: %s cannot be found in: %s" %
                           (class_name, module_str))
Beispiel #4
0
    def start(self):

        if not self.threaded:
            socket_class = socket
            _socket = eventlet.listen((self.host, int(self.port)))
        else:
            socket_class = orig_socket
            _socket = orig_socket.socket(
                orig_socket.AF_INET, orig_socket.SOCK_STREAM
            )

            if sys.platform[:3] != "win":
                _socket.setsockopt(socket_class.SOL_SOCKET,
                                   socket_class.SO_REUSEADDR, 1)

            _socket.bind((self.host, int(self.port)))
            _socket.listen(50)

        try:
            _socket.setsockopt(socket_class.SOL_SOCKET,
                               socket_class.SO_KEEPALIVE, 1)

            if hasattr(socket_class, 'TCP_KEEPIDLE'):
                _socket.setsockopt(socket_class.IPPROTO_TCP,
                                   socket_class.TCP_KEEPIDLE, 120)
                _socket.setsockopt(socket_class.IPPROTO_TCP,
                                   socket_class.TCP_KEEPCNT, 4)
                _socket.setsockopt(socket_class.IPPROTO_TCP,
                                   socket_class.TCP_KEEPINTVL, 15)

            srv = wsgi.server if not self.threaded else thread_server
            # if (isinstance(self.app, types.InstanceType) and not isinstance(
            #         self.app, BaseRequestHandler)) \
            #         or isinstance(self.app, types.FunctionType):
            #     srv(_socket, self.app, protocol=HttpProtocol, log=self.log,
            #         socket_timeout=self.socket_timeout,
            #         need_parallel_func=self.need_parallel_func)
            # elif isinstance(self.app, types.ClassType):
            #     srv(_socket, _default_app, protocol=self.app, log=self.log,
            #         socket_timeout=self.socket_timeout,
            #         need_parallel_func=self.need_parallel_func)
            # else:
            srv(_socket, self.app, protocol=HttpProtocol, log=self.log,
                socket_timeout=self.socket_timeout)
            # raise Exception("rest server only support func call or "
            #                 "instance of BaseRequestHandler")
        except Exception as e:
            log.error("HTTPServer start error: %s, trace; %s" %
                      (e, traceback.format_exc()))
        finally:
            _socket.close()
Beispiel #5
0
    def __build_success_response(self, class_path, method, environ):
        res = Response()
        res.content_type = self.CONTENT_TYPE
        try:
            obj = self.import_object(class_path)
            status, body = self.ex_func(obj, method, environ)
            log.info("the http status: %s" % status)

            res.body = json.dumps(body).encode("utf-8")
            res.status = status
        except Exception as e:
            res.status = "500"
            log.error("internal error, class: %s, method: %s, "
                      "error: %s, trace: %s" %
                      (class_path, method, e, traceback.format_exc()))
        return res
Beispiel #6
0
 def ti_auto_test_request(self, environ):
     req = Request(environ)
     action = req.method
     if action == "POST":
         # request body invalid
         try:
             request_body = json.loads(req.body.decode("utf-8"))
             workspace = request_body["workspace"]
             match_string = request_body["match_string"]
             if not match_string:
                 raise Exception(
                     "has not specified tiDB test match string.")
             if not workspace:
                 raise Exception("has not specified tiDB workspace.")
         except Exception as e:
             message = "%s request body invalid. body: %s" % \
                       (self.FAIL, req.body)
             log.error("%s, error: %s, trace: %s" %
                       (message, e, traceback.format_exc()))
             return HttpConstant.CODE500, {"code": 500, "message": message}
         try:
             AutoTest.ti_test_run(workspace, match_string)
         except Exception as e:
             message = "%s ti-server internal error." % self.FAIL
             log.error("%s, error: %s, trace: %s" %
                       (message, e, traceback.format_exc()))
             return HttpConstant.CODE500, {"code": 500, "message": message}
         message = "start to run tiDB auto test instance: %s, log " \
                   "dir path: %s" % (match_string, workspace)
         return HttpConstant.CODE200, {"code": 200, "message": message}
     else:
         message = "Request to run tempest failed with api not supported"
         log.error(message)
         return HttpConstant.CODE500, {"code": 500, "message": message}
Beispiel #7
0
                raise Exception("tiDB cluster has not started successfully "
                                "after %s s" % 60)
            time.sleep(30)

    @staticmethod
    def run_test(match_string):
        ti_db = TiDBService().ti_db
        log.info("get tiDB instance %s" % ti_db)
        log.info("start to run tiDB test instance %s" % match_string)
        try:
            test = TestBankTransfer(ti_db, log, BaseFun)
            test.setUp()
            test.test_bank_transfer()
            test.tearDown()
        except Exception as test_e:
            log.info("execute test %s failed. error: %s, trace: %s" %
                     (match_string, test_e, traceback.format_exc()))
        finally:
            ti_db.cursor().close()
            ti_db.close()


if __name__ == "__main__":
    log.init("ti-server")
    try:
        AutoTest.env_set(sys.argv[1], sys.argv[2])
        AutoTest.run_test(sys.argv[2])
    except Exception as e:
        log.error("execute tiDB test %s failed. error: %s trace: %s" %
                  (sys.argv[2], e, traceback.format_exc()))
Beispiel #8
0
 def __build_failed_response(self, request):
     res = Response()
     log.error("unsupported request! request body: %s" % str(request.body))
     res.status = "400"
     res.content_type = self.CONTENT_TYPE
     return res
Beispiel #9
0
        if arg.port:
            self.info["port"] = arg.port
        self.start_rest_server()
        while True:
            time.sleep(10)


def init_options():
    parser = argparse.ArgumentParser()
    parser.add_argument("-host",
                        "--host",
                        metavar="",
                        required=True,
                        help="ip of ti-server")
    parser.add_argument("-port",
                        "--port",
                        metavar="",
                        required=True,
                        help="port of ti-server")
    parsed_args = parser.parse_args()
    return parsed_args


if __name__ == "__main__":
    log.init("ti-server")
    try:
        TiServer().run()
    except Exception as e:
        log.error("ti-server exit abnormal. error: %s, trace: %s" %
                  (e, traceback.format_exc()))