-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.md
More file actions
147 lines (107 loc) · 3.7 KB
/
README.md
File metadata and controls
147 lines (107 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# cmdb
## 亮点
工厂模式
使用了插件式插入类
在__init__里对插件传到统一接口函数
使用api验证
# client端
## 流程
入口
在script总入口使用client的execute
client使用插件类获取server_info
插件类工作流程:
导入所有插件
插件运行process方法,实则执行cmd方法
插件获取content
parse解析content,获取结果
client发送到api
## 插件式导入模块
### setting.py
```
PLUGINS_DICT = {
"basic":"src.plugins.basic.Basic",
"board":"src.plugins.board.Board",
"cpu":"src.plugins.cpu.Cpu",
"disk":"src.plugins.disk.Disk",
"memory":"src.plugins.memory.Memory",
"nic":"src.plugins.nic.Nic",
}
```
### __init__.py
```
class PluginManager(object):
def exec_plugin(self):
"""
获取所有的插件,并执行获取插件返回值
:return:
"""
response = {}
for k,v in self.plugin_dict.items():
# 'basic': "src.plugins.basic.Basic",
ret = {'status':True,'data':None}
try:
module_path, class_name = v.rsplit('.', 1)
m = importlib.import_module(module_path)
cls = getattr(m,class_name)
if hasattr(cls,'initial'):
obj = cls.initial() # 钩子
else:
obj = cls()
```
# server端
repository 统一数据库
backend 后台管理+前端页面,可与api分离
api 可与backend分离
api认证,time-key-record
client端发送server_info到api,api把数据存入数据库
backend端访问数据,供前端页面使用
api认证
```
def API_check(func):
def wrapper(request,*args,**kwarg):
if request.method == "GET":
import hashlib
# ####################API认证##################
client_md5_time_key = request.META.get("HTTP_OPENKEY")
client_md5_key, client_ctime = client_md5_time_key.split("|")
client_ctime = float(client_ctime)
server_time = time.time()
# 第一关
if server_time - client_ctime > 10:
return HttpResponse("【第一关】时间有点长")
# 第二关
tmp = "%s|%s" % (settings.AUTH_KEY, str(client_ctime))
m = hashlib.md5()
m.update(bytes(tmp, encoding="utf-8"))
server_md5_key = m.hexdigest()
if server_md5_key != client_md5_key:
return HttpResponse("【第二关】规则不正确,验证码错误")
for k in list(api_key_record.keys()):
v = api_key_record[k]
if server_time > v:
api_key_record.pop(k)
# 第三关
if client_md5_time_key in api_key_record:
return HttpResponse("【第三关】有人已经来过了")
else:
api_key_record[client_md5_time_key] = client_ctime + 10
# ####################API认证##################
ret = func(request,*args,**kwarg)
return ret
return wrapper
```
## 插件式处理数据,与client端类似,__init__.py
```
class PluginManager(object):
def exec_plugin(self):
for name,p_path in settings.PLUGINS_DICT.items():
module_path,class_name = p_path.rsplit(".",1)
m = importlib.import_module(module_path)
if hasattr(m,class_name):
cls = getattr(m,class_name)
if hasattr(cls,'initial'):
obj = cls.initial()
else:
obj = cls()
obj.process(self.server_info,self.server_obj,self.compare_new_old)
```