def download_worker_driver(request, url, filepath): reqpath = requests.get(url, verify=False) if not status.is_success(reqpath.status_code): SaveOperationLog( request.user, OperationLog.TYPE_UPDATE_AIO_DRIVE, json.dumps( { '失败': '获取驱动下载路径失败,状态码:{} 路径:{}'.format(reqpath.status_code, url) }, ensure_ascii=False), get_operator(request)) _logger.error('获取驱动下载路径失败,状态码:{} 路径:{}'.format(reqpath.status_code, url)) return content = reqpath.content.decode('utf-8') content = json.loads(content) if content['r'] != 0: SaveOperationLog( request.user, OperationLog.TYPE_UPDATE_AIO_DRIVE, json.dumps( { '失败': '获取驱动下载路径失败,状态码:{}, 路径:{}'.format(reqpath.status_code, url), '原因': '{}'.format(content['e']) }, ensure_ascii=False), get_operator(request)) _logger.error('获取驱动下载路径失败,状态码:{}, 路径:{}, 原因:{}'.format( reqpath.status_code, url, content['e'])) return downurl = content['url'] download_worker(request, downurl, filepath)
def start_file_sync(request): paramsQD = request.GET planids = paramsQD.get('taskid', '') # '4,5,6' if not planids: return HttpResponse('{"r": "1", "e": "请求参数缺失:taskid"}') err = list() for planid in planids.split(','): planStat = FileSyncScheduleViews().get(request, planid).data is_enable = planStat['enabled'] if is_enable: rsp = FileSyncScheduleExecute().post(request=request, api_request={'schedule': planStat['id']}) if rsp.status_code == status.HTTP_201_CREATED: desc = {'操作': '执行导出计划', '计划名称': planStat['name'], "操作结果": "已发送命令"} SaveOperationLog( request.user, OperationLog.BACKUP_EXPORT, json.dumps(desc, ensure_ascii=False), get_operator(request)) else: err.append({'name': planStat['name'], 'e': rsp.data}) mylog = {'操作': '执行导出计划', '计划名称': planStat['name'], "操作结果": "执行失败{}".format(rsp.data)} SaveOperationLog( request.user, OperationLog.BACKUP_EXPORT, json.dumps(mylog, ensure_ascii=False), get_operator(request)) else: err.append({'name': planStat['name'], 'e': '已禁用'}) mylog = {'操作': '执行导出计划务', '计划名称': planStat['name'], "操作结果": "计划已禁用,执行失败"} SaveOperationLog( request.user, OperationLog.BACKUP_EXPORT, json.dumps(mylog, ensure_ascii=False), get_operator(request)) if len(err) > 0: return HttpResponse( json.dumps({"r": 1, "e": "共有{}个计划执行失败".format(len(err)), "err": err}, ensure_ascii=False)) return HttpResponse('{"r": "0","e": "操作成功"}')
def add_to_task(request): task_id = int(request.POST.get('task_id', 0)) host_ids = json.loads(request.POST.get('host_ids')) result = {'r': 0, 'e': '操作成功'} host_name_list = list() task = AutoVerifySchedule.objects.get(id=task_id) task.hosts.clear() task.host_groups.clear() for host_id in host_ids: if host_id.startswith('group_'): host_group = HostGroup.objects.get(id=host_id[6:]) host_name_list.append(host_group.name) task.host_groups.add(host_group) else: host = Host.objects.get(ident=host_id) host_name_list.append(host.name) task.hosts.add(host) desc = {'操作': '加入自动验证计划', '客户端名称': host_name_list, '自动验证计划名称': task.name} SaveOperationLog(request.user, OperationLog.TYPE_AUTO_VERIFY_TASK, json.dumps(desc, ensure_ascii=False), get_operator(request)) return HttpResponse(json.dumps(result, ensure_ascii=False))
def create_template(request): params = request.POST name = params['name'] desc = params['desc'] pointid = params.get('pointid', None) # 不一定有 snapshot_datetime = params.get('snapshot_datetime', None) # 不一定有 if pointid: pointid = pointid.split('|')[1] api_request = { 'name': name, 'desc': desc, 'host_snapshot_id': pointid, 'snapshot_datetime': snapshot_datetime } rsp = DeployTemplateCURD().post(request, api_request) if status.is_success(rsp.status_code): mylog = { '操作': '创建模板', '模板名称': name, "模板ID": rsp.data['id'], "操作结果": "创建成功" } SaveOperationLog(request.user, OperationLog.TYPE_TEMPLATE, json.dumps(mylog, ensure_ascii=False), get_operator(request)) return HttpResponse(json.dumps({'r': 0, 'e': ''})) else: return HttpResponse( json.dumps({ 'r': 1, 'e': rsp.data if rsp.data else '内部错误41' })) else: return HttpResponse(json.dumps({'r': 1, 'e': '内部错误42'}))
def modify(request): rsp = DeployTemplateCURD().put( request, { 'name': request.POST['name'], 'desc': request.POST['desc'], 'id': request.POST['id'] }) if status.is_success(rsp.status_code): mylog = { '操作': '编辑模板', '模板名称': rsp.data['name'], '模板描述': rsp.data['desc'], "模板ID": rsp.data['id'], "操作结果": "创建成功" } SaveOperationLog(request.user, OperationLog.TYPE_TEMPLATE, json.dumps(mylog, ensure_ascii=False), get_operator(request)) return HttpResponse('{"r": "0","e": "操作成功"}') else: return HttpResponse( json.dumps({ 'r': 1, 'e': rsp.data if rsp.data else '内部错误170' }))
def renameServer(request): ident = request.POST.get('id', 'none') display_name = request.POST.get('name', 'none') encipher = request.POST.get('encipher', '2') orgname = request.POST.get('orgname') systemname = request.POST.get('systemname') if display_name == 'none': return HttpResponse('{"r":"1","e":"名称不能为空"}') data = {"display_name": display_name, "network_transmission_type": encipher} data['orgname'] = orgname data['systemname'] = systemname myrequest = Obj() myrequest.__setattr__("data", data) hostinfoObj = HostInfo() oldname = hostinfoObj.get(request, ident).data['name'] oldencipher = hostinfoObj.get(request, ident).data['network_transmission_type'] desc = {'操作': '编辑客户端', '内容': {'旧名称': oldname, '新名称': display_name}} r = 0 if int(oldencipher) != int(encipher): desc['数据传输方式'] = '未加密' if int(encipher) == 2 else '加密' breakoffclient(ident) resp = hostinfoObj.put(myrequest, ident) if resp.status_code == status.HTTP_202_ACCEPTED: SaveOperationLog( request.user, OperationLog.TYPE_SERVER, json.dumps(desc, ensure_ascii=False), get_operator(request)) return HttpResponse(json.dumps({"r": r, "e": "操作成功", "id": ident}, ensure_ascii=False)) return HttpResponse('{"r":"1","e":"操作失败"}')
def run(self): from xdashboard.handle.authorize.authorize_init import get_separation_of_the_three_members from xdashboard.handle.logserver import SaveOperationLog from xdashboard.models import OperationLog, Email from django.contrib.auth.models import User from xdashboard.common.smtp import send_email i = 0 while self.run_status: if not get_separation_of_the_three_members( ).is_database_use_policy(): break is_full, database_can_use_size, database_used_bytes = is_database_full( ) if is_full: user = User.objects.get(username='******') database_alert_log = '日志数据存储空间(剩余5%)。分配空间:{}MB,已使用空间:{:.2f}MB。'.format( database_can_use_size, database_used_bytes / 1024**2) _logger.debug( 'database_alert_log={}'.format(database_alert_log)) SaveOperationLog( user, OperationLog.TYPE_QUOTA, json.dumps({"日志空间告警": database_alert_log}, ensure_ascii=False)) exc_inf = dict() exc_inf['user_id'] = user.id exc_inf['content'] = database_alert_log exc_inf['sub'] = '日志空间告警' send_email(Email.LOG_ALERT, exc_inf) time.sleep(1 * 24 * 60 * 60) time.sleep(300) _logger.debug('databaseSpaceAlertThread exit')
def disablePlan(request): paramsQD = request.GET planids = paramsQD.get('taskid', '') # '4,5,6' # enabled_yun = paramsQD.get('enabled_yun', '') user = request.user if not planids: return HttpResponse('{"r": "1", "e": "请求参数缺失:taskid"}') _failed_enable = list() for planid in planids.split(','): planStat = FileSyncScheduleViews().get(request, planid).data is_enable = planStat['enabled'] host_name = planStat['host']['name'] plan_name = planStat['name'] if is_enable: # 该计划当前"启用状态" api_request = {'enabled': False} desc = {'操作': '启用/禁用任务', '任务ID': planid, '状态': '禁用成功', '主机名称': host_name, '计划名称': plan_name} else: # 该计划当前"禁用状态" api_request = {'enabled': True} desc = {'操作': '启用/禁用任务', '任务ID': planid, '状态': '启用成功', '主机名称': host_name, '计划名称': plan_name} # 从yun过来的操作 # if enabled_yun != '': # api_request = {'enabled': enabled_yun} # desc = {'操作': '启用/禁用任务', '任务ID': planid, '状态': '启用成功' if enabled_yun else '禁用成功', '主机名称': host_name, # '计划名称': plan_name} resp = FileSyncScheduleViews().put(request, planid, api_request) if resp.status_code == status.HTTP_202_ACCEPTED: SaveOperationLog( request.user, OperationLog.BACKUP_EXPORT, json.dumps(desc, ensure_ascii=False), get_operator(request)) return HttpResponse('{"r": "0","e": "操作成功"}')
def update_callback(user, logfilename, bsuccess, reason, operator): if bsuccess: endlog = {'升级': '从文件{}执行升级脚本'.format(logfilename)} else: endlog = {'结束': '从文件{}更新失败。{}'.format(logfilename, reason)} SaveOperationLog(user, OperationLog.TYPE_UPDATE_AIO_BASE, json.dumps(endlog, ensure_ascii=False), operator)
def delPlans(request): paramsQD = request.GET planids = paramsQD.get('taskid', '') # '4,5,6' for planid in planids.split(','): DeployTemplateCURD().delete(request, {'id': planid}) desc = {'操作': '删除模板', '模板ID': planids.split(',')} SaveOperationLog(request.user, OperationLog.TYPE_TEMPLATE, json.dumps(desc, ensure_ascii=False), get_operator(request)) return HttpResponse('{"r": "0","e": "操作成功"}')
def delImportPlans(request): paramsQD = request.GET planids = paramsQD.get('taskid', '') # '4,5,6' err_list = list() for planid in planids.split(','): rsp = json.loads(ImportTaskExecute().delete(request, planid).data) if rsp['r'] != 0: err_list.append(planid) desc = {'操作': '删除导入任务', '任务ID': planids.split(',')} SaveOperationLog(request.user, OperationLog.BACKUP_EXPORT, json.dumps(desc, ensure_ascii=False), get_operator(request)) if len(err_list) > 0: return HttpResponse('{"r": "1","e": "任务正在执行中,请先取消该任务。"}') return HttpResponse('{"r": "0","e": "操作成功"}')
def enabletask(request): task_ids = request.GET.get('task_id', 0) result = {'r': 0, 'e': '操作成功'} task_name_list = list() for task_id in task_ids.split(','): task = AutoVerifySchedule.objects.filter(id=task_id) task_name_list.append(task.first().name) task.update(enabled=True) desc = {'操作': '启用自动验证计划', '自动验证计划名称': ','.join(task_name_list)} SaveOperationLog(request.user, OperationLog.TYPE_AUTO_VERIFY_TASK, json.dumps(desc, ensure_ascii=False), get_operator(request)) return HttpResponse(json.dumps(result, ensure_ascii=False))
def delPlans(request): paramsQD = request.GET planids = paramsQD.get('taskid', '') # '4,5,6' host_ident = paramsQD.get('host_ident', None) # 存在就删除主机所有计划 if host_ident: user_plans = FileSyncSchedule.objects.filter(enabled=True, deleted=False, host__ident=host_ident) planids = ','.join([str(plan.id) for plan in user_plans]) if not planids: return HttpResponse('{"r": "0","e": "操作成功"}') if not planids: return HttpResponse('{"r": "1", "e": "请求参数缺失:taskid"}') for planid in planids.split(','): # delete_shell_zip(planid) FileSyncScheduleViews().delete(request, planid) desc = {'操作': '删除导出计划', '计划ID': planids.split(',')} SaveOperationLog( request.user, OperationLog.BACKUP_EXPORT, json.dumps(desc, ensure_ascii=False), get_operator(request)) return HttpResponse('{"r": "0","e": "操作成功"}')
def report_update_ret(request): ver = request.POST.get('ver', '') update_type = request.POST.get('update_type', '') err_msg = request.POST.get('err_msg', None) if update_type == 'iso': if err_msg: endlog = {'结束': '启动介质升级失败,{}'.format(err_msg)} else: endlog = {'结束': '启动介质成功升级到版本{}'.format(ver)} else: if err_msg: endlog = {'结束': '升级失败,{}'.format(err_msg)} else: endlog = {'结束': '成功升级到版本{}'.format(ver)} user = User.objects.get(username='******') SaveOperationLog(user, OperationLog.TYPE_UPDATE_AIO_BASE, json.dumps(endlog, ensure_ascii=False), get_operator(request)) return HttpResponse(json.dumps({"r": 0, "e": '操作成功'}, ensure_ascii=False))
def update_one_driver(zipfile, jsonfile, user, operator): _logger.debug('update_one_driver zipfile = {},jsonfile={}\n'.format( zipfile, jsonfile)) jsonobj = None with open(os.path.join(jsonfile)) as file_object: jsonobj = json.load(file_object) all_sql_info_list = list() for item in jsonobj: one_sql_info_list = { 'server_id': None, 'del': 0, 'show_name': None, 'hard_or_comp_id': None, 'inf_driver_ver': None, 'inf_path': None, 'zip_path': None, 'system_name': None } one_sql_info_list["server_id"] = item["server_id"] one_sql_info_list["hard_or_comp_id"] = item["hard_or_comp_id"] one_sql_info_list["inf_driver_ver"] = item["inf_driver_ver"] one_sql_info_list["inf_path"] = item["inf_path"] if os.path.exists(zipfile): one_sql_info_list["zip_path"] = item["zip_path"] else: one_sql_info_list["zip_path"] = None one_sql_info_list["system_name"] = item["system_name"] one_sql_info_list["show_name"] = item["show_name"] one_sql_info_list["del"] = item["del"] all_sql_info_list.append(one_sql_info_list.copy()) mylog = { '硬件ID数量': len(jsonobj), '文件名': '{}'.format(os.path.basename(zipfile)) } SaveOperationLog(user, OperationLog.TYPE_UPDATE_AIO_DRIVE, json.dumps(mylog, ensure_ascii=False), operator) with sqlite3.connect(enum_id.g_db_path) as cx: cu = cx.cursor() enum_id.update_one_zip(cu, all_sql_info_list, enum_id.g_db_path, enum_id.g_driver_pool, zipfile) cx.commit()
def delserver(request): idents = request.POST.get('idents', '') ret_info = {"e": [], "r": 1} success_host_names = list() for ident in idents.split(","): rsp = Hosts().delete(request, ident) if status.is_success(rsp.status_code): remove_all_group(ident, '1') _remove_all_verify_task(ident) ret_info["e"].append(rsp.data['msg']) success_host_names.append(rsp.data['name']) else: ret_info["e"].append(rsp.data if rsp.data else '删除失败,内部异常') ret_info["code_is"] = rsp.status_code ret_info['e'].sort(key=lambda x: '删除成功' in x, reverse=True) ret_info["e"] = '* {}'.format('\n* '.join(ret_info["e"])) if success_host_names: desc = {'操作': '删除客户端', '客户端': '<br>'.join(success_host_names)} SaveOperationLog( request.user, OperationLog.TYPE_SERVER, json.dumps(desc, ensure_ascii=False), get_operator(request)) return HttpResponse(json.dumps(ret_info))
def delPlans(request): paramsQD = request.GET planids = paramsQD.get('taskid', '') # '4,5,6' host_ident = paramsQD.get('host_ident', None) # 存在就删除主机所有计划 if host_ident: user_plans = BackupTaskSchedule.objects.filter(enabled=True, deleted=False, host__ident=host_ident) planids = ','.join([str(plan.id) for plan in user_plans]) if not planids: return HttpResponse('{"r": "0","e": "操作成功"}') if not planids: return HttpResponse('{"r": "1", "e": "请求参数缺失:taskid"}') ids = list() for planid in planids.split(','): if planid.startswith('group_'): gs = GroupBackupTaskSchedule.objects.filter(id=planid[6:]) for schedules in gs.first().schedules.all(): ids.append(schedules.id) gs.delete() else: ids.append(planid) for planid in ids: delete_shell_zip(planid) BackupTaskScheduleSetting().delete(request=request, backup_task_schedule_id=planid) desc = {'操作': '删除备份计划', '计划ID': planids.split(',')} SaveOperationLog(request.user, OperationLog.TYPE_BACKUP, json.dumps(desc, ensure_ascii=False), get_operator(request)) return HttpResponse('{"r": "0","e": "操作成功"}')
def _record_log(request, detail): SaveOperationLog(request.user, OperationLog.VMWARE_BACKUP, json.dumps(detail, ensure_ascii=False), get_operator(request))
def operation_log(request, detial): user, log_event = request.user, OperationLog.REMOTE_BACKUP SaveOperationLog(user, log_event, json.dumps(detial, ensure_ascii=False), get_operator(request))
def processupload(filepath, user, operator): type = '0' rev = 0 msg = '' file_dir = os.path.splitext(filepath)[0] logfilename = os.path.basename(filepath) startlog = {'开始': '从文件{}更新'.format(logfilename)} endlog = {'结束': '从文件{}更新完成'.format(logfilename)} cmd = 'unzip -o -P {89B87785-0C5F-47eb-B7DE-73DD962B0FAE} "' + filepath + '" sig.ini -d "{}"'.format( file_dir) info = execute_cmd_and_return_code(cmd) if info[0] != 0: if os.path.isfile(filepath): os.remove(filepath) endlog = {'结果': '解压文件{}失败。'.format(logfilename)} SaveOperationLog(user, OperationLog.TYPE_UPDATE_AIO_DRIVE, json.dumps(endlog, ensure_ascii=False), operator) return 1, '从文件{}更新结束'.format(logfilename) sig_ini_path = os.path.join(file_dir, 'sig.ini') if os.path.isfile(sig_ini_path): cf = configparser.ConfigParser() cf.read_file(open(sig_ini_path)) type = cf.get("Main", "type") else: if os.path.isfile(filepath): os.remove(filepath) endlog = {'结果': '从文件{}更新失败,文件格式不正确。'.format(logfilename)} SaveOperationLog(user, OperationLog.TYPE_UPDATE_AIO_DRIVE, json.dumps(endlog, ensure_ascii=False), operator) return 1, '从文件{}更新失败,文件格式不正确。'.format(logfilename) logtype = None if type in ( '3', '5', ): logtype = OperationLog.TYPE_UPDATE_AIO_DRIVE else: logtype = OperationLog.TYPE_UPDATE_AIO_BASE cmd = 'unzip -o -P {89B87785-0C5F-47eb-B7DE-73DD962B0FAE} "' + filepath + '" -d "{}"'.format( file_dir) SaveOperationLog(user, logtype, json.dumps(startlog, ensure_ascii=False), operator) info = execute_cmd_and_return_code(cmd) if info[0] != 0: if os.path.isfile(filepath): os.remove(filepath) mylog = {'进度': '解压文件{}失败'.format(logfilename)} SaveOperationLog(user, logtype, json.dumps(mylog, ensure_ascii=False), operator) return 1, '解压文件{}失败'.format(logfilename) mylog = {'进度': '解压文件{}完成'.format(logfilename)} SaveOperationLog(user, logtype, json.dumps(mylog, ensure_ascii=False), operator) if os.path.isfile(filepath): os.remove(filepath) # 从外网升级驱动库 if type == "3": target_dir = file_dir or_db_name = 'drvierid.db' new_db_name = 'drvierid.db' driver_pool_path = '/home/aio/driver_pool/' update_type = 3 u_ins = enum_id.Update(target_dir, or_db_name, new_db_name, driver_pool_path, update_type) bSuccess = True try: u_ins.work() except Exception as e: bSuccess = False _logger.error("processupload error ,{}".format(e)) endlog = {'结束': '从文件{}更新失败,{}'.format(logfilename, e)} if bSuccess: driver_ver = '{}.{}'.format(getBigVersion(), enum_id.get_version(enum_id.g_db_path)) endlog = {'结束': '成功升级到版本{}'.format(driver_ver)} SaveOperationLog(user, OperationLog.TYPE_UPDATE_AIO_DRIVE, json.dumps(endlog, ensure_ascii=False), operator) shutil.rmtree(file_dir) # 用户手动上传,本地获取到的驱动 elif type == "5": target_dir = file_dir or_db_name = 'drvierid.db' new_db_name = 'drvierid_user.db' driver_pool_path = '/home/aio/driver_pool/' update_type = 5 u_ins = enum_id.Update(target_dir, or_db_name, new_db_name, driver_pool_path, update_type) try: u_ins.work() except Exception as e: _logger.error("processupload error ,{}".format(e)) SaveOperationLog(user, OperationLog.TYPE_UPDATE_AIO_DRIVE, json.dumps(endlog, ensure_ascii=False), operator) else: target_dir = file_dir CUpdate().Update(target_dir, update_callback, user, logfilename, operator) return 0, ''
def createModifyPlan(request): params_dict = request.POST source_host_ident = params_dict['source_host_ident'] target_host_ident = params_dict['target_host_ident'] sync_rules_str = params_dict['sync_rules'].strip() if not sync_rules_str: sync_rules_str = '{}' try: sync_rules = json.loads(sync_rules_str) except Exception as e: return HttpResponse(json.dumps({"r": "1", "e": '操作失败,导出规则不是JSON格式', "list": []})) schedule_name = params_dict['taskname'] # 立即导出吗 immediateBackup = int(params_dict.get('immediately', default=-1)) # 0/1 # 该计划的时间表类型 scheduleType = int(params_dict.get('bakschedule', default=-1)) # 1/2/3/4/5 # 计划开始日期 planStartDate = params_dict.get('starttime', default=None) # 按间隔时间: 分,时,天 backupDayInterval = get_normal_backup_interval_secs(params_dict) # 每周模式 daysInWeek = params_dict.getlist('perweek', default=[]) # [1, 3, 5] daysInWeek = list(map(int, daysInWeek)) # 每月模式 daysInMonth = params_dict.getlist('monthly', default=[]) # [1, 18, 27, 31] daysInMonth = list(map(int, daysInMonth)) extConfig = { 'backupDayInterval': backupDayInterval, # 按间隔时间: 秒数 'daysInWeek': daysInWeek, 'daysInMonth': daysInMonth, 'sync_rules': sync_rules, 'IntervalUnit': params_dict['intervalUnit'] } extConfig = json.dumps(extConfig, ensure_ascii=False) code = 0 # 更改计划(禁止修改cdp - syn / cdp - asyn等) if 'taskid' in params_dict: planId = int(params_dict['taskid']) params = { 'name': schedule_name, 'cycle_type': scheduleType, 'plan_start_date': planStartDate, 'ext_config': extConfig, } respn = FileSyncScheduleViews().put(request, planId, params) if respn.status_code == status.HTTP_202_ACCEPTED: infoCM = '修改计划成功' desc = {'操作': '修改导出计划', '任务ID': str(planId), '任务名称': schedule_name} else: code = 1 infoCM = '修改计划失败,{}'.format(respn.data) # 创建计划 else: result = authorize_init.check_backup_archive_license() if result['r'] != 0: return HttpResponse(json.dumps({"r": "1", "e": result['e'], "list": []})) params = {"source_host_ident": source_host_ident, "target_host_ident": target_host_ident, "name": schedule_name, "cycle_type": scheduleType, "plan_start_date": planStartDate, "ext_config": extConfig} respn = FileSyncScheduleViews().post(request, params) if respn.status_code == status.HTTP_201_CREATED: infoCM = '创建计划成功' desc = {'操作': '创建导出计划', '任务ID': str(respn.data['id']), '任务名称': schedule_name} else: code = 1 infoCM = '创建计划失败,{}'.format(respn.data) # 是否立即执行该计划一次 infoEcx = '' plan_id = respn.data['id'] if code == 0 else -1 if immediateBackup > 0 and code == 0: respn = FileSyncScheduleExecute().post(request=request, api_request={'schedule': plan_id}) if status.is_success(respn.status_code): infoEcx = '立即执行计划成功' else: code = 1 infoEcx = '立即执行计划失败,{}'.format(respn.data) if (infoCM == '修改计划成功') or (infoCM == '创建计划成功'): desc.update({'立即执行': infoEcx if infoEcx else '否'}) SaveOperationLog( request.user, OperationLog.BACKUP_EXPORT, json.dumps(desc, ensure_ascii=False), get_operator(request)) # 返回给界面信息 infoStr = '{} {}'.format(infoCM, infoEcx) return HttpResponse(json.dumps({"r": code, "e": "{}".format(infoStr), "plan_id": plan_id}, ensure_ascii=False))
def download_worker(request, url, filepath): taskname = request.GET.get('taskname', '') if taskname == 'aio_update': logtype = OperationLog.TYPE_UPDATE_AIO_BASE if taskname == 'iso_update': logtype = OperationLog.TYPE_UPDATE_AIO_ISO if taskname == 'driver_update': logtype = OperationLog.TYPE_UPDATE_AIO_DRIVE r = requests.get(url, stream=True, verify=False) if not status.is_success(r.status_code): SaveOperationLog( request.user, logtype, json.dumps( { '失败': '下载文件失败,状态码:{}'.format(r.status_code), '文件名': '{}'.format(url) }, ensure_ascii=False), get_operator(request)) _logger.error('下载文件失败,状态码:{}'.format(r.status_code)) return SaveOperationLog( request.user, logtype, json.dumps({ '状态': '开始下载文件', '文件名': '{}'.format(url) }, ensure_ascii=False), get_operator(request)) lastsize = 0 size = 0 bneedlog = True with open(filepath, 'wb') as fd: for chunk in r.iter_content(1024 * 1024): size += len(chunk) fd.write(chunk) bneedlog = True if size >= lastsize + 100 * 1024 * 1024: SaveOperationLog( request.user, logtype, json.dumps( {'下载状态': '已下载:{:.1f}MB'.format(size / (1024 * 1024))}, ensure_ascii=False), get_operator(request)) bneedlog = False lastsize = size if bneedlog: SaveOperationLog( request.user, logtype, json.dumps({'下载状态': '已下载:{:.1f}MB'.format(size / (1024 * 1024))}, ensure_ascii=False), get_operator(request)) _logger.info( 'download update file finish and bigin execute update, filepath:{}'. format(filepath)) g_thread_handle.setName('processing_' + taskname) # 判断是否 执行更新操作 if getattr(threading.current_thread(), 'do_run', True): processupload(filepath, request.user, get_operator(request)) try: os.remove(filepath) except: pass SaveOperationLog( request.user, logtype, json.dumps({ '状态': '更新完成', '文件名': '{}'.format(url) }, ensure_ascii=False), get_operator(request)) else: _logger.info( 'download update file is finished, but task has been canceled and file will be remouved, filepath:{}' .format(filepath)) SaveOperationLog( request.user, logtype, json.dumps({ '状态': '下载完成,任务被取消,文件将删除', '文件名': '{}'.format(url) }, ensure_ascii=False), get_operator(request)) try: os.remove(filepath) except: pass return
def startMigrate(request): if authorize_init.is_evaluation_and_expiration(): return HttpResponse(json.dumps({"r": "1", "e": "试用版已过期", "list": []})) if not UserQuotaTools.is_user_allocated_any_quota(request.user): return HttpResponse(json.dumps({"r": "1", "e": "用户无任何配额, 不能执行迁移<br>请联系管理员为用户分配配额", "list": []})) result = check_authorize_at_migrate() if not result[0]: return HttpResponse(json.dumps({"r": "1", "e": result[1], "list": []})) paramsQ = request.POST origin_ident = paramsQ.get('srcserverid', default=None) target_ident = paramsQ.get('destserverid', default=None) migr_disks_map = json.loads(paramsQ.get('disks', default=None)) ipconfig_infos = json.loads(paramsQ.get('adapters', '{}')) drivers_ids = request.POST['drivers_ids'] drivers_type = request.POST['drivers_type'] routers = request.POST['routers'] drivers_ids_force = request.POST.get('drivers_ids_force', '') clret = authorize_init.check_host_rebuild_count(target_ident) if clret.get('r', 0) != 0: return HttpResponse(json.dumps(clret, ensure_ascii=False)) desc = {'操作': '迁移', '源目标': str(origin_ident), '目的': str(target_ident)} if not origin_ident or not target_ident or not migr_disks_map or not ipconfig_infos: return HttpResponse('{"r":"1","e":"操作失败:请求参数错误"}') try: drivers_list_str = GetDriversVersions.get_drivers_list_str(target_ident, drivers_ids, origin_ident, drivers_type, restore_to_self=False, user_id=request.user.id, driver_ids_force=drivers_ids_force) drivers_list_str = drivers_list_str.replace('\\', '|') except Exception as e: msg = traceback.format_exc() _logger.error('startMigrate fail {}'.format(e)) _logger.error(msg) return HttpResponse(json.dumps({"r": "1", "e": "操作失败"}, ensure_ascii=False)) userid = str(request.user.id) user_fingerprint = request.user.userprofile.user_fingerprint agent_user_info = userid + '|*' + user_fingerprint.hex enablekvm = '0' remote_kvm_params = dict() remote_kvm_params['enablekvm'] = str(enablekvm) if enablekvm == '0': remote_kvm_params['ssh_ip'] = '' remote_kvm_params['ssh_port'] = '' remote_kvm_params['ssh_key'] = '' remote_kvm_params['aio_ip'] = '' remote_kvm_params['ssh_path'] = '' remote_kvm_params['ssh_os_type'] = '' else: remote_kvm_params['ssh_ip'] = str(paramsQ['ssh_ip']) remote_kvm_params['ssh_port'] = str(paramsQ['ssh_port']) remote_kvm_params['ssh_key'] = str(paramsQ['ssh_key']) remote_kvm_params['aio_ip'] = str(paramsQ['aio_ip']) remote_kvm_params['ssh_path'] = os.path.join(str(paramsQ['ssh_path']), '{}_{}'.format('migrate', time.time())) remote_kvm_params['ssh_os_type'] = str(paramsQ['ssh_os_type']) migr_params = { 'pe_host_ident': target_ident, 'disks': migr_disks_map, 'adapters': re_generate_adapters_params(ipconfig_infos), 'drivers_ids': drivers_list_str, 'agent_user_info': agent_user_info, 'routers': routers, 'disable_fast_boot': paramsQ['disable_fast_boot'] == 'true', 'replace_efi': paramsQ['replace_efi'] == 'true', 'remote_kvm_params': remote_kvm_params, 'diskreadthreadcount': int(paramsQ.get('thread_count', '4')) } add_shell_infos_to_host_ext(origin_ident, paramsQ.get('shell_infos', None)) resp = HostSessionMigrate().post(request=request, ident=origin_ident, api_request=migr_params) if status.is_success(resp.status_code): desc.update({'操作结果': '成功'}) SaveOperationLog( request.user, OperationLog.TYPE_MIGRATE, json.dumps(desc, ensure_ascii=False), get_operator(request)) authorize_init.save_host_rebuild_record(target_ident) return HttpResponse('{"r":"0","e":"操作成功"}') else: desc.update({'操作结果': '失败,{}'.format(resp.data)}) SaveOperationLog( request.user, OperationLog.TYPE_MIGRATE, json.dumps(desc, ensure_ascii=False), get_operator(request)) return HttpResponse(json.dumps({"r": "1", "e": resp.data}, ensure_ascii=False))
def excPlanNow(request): paramsQD = request.GET planids = paramsQD.get('taskid', '') # '4,5,6' backupmode = paramsQD.get('backupmode', '2') force_store_full = paramsQD.get('force_store_full', '0') api_request = {'type': backupmode, 'force_store_full': force_store_full} if not planids: return HttpResponse('{"r": "1", "e": "请求参数缺失:taskid"}') if backupmode == '1' and force_store_full == '0': # 完整备份启用智能增量存储 clret = _check_remove_duplicates_in_system_folder_license() if clret.get('r', 0) != 0: return HttpResponse(json.dumps(clret, ensure_ascii=False)) err = list() ids = list() for planid in planids.split(','): if planid.startswith('group_'): gs = GroupBackupTaskSchedule.objects.filter(id=planid[6:]) for schedules in gs.first().schedules.all(): ids.append(schedules.id) else: ids.append(planid) for planid in ids: planStat = BackupTaskScheduleSetting().get( request=request, backup_task_schedule_id=planid).data is_enable = planStat['enabled'] if is_enable: rsp = BackupTaskScheduleExecute().post( request=request, backup_task_schedule_id=planid, api_request=api_request) if rsp.status_code == status.HTTP_201_CREATED: desc = { '操作': '执行备份计划', '计划名称': planStat['name'], "操作结果": "已发送命令" } SaveOperationLog(request.user, OperationLog.TYPE_BACKUP, json.dumps(desc, ensure_ascii=False), get_operator(request)) else: err.append({'name': planStat['name'], 'e': rsp.data}) mylog = { '操作': '执行备份计划', '计划名称': planStat['name'], "操作结果": "执行失败{}".format(rsp.data) } SaveOperationLog(request.user, OperationLog.TYPE_BACKUP, json.dumps(mylog, ensure_ascii=False), get_operator(request)) else: err.append({'name': planStat['name'], 'e': '已禁用'}) mylog = { '操作': '执行备份计划务', '计划名称': planStat['name'], "操作结果": "计划已禁用,执行失败" } SaveOperationLog(request.user, OperationLog.TYPE_BACKUP, json.dumps(mylog, ensure_ascii=False), get_operator(request)) if len(err) > 0: return HttpResponse( json.dumps( { "r": 1, "e": "共有{}个计划执行失败".format(len(err)), "err": err }, ensure_ascii=False)) return HttpResponse('{"r": "0","e": "操作成功"}')
def disablePlan(request): paramsQD = request.GET planids = paramsQD.get('taskid', '') # '4,5,6' enabled_yun = paramsQD.get('enabled_yun', '') user = request.user if not planids: return HttpResponse('{"r": "1", "e": "请求参数缺失:taskid"}') _failed_enable = list() ids = list() for planid in planids.split(','): if planid.startswith('group_'): gs = GroupBackupTaskSchedule.objects.filter(id=planid[6:]) gs.update(enabled=not gs.first().enabled) for schedules in gs.first().schedules.all(): ids.append(schedules.id) else: ids.append(planid) for planid in ids: planStat = BackupTaskScheduleSetting().get( request=request, backup_task_schedule_id=planid).data node_ident = planStat['storage_node_ident'] is_enable = planStat['enabled'] host_name = planStat['host']['name'] plan_name = planStat['name'] if is_enable: # 该计划当前"启用状态" api_request = {'enabled': False} desc = { '操作': '启用/禁用任务', '任务ID': planid, '状态': '禁用成功', '主机名称': host_name, '计划名称': plan_name } else: # 该计划当前"禁用状态" api_request = {'enabled': True} desc = { '操作': '启用/禁用任务', '任务ID': planid, '状态': '启用成功', '主机名称': host_name, '计划名称': plan_name } if not _is_plan_in_user_quota(user.id, node_ident): desc = { '操作': '启用/禁用任务', '任务ID': planid, '操作结果': '没有可用配额,启用失败', '主机名称': host_name, '计划名称': plan_name } SaveOperationLog(request.user, OperationLog.TYPE_BACKUP, json.dumps(desc, ensure_ascii=False), get_operator(request)) _failed_enable.append(planStat['name']) continue # 从yun过来的操作 if enabled_yun != '': api_request = {'enabled': enabled_yun} desc = { '操作': '启用/禁用任务', '任务ID': planid, '状态': '启用成功' if enabled_yun else '禁用成功', '主机名称': host_name, '计划名称': plan_name } resp = BackupTaskScheduleSetting().put(request=request, backup_task_schedule_id=planid, api_request=api_request) if resp.status_code == status.HTTP_202_ACCEPTED: SaveOperationLog(request.user, OperationLog.TYPE_BACKUP, json.dumps(desc, ensure_ascii=False), get_operator(request)) if _failed_enable: ret_info = { "r": "1", "e": "{}:没有可用配额,启用失败".format(','.join(_failed_enable)) } return HttpResponse(json.dumps(ret_info)) return HttpResponse('{"r": "0","e": "操作成功"}')
def startexporttask(request): paramsQD = request.GET planids = paramsQD.get('taskid', '') # '4,5,6' exportmode = paramsQD.get('exportmode', '2') # False,默认为增量导出 if int(exportmode) == 2: force_full = False else: force_full = True if not planids: return HttpResponse('{"r": "1", "e": "请求参数缺失:taskid"}') err = list() for planid in planids.split(','): planStat = ArchiveScheduleViews().get(request, planid).data is_enable = planStat['enabled'] if is_enable: host_id = ArchiveSchedule.objects.get(id=int(planid)).host_id host_hostsnapshot = HostSnapshot.objects.filter( host_id=host_id).order_by('-id').first().id api_request = { 'schedule': planid, 'host_snapshot': host_hostsnapshot, 'snapshot_datetime': '', 'force_full': force_full } rsp = ArchiveScheduleExecute().post(request=request, api_request=api_request) if rsp.status_code == status.HTTP_201_CREATED: desc = { '操作': '执行导出计划', '计划名称': planStat['name'], "操作结果": "已发送命令" } SaveOperationLog(request.user, OperationLog.BACKUP_EXPORT, json.dumps(desc, ensure_ascii=False), get_operator(request)) else: err.append({'name': planStat['name'], 'e': rsp.data}) mylog = { '操作': '执行导出计划', '计划名称': planStat['name'], "操作结果": "执行失败{}".format(rsp.data) } SaveOperationLog(request.user, OperationLog.BACKUP_EXPORT, json.dumps(mylog, ensure_ascii=False), get_operator(request)) else: err.append({'name': planStat['name'], 'e': '已禁用'}) mylog = { '操作': '执行导出计划务', '计划名称': planStat['name'], "操作结果": "计划已禁用,执行失败" } SaveOperationLog(request.user, OperationLog.BACKUP_EXPORT, json.dumps(mylog, ensure_ascii=False), get_operator(request)) if len(err) > 0: return HttpResponse( json.dumps( { "r": 1, "e": "共有{}个计划执行失败".format(len(err)), "err": err }, ensure_ascii=False)) return HttpResponse('{"r": "0","e": "操作成功"}')
def createModifyPlan(request): paramsQDict = request.POST # host数据库id hostIdent = paramsQDict.get('serverid', default='') # 所选存储结点的ident storage_node_ident = paramsQDict.get('storagedevice', default=-1) api_response = HostSessionInfo().get(request=request, ident=hostIdent) if not status.is_success(api_response.status_code): return HttpResponse( json.dumps({ "r": 1, "e": "{}".format(get_response_error_string(api_response)) })) hostId = api_response.data['host']['id'] if hostIdent else -1 # 计划名称 planName = paramsQDict.get('taskname', default='') # 导出数据保留期限 天, 月 backupDataHoldDays = int(paramsQDict.get('retentionperiod', default=-1)) # 空间剩余XGB时,自动清理 autoCleanDataWhenlt = int(paramsQDict.get('cleandata', default=-1)) # 带宽限速 MB usemaxBroadband = float(paramsQDict.get('usemaxbandwidth', default=1)) maxBroadband = float(paramsQDict.get('maxbandwidth', default=-1)) if usemaxBroadband == 0: maxBroadband = -1 # 立即导出吗 immediateBackup = int(paramsQDict.get('immediately', default=-1)) # 0/1 # 该计划的时间表类型 scheduleType = int(paramsQDict.get('bakschedule', default=-1)) # 1/2/3/4/5 # 计划开始日期 planStartDate = paramsQDict.get('starttime', default=None) # 按间隔时间: 分,时,天 backupDayInterval = get_normal_backup_interval_secs(paramsQDict) # 每周模式 daysInWeek = paramsQDict.getlist('perweek', default=[]) # [1, 3, 5] daysInWeek = list(map(int, daysInWeek)) # 每月模式 daysInMonth = paramsQDict.getlist('monthly', default=[]) # [1, 18, 27, 31] daysInMonth = list(map(int, daysInMonth)) # 数据传输 是否加密 1 加密 0 不加密 isencipher = int(paramsQDict.get('isencipher', default=0)) # 0/1 backupmode = int(paramsQDict.get('backupmode', default=2)) # 系统文件夹,去重 SystemFolderDup = paramsQDict['SystemFolderDup'] == '1' # '1'、'0' # 导出重试 backup_retry_or = json.loads( paramsQDict.get('backup_retry', '{"enable":true,"count":"5","interval":"10"}')) # 开启/禁用 enabled = paramsQDict.get('enabled', True) # 1.静默 0.不静默 vmware_quiesce = int(paramsQDict.get('vmware_quiesce', '1')) backup_retry = { 'enable': backup_retry_or['enable'], 'count': int(backup_retry_or['count']), 'interval': int(backup_retry_or['interval']) } # 数据保留期 单位:day, month, None data_keeps_deadline_unit = paramsQDict.get('retentionperiod_unit', None) if data_keeps_deadline_unit == 'day' or data_keeps_deadline_unit is None: backupDataHoldDays = backupDataHoldDays * 1 if data_keeps_deadline_unit == 'month': backupDataHoldDays = backupDataHoldDays * 30 # 线程数 # thread_count = int(paramsQDict.get('thread_count', '4')) # 导出时候IO占用 BackupIOPercentage = int(paramsQDict.get('BackupIOPercentage', '30')) # 导出完整备份间隔 full_interval = int(paramsQDict.get('full_interval', '-1')) # 0 每次完整导出, -1 第一此完整备份 # BackupTaskSchedule的ext_config字段 extConfig = { 'backupDataHoldDays': backupDataHoldDays, # 导出数据保留期, 天 'autoCleanDataWhenlt': autoCleanDataWhenlt, 'maxBroadband': maxBroadband, 'backupDayInterval': backupDayInterval, # 按间隔时间: 秒数 'daysInWeek': daysInWeek, 'daysInMonth': daysInMonth, 'isencipher': isencipher, 'incMode': backupmode, 'removeDuplicatesInSystemFolder': SystemFolderDup, 'IntervalUnit': paramsQDict['intervalUnit'], # 按间隔时间, 单位: 'min', 'hour', 'day' 'backup_retry': backup_retry, 'data_keeps_deadline_unit': data_keeps_deadline_unit, 'vmware_quiesce': vmware_quiesce, 'BackupIOPercentage': BackupIOPercentage, 'full_interval': full_interval } extConfig = json.dumps(extConfig, ensure_ascii=False) code = 0 # 更改计划(禁止修改cdp - syn / cdp - asyn等) if 'taskid' in paramsQDict: planId = int(paramsQDict['taskid']) params = { 'name': planName, 'cycle_type': scheduleType, 'plan_start_date': planStartDate, 'ext_config': extConfig, 'storage_node_ident': storage_node_ident } respn = ArchiveScheduleViews().put(request, planId, params) if respn.status_code == status.HTTP_202_ACCEPTED: infoCM = '修改计划成功' desc = {'操作': '修改导出计划', '任务ID': str(planId), '任务名称': planName} else: code = 1 infoCM = '修改计划失败,{}'.format(respn.data) # 创建计划 else: result = authorize_init.check_backup_archive_license() if result['r'] != 0: return HttpResponse( json.dumps({ "r": "1", "e": result['e'], "list": [] })) params = { "host": hostId, "name": planName, "cycle_type": scheduleType, "plan_start_date": planStartDate, "ext_config": extConfig, 'storage_node_ident': storage_node_ident, 'enabled': enabled } respn = ArchiveScheduleViews().post(request, params) if respn.status_code == status.HTTP_201_CREATED: infoCM = '创建计划成功' desc = {'操作': '创建导出计划', '任务ID': str(hostId), '任务名称': planName} else: code = 1 infoCM = '创建计划失败,{}'.format(respn.data) # 是否立即执行该计划一次 infoEcx = '' plan_id = respn.data['id'] if code == 0 else -1 if immediateBackup > 0 and code == 0: respn = ArchiveScheduleExecute().post( request=request, api_request={'schedule': plan_id}) if status.is_success(respn.status_code): infoEcx = '立即执行计划成功' else: code = 1 infoEcx = '立即执行计划失败,{}'.format(respn.data) if (infoCM == '修改计划成功') or (infoCM == '创建计划成功'): desc.update({'立即执行': infoEcx if infoEcx else '否'}) SaveOperationLog(request.user, OperationLog.BACKUP_EXPORT, json.dumps(desc, ensure_ascii=False), get_operator(request)) # 返回给界面信息 infoStr = '{} {}'.format(infoCM, infoEcx) return HttpResponse( json.dumps({ "r": code, "e": "{}".format(infoStr), "plan_id": plan_id }, ensure_ascii=False))
def disablePlan(request): paramsQD = request.GET planids = paramsQD.get('taskid', '') # '4,5,6' # enabled_yun = paramsQD.get('enabled_yun', '') user = request.user if not planids: return HttpResponse('{"r": "1", "e": "请求参数缺失:taskid"}') _failed_enable = list() for planid in planids.split(','): planStat = ArchiveScheduleViews().get(request, planid).data node_ident = planStat['storage_node_ident'] is_enable = planStat['enabled'] host_name = planStat['host']['name'] plan_name = planStat['name'] if is_enable: # 该计划当前"启用状态" api_request = {'enabled': False} desc = { '操作': '启用/禁用任务', '任务ID': planid, '状态': '禁用成功', '主机名称': host_name, '计划名称': plan_name } else: # 该计划当前"禁用状态" api_request = {'enabled': True} desc = { '操作': '启用/禁用任务', '任务ID': planid, '状态': '启用成功', '主机名称': host_name, '计划名称': plan_name } if not _is_plan_in_user_quota(user.id, node_ident): desc = { '操作': '启用/禁用任务', '任务ID': planid, '操作结果': '没有可用配额,启用失败', '主机名称': host_name, '计划名称': plan_name } SaveOperationLog(request.user, OperationLog.BACKUP_EXPORT, json.dumps(desc, ensure_ascii=False), get_operator(request)) _failed_enable.append(planStat['name']) continue # 从yun过来的操作 # if enabled_yun != '': # api_request = {'enabled': enabled_yun} # desc = {'操作': '启用/禁用任务', '任务ID': planid, '状态': '启用成功' if enabled_yun else '禁用成功', '主机名称': host_name, # '计划名称': plan_name} resp = ArchiveScheduleViews().put(request, planid, api_request) if resp.status_code == status.HTTP_202_ACCEPTED: SaveOperationLog(request.user, OperationLog.BACKUP_EXPORT, json.dumps(desc, ensure_ascii=False), get_operator(request)) if _failed_enable: ret_info = { "r": "1", "e": "{}:没有可用配额,启用失败".format(','.join(_failed_enable)) } return HttpResponse(json.dumps(ret_info)) return HttpResponse('{"r": "0","e": "操作成功"}')
def createtask(request): taskid = request.POST.get('taskid') taskname = request.POST.get('taskname') storage_node_ident = request.POST.get('storagedevice') cycle_type = int(request.POST.get('schedule')) # 2仅验证一次 3按间隔时间 4每周 5每月 starttime = request.POST.get('starttime') timeinterval = request.POST.get('timeinterval') intervalUnit = request.POST.get('intervalUnit') perweek = request.POST.getlist('perweek', default=[]) perweek = list(map(int, perweek)) monthly = request.POST.getlist('monthly', default=[]) monthly = list(map(int, monthly)) verify_osname = int(request.POST.get('verify_osname', 0)) verify_osver = int(request.POST.get('verify_osver', 0)) verify_hdd = int(request.POST.get('verify_hdd', 0)) last_point = int(request.POST.get('last_point', 1)) # 只验证最后一个点 if timeinterval: backupDayInterval = int(timeinterval) * { 'min': 60, 'hour': 3600, 'day': 24 * 3600 }[intervalUnit] else: # 不参与运算,只是为了调用calc_next_run不出错 backupDayInterval = 0 script1 = request.POST.get('script1') script2 = request.POST.get('script2') script3 = request.POST.get('script3') script4 = request.POST.get('script4') script5 = request.POST.get('script5') script6 = request.POST.get('script6') script7 = request.POST.get('script7') script8 = request.POST.get('script8') kvm_memory_size = request.POST.get('kvm_memory_size') kvm_memory_unit = request.POST.get('kvm_memory_unit') script_list = list() script_list.append(script1) script_list.append(script2) script_list.append(script3) script_list.append(script4) script_list.append(script5) script_list.append(script6) script_list.append(script7) script_list.append(script8) result = {'r': 0, 'e': '操作成功'} ext_config = dict() ext_config['timeinterval'] = timeinterval ext_config['IntervalUnit'] = intervalUnit ext_config['daysInWeek'] = perweek ext_config['daysInMonth'] = monthly ext_config['backupDayInterval'] = backupDayInterval ext_config['script_list'] = script_list ext_config['kvm_memory_size'] = kvm_memory_size ext_config['kvm_memory_unit'] = kvm_memory_unit if last_point == 1: ext_config['verify_last_point_only'] = True else: ext_config['verify_last_point_only'] = False ext_config['verify_osname'] = True if verify_osname == 1 else False ext_config['verify_osver'] = True if verify_osver == 1 else False ext_config['verify_hdd'] = True if verify_hdd == 1 else False if not is_functional_available('auto_verify_task'): result = {'r': 1, 'e': '没有授权,请联系管理员'} return HttpResponse(json.dumps(result, ensure_ascii=False)) count = get_functional_int_value('auto_verify_task') sc_count = AutoVerifySchedule.objects.all().count() if sc_count >= count: result = {'r': 1, 'e': '自动验证授权数为{},当前已用{}'.format(count, sc_count)} return HttpResponse(json.dumps(result, ensure_ascii=False)) if taskid is None: verify_schedule = AutoVerifySchedule.objects.create( user=request.user, name=taskname, plan_start_date=starttime, cycle_type=cycle_type, storage_node_ident=storage_node_ident, ext_config=json.dumps(ext_config, ensure_ascii=False)) verify_schedule_id = verify_schedule.id desc = {'操作': '自动验证计划', '计划名称': taskname} SaveOperationLog(request.user, OperationLog.TYPE_AUTO_VERIFY_TASK, json.dumps(desc, ensure_ascii=False), get_operator(request)) else: verify_schedule_id = taskid AutoVerifySchedule.objects.filter(id=taskid).update( name=taskname, plan_start_date=starttime, cycle_type=cycle_type, storage_node_ident=storage_node_ident, ext_config=json.dumps(ext_config, ensure_ascii=False)) desc = {'操作': '更改自动验证计划', '计划名称': taskname} SaveOperationLog(request.user, OperationLog.TYPE_AUTO_VERIFY_TASK, json.dumps(desc, ensure_ascii=False), get_operator(request)) verify_schedule = AutoVerifySchedule.objects.get(id=verify_schedule_id) logicProcessor = BackupTaskScheduleLogicProcessor(verify_schedule) verify_schedule.next_run_date = logicProcessor.calc_next_run(True) verify_schedule.save(update_fields=['next_run_date']) return HttpResponse(json.dumps(result, ensure_ascii=False))