def do_auth(): config_token = config_data['application']["security"]["token"] if "token" not in request.headers: raise MyServiceException('request failure, because miss the token in request header') request_header_token = request.headers["token"] if not request_header_token == config_token: raise MyServiceException('request failure, because miss the token in request header')
def check(check_data_keys=[]): request_data = json.loads(request.get_data()) if check_data_keys and len(check_data_keys) > 0: for item in check_data_keys: if not request_data.__contains__(item): raise MyServiceException("missing param: %s" % item) return request_data
def update(): request_data = form.check(["id"]) params = {} update_set_sql_str = "" params["id"] = request_data["id"] if request_data.__contains__("name"): params["name"] = request_data["name"] update_set_sql_str = "name=%(name)s, " if request_data.__contains__("pid"): params["pid"] = request_data["pid"] update_set_sql_str = "pid=%(pid)s, " if request_data.__contains__("description"): params["description"] = request_data["description"] update_set_sql_str = "description=%(description)s, " if "" == update_set_sql_str: raise MyServiceException("no content for update set") update_set_sql_str = update_set_sql_str[:len(update_set_sql_str) - 2] return json.dumps( mymysql.execute("update designer_data_directory set " + update_set_sql_str + " where id = %(id)s", params))
def do_auth(): if SOA_TOKEN_STR not in request.headers: raise MyServiceException("未登录的请求") token = request.headers[SOA_TOKEN_STR] # url_root = request.url_root # 请求的根路径, 包含请求协议、域名、端口 # TODO 这里可以优化性能 query_token_by_remote(token)
def sync_files_2_remote(host_conf, local_basic_dir, remote_basic_dir, file_name_list): """ 同步文件列表到服务器 :param host_conf: :param local_basic_dir: :param remote_basic_dir: :param file_name_list: :return: """ if not isinstance(file_name_list, list): raise MyServiceException("文件名称参数必须为数组") t = paramiko.Transport(sock=(host_conf['ip'], int(host_conf['port']))) t.connect(username=host_conf['username'], password=host_conf['password']) sftp = paramiko.SFTPClient.from_transport(t) for file_name in file_name_list: local_file = local_basic_dir + "/" + file_name remote_file = remote_basic_dir + "/" + file_name log("sftp putting %s to %s" % (local_file, remote_file)) sftp.put(local_file, remote_file) log("sftp putted %s to %s" % (local_file, remote_file)) log("sync local:[%s] to remote:[%s] dirs:[%s] success" % (str(local_basic_dir), str(remote_basic_dir), str(file_name_list))) # 关闭sftp sftp.close() t.close()
def query_token_by_remote(token): # 请求授权接口 oauth_url = config.app_conf["oauth"]["url"] resp = requests.get(oauth_url + "/permission/verification_token", {"token": token}) result = resp.json() if not result or len(result) < 1: raise MyServiceException("请求令牌查询失败, 请重新登录")
def service_component(): request_data = rest_form.check(['component_type', 'tag_id', 'params']) component_type = request_data['component_type'] tag_id = request_data['tag_id'] params = request_data['params'] if component_type not in allow_component_type_list: raise MyServiceException( 'component_type not exists, please check again') return json.dumps(database_oracle.call_registration(tag_id, params))
def do_auth(): if SOA_TOKEN_STR not in request.headers: raise MyServiceException("未登录的请求") token = request.headers[SOA_TOKEN_STR] # url_root = request.url_root # 请求的根路径, 包含请求协议、域名、端口 if token in local_memory_token_record: local_memory_token_record_value = local_memory_token_record[token] if int(time.time( )) < local_memory_token_record_value["timestamp"] + 7 * 24 * 60 * 60: return query_token_by_remote(token)
def query_token_by_remote(token): # 请求授权接口 oauth_url = config.app_conf["oauth"]["url"] resp = requests.get(oauth_url + "/permission/verification_token", {"token": token}) result = resp.json() if not result or len(result) < 1: raise MyServiceException("请求令牌查询失败, 请重新登录") for item in result: record_2_local_memory(token, item["user_id"]) break
def data(): try: request_data = json.loads(request.get_data()) if not request_data.__contains__("execute"): raise MyServiceException("missing param: execute") execute = request_data["execute"] execute_result = mymysql.execute(execute) return dumps(execute_result) except MyServiceException as e: custom_res = make_response(e.msg) custom_res.status = "500" return custom_res
def load_local_data(tag_id): # config file if tag_id not in database_oracle.local_data_registration: database_oracle.local_data_registration = configer.load( os.path.join(database_oracle_dir_path, 'registration.yaml')) if tag_id not in database_oracle.local_data_registration: raise MyServiceException( 'the target tag id(%s) is not exists in service component: database_oracle' % tag_id) cur_config = database_oracle.local_data_registration[tag_id] if cur_config: if datasource_file_path_str in cur_config: datasource_file_path = cur_config[datasource_file_path_str] if sql_file_path_str in cur_config: sql_file_path = cur_config[sql_file_path_str] else: datasource_file_path = datasource_file_path_default sql_file_path = sql_file_path_default if '/' not in datasource_file_path: datasource_file_path = os.path.join(tag_id, datasource_file_path) if '/' not in sql_file_path: sql_file_path = os.path.join(tag_id, sql_file_path) cur_data_dir_path = os.path.join(database_oracle_dir_path, 'data') datasource_file_path = os.path.join(cur_data_dir_path, datasource_file_path) sql_file_path = os.path.join(cur_data_dir_path, sql_file_path) logging.info('datasource_file_path: ' + datasource_file_path) logging.info('sql_file_path: ' + sql_file_path) if not os.path.exists(datasource_file_path): raise MyServiceException('datasource is not exists') if not os.path.exists(sql_file_path): raise MyServiceException('sql is not exists') # data datasource_content = configer.load(datasource_file_path) with open(sql_file_path, 'r', encoding='utf-8') as f: sql_content = f.read() return datasource_content, sql_content
def execute(sql): try: conn = mymysql.db_pool.connection() cursor = conn.cursor() cursor.execute(sql) # consider it INSERT or other if sql.strip().upper().startswith("INSERT"): execute_result = cursor.lastrowid else: execute_result = cursor.fetchall() conn.commit() except Exception as e: raise MyServiceException("execute sql error" + str(e)) finally: cursor.close() conn.close() return execute_result
def call_registration(tag_id, params): datasource_content, sql_content = load_local_data(tag_id) check_datasource_content( datasource_content, ['ip', 'port', 'instance', 'username', 'password']) if not sql_content or sql_content.strip() == '': raise MyServiceException('sql_content is None or space') ip = datasource_content['ip'] port = datasource_content['port'] instance = datasource_content['instance'] username = datasource_content['username'] password = datasource_content['password'] oracle = Oracle(ip=ip, port=port, instance=instance, username=username, password=password) return oracle.select(sql_content, params=params)
def sync_dirs_2_remote(host_conf, local_basic_dir, remote_basic_dir, dir_name_list): """ 同步本地文件夹列表到远程服务器 两种实现方式, 判断条件为文件夹中文件数量是否超过一定数量 1、当超过时, 打包文件夹成tar包, 传输tar包到远程服务器指定的目录, 解压tar包 2、当没超过时, 遍历文件夹进行单个传输到指定路径 :param host_conf: :param local_basic_dir: :param remote_basic_dir: :param dir_name_list: :return: """ if not isinstance(dir_name_list, list): raise MyServiceException("目录名称参数必须为数组") t = paramiko.Transport(sock=(host_conf['ip'], int(host_conf['port']))) t.connect(username=host_conf['username'], password=host_conf['password']) sftp = paramiko.SFTPClient.from_transport(t) for dir_name in dir_name_list: # 先移除, 保险起见应该是先上传到该同级_upload_temp路径, 再删除 execute_remote_command(host_conf, "rm -rf " + remote_basic_dir + "/" + dir_name) local_files = get_local_files(local_basic_dir + "/" + dir_name) remote_files = convert_local_files_2_remote_files( local_files, local_basic_dir, remote_basic_dir) for index in range(len(local_files)): local_file = local_files[index] remote_file = remote_files[index] if local_file.endswith("/"): try: sftp.mkdir(remote_file[:-1]) except IOError: pass continue log("sftp putting %s to %s" % (local_file, remote_file)) sftp.put(local_file, remote_file) log("sftp putted %s to %s" % (local_file, remote_file)) log("sync local:[%s] to remote:[%s] dirs:[%s] success" % (str(local_basic_dir), str(remote_basic_dir), str(dir_name_list))) # 关闭sftp sftp.close() t.close()
def execute(db_pool, sql, is_query, parameters=None): # print("sql: \n %s \n parameters: %s" % (sql, parameters)) execute_result = None if not parameters: parameters = {} try: with closing(db_pool.connection()) as conn: with closing(conn.cursor()) as cursor: if is_query: cursor.execute(sql, parameters) execute_result = cursor.fetchall() else: num = cursor.executemany(sql, parameters) if num > 0: last_rowid = int(cursor.lastrowid) execute_result = list(range(last_rowid - num + 1, last_rowid + 1)) conn.commit() except Exception: import traceback, sys traceback.print_exc() # 打印异常信息 exc_type, exc_value, exc_traceback = sys.exc_info() error = str(repr(traceback.format_exception(exc_type, exc_value, exc_traceback))) raise MyServiceException("execute sql error: " + error) return execute_result
def check_datasource_content(datasource_content, check_list): if not datasource_content: raise MyServiceException('datasource_content is None') for item in check_list: if item not in datasource_content: raise MyServiceException('%s not in datasource.yml' % item)