def incep_async_tasks(self, id=None, user=None, sql=None, sqlsha1=None, host=None, port=None, database=None, exec_status=None, backup=None): # 更新任务状态为: PROGRESS self.update_state(state="PROGRESS", meta={ 'user': user, 'host': host, 'port': port, 'database': database, 'sqlsha1': sqlsha1 }) of_audit = IncepSqlCheck(sql, host, port, database, user) # 执行SQL exec_result = of_audit.run_exec(0, backup) # 更新任务进度 update_tasks_status(id=id, exec_result=exec_result, exec_status=exec_status) # 更新任务状态为: SUCCESS self.update_state(state="SUCCESS")
def post(self, request): id = request.POST.get('id') envi_desc = request.POST.get('envi_desc') obj = get_object_or_404(AuditContents, pk=id) status, msg = check_db_conn_status(obj.host, obj.port) if status: # 只要审核通过后,才能生成执行任务 if obj.progress in ('2', '3', '4', '6'): if IncepMakeExecTask.objects.filter(related_id=id).first(): taskid = IncepMakeExecTask.objects.filter(related_id=id).first().taskid context = {'status': 0, 'jump_url': f'/projects/pt/perform_records/perform_details/{taskid}'} else: host = obj.host database = obj.database port = obj.port sql_content = obj.contents # 实例化 of_audit = IncepSqlCheck(sql_content, host, port, database, request.user.username) # 对OSC执行的SQL生成sqlsha1 result = of_audit.make_sqlsha1() taskid = datetime.now().strftime("%Y%m%d%H%M%S%f") # 生成执行任务记录 for row in result: IncepMakeExecTask.objects.create( uid=request.user.uid, user=obj.proposer, taskid=taskid, dst_host=host, dst_port=port, dst_database=database, sql_content=row['SQL'], sqlsha1=row['sqlsha1'], affected_row=row['Affected_rows'], type=obj.operate_type, envi_desc=envi_desc, related_id=id ) context = {'status': 0, 'jump_url': f'/projects/pt/perform_records/perform_details/{taskid}'} else: context = {'status': 2, 'msg': '审核未通过或任务已关闭'} else: context = {'status': 2, 'msg': f'无法连接到数据库,请联系DBA\n主机: {obj.host}\n端口: {obj.port}'} return HttpResponse(json.dumps(context))
def post(self, request): id = request.POST.get('id') obj = get_object_or_404(AuditContents, pk=id) data = get_object_or_404(OlAuditDetail, ol=id) # 只要审核通过后,才能执行生成执行任务 if obj.progress in ('2', '3', '4'): if IncepMakeExecTask.objects.filter(related_id=id).first(): taskid = IncepMakeExecTask.objects.filter(related_id=id).first().taskid context = {'status': 0, 'jump_url': f'/projects/pt/incep_perform_records/incep_perform_details/{taskid}'} else: host = obj.host database = obj.database sql_content = data.contents # 实例化 incep_of_audit = IncepSqlCheck(sql_content, host, database, request.user.username) # 对OSC执行的SQL生成sqlsha1 result = incep_of_audit.make_sqlsha1() taskid = datetime.now().strftime("%Y%m%d%H%M%S%f") # 生成执行任务记录 for row in result: IncepMakeExecTask.objects.create( uid=request.user.uid, user=obj.proposer, taskid=taskid, dst_host=host, dst_database=database, sql_content=row['SQL'], sqlsha1=row['sqlsha1'], affected_row=row['Affected_rows'], type=obj.operate_type, category='1', related_id=id, group_id=obj.group_id ) context = {'status': 0, 'jump_url': f'/projects/pt/incep_perform_records/incep_perform_details/{taskid}'} else: context = {'status': 2, 'msg': '审核未通过或任务已关闭'} return HttpResponse(json.dumps(context))
def save(self, request): cleaned_data = super(OflineAuditForm, self).clean() host = cleaned_data['host'] database = cleaned_data['database'] operate_type = cleaned_data.get('operate_type') group_id = cleaned_data['group_id'] sql_content = cleaned_data['contents'] # 实例化 of_audit = IncepSqlCheck(sql_content, host, database, request.user.username) # 生成执行任务 check_result = of_audit.is_check_pass() if check_result['status'] == 2: context = check_result else: # 对OSC执行的SQL生成sqlsha1 result = of_audit.make_sqlsha1() taskid = datetime.now().strftime("%Y%m%d%H%M%S%f") # 生成执行任务记录 for row in result: IncepMakeExecTask.objects.create( uid=request.user.uid, user=request.user.username, taskid=taskid, dst_host=host, group_id=group_id, dst_database=database, sql_content=row['SQL'], sqlsha1=row['sqlsha1'], affected_row=row['Affected_rows'], type=operate_type) context = { 'status': 0, 'msg': '', 'jump_url': f'/projects/pt/perform_records/perform_details/{taskid}' } return context
def is_save(self, request): cleaned_data = super(OnlineAuditForm, self).clean() title = cleaned_data.get('title') + '_[' + datetime.now().strftime( "%Y%m%d%H%M%S") + ']' verifier = cleaned_data.get('verifier') operator = cleaned_data.get('operator') email_cc = self.data.get('email_cc_id') group_id = cleaned_data.get('group_id') host = cleaned_data.get('host') database = cleaned_data.get('database') operate_type = cleaned_data.get('operate_type') contents = cleaned_data.get('contents') result = IncepSqlCheck(contents, host, database, request.user.username).is_check_pass() if result.get('status') == 2: context = result else: AuditContents.objects.create(title=title, operate_type=operate_type, host=host, database=database, group_id=group_id, proposer=request.user.username, operator=operator, verifier=verifier, email_cc=email_cc) # 向子表插入关联数据 OlAuditDetail.objects.create(ol=AuditContents.objects.latest('id'), contents=contents) # 发送通知邮件 latest_id = AuditContents.objects.latest('id').id send_commit_mail.delay(latest_id=latest_id) # 发送钉钉推送 xiaoding_pull.delay(user=request.user.username, title=title, type='commit') context = {'status': 0, 'jump_url': '/projects/ol/ol_records/'} return context
def get_osc_percent(task_id): """实时获取pt-online-schema-change执行进度""" task = AsyncResult(task_id) while task.state in ('PENDING', 'STARTED', 'PROGRESS'): while task.state == 'PROGRESS': user = task.result.get('user') host = task.result.get('host') database = task.result.get('database') sqlsha1 = task.result.get('sqlsha1') sql = f"inception get osc_percent '{sqlsha1}'" of_audit = IncepSqlCheck(sql, host, database, user) # 执行SQL of_audit.run_status(1) # 每1s获取一次 time.sleep(1) else: continue
def is_save(self, request): cleaned_data = super(WorkOrderAuditForm, self).clean() title = cleaned_data.get('title') + '_[' + datetime.now().strftime("%Y%m%d%H%M%S") + ']' url = cleaned_data.get('url') tasks = cleaned_data.get('tasks') operator = cleaned_data.get('operator') envi_desc = cleaned_data.get('envi_desc') remark = cleaned_data.get('remark') jump_url = cleaned_data.get('jump_url') host, port, database = cleaned_data.get('database').split(',') operate_type = cleaned_data.get('operate_type') contents = cleaned_data.get('contents') result = IncepSqlCheck(contents, host, port, database, request.user.username).is_check_pass() if result.get('status') == 2: context = result else: obj = AuditContents.objects.create( title=title, url=url, tasks=tasks, operate_type=operate_type, host=host, database=database, port=port, envi_desc=envi_desc, remark=remark, proposer=request.user.username, operator=operator, contents=contents ) # 发送钉钉推送 xiaoding_pull.delay(user=request.user.username, id=obj.id, type='commit') # 跳转到工单记录页面 context = {'status': 0, 'jump_url': jump_url} return context
def stop_incep_osc(user, id=None, celery_task_id=None): obj = IncepMakeExecTask.objects.get(id=id) host = obj.dst_host database = obj.dst_database exec_status = None if obj.exec_status == '2': sqlsha1 = obj.sqlsha1 exec_status = 0 elif obj.exec_status == '3': sqlsha1 = obj.rollback_sqlsha1 exec_status = 1 sql = f"inception stop alter '{sqlsha1}'" # 执行SQL task = AsyncResult(celery_task_id) if task.state == 'PROGRESS': of_audit = IncepSqlCheck(sql, host, database, user) of_audit.run_status(0) # 更新任务进度 update_tasks_status(id=id, exec_status=exec_status)