示例#1
0
def touchcron(request):
    "立刻执行cron"
    getinfo = request.GET
    postinfo = request.POST
    tid = getinfo.get("tid")
    server = get_cron_serve()
    buff = {}
    try:
        int(tid)
    except:
        buff["status"], buff["message"] = False, "tid不合法!"
    else:
        try:
            _cron = Task.objects.get(pk=int(tid))
        except ObjectDoesNotExist:
            buff["status"], buff["message"] = False, "Error! 此Cron不存在!请重试!"
        else:
            if _cron.status != 1:
                buff["status"] = False
                buff["message"] = "此task状态为%s,不可触发!" % _cron.get_status()
            else:
                try:
                    result = server.runcron(tid)
                    if isinstance(result, tuple):
                        buff["status"], buff["message"] = result
                    else:
                        buff["status"] = True
                        buff["message"] = "Success!已触发此cron!"
                except Exception as e:
                    buff["status"] = False
                    buff["message"] = str(e)
    return HttpResponse(json.dumps(buff, ensure_ascii=False),
                        mimetype="application/javascript")
示例#2
0
def removecron(request):
    "删除cron"
    getinfo = request.GET
    postinfo = request.POST
    tid = getinfo.get("tid")
    server = get_cron_serve()
    buff = {}
    try:
        int(tid)
    except:
        buff["status"], buff["message"] = False, "tid不合法!"
    else:
        try:
            task = Task.objects.get(pk=int(tid))
        except ObjectDoesNotExist:
            buff["status"], buff["message"] = True, "Success! 已删除此cron!"
        else:
            try:
                result = server.removecron(tid)
                if isinstance(result, tuple):
                    buff["status"], buff["message"] = result
                else:
                    buff["status"] = True
                    buff["message"] = "Success!已删除此cron!"
            except Exception as e:
                buff["status"] = False
                buff["message"] = str(e)
    return HttpResponse(json.dumps(buff, ensure_ascii=False),
                        mimetype="application/javascript")
示例#3
0
def  stopcron(request):
    "stop cron "
    getinfo=request.GET
    postinfo=request.POST
    tid=getinfo.get("tid")
    server=get_cron_serve()
    buff={}
    try:
        int(tid)
    except:
        buff["status"]=False
        buff["message"]="ID不合法!"
    try:
        cron= Task.objects.get(pk=int(tid))
    except:
        buff["status"]=False
        buff["message"]="此tid不存在"
    else:
        if cron.status==1:
            try:
                result=server.stopcron(int(tid))
                if isinstance(result,tuple):
                    buff["status"],buff["message"]=result
                else:
                    buff["status"]=True
                    buff["message"]="Success!已禁用此cron!"
            except Exception as e:
                buff["status"]=False
                buff["message"]=str(e)
        elif cron.status==-1:
            buff["status"],buff["message"]=True,"Success!已禁用此Cron!"
        else:
            buff["status"],buff["message"]=False,"Error,此task状态为%s,不可禁用!"%cron.get_status()
    return  HttpResponse(json.dumps(buff,ensure_ascii=False),mimetype="application/javascript")
示例#4
0
def modifycron(request):
    "更改cron信息"
    getinfo = request.GET
    postinfo = request.POST
    tid = getinfo.get("tid")
    _ip = postinfo.get("ip")
    name = postinfo.get("name")
    rule = postinfo.get("rule")

    svnpath = postinfo.get("url").strip()
    version = postinfo.get("version")
    svnuser = postinfo.get("svnuser")
    svnpasswd = postinfo.get("svnpasswd")
    info = postinfo.get("fn")
    args = ""
    filename = postinfo.get("filename")

    serve = get_cron_serve()
    buff = {}
    try:
        task = Task.objects.get(pk=int(tid))
    except:
        buff["status"] = False
        buff["message"] = "Error!无效tid!"
    else:
        ip = task.ip
        if ip != _ip:
            buff["status"] = False
            buff["message"] = "不允许修改IP!"
        elif name != task.name:
            buff["status"] = False
            buff["message"] = "不允许修改计划任务名!"

        else:
            valid_result = valid_input(ip, name, rule, svnpath, version,
                                       svnuser, svnpasswd, info, args,
                                       filename)
            if valid_result != True:
                buff["status"], buff["message"] = valid_result
            else:
                if version == "*":
                    versioninfo = get_svn_top_version(svnpath, svnuser,
                                                      svnpasswd)
                    if versioninfo[0] == False:
                        buff["status"], buff["message"] = versioninfo
                    else:
                        version = versioninfo[1]
                if buff == {}:
                    result = serve.modifycron(tid, name, svnpath, str(version),
                                              svnuser, svnpasswd, rule, info,
                                              request.user.username, args,
                                              filename)
                    if result != True:
                        buff["status"], buff["message"] = result
                    else:
                        buff["status"] = True
                        buff["message"] = "Success!"
    return HttpResponse(json.dumps(buff, ensure_ascii=False, indent=True),
                        mimetype="application/javascript")
示例#5
0
def add(request):
    "add cron"
    getinfo = request.GET
    postinfo = request.POST
    print getinfo
    print postinfo
    if getinfo.has_key("data"):
        "json data "
        datatype = getinfo.get("data")
        if datatype == "table":
            "表格数据填充"
            pageindex = getinfo.get("page")
            pageindex = int(pageindex)
            rowsnum = getinfo.get("rows")
            rowsnum = int(rowsnum)
            task_queryset = Task.objects.all().order_by("-tid")
            buff = {}
            #buff["page"]=pageindex
            task_pagiobj = Paginator(task_queryset, rowsnum)
            buff["records"] = task_pagiobj.count
            buff["total"] = task_pagiobj.num_pages
            try:
                task_list = task_pagiobj.page(pageindex)
            except:
                task_list = task_pagiobj.page(task_pagiobj.num_pages)
            buff["page"] = task_list.number
            buff["rows"] = [{
                "cell": [
                    i.tid, i.name,
                    i.get_status(), i.rule, i.project, i.app,
                    i.get_info(),
                    i.get_owner_and_type(), i.ip
                ]
            } for i in task_list]
        elif datatype == "add":
            "增加计划任务"
            ip = postinfo.get("ip")
            name = postinfo.get("name")
            rule = postinfo.get("rule")
            svnpath = postinfo.get("url").strip()
            version = postinfo.get("version")
            svnuser = postinfo.get("svnuser")
            svnpasswd = postinfo.get("svnpasswd")
            info = postinfo.get("fn")
            _type = postinfo.get("type")
            args = ""
            filename = postinfo.get("filename")
            buff = {}

            valie_result = valid_input(ip, name, rule, svnpath, version,
                                       svnuser, svnpasswd, info, args,
                                       filename)
            if valie_result != True:
                buff["status"], buff["message"] = valie_result
            else:
                if version == "*":
                    versioninfo = get_svn_top_version(svnurl=svnpath,
                                                      svnuser=svnuser,
                                                      svnpasswd=svnpasswd)
                    if versioninfo[0] == False:
                        buff["status"], buff["message"] = versioninfo
                    else:
                        version = versioninfo[1]
                if buff == {}:
                    server = get_cron_serve()

                    result = server.addcron(name, svnpath, str(version),
                                            svnuser, svnpasswd, rule, info,
                                            request.user.username, args,
                                            filename)
                    if result != True:
                        buff["status"], buff["message"] = result
                    else:
                        buff["status"] = True
                        buff["message"] = "Success!"
            return HttpResponse(json.dumps(buff, ensure_ascii=False),
                                mimetype="application/javascript")

    else:
        return render_to_response("add.html",
                                  locals(),
                                  context_instance=RequestContext(request))