def get_process_template_role(): try: request_data = common_service.check_request_dat_not_null( ["process_template_id"]) env = request_data["env"] process_template_id = request_data["process_template_id"] # 获取该模板对应的值 process_template_level_top = json.loads(get()) process_template_level_second = process_template_level_top[0] process_template_level_data = process_template_level_second[ "process_template"] if not process_template_level_data.__contains__(env): raise common_service.MyServiceException("该流程模板环境(%s)不存在" % env) process_template_level_env = process_template_level_data[env] if not process_template_level_env.__contains__(process_template_id): raise common_service.MyServiceException("该流程模板id(%s)不存在" % process_template_id) process_template = process_template_level_env[process_template_id] process_template = process_template.strip() if not process_template or process_template.strip() == "": raise common_service.MyServiceException("该流程模板值为空") return process_template except common_service.MyServiceException as e: print(e) return common_service.ResResult.return500(str(e))
def get_crontab_file_content_list(): try: request_data = common_service.check_request_dat_not_null(["env", "host"]) env = request_data["env"] host = request_data["host"] host = host.replace(".", "_") # 判断来访数据合理性 schedule_server_config_datas = json.loads(schedule_server_config.get()) schedule_server_config_data = schedule_server_config_datas[0] schedule_server_config_data_data = schedule_server_config_data["schedule_server_config"] level_env = schedule_server_config_data_data[env] level_host = level_env[host] if not level_host: raise common_service.MyServiceException("非法入侵, 该服务器不存在调度服务器配置数据中") # 获取服务器连接信息 server_manage_datas = json.loads(server_manage.get_decrypt()) server_manage_data = server_manage_datas[0] server_manage_data_data = server_manage_data["server_manage"] server_manage_level_host = server_manage_data_data[host] conn_host = host.replace("_", ".") for key in server_manage_level_host: username = key password = server_manage_level_host[username] my_server = MyServer(conn_host, username, password) filepath = "/etc/crontab" file_content = my_server.get_file_content(filepath) result = {} for index, item in enumerate(file_content): result[index] = item return result break except common_service.MyServiceException as e: return common_service.ResResult.return500(str(e))
def get_alarm_config_file(): request_data = common_service.check_request_dat_not_null(["name"]) name = request_data["name"] if not name or name.strip() == "": raise common_service.MyServiceException("name不能为空") return dumps( monitoring__alarm_config_file_co.find(filter={"name": name}, limit=1, sort=[("_id", -1)]))
def exe_command(self, command): print(">" * 200) print("开始执行: ", "exe_command: ", command) stdin, stdout, stderr = self.client.exec_command(command) exe_result = "\n".join(stdout.readlines()) exe_err_result = stderr.readlines() print("执行结果: ", "exe_command: ", command, "exe_result: ", exe_result, "exe_err_result: ", exe_err_result) print("<" * 200) if 0 != stdout.channel.recv_exit_status(): raise common_service.MyServiceException("执行远程命令(%s)失败" % command) return exe_result, stdin
def put_alarm_config_file(): try: # 入参 request_data = common_service.check_request_dat_not_null( ["config_file", "name"]) file_content = request_data["config_file"] if not file_content or file_content.strip() == "": raise common_service.MyServiceException("文件内容不能为空") name = request_data["name"] # 查询alarm文件位置 alarm_config_data = json.loads(alarm_config.get()) alarm_config_data_obj = alarm_config_data[0] level_data_alarm__config = alarm_config_data_obj["alarm__config"] level_data_component = level_data_alarm__config["component"] level_data_name = level_data_component[name] config_path = level_data_name["config_path"] # 文件位置 # 得到prometheus server 服务器的连接信息 prometheus_server_config = json.loads( get())[0]["monitoring__config"]["prometheus_server"] server_ip_tag = prometheus_server_config["server"] config_dirpath = prometheus_server_config["config_dirpath"] load_config_file_command = prometheus_server_config[ "load_config_file_command"] # 修改prometheus server 服务器中的文件 local_dirs = ["rule_files"] local_file_path = config_path target_file_path = config_dirpath + "/" + config_path service_type = "monitoring" modify_server_file(service_type, local_dirs, local_file_path, target_file_path, server_ip_tag, file_content, load_config_file_command) # 更新数据库数据 monitoring__alarm_config_file_co.insert(request_data) return {} except common_service.MyServiceException as e: print(e) return common_service.ResResult.return500(str(e))
def put_config_file(request_data=None): # 校验入参 try: if not request_data: request_data = common_service.check_request_dat_not_null( ["config_file"]) config_file = request_data["config_file"] if not config_file or config_file.strip() == "": raise common_service.MyServiceException("config_file不能为空") # 加载prometheus server服务器 prometheus_server_config = json.loads( get())[0]["monitoring__config"]["prometheus_server"] server = prometheus_server_config["server"] config_filepath = prometheus_server_config["config_filepath"] load_config_file_command = prometheus_server_config[ "load_config_file_command"] # 推断数据 config_dir_path = config_filepath[0:config_filepath.rfind("/")] # 生成本地文件 local_monitoring_path = project_root_path + "/" + "temp" + "/" + "monitoring" if not os.path.exists(local_monitoring_path): os.mkdir(local_monitoring_path) local_monitoring_config_file_path = project_root_path + "/" + "temp" + "/" + "monitoring" + "/" + "config_file" if not os.path.exists(local_monitoring_config_file_path): os.mkdir(local_monitoring_config_file_path) cur_file_name = "prometheus-%s.yml" % datetime.datetime.strftime( datetime.datetime.now(), '%Y%m%d_%H%M_%S') local_prometheus_yml = local_monitoring_config_file_path + "/" + cur_file_name local_file = open(local_prometheus_yml, "w", encoding="utf-8") local_file.write(config_file) local_file.flush() local_file.close() # 修改远程服务器的文件内容 filepath_new_backup = config_dir_path + "/" + cur_file_name filepath_old_backup = config_dir_path + "/" + "prometheus-old-backup.yml" filepath = config_filepath # 获取服务器连接信息 server_conn_info = json.loads( server_manage.get_decrypt())[0]["server_manage"][server] server_ip = server.replace("_", ".") server_username = list(server_conn_info.keys())[0] server_password = server_conn_info[server_username] my_server = MyServer(server_ip, server_username, server_password) # # 备份文件 my_server.backup_file(filepath, filepath_old_backup) # # 上传文件 my_server.upload_local_file(local_prometheus_yml, filepath_new_backup) # # 移动文件 my_server.move_file(filepath_new_backup, filepath) # 运行加载配置文件的指令 my_server.exe_command(load_config_file_command) request_data = common_service.clear_id(request_data) monitoring__config_file_co.insert(request_data) return {} except common_service.MyServiceException as e: print(e) return common_service.ResResult.return500(str(e))
def request_schedule_work_order(): try: # 之前配置的 old_crontab_file_content_list = get_crontab_file_content_list() request_data = common_service.check_request_dat_not_null(["env", "host", "process_template_id"]) title = request_data["title"] env = request_data["env"] process_template_id = request_data["process_template_id"] host_original = request_data["host"] modify_schedule_line_req = request_data["modify_schedule_line"] apply_process = request_data["apply_process"] host = host_original.replace(".", "_") # 解析数据 modify_schedule_line_display = "" log = "为回滚做好准备,具体内容为: <br/>" is_has_data = False old_modify_schedule_line = {} modify_schedule_line = {} for item in modify_schedule_line_req: is_has_data = True line_content = modify_schedule_line_req[item] old_line_content = old_crontab_file_content_list[int(item)] old_modify_schedule_line[str(item)] = old_line_content modify_schedule_line_display += "-" * 100 modify_schedule_line_display += "修改第%s行:\n %s \n==>\n %s\n" % (item, old_line_content, line_content) log += "还原第%s行:<br/> %s <br/>==><br/> %s<br/>" % (item, line_content, old_line_content) modify_schedule_line[str(item)] = line_content if not is_has_data: raise common_service.MyServiceException("需要修改的计划任务的内容不能为空") # 加锁 一个服务同时只能有一个流程在跑,否则就会造成数据混乱 is_locked = True is_exist_host_lock_data = False project_manage__schedule__schedule_work_order__lock_data = json.loads( dumps(project_manage__schedule__schedule_work_order__lock_co.find_one( filter={'host': host}))) if project_manage__schedule__schedule_work_order__lock_data: is_exist_host_lock_data = True process_id = project_manage__schedule__schedule_work_order__lock_data["process_id"] project_manage__work_order_process__run_manage_data = project_manage__work_order_process__run_manage_co \ .find_one(filter={'_id': ObjectId(process_id)}) if project_manage__work_order_process__run_manage_data and \ project_manage__work_order_process__run_manage_data["status"] != "RUNNING": is_locked = False else: is_locked = False if is_locked: raise common_service.MyServiceException("操作失败,修改服务器的计划任务文件的操作只能同时只有一个流程在跑,否则就会造成数据混乱") # 判断时间 # 流程 finish_result = { "method": "POST", "url": "http://localhost:8080/project_manage/schedule/schedule_work_order/finish_process", "rb_url": "http://localhost:8080/project_manage/schedule/schedule_work_order/rollback_process", } display_content = "执行环境为: %s \n 调度服务器为: %s \n 修改调度的内容为: \n %s \n\n\n " % ( env, host, modify_schedule_line_display) process_id, next_step = run_manage.process_start(apply_process, process_type, finish_result, title, display_content, env, process_template_id, system_tag=host_original) # 存储内容等待流程完成时去执行 project_manage__work_order_process__run_manage_process__schedule_work_order_co.insert_one({ "process_id": process_id, "env": env, "host": host, "modify_schedule_line": modify_schedule_line }) # 生成备份文件 project_manage__work_order_process__run_manage_process__schedule_work_order_rb_co.insert_one({ "process_id": process_id, "env": env, "host": host, "modify_schedule_line": old_modify_schedule_line, }) # 插入日志 project_manage__work_order_process__run_manage_result_log_co.insert_one({ "process_id": process_id, "log": log, "is_exe_success": True, }) # 更新锁的流程 if is_exist_host_lock_data: project_manage__schedule__schedule_work_order__lock_co.update_one(filter={'host': host}, update={'$set': { "process_id": process_id }}) else: project_manage__schedule__schedule_work_order__lock_co.insert_one({"host": host, "process_id": process_id}) run_manage.process_after_trigger(process_id, next_step) return {} except common_service.MyServiceException as e: return common_service.ResResult.return500(str(e))
def do_get_process_template(env, process_template_id): # 获取该模板对应的值 process_template_level_top = json.loads(get()) process_template_level_second = process_template_level_top[0] process_template_level_data = process_template_level_second[ "process_template"] if not process_template_level_data.__contains__(env): raise common_service.MyServiceException("该流程模板环境(%s)不存在" % env) process_template_level_env = process_template_level_data[env] if not process_template_level_env.__contains__(process_template_id): raise common_service.MyServiceException("该流程模板id(%s)不存在" % process_template_id) process_template = process_template_level_env[process_template_id] process_template = process_template.strip() if not process_template or process_template.strip() == "": raise common_service.MyServiceException("该流程模板值为空") # 当前登录的用户 cur_user_id = session.get("user_id") # 生成流程 process_template_list = process_template.split(">") # 为方便使用,倒序 process_template_list = list(reversed(process_template_list)) res_result = [] is_already_in_process = False # 当前登录人的产品标签 cur_user_label_list = access_control.get_user_label_list(cur_user_id) for index, item in enumerate(process_template_list): if is_already_in_process: break res_result_item = [] item = item.strip() group_struct_has_user = json.loads( access_control.get_group_struct_has_user(item)) # 判断该角色是否包含当前登录人 for group_struct_has_user_index, group_struct_has_user_item in enumerate( group_struct_has_user): user_nick = group_struct_has_user_item["nick"] user_label = group_struct_has_user_item["label"] user_open_id = group_struct_has_user_item["user_open_id"] if cur_user_id == user_open_id: is_already_in_process = True res_result_item = [{"openid": user_open_id, "nick": user_nick}] break else: # 流程开始的第一个人必须是当前登录人 if index == len(process_template_list ) - 1 and group_struct_has_user_index == len( group_struct_has_user) - 1: raise common_service.MyServiceException( "流程开始的第一个人必须是当前登录人") if SPECIAL_ALLOW_STR not in user_label: if len(cur_user_label_list) != len(user_label): continue for cur_user_label_list_item in cur_user_label_list: if cur_user_label_list_item not in user_label: continue res_result_item.append({ "openid": user_open_id, "nick": user_nick }) # 判断流程是否至少有一个人 if len(res_result_item) < 1: raise common_service.MyServiceException( "当前用户拥有的流程在 %s 流程节点上 没有一个人" % item) res_result.append(res_result_item) if is_already_in_process: continue # 判断整体流程至少含有一个人 if len(res_result) < 1: raise common_service.MyServiceException("当前登录人所需要的流程至少需要一个人") return res_result
def check_input_process_in_process_template(input_process, env, process_template_id): process_template_data = do_get_process_template(env, process_template_id) all_process = gen_process_step_str(process_template_data) if input_process not in all_process: raise common_service.MyServiceException("非法入侵,非法流程")