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 serveradd(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method == 'GET': idc = lists('idc', idc_fields) cabinet = lists('cabinet', cabinet_fields) result = {'code': 0, 'idc': idc['msg'], 'cabinet': cabinet['msg']} return json.dumps(result) 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 jobhistory(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method == 'GET': job = lists('job', field) return render_template('jobhistory.html', msg=msg, joblist=job['msg'])
def idc(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() result = lists('idc', fields) return render_template('idc.html', msg=msg, idc=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 server(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() idcs = lists('idc', idc_fields) cabinets = lists('cabinet', cabinet_fields) servers = lists('server', server_fields) for cab in servers['msg']: for items in cabinets['msg']: if cab['cabinet'] == items['id']: cab['cabinet'] = items['name'] for cac in idcs['msg']: if cab['idc'] == cac['id']: cab['idc'] = cac['name'] return render_template('server.html', msg=msg, server_list=servers['msg'])
def userlist(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() field = [ "id", "username", "name_cn", "password", "mobile", "email", "role", "status" ] result = lists('user', field) return render_template('userlist.html', msg=msg, result=result['msg'])
def joblist(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method == 'GET': jobs = [] result = lists('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 serverupdate(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method == 'GET': id = request.args.get('id') data = {'id': id} server = getone('server', data, server_fields) idc = lists('idc', idc_fields) cabinet = lists('cabinet', cabinet_fields) result = { 'code': 0, 'idc': idc['msg'], 'cabinet': cabinet['msg'], 'server': server['msg'] } return json.dumps(result) if request.method == 'POST': server = {k: v[0] for k, v in dict(request.form).items()} print server result = _update('server', server_fields, server) if result['code'] == 0: return json.dumps(result)
def cabinetadd(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method == 'GET': fields = ['id', 'name'] result = lists('idc', fields) return json.dumps(result) if request.method == 'POST': cabinet = {k: v[0] for k, v in dict(request.form).items()} print cabinet 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 cabinetupdate(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() if request.method == 'GET': id = request.args.get('id') data = {'id': id} cabinet = getone('cabinet', data, field) idc = lists('idc', idc_fields) result = {'code': 0, 'idc': idc['msg'], 'cabinet': cabinet['msg']} return json.dumps(result) if request.method == 'POST': cabinet = {k: v[0] for k, v in dict(request.form).items()} print cabinet result = _update('cabinet', field, cabinet) if result['code'] == 0: result = {'code': 0, 'msg': "Update success"} return json.dumps(result)
def namedlist(): if 'username' not in session: return redirect('/login/') msg = sessionmsg() result = lists('dns_records', field) return render_template('named.html', msg=msg, result=result['msg'])