Exemplo n.º 1
0
def template_step_delete(request, template_step_id):
    """
        check and delete template_step by id.
    """
    user = request.user
    status = 500
    data = {}
    msg = ""

    try:
        template_step = _template.get_templateStep_by_params(id=template_step_id,is_delete=False)
        if not template_step:
            msg = u"template step not exist."
            raise Exception(msg)
        elif template_step.template.create_user!=user:
            msg = u"authorize fail, template not belong to you."
            raise Exception(msg)
        template_step.is_delete = True
        template_step.save()
        old_steps = _template.get_templateSteps_by_params(template=template_step.template,is_delete=False,order__gt=template_step.order)
        for step in old_steps:
            step.order -= 1
            step.save()
        status=200
        data['msg'] = u"模板步骤删除成功"
        
        change_template_sync_status(template_step.template)
    except:
        data['msg'] = msg if msg else u"模板步骤删除失败"
        error = traceback.format_exc()
        print error
    return HttpResponse(json.dumps({
        'status':status,
        'result':data
    }, cls=LazyEncoder))
Exemplo n.º 2
0
def template_sort(request, template_id):
    """
        re-order template steps.
    """
    user = request.user
    template = _template.get_template_by_params(id=template_id)
    status = 500
    data = {}
    msg = ""
    try:
        step_id_list = request.POST.get("step_id_list","")
        order = 1
        for step_id in step_id_list.split(","):
            step = _template.get_templateStep_by_params(id=step_id)
            if not step:
                msg = "template step not exist: {0}".format(step_id)
                raise Exception(msg)
            step.order = order
            step.save()
            order += 1
        status = 200
        
        change_template_sync_status(template)
    except:
        msg = u"模板步骤移动失败" if not msg else msg
        error = traceback.format_exc()
        print error
    data['msg']=msg
    return HttpResponse(json.dumps({
        'status':status,
        'result':data
    }, cls=LazyEncoder))
Exemplo n.º 3
0
def template_sort(request, template_id):
    """
        re-order template steps.
    """
    user = request.user
    template = _template.get_template_by_params(id=template_id)
    status = 500
    data = {}
    msg = ""
    try:
        step_id_list = request.POST.get("step_id_list", "")
        order = 1
        for step_id in step_id_list.split(","):
            step = _template.get_templateStep_by_params(id=step_id)
            if not step:
                msg = "template step not exist: {0}".format(step_id)
                raise Exception(msg)
            step.order = order
            step.save()
            order += 1
        status = 200

        change_template_sync_status(template)
    except:
        msg = u"模板步骤移动失败" if not msg else msg
        error = traceback.format_exc()
        print error
    data['msg'] = msg
    return HttpResponse(
        json.dumps({
            'status': status,
            'result': data
        }, cls=LazyEncoder))
Exemplo n.º 4
0
def template_step_delete(request, template_step_id):
    """
        check and delete template_step by id.
    """
    user = request.user
    status = 500
    data = {}
    msg = ""

    try:
        template_step = _template.get_templateStep_by_params(
            id=template_step_id, is_delete=False)
        if not template_step:
            msg = u"template step not exist."
            raise Exception(msg)
        elif template_step.template.create_user != user:
            msg = u"authorize fail, template not belong to you."
            raise Exception(msg)
        template_step.is_delete = True
        template_step.save()
        old_steps = _template.get_templateSteps_by_params(
            template=template_step.template,
            is_delete=False,
            order__gt=template_step.order)
        for step in old_steps:
            step.order -= 1
            step.save()
        status = 200
        data['msg'] = u"模板步骤删除成功"

        change_template_sync_status(template_step.template)
    except:
        data['msg'] = msg if msg else u"模板步骤删除失败"
        error = traceback.format_exc()
        print error
    return HttpResponse(
        json.dumps({
            'status': status,
            'result': data
        }, cls=LazyEncoder))
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
Exemplo n.º 7
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.º 8
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