def vmadd(): if 'username' not in session: return redirect('/login/') if request.method == 'POST': msg = sessionmsg() if msg['role'] != 0 and msg['role'] != 1: owner = msg['username'] status = 0 create_date = "1900-01-01" expire_date = "1900-01-01" #form_result数据样例{'project': u'1', 'term': u'1', 'used': u'22222222222', 'resource': u'2', 'system': u'os6'} form_result = request.form.to_dict() data = vmappend(form_result) field = [ "hostname", "system", "term", "cpu", "mem", "disk", "project", "used", "ip", "mask", "gateway", "create_date", "expire_date", "owner", "status" ] data.update(create_date=create_date, expire_date=expire_date, owner=owner, status=status) try: result = insert_sql(table_name, field, data) if result['code'] == 0: result = { 'code': 0, 'msg': data['hostname'] + "虚机申请成功,等待审核" } except: result = {'code': 1, 'msg': data['hostname'] + "虚机已存在"} else: result = {'code': 1, 'msg': "管理员无法申请账号"} return json.dumps(result)
def resetpwdone(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() field = [ "id", "username", "name_cn", "password", "mobile", "email", "role", "status" ] if request.method == 'GET': userid = request.args.get('id') data = {'id': userid} result = getone('user', data, field) return json.dumps(result['msg']) else: chpwd = {k: v[0] for k, v in dict(request.form).items()} chpwd['newpasswd'] = hashlib.md5(chpwd['newpasswd'] + salt).hexdigest() #重置密码只需校验用户id,无需校验旧密码 where = {'id': chpwd['id']} field = ['id'] result = check('user', field, where) if result['code'] == 0: data = {'id': chpwd['id'], 'password': chpwd['newpasswd']} result = _update('user', field, data) else: result = {'code': 1, 'msg': u"重置密码失败!"} return json.dumps(result)
def vpnaudit(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method=='GET': vpnid = request.args.get('id') data={'id':vpnid} field = ['status'] res = getone(table_name,data,field)['msg'] if res['status'] != 0: result ={'code':1, 'msg':"该申请已被审核"} return json.dumps(result) field = ["create_date","expire_date","status"] status = 1 #审核后失效日期增加一年 create_date = datetime.datetime.now() expire_date = create_date + datetime.timedelta(days = 365) create_date = create_date.strftime('%Y-%m-%d') expire_date = expire_date.strftime('%Y-%m-%d') data.update(create_date=create_date,expire_date=expire_date,status=status) if _update(table_name,field,data): #vpn自动开通功能开启,自动生成vpn证书 if config.vpn_fun == 0: if ok: result ={'code':0, 'msg':"审核成功,已自动开通vpn"} else: result ={'code':1, 'msg':"审核成功,自动开通vpn失败"} return json.dumps(result) result ={'code':0, 'msg':"审核成功"} else: result ={'code':1, 'msg':"审核失败"} return json.dumps(result)
def vpnupdate(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() #vpn详情 field = ["id","name","term","used","create_date","expire_date","owner","status"] if request.method=='GET': vpnid = request.args.get('id') data={'id':vpnid} result = getone(table_name,data,field) return json.dumps(result['msg'],default=datetime_handler) #超级管理员编辑vpn,更新vpn信息 else: field = ["name","term","used","create_date","expire_date","owner","status"] user = {k:v[0] for k,v in dict(request.form).items()} #简单校验expire日期输入格式是否正确 expire_check = user['expire_date'] if not re.match(r'^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$',expire_check): result ={'code':1, 'msg':"失效日期格式错误"} return json.dumps(result) else: result = _update(table_name,field,user) if result['code'] == 0: result ={'code':0, 'msg':"更新成功"} else: result ={'code':1, 'msg':"更新失败"} return json.dumps(result)
def jobhistory(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method=='GET': job = list('job',field) return render_template('jobhistory.html',msg=msg,joblist=job['msg'])
def userlist(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() field = ["id","username","name_cn","password","mobile","email","role","status"] result = list('user',field) return render_template('userlist.html',msg=msg,result=result['msg'])
def ansible(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method == 'GET': server = lists('server', server_fields) return render_template('ansible.html', msg=msg, server=server['msg']) if request.method == 'POST': cmdmsg = {k: v[0] for k, v in dict(request.args).items()} #记录日志写入文件 cmd_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') cmd_history = "Time: %s User: %s Host: %s Cmd: %s \n" % ( cmd_time, msg['username'], cmdmsg['pattern'], cmdmsg['cmd']) with open("/tmp/ansible.log", 'a') as ansible_log: ansible_log.write(cmd_history) ansible_log.close ansiblecmd = ansiblecommand(cmdmsg['cmd'], cmdmsg['pattern']) pattern = cmdmsg['pattern'] result = ansiblecmd['contacted'][pattern]['stdout'] if result: ansible_cmd = "host: %s | CMD: %s | success >> \n" % ( cmdmsg['pattern'], cmdmsg['cmd']) ansible_msg = ansible_cmd + result results = ansible_msg.replace('\n', '<br>') return json.dumps(results)
def idc(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() result = list('idc', fields) return render_template('idc.html', msg=msg, idc=result['msg'])
def vpnadd(): if 'username' not in session: return redirect('/login/') if request.method=='POST': msg = sessionmsg() if msg['role'] != 0 and msg['role'] != 1: #user_field = ["id","username","name_cn","password","mobile","email","role","status"] #data={'username':msg['username']} #user_data = getone('user',data,user_field) #user_id = (user_data['msg']).get('id') owner = msg['username'] status = 0 #create_date = datetime.datetime.now() #expire_date = create_date + datetime.timedelta(days = 365) #create_date = create_date.strftime('%Y-%m-%d') #expire_date = expire_date.strftime('%Y-%m-%d') create_date = "1900-01-01" expire_date = "1900-01-01" field = ["name","term","used","create_date","expire_date","owner","status"] data= {k:v[0] for k,v in dict(request.form).items()} data.update(create_date=create_date,expire_date=expire_date,owner=owner,status=status) try: result = insert_sql(table_name,field,data) if result['code'] == 0: result ={'code':0, 'msg':data['name'] + "账号申请成功,等待审核"} except: result ={'code':1, 'msg':data['name'] + "已存在"} else: result ={'code':1, 'msg':"管理员无法申请账号"} return json.dumps(result)
def mapdata(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method == 'GET': result = maps('map') print result return json.dumps(result)
def status(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method == 'GET': field = ["200", "304", "404", "206", "301", "403"] result = logs('log', field) return json.dumps(result)
def vpnlist(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() data=msg['username'] field = ["id","name","term","used","create_date","expire_date","owner","status"] result = list(table_name,field,data) print result return render_template('vpnlist.html',msg=msg,result=result['msg'])
def vpnadmin(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() #data为None获取所有vpn列表数据 data=None field = ["id","name","term","used","create_date","expire_date","owner","status"] result = list(table_name,field,data) return render_template('vpnadmin.html',msg=msg,result=result['msg'])
def cabinetdelete(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method == 'POST': cabinet = {k: v[0] for k, v in dict(request.form).items()} if _delete('cabinet', cabinet): result = {'code': 0, 'msg': "delete success"} return json.dumps(result)
def monitor(): msg = sessionmsg() result = getMem() print result data = {'data': []} times = int(time.time()) for mem in result: data['data'].append({'name': mem.keys()[0], 'value': mem.values()[0]}) return json.dumps(data)
def namedadd(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method == 'POST': data = {k: v[0] for k, v in dict(request.form).items()} result = insert_sql('dns_records', fields, data) if result['code'] == 0: result = {'code': 0, 'msg': "Add Zone Successful"} return json.dumps(result)
def nameddelete(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method == 'GET': dataid = request.args.get('id') data = {'id': dataid} if _delete('dns_records', data): result = {'code': 0, 'msg': "delete user success"} return json.dumps(result)
def jobdetail(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method=='GET': job = {k:v[0] for k,v in dict(request.args).items()} field = ['id','apply_name','created','apply_desc','apply_desc','handle_desc'] result = getone('job',job,field) if result['code'] == 0: return json.dumps(result, cls=DatetimeEncoder)
def cabinet(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() idcs = lists('idc', idc_fields)['msg'] cabinets = lists('cabinet', field)['msg'] for cab in cabinets: for items in idcs: if cab['idc_id'] == items['id']: cab['idc_id'] = items['name'] return render_template('cabinet.html', msg=msg, cabinet=cabinets)
def history(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method == 'GET': ansible_history = "" with file("/tmp/ansible.log") as f: for line in reversed(f.readlines()): ansible_history += line + "</br>" f.close return json.dumps(ansible_history)
def idcadd(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method=='POST': idc = {k:v[0] for k,v in dict(request.form).items()} field = ['name','name_cn','address','adminer','phone'] result = insert_sql('idc',field,idc) if result['code'] == 0: result ={'code':0, 'msg':"IDC user success"} return json.dumps(result)
def chmessageoneself(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method == 'POST': field = ["username", "name_cn", "mobile", "email"] user = {k: v[0] for k, v in dict(request.form).items()} result = _update('user', field, user) if result['code'] == 0: result = {'code': 0, 'msg': "add user success"} return json.dumps(result)
def center(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() field = [ "id", "username", "name_cn", "password", "mobile", "email", "role", "status" ] data = {'username': session['username']} result = getone('user', data, field) return render_template('center.html', msg=result['msg'])
def vmlist(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() data = msg['username'] field = [ "id", "hostname", "system", "term", "cpu", "mem", "disk", "project", "used", "ip", "mask", "gateway", "create_date", "expire_date", "owner", "status" ] result = list(table_name, field, data) return render_template('vmlist.html', msg=msg, result=result['msg'])
def joblist(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method=='GET': jobs = [] result = list('job',field) results = result['msg'] for x in results: if x['status'] < 2: jobs.append(x) return render_template('joblist.html',msg=msg,joblist=jobs)
def delete(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method == 'GET': userid = request.args.get('id') data = {'id': userid} if _delete('user', data): result = {'code': 0, 'msg': "delete user success"} else: result = {'code': 1, 'msg': "delete user failed"} return json.dumps(result)
def namedupdate(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method == 'GET': named_id = request.args.get('id') data = {'id': named_id} result = getone('dns_records', data, field) return json.dumps(result['msg']) if request.method == 'POST': data = {k: v[0] for k, v in dict(request.form).items()} result = _update('dns_records', field, data) return json.dumps(result)
def serveradd(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method=='GET': idc = list('idc',idc_fields) cabinet = list('cabinet',cabinet_fields) return render_template('serveradd.html',msg=msg,idc=idc['msg'],cabinet=cabinet['msg']) if request.method=='POST': server = {k:v[0] for k,v in dict(request.form).items()} result = insert_sql('server',server_field,server) if result['code'] == 0: result ={'code':0, 'msg':"success"} return json.dumps(result)
def cabinetadd(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method == 'GET': fields = ['id', 'name'] result = list('idc', fields) return render_template('cabinetadd.html', msg=msg, idc=result['msg']) if request.method == 'POST': cabinet = {k: v[0] for k, v in dict(request.form).items()} field = ['name', 'idc_id', 'u_num', 'power'] result = insert_sql('cabinet', field, cabinet) if result['code'] == 0: result = {'code': 0, 'msg': "success"} return json.dumps(result)
def jobadd(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method=='GET': return render_template('jobadd.html',msg=msg) if request.method=='POST': job = {k:v[0] for k,v in dict(request.form).items()} job['apply_name'] = session['username'] field = ['apply_type','apply_name','apply_desc'] result = insert_sql('job',field,job) if result['code'] == 0: result ={'code':0, 'msg':"Job Add success"} return json.dumps(result)