Exemplo n.º 1
0
def template_step_list(request, template_id):
    """
        show template step list of given template.
    """
    data = request.GET

    iDisplayLength = int(data.get('iDisplayLength'))
    iDisplayStart = int(data.get('iDisplayStart'))
    sEcho = int(data.get('sEcho'))
    iSortCol_0 = int(data.get('iSortCol_0', 0))
    sSortDir_0 = data.get('sSortDir_0')
    order_list = [None, None, None, None]
    order_item = order_list[iSortCol_0]

    template_steps = _template.get_templateSteps_by_params(
        template__id=template_id, is_delete=False)
    template_steps = template_steps.order_by('order')
    sdicts = {}
    sdicts["aaData"] = []
    if iDisplayLength > -1:
        p = Paginator(template_steps, iDisplayLength)
        total = p.count  #总数
        page_range = p.page_range  #页数list
        sdicts["sEcho"] = sEcho
        sdicts["iTotalRecords"] = total
        sdicts["iTotalDisplayRecords"] = total

        page = p.page(page_range[iDisplayStart / iDisplayLength])
        object_list = page.object_list
    else:
        object_list = template_steps

    for obj in object_list:
        if obj.step_type == enum_template.STEP_TYPE_SCRIPT:
            sub_step = _template.get_templateStepScript_by_params(step=obj)
        elif obj.step_type == enum_template.STEP_TYPE_PULL_FILE:
            sub_step = _template.get_templateStepPullFile_by_params(step=obj)
        elif obj.step_type == enum_template.STEP_TYPE_PUSH_FILE:
            sub_step = _template.get_templateStepPushFile_by_params(step=obj)
        elif obj.step_type == enum_template.STEP_TYPE_TEXT:
            sub_step = _template.get_templateStepText_by_params(step=obj)
        else:
            raise Exception("================ invalid step:" + obj.step_type)
        template_step_name = u'<a href="javascript:void(0)" onclick="template_step_edit(\'%s\',\'%s\');">%s</a>' % (
            obj.id, "script" if obj.step_type == enum_template.STEP_TYPE_SCRIPT
            else "", obj.name)
        operation = u"""<div class='btn-group'><a class='dropdown-toggle' data-toggle='dropdown' href='#'>增加步骤</a><ul class='dropdown-menu add-step-list'><li value='1'><a>执行脚本</a></li><li value='2'><a>分发文件</a></li><li value='3'><a>拉取文件</a></li><li value='4'><a>文本步骤</a></li></ul></div>&nbsp;&nbsp;<a href='javascript:void(0)' class='remove-step' step_id="{0}">删除</a>""".format(
            obj.id)
        data = [
            u"第%s步" % (obj.order), template_step_name,
            obj.get_step_type_display(), operation
        ]
        sdicts["aaData"].append(data)
    return HttpResponse(json.dumps(sdicts, cls=LazyEncoder))
Exemplo n.º 2
0
def template_step_list_v2(request,template_id):
    """
        show template step list of given template.
    """
    data = request.GET

    iDisplayLength = int(data.get('iDisplayLength'))
    iDisplayStart = int(data.get('iDisplayStart'))
    sEcho = int(data.get('sEcho'))
    iSortCol_0 = int(data.get('iSortCol_0',0))
    sSortDir_0 = data.get('sSortDir_0')
    order_list = [None,None,None, None]
    order_item = order_list[iSortCol_0]

    template_steps = _template.get_templateSteps_by_params(template__id=template_id,is_delete=False)
    template_steps = template_steps.order_by('order')
    sdicts = {}
    sdicts["aaData"] = []
    if iDisplayLength>-1:
        p = Paginator(template_steps, iDisplayLength)
        total = p.count #总数
        page_range = p.page_range #页数list
        sdicts["sEcho"] = sEcho
        sdicts["iTotalRecords"] = total
        sdicts["iTotalDisplayRecords"] = total

        page = p.page(page_range[iDisplayStart / iDisplayLength])
        object_list = page.object_list
    else:
        object_list = template_steps

    for obj in object_list:
        if obj.is_checked:
            checkbox = '<input value="%s" class="jobview-checked-it-%s" type="checkbox" checked>'%(obj.id,obj.template.id)
        else:
            checkbox = '<input value="%s" class="jobview-checked-it-%s" type="checkbox">'%(obj.id,obj.template.id)
        if obj.step_type == enum_template.STEP_TYPE_SCRIPT:
            sub_step = _template.get_templateStepScript_by_params(step=obj)
        elif obj.step_type == enum_template.STEP_TYPE_PULL_FILE:
            sub_step = _template.get_templateStepPullFile_by_params(step=obj)
        elif obj.step_type == enum_template.STEP_TYPE_PUSH_FILE:
            sub_step = _template.get_templateStepPushFile_by_params(step=obj)
        elif obj.step_type == enum_template.STEP_TYPE_TEXT:
            sub_step = _template.get_templateStepText_by_params(step=obj)
        else:
            raise Exception("================ invalid step:"+obj.step_type)
        template_step_name = u'<a href="javascript:void(0)" onclick="template_step_edit_v2(\'%s\',\'%s\');">%s</a>' % (obj.id,"script" if obj.step_type == enum_template.STEP_TYPE_SCRIPT else "", obj.name)
        operation = u"""<div class='btn-group'><a class='dropdown-toggle' data-toggle='dropdown' href='#'>增加步骤</a><ul class='dropdown-menu add-step-list'><li value='1'><a>执行脚本</a></li><li value='2'><a>分发文件</a></li><li value='3'><a>拉取文件</a></li><li value='4'><a>文本步骤</a></li></ul></div>&nbsp;&nbsp;<a href='javascript:void(0)' class='remove-step' step_id="{0}">删除</a>""".format(obj.id)
        data = [checkbox, u"第%s步"%(obj.order), template_step_name, obj.get_step_type_display(),operation]
        sdicts["aaData"].append(data)
    return HttpResponse(json.dumps(sdicts, cls=LazyEncoder))
Exemplo n.º 3
0
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
Exemplo n.º 4
0
def template_step_edit_v2(request,template_step_id):
    """
        edit template step.
    """
    req = request.POST
    user = request.user
    status = 500
    data = {}
    msg = ""
    try:
        step = _template.get_templateStep_by_params(id=template_step_id)
        if not step:
            msg = u"步骤不存在"
            raise Exception,msg
        # if step.template.create_user!=user:
        #     msg = u"您不是该步骤的所有者"
        #     raise Exception,msg

        type_value = step.step_type
        template_file = ""
        if type_value == enum_template.STEP_TYPE_SCRIPT:
            template_file = "job_manage/edit_script.html"
            sub_step = _template.get_templateStepScript_by_params(step=step)
            perms = _account.get_perms_by_params(to_user=user, is_delete=False, ptype=enum_account.PERM_PTYPE_SCRIPT)
            scripts = _script.Script.objects.filter(Q(create_user=user)|Q(pk__in=[perm.object_id for perm in perms]),
                                                    is_delete=False,is_once=False)
            version = sub_step.version
            script = version.script if version and version.script in scripts else None
            versions = _script.get_versions_by_params(script=script,is_delete=False) if script else None
            
            scrip_id_list = [script.id for script in scripts]
            user_set = set()
            user_set.add(user)
            accounts_perm = _account.get_perms_by_params(object_id__in=scrip_id_list, is_delete=False)
            for account_perm in accounts_perm:
                user_set.add(account_perm.create_user)
                user_set.add(account_perm.to_user)
            accounts = _account.get_accounts_by_params(is_delete=False,create_user__in=list(user_set))
            
            accountForm = AccountForm(auto_id="step_script_%s")
            account_id = step.account.id if step.account else 0

        elif type_value == enum_template.STEP_TYPE_PULL_FILE:
            template_file = "job_manage/edit_pull_file.html"
            sub_step = _template.get_templateStepPullFile_by_params(step=step)
            file_paths = [] if not sub_step.file_paths else json.loads(sub_step.file_paths)

        elif type_value == enum_template.STEP_TYPE_PUSH_FILE:
            template_file = "job_manage/edit_send_file.html"
            sub_step = _template.get_templateStepPushFile_by_params(step=step)
            file_infos = _template.get_templateFileInfos_by_params(step=step)
            local_uploads = file_infos.filter(location_type=enum_template.UPLOAD_TYPE_LOCAL).values('record')
            remote_uploads = file_infos.filter(location_type=enum_template.UPLOAD_TYPE_REMOTE)
            local_ids = "" if not local_uploads else " ".join(str(item.get('record')) for item in local_uploads)+" "
            remote_uploads_str = ""
            if remote_uploads.count()>0:
                remote_dict = {}
                remote_dict['remote'] = [{"name":item.remote_path,"ip":item.remote_ip} for item in remote_uploads]
                remote_uploads_str = json.dumps(remote_dict)
            user = request.user
            max_upload_size = MAX_UPLOAD_SIZE
            fileserver_url = FILE_SERVER_HOST

        elif type_value == enum_template.STEP_TYPE_TEXT:
            template_file = "job_manage/edit_text.html"
            sub_step = _template.get_templateStepText_by_params(step=step)

        if type_value in [enum_template.STEP_TYPE_PULL_FILE,enum_template.STEP_TYPE_PUSH_FILE,enum_template.STEP_TYPE_SCRIPT]:
            target_ips = json.loads(step.target) if step.target else []
            hide_ip_dict = build_hidden_ip_dict(target_ips)
            hide_ip_json = json.dumps(hide_ip_dict)

        status=200
        data['html'] = render_to_string(template_file,locals())
        data['name'] = step.name
    except Exception,e:
        data['msg'] = msg if msg else u"步骤信息获取失败"
        error = traceback.format_exc()
        logger.error(error)
        print error
Exemplo n.º 5
0
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
Exemplo n.º 6
0
def template_step_edit_v2(request, template_step_id):
    """
        edit template step.
    """
    req = request.POST
    user = request.user
    status = 500
    data = {}
    msg = ""
    try:
        step = _template.get_templateStep_by_params(id=template_step_id)
        if not step:
            msg = u"步骤不存在"
            raise Exception, msg
        # if step.template.create_user!=user:
        #     msg = u"您不是该步骤的所有者"
        #     raise Exception,msg

        type_value = step.step_type
        template_file = ""
        if type_value == enum_template.STEP_TYPE_SCRIPT:
            template_file = "job_manage/edit_script.html"
            sub_step = _template.get_templateStepScript_by_params(step=step)
            perms = _account.get_perms_by_params(
                to_user=user,
                is_delete=False,
                ptype=enum_account.PERM_PTYPE_SCRIPT)
            scripts = _script.Script.objects.filter(
                Q(create_user=user)
                | Q(pk__in=[perm.object_id for perm in perms]),
                is_delete=False,
                is_once=False)
            version = sub_step.version
            script = version.script if version and version.script in scripts else None
            versions = _script.get_versions_by_params(
                script=script, is_delete=False) if script else None

            scrip_id_list = [script.id for script in scripts]
            user_set = set()
            user_set.add(user)
            accounts_perm = _account.get_perms_by_params(
                object_id__in=scrip_id_list, is_delete=False)
            for account_perm in accounts_perm:
                user_set.add(account_perm.create_user)
                user_set.add(account_perm.to_user)
            accounts = _account.get_accounts_by_params(
                is_delete=False, create_user__in=list(user_set))

            accountForm = AccountForm(auto_id="step_script_%s")
            account_id = step.account.id if step.account else 0

        elif type_value == enum_template.STEP_TYPE_PULL_FILE:
            template_file = "job_manage/edit_pull_file.html"
            sub_step = _template.get_templateStepPullFile_by_params(step=step)
            file_paths = [] if not sub_step.file_paths else json.loads(
                sub_step.file_paths)

        elif type_value == enum_template.STEP_TYPE_PUSH_FILE:
            template_file = "job_manage/edit_send_file.html"
            sub_step = _template.get_templateStepPushFile_by_params(step=step)
            file_infos = _template.get_templateFileInfos_by_params(step=step)
            local_uploads = file_infos.filter(
                location_type=enum_template.UPLOAD_TYPE_LOCAL).values('record')
            remote_uploads = file_infos.filter(
                location_type=enum_template.UPLOAD_TYPE_REMOTE)
            local_ids = "" if not local_uploads else " ".join(
                str(item.get('record')) for item in local_uploads) + " "
            remote_uploads_str = ""
            if remote_uploads.count() > 0:
                remote_dict = {}
                remote_dict['remote'] = [{
                    "name": item.remote_path,
                    "ip": item.remote_ip
                } for item in remote_uploads]
                remote_uploads_str = json.dumps(remote_dict)
            user = request.user
            max_upload_size = MAX_UPLOAD_SIZE
            fileserver_url = FILE_SERVER_HOST

        elif type_value == enum_template.STEP_TYPE_TEXT:
            template_file = "job_manage/edit_text.html"
            sub_step = _template.get_templateStepText_by_params(step=step)

        if type_value in [
                enum_template.STEP_TYPE_PULL_FILE,
                enum_template.STEP_TYPE_PUSH_FILE,
                enum_template.STEP_TYPE_SCRIPT
        ]:
            target_ips = json.loads(step.target) if step.target else []
            hide_ip_dict = build_hidden_ip_dict(target_ips)
            hide_ip_json = json.dumps(hide_ip_dict)

        status = 200
        data['html'] = render_to_string(template_file, locals())
        data['name'] = step.name
    except Exception, e:
        data['msg'] = msg if msg else u"步骤信息获取失败"
        error = traceback.format_exc()
        logger.error(error)
        print error