-
Notifications
You must be signed in to change notification settings - Fork 0
/
openapi.py
301 lines (273 loc) · 10.4 KB
/
openapi.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# coding=gbk
'''
Created on 2015年12月26日
@author: 大雄
'''
import logging
import os
import select
import signal
import sys
import time
from peewee import datetime
import app as appmodule
import command
from db import App, Server, RedisHelper, Operation, Script, Host, AppHistory
import env
from utils import SVNHelper
import utils
def deployApp(brand, app, master_rev, config_rev):
try :
app_instance = App.get(App.brand == brand, App.name == app)
app_instance.master_rev = master_rev
app_instance.config_rev = config_rev
servers = Server.raw(
'''select * from t_server where id in ({0})'''.format(app_instance.getConfig("SERVERS"))
)
# if prd:
# profile = DBHelper().getProfile("prd")
# else:
# profile = DBHelper().getProfile("dev")
deploy_instance = appmodule.createAppDeploy(app_instance, [s for s in servers])
if deploy_instance:
deploy_instance.deploy()
deploy_instance.saveHistory()
return None
else:
raise Exception("None Object AppDeploy")
except Exception as e:
logging.debug(e)
raise Exception("app deploy fail:" + str(e))
def rollbackApp(brand, app):
apps = AppHistory.select().where(
AppHistory.brand == brand and AppHistory.name == app
).order_by(-AppHistory.createtime).limit(2)
if len(apps) != 2:
raise Exception("only one history has been found. Cannot rollback")
app = apps[1]
yesorno = input('RollBack App:' + "\n"
+ " Brand: " + app.brand + "\n"
+ " App: " + app.name + "\n"
+ " Master rev: " + app.master_rev + "\n"
+ " Config rev: " + app.config_rev +"\n"
+ " CreateTime: " + str(app.createtime) + "\n"
+ " (Y/N)")
if yesorno =="Y":
print("Starting RollBack...")
#deployApp(brand, app, apps[1].master_rev, apps[1].config_rev)
elif yesorno =="N":
print("End RollBack")
else:
print("Wrong Input!")
def infoApp(brand, app):
try :
app = App.get(App.brand == brand, App.name == app)
servers = Server.raw(
'''select * from t_server where id in ({0})'''.format(app.getConfig("SERVERS"))
)
deploy = appmodule.createAppDeploy(app, [s for s in servers])
if deploy:
app_status = deploy.getAppStatus()
if len(app_status.keys()) > 0:
return app_status
else:
return None
else:
raise Exception("None Object appdeploy")
except Exception as e:
logging.debug(e)
raise Exception("infoApp: " + str(e))
def keysMem(brand, server):
try:
server1 = Server.getServer(brand, server)
if server1:
out = RedisHelper.keys(server1.host)
return out
else:
raise Exception("None Object Server")
except Exception as e:
logging.debug(e)
raise Exception("keysMem: " + str(e))
def flushMem(brand, server):
try:
servername = server + "." + brand
server1 = Server.get(Server.name == servername)
if server1:
out = RedisHelper.flushdb(server1.host)
return out
else:
raise Exception("None Object Host")
except Exception as e:
logging.debug(e)
raise Exception("flushMem:" + str(e))
def stopServer(brand, name):
try:
server1 = Server.getServer(brand, name)
command.createCommand(server1.host, server1.ssh_port, "service " + server1.type + " stop", type="SSH").execute()
return None
except Exception as e:
logging.debug(e)
raise Exception("stopServer: " + str(e))
def getSVNLog(brand, app):
try :
app = App.get(App.brand == brand, App.name == app)
if app:
return SVNHelper.getSVNLogByApp(app)
else:
raise Exception("None Object App")
except Exception as e:
logging.debug(e)
raise Exception("getSVNLog: " + str(e))
def startServer(brand, name):
try:
server1 = Server.getServer(brand, name)
command.createCommand(server1.host, server1.ssh_port, "service " + server1.type + " start", type="SSH").execute()
return None
except Exception as e:
logging.debug(e)
raise Exception("startServer: " + str(e))
def restartServer(brand, name):
try:
server1 = Server.getServer(brand, name)
command.createCommand(server1.host, server1.ssh_port, "service " + server1.type + " restart", type="SSH").execute()
if (env.config.getboolean("SERVER_RELOAD_SYNC")):
if checkServer(brand, name):
return None
else:
raise Exception("server not sync")
else:
return None
except Exception as e:
logging.debug(e)
raise Exception("restartServer: " + str(e))
def checkServers(brand, app):
try :
app = App.get(App.brand == brand, App.name == app)
servers = Server.raw(
'''select * from t_server where id in ({0})'''.format(app.getConfig("SERVERS"))
)
check_list = []
for server in servers:
th = utils.CheckThread(server.host, server.port)
check_list.append(th)
th.start()
for th in check_list:
th.join()
time.sleep(2)
flag = True
for th in check_list:
flag = flag and th.retcode
return flag
except Exception as e:
logging.debug(e)
logging.debug("server not exist, check server fails:" + str(e))
return False
def checkServer(brand, name):
try:
server = Server.getServer(brand, name)
return utils.check_port(server.host, server.port)
except Exception:
logging.debug("server not exist, check server fails")
return False
def runScript(script_id, host_id):
s = Script.getScript(script_id)
h = Host.getHost(host_id)
filename = "/tmp/script_" + str(script_id)
with open(filename, "wb") as file:
file.write(s.content)
try:
command.createCommand(filename, h.ip, h.ssh_port, filename, type="SCP").execute()
command.createCommand(h.ip, h.ssh_port, "sh " + filename, type="SSH").execute()
return "success"
except:
return "false"
class OpenAPI():
def __init__(self, module_name, class_name, method_name, *args, **kargs):
self.module_name = module_name
self.class_name = class_name
self.method_name = method_name
self.args = kargs.get("args")
self.env = kargs.get("env")
# env is a namespace
# including some env parameters
def __call(self):
try:
op = self.__loggingOpenAPI()
if self.class_name:
cls = utils.getClass(self.module_name, self.class_name)
o = getattr(cls, self.method_name)
else:
mod = utils.getModule(self.module_name)
o = getattr(mod, self.method_name)
# 设置开始时间
op.start_time = datetime.datetime.now()
# 执行任务
ret = o(*self.args)
# 设置结束时间
op.end_time = datetime.datetime.now()
# 强制写入operation log
#op.save(force_insert=False)
# 返回结果
return 0, "success", ret
except Exception as e:
logging.debug(e)
return 1, str(e), None
def __loggingOpenAPI(self):
op = Operation()
if self.class_name:
op.name = self.module_name + "." + self.class_name + "." + self.method_name
else:
op.name = self.module_name + "." + self.method_name
op.data = str(self.args)
op.machine = self.env.machine
op.ipaddr = self.env.ipaddr
op.username = self.env.username
op.program = self.env.program
return op
def socketCall(self, socket, timeout=60):
try:
readEnd, writeEnd = os.pipe()
# windows平台上没有fork的api
pid = os.fork()
# parent process, pid是child pid
if pid > 0:
# 关闭parent pipe write
os.close(writeEnd)
readFile = os.fdopen(readEnd)
while 1:
f = select.select([readFile], [], [], timeout)
if readFile in f[0]:
line = readFile.readline()
if line:
socket.write_message(utils.formatJsonRet(msg=line))
else:
break
else:
logging.debug("timeout " + str(timeout) + "s")
os.kill(pid, signal.SIGILL)
break
fork_proc_exitCode = utils.waitCode2exitCode(os.waitpid(pid, 0)[1])
if fork_proc_exitCode == "0":
socket.write_message(utils.formatJsonRet(code="success", msg="return code:" + fork_proc_exitCode))
else:
socket.write_message(utils.formatJsonRet(code="error", msg="return code:" + fork_proc_exitCode))
# child process
if pid == 0:
os.close(readEnd)
os.dup2(writeEnd, sys.stdout.fileno())
os.dup2(writeEnd, sys.stderr.fileno())
os.close(writeEnd)
response = self.__call()
print(response[1])
os._exit(response[0])
except os.error as e:
logging.debug(e)
raise Exception("fork process fail")
def call(self):
response = self.__call()
print(response[1])
def webCall(self, request, timeout=60):
response = self.__call()
request.write(utils.formatJsonRet(response[0], response[1], response[2]))
if __name__ == '__main__':
runScript(13, "192.168.46.102")