def useradd(request): path = '添加用户' menuif_5 = 1 menuif_5_1 = 1 if request.method == 'POST': userid = request.POST.get('id', None) username = request.POST['username'] email = request.POST['email'] if userid == None: userslist.objects.create(username=username, email=email) #写入日志 optype = '添加用户' describe = username date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") opnotes(date, optype, describe) return HttpResponseRedirect('/users/userlist') else: userslist.objects.filter(id=userid).update(username=username, email=email) #写入日志 optype = '修改用户' describe = username date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") opnotes(date, optype, describe) return HttpResponseRedirect('/users/userlist') else: return render_to_response('useradd.html', { 'menuif_5': menuif_5, 'menuif_5_1': menuif_5_1, 'path': path })
def addtask(request): menuif_2 =1 menuif_2_1 =1 menuif_2_1_1 =1 users=userslist.objects.all().order_by("id") path='添加任务' if request.method == 'POST': cronid=request.POST.get('id', None) personal=request.POST['personal'] describe=request.POST['describe'] expression=request.POST['expression'] if cronid == None: cronlist.objects.create(personal=personal,describe=describe,expression=expression) #写入日志 optype='步骤添加' describe=describe date=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") opnotes(date,optype,describe) return HttpResponseRedirect('/task/tasklist') else: cronlist.objects.filter(id=cronid).update(personal=personal,describe=describe,expression=expression) #写入日志 optype='任务修改' describe=str(cronid)+describe date=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") opnotes(date,optype,describe) return HttpResponseRedirect('/task/checke/'+str(cronid)) else: alert="alert-success hide-default" return render_to_response('addtask.html',{'menuif_2':menuif_2,'menuif_2_1':menuif_2_1,'menuif_2_1_1':menuif_2_1_1,'path':path,'alert':alert,'users':users})
def sendfile(request): if request.method == 'POST': operation = request.POST.get('operation', None) if operation == 'end': cron=cronedition.objects.all().order_by("-id")[0] nodes=nodeslist.objects.all() for node in nodes: hostname = node.nodeip port = int(node.nodeport) username = node.nodename password = node.nodepass paramiko.util.log_to_file('/data/crontab/logs/paramiko.log') client = paramiko.Transport((hostname, port)) client.connect(username = username, password = password) sftp = paramiko.SFTPClient.from_transport(client) localpath='/data/crontab/cronfile/%s' % (cron.edition) remotepath='/var/spool/cron/root' sftp.put(localpath,remotepath) client.close() #写入日志 optype='分发cron' describe='分发cron文件至各节点' date=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") opnotes(date,optype,describe) return HttpResponse(u"Success")
def cronfile(request): if request.method == 'POST': operation = request.POST.get('operation', None) if operation == 'create': tasklist=cronlist.objects.all().order_by("id") #获取当前时间 date=datetime.datetime.now() #版本 edition=date.strftime("%Y%m%d%H%M%S") #更新时间 updatetime=date.strftime("%Y-%m-%d %H:%M:%S") #统计任务数 crontotal=len(tasklist) cronfilepath="/data/crontab/cronfile/%s" % (str(edition)) f=file(cronfilepath,"a+") f.write('#crontab配置文件由XesCron系统生成,共 '+str(crontotal)+' 条任务,更新时间 '+updatetime+'\n\n') for task in tasklist: Notes='#%s-%s#\n' % (task.personal,task.describe) expression="%s\n\n" %(task.expression) f.write(Notes.encode("utf-8")) f.write(expression.encode("utf-8")) f.close() #操作完成入库 cronstate=True cronedition.objects.create(edition=edition,updatetime=updatetime,crontotal=crontotal,cronstate=cronstate) #写入日志 optype='生成cron' describe='任务数'+str(crontotal) date=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") opnotes(date,optype,describe) return HttpResponse(u"Success")
def redeleteall(request): recyclelist.objects.all().delete() #写入日志 optype='清空回收站' describe='清空回收站' date=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") opnotes(date,optype,describe) return HttpResponseRedirect('/task/recycle')
def nodedelete(request, id): node = nodeslist.objects.get(id=id) nodeslist.objects.get(id=id).delete() #写入日志 optype = '节点删除' describe = node.nodeip date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") opnotes(date, optype, describe) return HttpResponseRedirect('/nodes/list')
def delete(request,a): cron=cronlist.objects.get(id=a) date=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") recyclelist.objects.create(id=cron.id,personal=cron.personal,describe=cron.describe,expression=cron.expression,date=date) cronlist.objects.get(id=a).delete() #写入日志 optype='删除到回收站' describe=str(cron.id)+cron.personal date=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") opnotes(date,optype,describe) return HttpResponseRedirect('/task/tasklist')
def userdelete(request, id): menuif_5 = 1 menuif_5_1 = 1 users = userslist.objects.get(id=id) userslist.objects.get(id=id).delete() #写入日志 optype = '删除用户' describe = users.username date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") opnotes(date, optype, describe) return HttpResponseRedirect('/users/userlist')
def redelete(request,a): menuif_2 =1 menuif_2_3 =1 path='任务回收站' cron=recyclelist.objects.get(id=a) recyclelist.objects.get(id=a).delete() #写入日志 optype='永久删除' describe=str(cron.id)+cron.personal date=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") opnotes(date,optype,describe) return HttpResponseRedirect('/task/recycle')
def reedit(request,a): menuif_2 =1 menuif_2_3 =1 path='恢复任务' if request.method == 'POST': path='任务回收站' cronid=request.POST.get('id', None) personal=request.POST['personal'] describe=request.POST['describe'] expression=request.POST['expression'] cronlist.objects.create(id=cronid,personal=personal,describe=describe,expression=expression) recyclelist.objects.get(id=a).delete() #写入日志 optype='恢复任务' describe=str(cron.id)+cron.personal date=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") opnotes(date,optype,describe) return HttpResponseRedirect('/task/recycle') cron=recyclelist.objects.get(id=a) return render_to_response('reedit.html',{'menuif_2':menuif_2,'menuif_2_3':menuif_2_3,'path':path,'cron':cron})
def nodeadd(request): path = '节点添加' menuif_1 = 1 menuif_1_1 = 1 if request.method == 'POST': nodeid = request.POST.get('id', None) nodeip = request.POST.get('nodeip', None) nodename = request.POST.get('nodename', None) nodepass = request.POST.get('nodepass', None) nodeport = request.POST.get('nodeport', None) if nodeid == None: nodeslist.objects.create(nodeip=nodeip, nodename=nodename, nodepass=nodepass, nodeport=nodeport) #写入日志 optype = '节点添加' describe = nodeip date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") opnotes(date, optype, describe) return HttpResponseRedirect('/node/list') else: nodeslist.objects.filter(id=nodeid).update(nodeip=nodeip, nodename=nodename, nodepass=nodepass, nodeport=nodeport) #写入日志 optype = '节点修改' describe = nodeip date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") opnotes(date, optype, describe) return HttpResponseRedirect('/node/list') else: return render_to_response('nodeadd.html', { 'menuif_1': menuif_1, 'menuif_1_1': menuif_1_1, 'path': path })
def addstask(request): menuif_2 =1 menuif_2_1 =1 menuif_2_1_2 =1 path='批量添加任务' count=0 if request.method == 'POST': #获取前台数据 batchtask=request.POST.get('batchtask', None) #按\n分隔符分割字符串为数组 batchtasks = batchtask.split('\n') batchtasks2 = batchtask.split('\n') #挑出空行 for i in range(len(batchtasks)): if len(batchtasks[i]) < 4: batchtasks2.remove(batchtasks[i]) #区分注释和crontab表达式 print batchtasks2 for line in batchtasks2: if line.find("#") == 0 : #去除\r字符 Notes=line.replace("#","").replace("\r","") Note = Notes.split('-') personal=Note[0] describe=Note[1] else: count+=1 expression=line.replace("\r","") cronlist.objects.create(personal=personal,describe=describe,expression=expression) #写入日志 optype='批量添加' describe='批量添加数:'+str(count) date=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") opnotes(date,optype,describe) return HttpResponseRedirect('/task/tasklist') else: return render_to_response('addstask.html',{'menuif_2':menuif_2,'menuif_2_1':menuif_2_1,'menuif_2_1_2':menuif_2_1_2,'path':path})