def change_group_own(_path): code, output = TiServerControl.__exe_cmd( "sudo -n chgrp ${USER} -R %s" % _path) if code: log.error("Change group of %s failed." % _path) log.info("Change group of %s successfully.") return code
def do_run(proxy, args): log.info("start request to ti-server to execute test instance. workspace" ": %s, test match string: %s" % (args.workspace, args.regex)) _code, body = proxy.ti_server_rest.auto_test(args.workspace, args.regex) if _code in RIGHT_RESPONSE_CODE: print("request to execute TiDB auto test successfully. you can view " "log in your workspace: %s" % args.workspace) else: print("request to ti-server failed. please view log in you workspace:" " %s for detail info" % args.workspace)
def run(self): args = init_options() log.info("input argument: %s" % args) if args.config: self.__load_config(args.config) if args.action: action = args.action.lower() if action not in self.ACTION_LIST: log.error("action to control ti-server invalid.") raise Exception("Action to control ti-server invalid.") self.operate_ti_server(action)
def __load_config(self, config): if not os.path.exists(config): log.error("ti server configuration file not exist. " "path: %s" % config) raise Exception("ti server configuration file not exist. " "path: %s" % config) log.info("ti-server config path: %s" % config) ti_config = ConfigParser() ti_config.read(config) if not ti_config.has_section(self.TI_SERVER_SECTION): log.error("there is not ti server configuration in config " "file: %s" % config) raise NoSectionError(self.TI_SERVER_SECTION) for option in self.ti_server_info: if not ti_config.has_option(self.TI_SERVER_SECTION, option): log.error("there is not ti server %s in config " "file: %s" % (option, config)) raise NoOptionError(option, self.TI_SERVER_SECTION) self.ti_server_info[option] = \ ti_config.get(self.TI_SERVER_SECTION, option) self.ti_server_url = "http://%s:%s" % (self.ti_server_info["host"], self.ti_server_info["port"])
def __start_ti_server(self): log.info("begin to start ti-server..") with open(TiServerControl.TI_CTL_LOCK, "r+") as fd: fcntl.flock(fd.fileno(), fcntl.LOCK_EX) is_run = self.is_ti_server_run() if is_run: log.info("ti server is run, exist ti_server_control") else: start_cmd = "python3 %s" % self.ti_server_path for arg, value in self.ti_server_info.items(): start_cmd = "%s -%s %s" % (start_cmd, arg, value) log.info("run ti-server start cmd: %s" % start_cmd) pro = subprocess.Popen(start_cmd, close_fds=True, shell=True) self.set_ti_server_pid(pro.pid) log.info("start ti server successfully for: %s" % self.ti_server_path)
def __stop_ti_server(self): log.info("begin to stop ti-server..") with open(TiServerControl.TI_CTL_LOCK, "r+") as fd: fcntl.flock(fd.fileno(), fcntl.LOCK_EX) is_run = self.is_ti_server_run() if not is_run: log.info("ti server is not run, exist ti_server_control") else: ti_server_pid = self.get_ti_server_pid() kill_cmd = "kill -9 %s" % ti_server_pid code, output = self.__exe_cmd(kill_cmd) if code: log.error("ti server stop failed, code: %s, output: " "%s" % (code, output)) raise Exception(output[-1]) if os.path.exists(self.TI_SERVER_PID): os.remove(self.TI_SERVER_PID) log.info("ti server stop successfully.")
def json_request(self, method, path, **kwargs): """Encapsulate urllib2 request to json request :param method: str, request method :param path: str, request path :param kwargs: tuple list, other specified arguments :return: tuple, (response, response body) """ input_arg = dict() # set request headers headers = input_arg.setdefault("headers", dict()) headers["Content-Type"] = "application/json" if "headers" in kwargs: headers.update(kwargs["headers"]) # set request body if "body" in kwargs: input_arg["body"] = kwargs["body"] # set request transfer time if "timeout" in kwargs and kwargs["timeout"]: input_arg["timeout"] = kwargs["timeout"] else: input_arg["timeout"] = 180 # set ssl validate path if self.is_https: if "verify" in kwargs: input_arg["verify"] = kwargs["verify"] else: input_arg["verify"] = False # set request url url = "".join([self.str_url, path]) content = kwargs.get("body", {}) if type(content) != dict: content = dict() log.info("Request... method: %s, url: %s, body: %s" % (method, url, content)) # request to upgrade service timeout_retry = 3 res_code, res_data = None, None for i in range(timeout_retry): time.sleep(0.5) try: log.debug("rest call try cnt %d" % i) if self.is_https: res_code, res_data = self.https_request( method, url, **input_arg) else: res_code, res_data = self.http_request( method, url, **input_arg) if res_code in RIGHT_RESPONSE_CODE: break except Exception as te: if i == timeout_retry - 1: raise te else: continue try: body = json.loads(res_data) except ValueError as e: log.warn("response body is json object. res body: %s" % res_data) raise e log.warn("code: %s, data: %s" % (res_code, body)) return res_code, body
def set_ti_server_pid(self, pid): with open(self.TI_SERVER_PID, "w+") as fd_pid: fd_pid.write(str(pid)) log.info("write ti-server pid to pid file: %s. pid: %s" % (self.TI_SERVER_PID, pid))
"--action", metavar="", required=True, help="Action to control ti-server") parser_sub.add_argument("-c", "--config", metavar="", required=True, help="configuration file of ti-server") parsed_args = parser.parse_args() return parsed_args if __name__ == "__main__": log.init("ti_server_control") ti_server = "ti_server_process.py" ti_server_root = TiServerControl.TI_SERVER_ROOT log.info("ti_server_root: %s, ti_server: %s" % (ti_server_root, ti_server)) if not os.path.exists(TiServerControl.TI_CTL_LOCK): with open(TiServerControl.TI_CTL_LOCK, "w"): TiServerControl.change_group_own(TiServerControl.TI_CTL_LOCK) try: ti_server_ctl = TiServerControl(ti_server_root, ti_server) ti_server_ctl.run() except Exception as e: log.error("operate ti-server failed. error: %s, trace: %s" % (e, traceback.format_exc()))