def setting_main(): if request.method == 'GET': data = request.args data = query_portal_label_info(data['portal_label']) return Result.success_response(data=data, msg='查询成功') elif request.method == 'POST': data_dict = request.get_json() save_portal_label_info(data_dict['form']) return Result.success_response(msg='保存成功')
def soft_package(): if request.method == 'GET': data = request.args return_data = get_software_package() return Result.success_response(data=return_data, msg='上传成功') elif request.method == 'POST': f_obj = request.files['file'] data_dict = request.form.to_dict() current_app_ = current_app soft_path = os.path.join(current_app_.config.xlautoenv['PROJECT_PATH'], current_app_.config['PACKAGE'], 'software', data_dict['software_name'], data_dict['software_versions']) if not os.path.exists(soft_path): os.makedirs(soft_path) script_path_file = os.path.join(soft_path, f_obj.filename) f_obj.save(script_path_file) data_dict['package_path'] = soft_path # 解压包文件 unzip_info = unzip_distinguish( data_dict['software_package_zipanalysis_type'], script_path_file, soft_path) if not ReturnG.if_ft(unzip_info): Result.fail_response(msg=unzip_info[1]) # 包信息入库 save_software_package(data_dict) return Result.success_response(msg='上传成功') elif request.method == 'DELETE': data_dict = request.get_json() software_package_info = data_dict['software_package_info'] del_list = [] path_list = [] for software_package_info_one in software_package_info: del_list.append(software_package_info_one['software_package_id']) if os.path.isdir(software_package_info_one['package_path']): path_list.append(software_package_info_one['package_path']) for path_list_one in path_list: shutil.rmtree(path_list_one) del_software_package(del_list) return Result.success_response(msg='删除成功')
def setting_main_zabbix(): if request.method == 'POST': data_dict = request.get_json() data_dict_form = data_dict['form'] zabbix_setting_control_status = zabbix_setting_control(current_app) if zabbix_setting_control_status != True: Result.fail_response(msg="保存失败:%s" % zabbix_setting_control_status[1]) save_portal_label_info(data_dict_form) return Result.success_response(msg='保存成功')
def script_execute_query_history(): script_file_name = request.args.get('script_file_name') if script_file_name: sql_fragment = "WHERE b.script_file_name LIKE :script_file_name" else: sql_fragment = '' sql = """ SELECT a.script_file_execute_event_id, a.script_execute_event_batch_id, a.execute_time, a.execute_end_time, a.script_file_id, a.script_file_content, b.script_file_name, c.code_name AS script_file_execute_result_text FROM script_file_execute_event a LEFT JOIN script_file_cabinet b ON a.script_file_id = b.script_file_id LEFT JOIN sys_code c ON c.code_key = a.execute_result AND c.code_type = "standard_execution_results" ORDER BY execute_time DESC %s """ % sql_fragment sqla = Sqla(current_app) data = sqla.fetch_to_dict( sql, {'script_file_name': '%%%s%%' % script_file_name}) return Result.success_response(data=data, msg='查询成功')
def update_script_query(): script_file_group = request.args.get('script_file_group') script_file_type = request.args.get('script_file_type') if script_file_group: sql_file_group, sql_file_group_null = '%%%s%%' % script_file_group, '' else: sql_file_group, sql_file_group_null = '%', 'OR a.script_file_type IS NULL' if script_file_type: sql_file_type, sql_file_type_null = '%%%s%%' % script_file_type, '' else: sql_file_type, sql_file_type_null = '%', 'OR a.script_file_group IS NULL' sql_parameter = { "script_file_group": sql_file_group, "script_file_type": sql_file_type } sql = """ SELECT a.`*`,b.code_name AS script_file_type_text,c.code_name AS script_file_group_text FROM script_file_cabinet a LEFT JOIN sys_code c ON a.script_file_group = c.code_key AND c.code_type = 'execute_script_group' LEFT JOIN sys_code b ON a.script_file_type = b.code_key AND b.code_type = 'execute_script_type' WHERE (a.script_file_type LIKE :script_file_type %s) AND (a.script_file_group LIKE :script_file_group %s) """ % (sql_file_type_null, sql_file_group_null) sqla = Sqla(current_app) data = sqla.fetch_to_dict(sql, sql_parameter) # update_script_obj = db.session.query(ScriptFileCabinet.id, ScriptFileCabinet.script_file_path, DocumentCabinet.script_file_name, # ScriptFileCabinet.script_file_group, ScriptFileCabinet.script_file_type, DocumentCabinet.comment, ).all() # db.session.close() # project_data = model_to_dict(update_script_obj) return Result.success_response(data=data, msg='查询成功')
def zabbix_agents_install(): if request.method == 'GET': """ 获取已存Zabbix信息 """ data = request.args host_ids_list = data['host_ids'].split(",") zabbix_agent_info = get_zabbix_agent_info(host_ids_list) return Result.success_response(data=zabbix_agent_info, msg="查询成功") if request.method == 'POST': """ 同步Zabbix信息 """ data = request.get_json() host_info = get_host_info(data['host_ids']) conjecture_zabbix_host_name_list = [] for host_info_one in host_info: conjecture_zabbix_host_name = "%s.%s" % ( host_info_one['host_ip'], host_info_one['project_code']) conjecture_zabbix_host_name_list.append( conjecture_zabbix_host_name) host_info_one['zabbix_host_name'] = conjecture_zabbix_host_name #向zabbix请求同步数据 z_c = ZabbixCollect() zabbix_info_host = z_c.get_host(conjecture_zabbix_host_name_list) #新建同步数据,清理已经不存在的数据 delete_zabbix_agent(data['host_ids']) update_zabbix_agent(zabbix_info_host, host_info) #查询最新的数据返回前端 zabbix_agent_info = get_zabbix_agent_info(data['host_ids']) return Result.success_response(data=zabbix_agent_info, msg="同步成功") if request.method == 'PUT': """ 安装并记录Zabbix信息 """ data = request.get_json() return Result.success_response(data=data, msg="安装成功")
def software_conf_parameter_analysis(): if request.method == 'GET': # data = request.args # return_data = get_software_conf_parameter(data['software_conf_id']) return Result.success_response(data='', msg='参数查询成功') elif request.method == 'POST': data_dict = request.get_json() software_conf_info = data_dict['software_conf_info'] conf = Configuration() software_conf_parameter_list = [] for software_conf_info_one in software_conf_info: if data_dict['software_conf_type'] == "template": software_conf_path = software_conf_info_one[ 'software_conf_path'] software_conf_parameter = conf.template_conf_analysis( software_conf_path) software_conf_parameter_dict_list = [] for software_conf_parameter_one in software_conf_parameter: software_conf_parameter_dict = { 'replacement_entry': software_conf_parameter_one, 'default_value': '' } software_conf_parameter_dict_list.append( software_conf_parameter_dict) software_conf_parameter_list.append({ 'software_conf_id': software_conf_info_one['software_conf_id'], 'software_conf_parameter': software_conf_parameter_dict_list }) # save_software_conf_parameter(software_conf_ids) # for software_conf_info_one in software_conf_info: # try: # conf_content = conf.ini_conf_analysis(software_conf_info_one['software_conf_path']) # except Exception as e: # return Result.fail_response(msg='解析失败:%s' %e) return Result.success_response(data=software_conf_parameter_list, msg='解析成功')
def hosts_add_host(): data_dict = request.get_json() if not data_dict.get('user_role'): user_role = 'root' else: user_role = data_dict['user_role'] if not data_dict.get('host_port'): host_port = '22' else: host_port = data_dict['host_port'] if not data_dict['hosts_input_text'] : return Result.fail_response(msg="不支持的IP输入") ip_c = Ip_c() ip_list = ip_c.string_formatting(data_dict['hosts_input_text']) if not ip_list: return Result.fail_response(msg="IP格式异常") sqla = Sqla(current_app) for ip_one in ip_list: host_obj = HostInstance(host_ip=ip_one, host_project=data_dict['host_project'], host_type_key=data_dict['host_type'], host_port=host_port ) db.session.add(host_obj) db.session.flush() host_id = host_obj.host_id user_pass = sqla.sql_encryption(data_dict['user_pass']) user_obj = HostUser(host_id=host_id, user_name=data_dict['user_name'], user_pass=user_pass, user_role=user_role ) db.session.add(user_obj) db.session.commit() db.session.close() db.session.remove() return Result.success_response(msg='新增成功')
def home_menu_query(): sys_menu = db.session.query(SysMenu.id, SysMenu.parent_id, SysMenu.name, SysMenu.icon, SysMenu.title).all() db.session.close() db.session.remove() sys_menu_data = model_to_dict(sys_menu) data = list_to_tree(sys_menu_data) return Result.success_response(data, '菜单加载成功')
def system_action_query(): system_action = request.args.get('system_action') sys_code_data = db.session.query(SystemFunction.system_function_id, SystemFunction.system_action_name).filter( SystemFunction.system_action == system_action).all() db.session.close() db.session.remove() data = model_to_dict(sys_code_data) return Result.success_response(data,'类型查询成功')
def code_query(): code_type = request.args.get('code_type') sys_code_data = db.session.query(SysCode.code_id, SysCode.code_key, SysCode.code_name).filter( SysCode.code_type == code_type).all() db.session.close() db.session.remove() data = model_to_dict(sys_code_data) return Result.success_response(data,'类型查询成功')
def software_conf(): if request.method == 'GET': data = request.args return_data = get_software_conf(data['software_package_id']) return Result.success_response(data=return_data, msg='配置查询成功') elif request.method == 'POST': data_dict = request.get_json() software_conf_data = data_dict['software_conf_data'] save_software_conf(software_conf_data) return Result.success_response(msg='保存成功') elif request.method == 'DELETE': data_dict = request.get_json() software_conf_ids = data_dict['software_conf_ids'] software_conf_del(software_conf_ids) return Result.success_response(msg='删除成功')
def kubernetes_install(): configuration_info = request.get_json() configuration_info[ 'firewalld'] = 0 if configuration_info['firewalld'] == False else 1 configuration_info[ 'selinux'] = 0 if configuration_info['selinux'] == False else 1 configuration_info[ 'powerboot'] = 0 if configuration_info['powerboot'] == False else 1 sqla = Sqla(current_app) sql = """ SELECT * FROM system_function a WHERE a.system_name = :system_name AND a.system_version = :system_version AND ( (a.system_action = "firewalld_control" AND a.action_service_switch = :firewalld) OR (a.system_action = "selinux_control" AND a.action_service_switch = :selinux) OR (a.system_function_id = :repository) OR (a.system_function_id = 8) OR (a.system_action = "docker_repository") OR (a.system_action = "yum_install_docker") OR (a.system_action = "enable_docker" AND a.action_service_switch = :powerboot) OR (a.system_action = "enable_kubelet" AND a.action_service_switch = :powerboot) OR (a.system_action = "start_docker" AND a.action_service_switch = :powerboot) OR (a.system_action = "start_kubelet" AND a.action_service_switch = :powerboot) ) order by a.order_by desc """ host_execute_info = sqla.fetch_to_dict( sql, { 'system_name': configuration_info['system_name'], 'system_version': configuration_info['system_version'], 'firewalld': configuration_info['firewalld'], 'selinux': configuration_info['selinux'], 'repository': configuration_info['repository'], 'powerboot': configuration_info['powerboot'] }) system_function_ids = [] for host_execute_info_one in host_execute_info: system_function_ids.append(host_execute_info_one['system_function_id']) host_user_infos = [] for host_id in configuration_info['host_ids']: host_user_info = hosts.get_hotst_connect_info(host_id) host_user_infos.append(host_user_info) host_action_execute(host_user_infos, system_function_ids) return Result.success_response(msg='提交安装申请成功,正在处理')
def install(self, name, othe_parameter): result = [] install_cmd = "yum install -y %s" % name pool = Pool(5) for i in range(len(othe_parameter["hosts"])): result.append( pool.apply_async(func=self.ssh_d, args=(othe_parameter['hosts'][i], install_cmd)).get()) pool.close() pool.join() # test = self.ssh_d(othe_parameter['hosts'][0],install_cmd) return Result.success_response(result)
def hosts_del_host(): del_host_info = request.json.get('del_host_info') # delete_script_obj = HostInstance.query.filter(HostInstance.host_id.in_(del_host_info)).all() # delete_script_obj.is_remove = 1 db.session.query(HostInstance).filter(HostInstance.host_id.in_(del_host_info)).update({"is_remove": 1},synchronize_session='fetch') db.session.commit() db.session.close() db.session.remove() return Result.success_response(msg='删除成功')
def software_conf(): if request.method == 'GET': data = request.args software_package_id = data['software_package_id'] soft_obj = get_software_package_one(software_package_id) if soft_obj: return_data = { 'package_path': soft_obj['package_path'].replace('\\', '/') + '/', 'package_path_dir_list': get_dir_file(soft_obj['package_path']) } return Result.success_response(data=return_data, msg='查询成功') else: return Result.fail_response(data=[], msg='查询成功') elif request.method == 'POST': f_obj = request.files['file'] data_dict = request.form.to_dict() return Result.success_response(msg='保存成功')
def edit_script(): data_dict = request.get_json() delete_script_obj = ScriptFileCabinet.query.filter_by( script_file_id=data_dict['script_file_id']).first() delete_script_obj.script_file_group = data_dict['script_file_group'] delete_script_obj.script_file_type = data_dict['script_file_type'] db.session.commit() db.session.close() db.session.remove() return Result.success_response(msg='修改成功')
def home_projects_query(): project_obj = db.session.query( Project.project_id, Project.project_name, Project.project_code, Project.controller_ip, Project.order_id, Project.comments, ).all() db.session.close() db.session.remove() project_data = model_to_dict(project_obj, onedict=False) return Result.success_response(project_data, '项目查询成功')
def home_hosts_query(): parameter_dict = request.args.to_dict() data = home_hosts_query_filter(parameter_dict) # host_obj = db.session.query(HostInstance.host_id, HostInstance.host_ip, # HostInstance.host_name,SysCode.code_name.label('type_name')).join( # HostInstance,HostInstance.host_type == SysCode.code_key).filter( # SysCode.code_type == "host_type")\ # .all() # data = model_to_dict(host_obj) #db.session.close() return Result.success_response(data=data[0], msg='目标主机加载成功', count=data[1])
def execute_script(): data_dict = request.get_json() hosts_table_data = data_dict['hosts_table_data'] timeout = data_dict.get('execute_timeout') if not timeout: timeout = 3600 #生成执行批次ID script_execute_event_batch_id = str(uuid.uuid1()) for hosts_table_data_one in hosts_table_data: existing_script_total_list = hosts_table_data_one.get( 'existing_script_total') history_script_total_list = hosts_table_data_one.get( 'history_script_total') temporary_script_total_str = hosts_table_data_one.get( 'temporary_script_total') #获取主机信息 host_id = hosts_table_data_one['host_id'] info_dict = get_hotst_connect_info(host_id) info_dict[ 'script_execute_event_batch_id'] = script_execute_event_batch_id #拼接此主机下各类脚本信息 script_total_list = [] if existing_script_total_list: script_total_list += existing_script_total_list if history_script_total_list: script_total_list += history_script_total_list if temporary_script_total_str: for temporary_script_total_str_one in temporary_script_total_str: temporary_script_total_str_one[ 'script_file_path'] = str_save_file( temporary_script_total_str_one['script_file_content']) script_total_list += temporary_script_total_str xlauto = current_app._get_current_object() # run_script_worker(info_dict, script_total_list, timeout, xlauto) p = threading.Thread(target=run_script_worker, args=(info_dict, script_total_list, timeout, xlauto)) p.start() return Result.success_response(data=script_execute_event_batch_id, msg='请求成功')
def nmap(): data_dict = request.get_json() return_dict = {"succeed": {}, "defeated": {}} ip_c = Ip_c() ip_list = ip_c.string_formatting(data_dict['ips']) for host in ip_list: for port in data_dict['post'].split(','): if get_ip_status(host, port): return_dict["succeed"][host] = port else: return_dict["defeated"][host] = port return Result.success_response(return_dict)
def rm_script(): id = request.json.get('script_file_id') delete_script_obj = ScriptFileCabinet.query.filter( ScriptFileCabinet.script_file_id == id).first() try: os.remove(delete_script_obj.script_file_path) except FileNotFoundError as e: current_app.logger.warning(e) db.session.delete(delete_script_obj) db.session.commit() db.session.close() db.session.remove() return Result.success_response(msg='删除成功')
def update_script_file(): f_obj = request.files['file'] current_app_ = current_app script_path = os.path.join(current_app_.config.xlautoenv['PROJECT_PATH'], current_app_.config['DATA_PATH'], 'scriptfiles') if not os.path.exists(script_path): os.makedirs(script_path) script_path_file = os.path.join(script_path, str(uuid.uuid1())) file_sql_obj = ScriptFileCabinet(script_file_path=script_path_file, script_file_name=f_obj.filename) db.session.add(file_sql_obj) db.session.commit() db.session.close() db.session.remove() f_obj.save(script_path_file) return Result.success_response(msg='上传成功')
def zabbix_templates(): if request.method == 'GET': z_c = ZabbixCollect() info = z_c.get_template() return Result.success_response(data=info)
def zabbix_host_groups(): if request.method == 'GET': z_c = ZabbixCollect() info = z_c.get_host_group() return Result.success_response(data=info)