Skip to content

Commit 496396c

Browse files
committed
first commit
0 parents  commit 496396c

File tree

863 files changed

+169563
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

863 files changed

+169563
-0
lines changed

cmdb/README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Flask CMDB
2+
3+
* [安装指南](#安装指南)
4+
* [结构与功能](#结构与功能)
5+
* [项目截图](#项目截图)
6+
7+
8+
## 安装指南:
9+
* 需要python2.7环境
10+
* 需要模块,查看requestment.txt
11+
* 初始化数据
12+
* 数据库sql/reboot10.sql
13+
14+
### MYSQL数据库导入
15+
```
16+
mysql -uroot -p123456 -e 'create database reboot10'
17+
mysql -uroot -p123456 reboot10 < sql/reboot10.sql
18+
```
19+
### MYSQL数据库字符集设置
20+
```
21+
[client]
22+
default-character-set=utf8
23+
24+
[mysqld]
25+
character-set-server=utf8
26+
collation-server=utf8_general_ci
27+
```
28+
29+
### 登录用户名和密码
30+
* 用户: test
31+
* 密码: 123123
32+
33+
## 结构与功能
34+
* 仪表盘
35+
* 欢迎界面
36+
* 用户管理
37+
* 个人用户信息
38+
* 显示当前登录用户的个人信息
39+
* 所有用户信息
40+
* 显示所有用户信息(只管理员可见)
41+
* 资产管理
42+
* 服务器管理
43+
* 服务器信息
44+
* 机柜管理
45+
* 机柜信息
46+
* 机房管理
47+
* 机房信息
48+
* 值班系统
49+
* 当前值班人
50+
* 当前值班人个人信息
51+
* 值班列表
52+
* 本周值班人信息列表
53+
* 工单管理
54+
* 工单申请
55+
* 显示申请界面
56+
* 工单申请列表
57+
* 已申请和待处理工单列表
58+
* 工单历史列表
59+
* 处理结束的历史工单列表
60+
* 代码更新
61+
* 更新信息
62+
* 代码更新界面
63+
* 更新历史
64+
* 更新成功的历史信息列表
65+
* 设置
66+
* 访问官网
67+
68+
## 项目截图
69+
![sec](https://ooo.0o0.ooo/2016/11/22/5834136d7e85b.png)
70+
![sec](https://ooo.0o0.ooo/2016/11/22/5834136dc8fce.png)
71+
![sec](https://ooo.0o0.ooo/2016/11/22/5834136f8b787.png)
72+
![sec](https://ooo.0o0.ooo/2016/11/22/5834136eda8be.png)
73+
![sec](https://ooo.0o0.ooo/2016/11/22/5834136ed0fe2.png)
74+
![sec](https://ooo.0o0.ooo/2016/11/22/5834137b087e9.png)
75+
![sec](https://ooo.0o0.ooo/2016/11/23/58357428e7523.png)
76+
![sec](https://ooo.0o0.ooo/2016/11/23/583574290c5cd.png)
77+
![sec](https://ooo.0o0.ooo/2016/11/22/5834137ab0be7.png)
78+
![sec](https://ooo.0o0.ooo/2016/11/22/5834137b54126.png)
79+
![sec](https://ooo.0o0.ooo/2016/11/22/5834137bb871b.png)
80+
![sec](https://ooo.0o0.ooo/2016/12/08/5848c98b75376.png)
81+
![sec](https://ooo.0o0.ooo/2016/12/08/5848c98b85e61.png)

cmdb/app/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/python
2+
#coding:utf-8
3+
4+
from flask import Flask
5+
6+
app = Flask(__name__)
7+
app.secret_key='\xd1\xfe\xfb\x7fH\xbf\xc8Q\x17-\xab\xc6\x80u2\xc2\xb6\n9\x00\x87\xa7\xa75'
8+
9+
10+
import user,idc,cabinet,server,job,code,duty

cmdb/app/cabinet.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/python
2+
#coding:utf-8
3+
4+
from flask import render_template,request,redirect,session
5+
from . import app
6+
from config import *
7+
from utils import login_request
8+
import json
9+
import db
10+
11+
app.config.from_object(Table)
12+
fields_idc=app.config.get('FIELDS_IDC')
13+
fields_cabinet=app.config.get('FIELDS_CABINET')
14+
15+
@app.route('/cabinet/')
16+
@login_request.login_request
17+
def cabinet():
18+
role = session.get('role')
19+
cabinets = db.list('cabinet',fields_cabinet)
20+
for i in cabinets:
21+
idc = db.list('idc',fields_idc,i['idc_id'])
22+
i['idc_id'] = idc['name']
23+
24+
return render_template("/cabinet/cabinetlist.html",cabinets = cabinets,role = role)
25+
26+
@app.route('/cabinet_msg/')
27+
@login_request.login_request
28+
def cabinet_msg():
29+
cabinets = db.list('cabinet',fields_cabinet)
30+
return json.dumps({'result':cabinets})
31+
32+
@app.route("/cabinetadd/",methods=['GET','POST'])
33+
@login_request.login_request
34+
def cabinetadd():
35+
if request.method == 'GET':
36+
return render_template('/cabinet/cabinetadd.html',info = session,role = session.get('role'))
37+
if request.method == 'POST':
38+
data = dict((k,v[0]) for k,v in dict(request.form).items())
39+
l = []
40+
41+
for i in db.list('cabinet',fields_cabinet):
42+
l.append(i['name'])
43+
if not data['name']:
44+
return json.dumps({'code':1,'errmsg':'name can not be null'})
45+
elif data['name'] not in l:
46+
conditions = [ "%s='%s'" % (k,v) for k,v in data.items()]
47+
db.add('cabinet',conditions)
48+
return json.dumps({'code':0,'result':'add cabinet success'})
49+
return json.dumps({'code':1,'errmsg':'cabinet name is exist'})
50+
51+
@app.route('/cabinet_update_msg/')
52+
@login_request.login_request
53+
def cabinet_update_msg():
54+
id = request.args.get('id')
55+
cabinet = db.list('cabinet',fields_cabinet,id)
56+
return json.dumps({'code':0,'result':cabinet})
57+
58+
@app.route('/cabinet_update/',methods=['GET','POST'])
59+
@login_request.login_request
60+
def cabinet_update():
61+
data = dict((k,v[0]) for k,v in dict(request.form).items())
62+
conditions = [ "%s='%s'" % (k,v) for k,v in data.items()]
63+
db.update('cabinet',conditions,data['id'])
64+
return json.dumps({'code':0,'result':'update completed!'})
65+
66+
@app.route('/cabinet_delete/',methods=['POST'])
67+
@login_request.login_request
68+
def cabinet_delete():
69+
id = request.form.get('id')
70+
db.delete('cabinet',id)
71+
return json.dumps({'code':0,'result':'delete success!'})

cmdb/app/code.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#!/usr/bin/python
2+
#coding:utf-8
3+
4+
from flask import render_template,request,redirect,session
5+
from . import app
6+
from config import *
7+
from utils import login_request
8+
from werkzeug import secure_filename
9+
import paramiko
10+
import json,os,sys,re,time
11+
import db
12+
import pysvn
13+
import zipfile
14+
15+
reload(sys)
16+
sys.setdefaultencoding('utf-8')
17+
18+
app.config.from_object(RemoteHost)
19+
app.config.from_object(Table)
20+
app.config.from_object(ProUrl)
21+
fields_code = app.config.get('FIELDS_CODE')
22+
url = app.config.get('URL') # 字典格式
23+
path = '/data/'
24+
hosts = []
25+
allfile = []
26+
27+
for i in app.config:
28+
if re.findall('HOST.',i):
29+
hosts.append(app.config.get(i))
30+
# hosts格式为[['192.168.1.100', 22, 'root', '123456'],['192.168.1.101', 22, 'root', '123456'],['192.168.1.102', 22, 'root', '123456']]
31+
32+
def get_login(realm,username,may_save):
33+
return True,'test','123456',True
34+
35+
def svncheckout(url,where):
36+
client = pysvn.Client()
37+
client.callback_get_login = get_login
38+
try:
39+
client.checkout(url,where)
40+
except Exception,e:
41+
print 'Error: {}'.format(e)
42+
43+
# 遍历目录中的所有文件
44+
#def dirlist(where):
45+
# filelist = os.listdir(where)
46+
#
47+
# for filename in filelist:
48+
# filepath = os.path.join(where, filename)
49+
# if os.path.isdir(filepath):
50+
# dirlist(filepath)
51+
# else:
52+
# allfile.append(filepath)
53+
# return allfile
54+
55+
def trans(where,filename,host):
56+
# 文件传输
57+
tus = (host[0],host[1])
58+
t = paramiko.Transport(tus)
59+
t.connect(username=host[2],password=host[3])
60+
sftp = paramiko.SFTPClient.from_transport(t)
61+
sftp.put(where,filename)
62+
t.close()
63+
64+
def exec_comm(comm,host):
65+
ssh = paramiko.SSHClient()
66+
#comm = '/root/update.sh '+where
67+
68+
# 执行远程命令
69+
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
70+
ssh.connect(host[0],host[1],host[2],host[3],timeout=10)
71+
stdin,stdout,stderr = ssh.exec_command(comm)
72+
ssh.close()
73+
74+
def zip_dir(dirname,zipfilename):
75+
filelist = []
76+
if os.path.isfile(dirname):
77+
filelist.append(dirname)
78+
else :
79+
for root, dirs, files in os.walk(dirname):
80+
for name in files:
81+
filelist.append(os.path.join(root, name))
82+
83+
zf = zipfile.ZipFile(zipfilename, "w", zipfile.zlib.DEFLATED)
84+
for tar in filelist:
85+
arcname = tar[len(dirname):]
86+
#print arcname
87+
zf.write(tar,arcname)
88+
zf.close()
89+
90+
@app.route('/code/',methods=['GET','POST'])
91+
@login_request.login_request
92+
def code():
93+
role = session.get('role')
94+
if request.method=='GET':
95+
return render_template('/code/code.html',role=role)
96+
else:
97+
data = dict((k,v[0]) for k,v in dict(request.form).items()) # message, key, project
98+
print data
99+
key = data.pop('key')
100+
project = data['project']
101+
data['update_persion'] = session.get('name')
102+
conditions = [ "%s='%s'" % (k,v) for k,v in data.items()]
103+
if key == 'mmnn88' and data['project']:
104+
try:
105+
localtime = time.strftime('-%Y-%m-%d-%H:%M:%S')
106+
project_name = project+localtime # 例如: 'ecg-2016-12-07-10:00:37'
107+
zip_name = project_name+'.zip'
108+
print zip_name
109+
svncheckout(url[project],path+project_name) # co 到 '/data/ecg-2016-12-07-10:00:37'
110+
os.chdir(path) # cd '/data'
111+
zip_dir(project_name,zip_name)
112+
for host in hosts:
113+
trans(zip_name,path+zip_name,host)
114+
exec_comm('/bin/bash /root/update.sh '+zip_name,host)
115+
db.add('code',conditions)
116+
return json.dumps({'code':0,'result':'更新成功'})
117+
except Exception, e:
118+
errmsg = '失败信息 error: '+str(e)
119+
return json.dumps({'code':1,'errmsg':errmsg})
120+
elif not data['project']:
121+
return json.dumps({'code':1,'errmsg':errmsg})
122+
else:
123+
errmsg = '许可码无效!'
124+
return json.dumps({'code':1,'errmsg':errmsg})
125+
126+
@app.route('/codelist/')
127+
@login_request.login_request
128+
def codelist():
129+
role = session.get('role')
130+
codes = db.list('code',fields_code)
131+
return render_template("/code/codelist.html",codes = codes,role = role)

cmdb/app/duty.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/python
2+
#coding:utf-8
3+
4+
from flask import render_template,request,redirect,session
5+
from . import app
6+
from hashlib import md5
7+
from config import *
8+
from utils import login_request
9+
from datetime import date
10+
import json
11+
import db,time
12+
13+
salt = '890iop*()'
14+
app.config.from_object(Table)
15+
fields_user = app.config.get('FIELDS_USER')
16+
17+
def duty(day): # 查看星期几的值班人的编号
18+
duty_dict = {} # 带索引的值班人员字典
19+
duty_week = {} # k: 星期几 ,v: 为list 值班人 ,v[0][1] 表示第二周的星期一
20+
duty_list = [] # 所有值班人员列表
21+
users = db.list('users',fields_user)
22+
for user in users:
23+
if user['role'] == 'admin' and user['status'] == 0:
24+
duty_list.append(user['id'])
25+
duty_list *= 5
26+
duty_dict = { k:v for k,v in enumerate(duty_list)}
27+
for k,v in duty_dict.items():
28+
duty_week.setdefault(k,[])
29+
duty_week[k%5].append(v)
30+
31+
week_num = (int(time.strftime('%W')))%(int(len(duty_list))/5) # 第几个星期 , 'int(len(duty_list))/5'周重置一次
32+
return duty_week[day][week_num]
33+
34+
@app.route("/dutyself/")
35+
@login_request.login_request
36+
def dutyself():
37+
today = date.weekday(date.today()) # 今天是星期几
38+
id = duty(today)
39+
40+
user = db.list('users',fields_user,id)
41+
role = session.get('role')
42+
return render_template("/duty/dutyself.html",user = user,role = role)
43+
44+
@app.route('/dutylist/')
45+
@login_request.login_request
46+
def dutylist():
47+
every_week = [] # 每周值班人汇总
48+
role = session.get('role')
49+
for i in range(5):
50+
data = db.list('users',fields_user,duty(i))
51+
if i == 0:
52+
data['week'] = '星期一'
53+
data['id'] = i
54+
elif i == 1:
55+
data['week'] = '星期二'
56+
data['id'] = i
57+
elif i == 2:
58+
data['week'] = '星期三'
59+
data['id'] = i
60+
elif i == 3:
61+
data['week'] = '星期四'
62+
data['id'] = i
63+
else:
64+
data['week'] = '星期五'
65+
data['id'] = i
66+
every_week.append(data)
67+
68+
return render_template("/duty/dutylist.html",users = every_week,role = role)

0 commit comments

Comments
 (0)