def template_list(request, version_id): ''' 显示版本模板列表. ''' user = request.user template_name = request.GET.get('template_name', None) iDisplayLength = int(request.GET.get('iDisplayLength')) iDisplayStart = int(request.GET.get('iDisplayStart')) sEcho = int(request.GET.get('sEcho')) iSortCol_0 = int(request.GET.get('iSortCol_0', 0)) sSortDir_0 = request.GET.get('sSortDir_0') order_list = [ None, 'step__template__name', 'step__template__create_user__username', 'step__name', 'version__script__name', 'version__name', 'version__remarks', None ] order_item = order_list[iSortCol_0] version = _script.get_version_by_params(id=version_id) script = version.script templateStepScripts = _template.get_templateStepScripts_by_params( version__script=script, step__template__is_delete=False) #查询 if template_name: templateStepScripts = templateStepScripts.filter( step__template__name__icontains=template_name) if order_item: if sSortDir_0 == "desc": order_item = '-%s' % order_item templateStepScripts = templateStepScripts.order_by(order_item) else: templateStepScripts = templateStepScripts.order_by('id') p = Paginator(templateStepScripts, iDisplayLength) total = p.count page_range = p.page_range page = p.page(page_range[iDisplayStart / iDisplayLength]) object_list = page.object_list sdicts = {} sdicts["sEcho"] = sEcho sdicts["iTotalRecords"] = total sdicts["iTotalDisplayRecords"] = total sdicts["aaData"] = [] for obj in object_list: box_field = '<td class="text-center"><input class="update_checked_it_%s" type="checkbox" value="%s"></td>' % ( version.id, obj.id) operation = '<td><a href="javascript:void(0);" onclick="version_job_sync(%s,%s);">选择实例步骤进行替换</a></td>' % ( version.id, obj.step.template.id) data = [ box_field, obj.step.template.name, obj.step.template.create_user.username, obj.step.name, obj.version.script.name, obj.version.name, obj.version.remarks, operation ] sdicts["aaData"].append(data) return HttpResponse(json.dumps(sdicts, cls=LazyEncoder))
def version_template_sync_v2(request): ''' version template sync v2 ''' sdicts = {} user = request.user code = 500 if request.method == 'POST': try: version_id = request.POST.get('version_id', 0) check_list = request.POST.getlist('check_list[]', []) version = _script.get_version_by_params(id=version_id) for templateStepScript_id in check_list: if templateStepScript_id: templateStepScript = _template.get_templateStepScript_by_params( id=templateStepScript_id) templateStepScript.version = version templateStepScript.save() code = 200 except Exception, e: code = 500 sdicts['status'] = code return HttpResponse(json.dumps(sdicts))
def version_job_sync(request): ''' version job sync ''' sdicts = {} user = request.user code = 500 if request.method == 'POST': try: version_id = request.POST.get('version_id', 0) check_list = request.POST.getlist('check_list[]', []) version = _script.get_version_by_params(id=version_id) for jobStepScript_id in check_list: if jobStepScript_id: jobStepScript = _job.get_jobStepScript_by_params( id=jobStepScript_id) jobStepScript.version = version jobStepScript.save() code = 200 except Exception, e: code = 500 sdicts['status'] = code return HttpResponse(json.dumps(sdicts))
def version_copy(request): ''' copy version ''' sdicts = {} user = request.user code = 500 if request.method == 'POST': version_id = request.POST.get('version_id',0) file_content = request.POST.get('file_content','') version_copy = _script.get_version_by_params(id=version_id) if version_copy: script = version_copy.script script_type = script.script_type now = datetime.datetime.now() version_name = '%s_%s'%(now.strftime("%Y%m%d%H%M%S"),user.username) version = _script.get_or_create_version_by_params(script=script,name=version_name)[0] file_doc_num = version.id % 100 file_doc_path = os.path.join(MEDIA_ROOT,'scripts','%s'%file_doc_num) if not os.path.exists(file_doc_path): os.mkdir(file_doc_path) suffix = enum_script.SCIPT_TYPE_DICT.get(int(script.script_type)) filename = u'%s.%s'%(version_name, suffix) file_path = os.path.join(file_doc_path,filename) f = open(file_path, 'wb') try: f.write(file_content) except Exception,e: print e finally:
def template_list(request,version_id): ''' 显示版本模板列表. ''' user = request.user template_name = request.GET.get('template_name', None) iDisplayLength = int(request.GET.get('iDisplayLength')) iDisplayStart = int(request.GET.get('iDisplayStart')) sEcho = int(request.GET.get('sEcho')) iSortCol_0 = int(request.GET.get('iSortCol_0',0)) sSortDir_0 = request.GET.get('sSortDir_0') order_list = [None,'step__template__name', 'step__template__create_user__username', 'step__name', 'version__script__name', 'version__name', 'version__remarks', None] order_item = order_list[iSortCol_0] version = _script.get_version_by_params(id=version_id) script = version.script templateStepScripts = _template.get_templateStepScripts_by_params(version__script=script,step__template__is_delete=False) #查询 if template_name: templateStepScripts = templateStepScripts.filter(step__template__name__icontains = template_name) if order_item: if sSortDir_0 == "desc": order_item = '-%s' % order_item templateStepScripts = templateStepScripts.order_by(order_item) else: templateStepScripts = templateStepScripts.order_by('id') p = Paginator(templateStepScripts, iDisplayLength) total = p.count page_range = p.page_range page = p.page(page_range[iDisplayStart / iDisplayLength]) object_list = page.object_list sdicts = {} sdicts["sEcho"] = sEcho sdicts["iTotalRecords"] = total sdicts["iTotalDisplayRecords"] = total sdicts["aaData"] = [] for obj in object_list: box_field = '<td class="text-center"><input class="update_checked_it_%s" type="checkbox" value="%s"></td>'%(version.id,obj.id) operation = '<td><a href="javascript:void(0);" onclick="version_job_sync(%s,%s);">选择实例步骤进行替换</a></td>'%(version.id,obj.step.template.id) data = [box_field,obj.step.template.name,obj.step.template.create_user.username, obj.step.name, obj.version.script.name, obj.version.name, obj.version.remarks, operation] sdicts["aaData"].append(data) return HttpResponse(json.dumps(sdicts, cls=LazyEncoder))
def version_remarks_edit(request): ''' version remarks edit ''' sdicts = {} version_id = request.GET.get('version_id', 0) user = request.user version = _script.get_version_by_params(id=version_id) if version: sdicts['result'] = 1 else: sdicts = {'result': 0} template_file = "script_manage/version_remarks_edit.html" html = render_to_string(template_file, locals()) sdicts['html'] = html sdicts['version_id'] = version_id return HttpResponse(json.dumps(sdicts, cls=LazyEncoder))
def version_remarks_edit(request): ''' version remarks edit ''' sdicts = {} version_id = request.GET.get('version_id',0) user = request.user version = _script.get_version_by_params(id=version_id) if version: sdicts['result'] = 1 else: sdicts = {'result':0} template_file = "script_manage/version_remarks_edit.html" html = render_to_string(template_file, locals()) sdicts['html'] = html sdicts['version_id'] = version_id return HttpResponse(json.dumps(sdicts, cls=LazyEncoder))
def version_remarks_save(request): ''' version remarks save ''' sdicts = {} user = request.user version_id = request.GET.get('version_id',0) version_remarks = request.GET.get('version_remarks','') code = 500 try: version = _script.get_version_by_params(id=version_id) version.remarks = version_remarks version.save() script = version.script script_type_display = script.get_script_type_display() version_list = _script.get_versions_by_params(script=script,is_delete=False) code = 200 except Exception,e: code = 500
def get_version_content(request, version_id): """ 获取脚本详情. """ version = _script.get_version_by_params(id=version_id) describe = version.script.describe status = 500 data = {'describe': describe} try: file_path = os.path.join(MEDIA_ROOT, 'scripts', '%s' % version.sfile) f = open(file_path) try: file_content = f.read() data.update({"content": file_content}) status = 200 except Exception, e: print e finally: f.close()
def get_version_content(request, version_id): """ 获取脚本详情. """ version = _script.get_version_by_params(id=version_id) describe = version.script.describe status = 500 data = {'describe':describe} try: file_path = os.path.join(MEDIA_ROOT,'scripts','%s'%version.sfile) f = open(file_path) try: file_content = f.read() data.update({"content":file_content}) status=200 except Exception,e: print e finally: f.close()
def version_remarks_save(request): ''' version remarks save ''' sdicts = {} user = request.user version_id = request.GET.get('version_id', 0) version_remarks = request.GET.get('version_remarks', '') code = 500 try: version = _script.get_version_by_params(id=version_id) version.remarks = version_remarks version.save() script = version.script script_type_display = script.get_script_type_display() version_list = _script.get_versions_by_params(script=script, is_delete=False) code = 200 except Exception, e: code = 500
def show_version_detail(request): user = request.user sdicts = {} code = 500 try: version_id = request.POST.get('version_id') version = _script.get_version_by_params(id=version_id) script = version.script file_path = os.path.join(MEDIA_ROOT, 'scripts', '%s' % version.sfile) file_content = '' f = open(file_path) try: file_content = f.read() except Exception, e: print e finally: f.close() code = 200
def show_version_detail(request): user = request.user sdicts = {} code = 500 try: version_id = request.POST.get('version_id') version = _script.get_version_by_params(id=version_id) script = version.script file_path = os.path.join(MEDIA_ROOT,'scripts','%s'%version.sfile) file_content = '' f = open(file_path) try: file_content = f.read() except Exception,e: print e finally: f.close() code = 200
def version_delete(request): ''' delete version ''' sdicts = {} version_id = request.POST.get('version_id', 0) user = request.user version = _script.get_version_by_params(id=version_id) if version: # jobStepScripts = _job.get_jobStepScripts_by_params(step__job__is_delete=False,version=version) templateStepScripts = _template.get_templateStepScripts_by_params( step__template__is_delete=False, version=version) # if jobStepScripts: # sdicts = {'result':0} # sdicts['showMsg'] = _(u"删除失败: 当前版本存在关联的作业实例") # elif templateStepScripts: # sdicts = {'result':0} # sdicts['showMsg'] = _(u"删除失败: 当前版本存在关联的作业模板") if templateStepScripts: sdicts = {'result': 0} sdicts['showMsg'] = _(u"删除失败: 当前版本存在关联的作业实例") else: version.is_delete = True version.save() sdicts['result'] = 1 sdicts['showMsg'] = _(u"删除成功") script = version.script script_type_display = script.get_script_type_display() version_list = _script.get_versions_by_params(script=script, is_delete=False) sdicts['script_id'] = script.id else: sdicts = {'result': 0} sdicts['showMsg'] = _(u"删除失败") template_file = "script_manage/script_view.html" html = render_to_string(template_file, locals()) sdicts['html'] = html return HttpResponse(json.dumps(sdicts, cls=LazyEncoder))
def version_delete(request): ''' delete version ''' sdicts = {} version_id = request.POST.get('version_id',0) user = request.user version = _script.get_version_by_params(id=version_id) if version: # jobStepScripts = _job.get_jobStepScripts_by_params(step__job__is_delete=False,version=version) templateStepScripts = _template.get_templateStepScripts_by_params(step__template__is_delete=False,version=version) # if jobStepScripts: # sdicts = {'result':0} # sdicts['showMsg'] = _(u"删除失败: 当前版本存在关联的作业实例") # elif templateStepScripts: # sdicts = {'result':0} # sdicts['showMsg'] = _(u"删除失败: 当前版本存在关联的作业模板") if templateStepScripts: sdicts = {'result':0} sdicts['showMsg'] = _(u"删除失败: 当前版本存在关联的作业实例") else: version.is_delete = True version.save() sdicts['result'] = 1 sdicts['showMsg'] = _(u"删除成功") script = version.script script_type_display = script.get_script_type_display() version_list = _script.get_versions_by_params(script=script,is_delete=False) sdicts['script_id'] = script.id else: sdicts = {'result':0} sdicts['showMsg'] = _(u"删除失败") template_file = "script_manage/script_view.html" html = render_to_string(template_file, locals()) sdicts['html'] = html return HttpResponse(json.dumps(sdicts, cls=LazyEncoder))
def version_copy(request): ''' copy version ''' sdicts = {} user = request.user code = 500 if request.method == 'POST': version_id = request.POST.get('version_id', 0) file_content = request.POST.get('file_content', '') version_copy = _script.get_version_by_params(id=version_id) if version_copy: script = version_copy.script script_type = script.script_type now = datetime.datetime.now() version_name = '%s_%s' % (now.strftime("%Y%m%d%H%M%S"), user.username) version = _script.get_or_create_version_by_params( script=script, name=version_name)[0] file_doc_num = version.id % 100 file_doc_path = os.path.join(MEDIA_ROOT, 'scripts', '%s' % file_doc_num) if not os.path.exists(file_doc_path): os.mkdir(file_doc_path) suffix = enum_script.SCIPT_TYPE_DICT.get(int(script.script_type)) filename = u'%s.%s' % (version_name, suffix) file_path = os.path.join(file_doc_path, filename) f = open(file_path, 'wb') try: f.write(file_content) except Exception, e: print e finally:
def version_job_sync(request): ''' version job sync ''' sdicts = {} user = request.user code = 500 if request.method == 'POST': try: version_id = request.POST.get('version_id',0) check_list = request.POST.getlist('check_list[]',[]) version = _script.get_version_by_params(id=version_id) for jobStepScript_id in check_list: if jobStepScript_id: jobStepScript = _job.get_jobStepScript_by_params(id=jobStepScript_id) jobStepScript.version = version jobStepScript.save() code = 200 except Exception,e: code = 500 sdicts['status'] = code return HttpResponse(json.dumps(sdicts))
def version_template_sync_v2(request): ''' version template sync v2 ''' sdicts = {} user = request.user code = 500 if request.method == 'POST': try: version_id = request.POST.get('version_id',0) check_list = request.POST.getlist('check_list[]',[]) version = _script.get_version_by_params(id=version_id) for templateStepScript_id in check_list: if templateStepScript_id: templateStepScript = _template.get_templateStepScript_by_params(id=templateStepScript_id) templateStepScript.version = version templateStepScript.save() code = 200 except Exception,e: code = 500 sdicts['status'] = code return HttpResponse(json.dumps(sdicts))
version_list = _script.get_versions_by_params(script=script, is_delete=False) code = 200 else: code = 500 template_file = "script_manage/script_view.html" html = render_to_string(template_file, locals()) sdicts['status'] = code sdicts['script_id'] = script.id sdicts['html'] = html return HttpResponse(json.dumps(sdicts)) else: version_id = request.GET.get('version_id', 0) version = _script.get_version_by_params(id=version_id) if version: script = version.script script_type = script.script_type file_path = os.path.join(MEDIA_ROOT, 'scripts', '%s' % version.sfile) file_content = '' f = open(file_path) try: file_content = f.read() except Exception, e: print e finally: f.close()
def template_step_save(request, template_step_id): """ save template step. """ req = request.POST user = request.user status = 500 data = {} msg = "" logger.debug("start template step save...") try: step = _template.get_templateStep_by_params(id=template_step_id) if not step: msg = u"步骤不存在" raise # if step.template.create_user!=user: # msg = u"您不是该步骤的所有者" # raise type_value = step.step_type sub_step = None if type_value == enum_template.STEP_TYPE_SCRIPT: sub_step = _template.get_templateStepScript_by_params(step=step) version_id = req.get("step_version", "") parameter = req.get("parameter", "") timeout = req.get("timeout", 0) if version_id and version_id: version = _script.get_version_by_params(id=version_id) sub_step.version = version sub_step.parameter = "" if not parameter else parameter sub_step.timeout = 0 if not timeout else timeout elif type_value == enum_template.STEP_TYPE_PULL_FILE: sub_step = _template.get_templateStepPullFile_by_params(step=step) target_path = req.get("pull_file_target_path", "") pull_to_ip = req.get("pull_to_ip", "") src_paths = req.get("edit_pullfile_hide", "") path_list = list(set(src_paths.split(","))) path_list = sorted(path_list) sub_step.pull_to = target_path sub_step.pull_to_ip = pull_to_ip sub_step.file_paths = json.dumps(path_list) elif type_value == enum_template.STEP_TYPE_PUSH_FILE: sub_step = _template.get_templateStepPushFile_by_params(step=step) send_file_target_path = req.get("send_file_target_path", "") record_local_ids_data = req.get("edit_sendfile_record", "") send_file_remote_json = req.get("send_file_remote_files_list", {}) sub_step.push_to = send_file_target_path file_info_ids = set() ### add remote file info. if send_file_remote_json: send_file_remote_json = json.loads(send_file_remote_json) send_file_remote_json = send_file_remote_json.get("remote", []) if send_file_remote_json: for item in send_file_remote_json: path = item.get("name", "").strip() remote_ip = item.get("ip", "").strip() # remote_account = item.get("account_id","").strip() file_info, _ = _template.get_or_create_templateFileInfo_by_params( step=step, remote_ip=remote_ip, remote_path=path, location_type=enum_template.UPLOAD_TYPE_REMOTE, push_account=None) file_info_ids.add(file_info.id) # add local file record ids. if record_local_ids_data: record_local_ids_data = record_local_ids_data.strip(' ') record_local_ids = record_local_ids_data.split(' ') for record_id in record_local_ids: record = _file.get_uploadRecord_by_params(pk=record_id) file_info, _ = _template.get_or_create_templateFileInfo_by_params( step=step, remote_ip=record.src_ip, remote_path=record.remote_path, location_type=enum_template.UPLOAD_TYPE_LOCAL) file_info.record = record file_info.save() print "==== id: ", file_info.id file_info_ids.add(file_info.id) #### delete old pushfileinfo records. _template.get_templateFileInfos_by_params(step=step).exclude( pk__in=file_info_ids).delete() elif type_value == enum_template.STEP_TYPE_TEXT: sub_step = _template.get_templateStepText_by_params(step=step) sub_step.describe = req.get("step_description", "") if type_value in [ enum_template.STEP_TYPE_PULL_FILE, enum_template.STEP_TYPE_PUSH_FILE, enum_template.STEP_TYPE_SCRIPT ]: if req.get("dst_use_own", ""): ips_json = req.get('target_ips_hide', "") ip_set = get_input_ips(ips_json) step.target = json.dumps(list(ip_set)) step.is_setting = 1 if type_value == enum_template.STEP_TYPE_SCRIPT: account_id = req.get("template_account", "") if account_id: account = _account.get_account_by_params( id=int(account_id)) step.account = account else: step.target = "" step.is_setting = 0 step.name = req.get("step_name", step.name) step.describe = req.get("step_description", "") step.save() sub_step.save() status = 200 data['msg'] = u"步骤保存成功" change_template_sync_status(step.template) except Exception, e: data['msg'] = msg if msg else u"步骤保存失败" error = traceback.format_exc() logger.error(error) print error
def template_step_save(request,template_step_id): """ save template step. """ req = request.POST user = request.user status = 500 data = {} msg = "" logger.debug("start template step save...") try: step = _template.get_templateStep_by_params(id=template_step_id) if not step: msg = u"步骤不存在" raise # if step.template.create_user!=user: # msg = u"您不是该步骤的所有者" # raise type_value = step.step_type sub_step = None if type_value == enum_template.STEP_TYPE_SCRIPT: sub_step = _template.get_templateStepScript_by_params(step=step) version_id = req.get("step_version","") parameter = req.get("parameter","") timeout = req.get("timeout",0) if version_id and version_id: version = _script.get_version_by_params(id=version_id) sub_step.version = version sub_step.parameter = "" if not parameter else parameter sub_step.timeout = 0 if not timeout else timeout elif type_value == enum_template.STEP_TYPE_PULL_FILE: sub_step = _template.get_templateStepPullFile_by_params(step=step) target_path = req.get("pull_file_target_path","") pull_to_ip = req.get("pull_to_ip","") src_paths = req.get("edit_pullfile_hide","") path_list = list(set(src_paths.split(","))) path_list = sorted(path_list) sub_step.pull_to = target_path sub_step.pull_to_ip = pull_to_ip sub_step.file_paths = json.dumps(path_list) elif type_value == enum_template.STEP_TYPE_PUSH_FILE: sub_step = _template.get_templateStepPushFile_by_params(step=step) send_file_target_path = req.get("send_file_target_path","") record_local_ids_data = req.get("edit_sendfile_record","") send_file_remote_json = req.get("send_file_remote_files_list",{}) sub_step.push_to = send_file_target_path file_info_ids = set() ### add remote file info. if send_file_remote_json: send_file_remote_json = json.loads(send_file_remote_json) send_file_remote_json = send_file_remote_json.get("remote",[]) if send_file_remote_json: for item in send_file_remote_json: path = item.get("name","").strip() remote_ip = item.get("ip","").strip() # remote_account = item.get("account_id","").strip() file_info,_ = _template.get_or_create_templateFileInfo_by_params(step=step, remote_ip=remote_ip, remote_path=path, location_type=enum_template.UPLOAD_TYPE_REMOTE, push_account=None) file_info_ids.add(file_info.id) # add local file record ids. if record_local_ids_data: record_local_ids_data = record_local_ids_data.strip(' ') record_local_ids = record_local_ids_data.split(' ') for record_id in record_local_ids: record = _file.get_uploadRecord_by_params(pk=record_id) file_info,_ = _template.get_or_create_templateFileInfo_by_params(step=step, remote_ip=record.src_ip, remote_path=record.remote_path, location_type=enum_template.UPLOAD_TYPE_LOCAL) file_info.record = record file_info.save() print "==== id: ",file_info.id file_info_ids.add(file_info.id) #### delete old pushfileinfo records. _template.get_templateFileInfos_by_params(step=step).exclude(pk__in=file_info_ids).delete() elif type_value == enum_template.STEP_TYPE_TEXT: sub_step = _template.get_templateStepText_by_params(step=step) sub_step.describe = req.get("step_description","") if type_value in [enum_template.STEP_TYPE_PULL_FILE,enum_template.STEP_TYPE_PUSH_FILE,enum_template.STEP_TYPE_SCRIPT]: if req.get("dst_use_own",""): ips_json = req.get('target_ips_hide',"") ip_set = get_input_ips(ips_json) step.target = json.dumps(list(ip_set)) step.is_setting = 1 if type_value == enum_template.STEP_TYPE_SCRIPT: account_id = req.get("template_account","") if account_id: account = _account.get_account_by_params(id=int(account_id)) step.account = account else: step.target = "" step.is_setting = 0 step.name = req.get("step_name",step.name) step.describe = req.get("step_description","") step.save() sub_step.save() status=200 data['msg'] = u"步骤保存成功" change_template_sync_status(step.template) except Exception,e: data['msg'] = msg if msg else u"步骤保存失败" error = traceback.format_exc() logger.error(error) print error
script_type_display = script.get_script_type_display() version_list = _script.get_versions_by_params(script=script,is_delete=False) code = 200 else: code = 500 template_file = "script_manage/script_view.html" html = render_to_string(template_file, locals()) sdicts['status'] = code sdicts['script_id'] = script.id sdicts['html'] = html return HttpResponse(json.dumps(sdicts)) else: version_id = request.GET.get('version_id',0) version = _script.get_version_by_params(id=version_id) if version: script = version.script script_type = script.script_type file_path = os.path.join(MEDIA_ROOT,'scripts','%s'%version.sfile) file_content = '' f = open(file_path) try: file_content = f.read() except Exception,e: print e finally: f.close() code = 200