forked from NJ-zero/PyQt5-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChange.py
305 lines (261 loc) · 10.2 KB
/
Change.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
302
303
304
305
#coding=utf-8
#author='Shichao-Dong'
import sys
from PyQt5.QtWidgets import *
from FirstMainWin import Ui_Form
import time
import paramiko
class Main(QMainWindow,Ui_Form):
def __init__(self,parent=None):
super(Main,self).__init__(parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
self.setWindowTitle('Waiqin365-DATT-V1.0.0')
self.password = 'ZKwaiqin123'
self.username='root'
self.ui.environment.activated.connect(self.printtomcat)
self.ui.filename.editingFinished.connect(self.filename)
self.ui.filenumber.activated.connect(self.filenumber)
self.ui.filetype.activated.connect(self.filetype)
self.ui.restart.activated.connect(self.needrestart)
self.ui.start.clicked.connect(self.start)
def printtomcat(self):
'''
根据选择的环境返回Index值,供下面connect使用
1 231
2 233
'''
print(self.ui.environment.currentText())
tomcat = self.ui.environment.currentIndex()
self.ui.log.setPlainText('部署的环境为:'+self.ui.environment.currentText())
return tomcat
def filenumber(self):
'''
根据选择的文件数量:单个文件还是web.zip,返回Index供下面onefile和zip使用
1 单个文件
2 web.zip
'''
print(self.ui.filenumber.currentIndex())
filenumber = self.ui.filenumber.currentIndex()
self.ui.log.setPlainText('文件类型为:'+self.ui.filenumber.currentText())
return filenumber
def filetype(self):
'''
根据选择的文件类型:平台文件还是应用文件,自动加上前缀 /home/...web/WEB-INFO/class 还是 /home/...web/
1 应用文件
2 平台文件
'''
print(self.ui.filetype.currentIndex())
filetype = self.ui.filetype.currentIndex()
self.ui.log.setPlainText('文件为:'+self.ui.filetype.currentText())
return filetype
def filename(self):
'''
返回填写的filename
'''
print(self.ui.filename.text())
filename = self.ui.filename.text()
self.ui.log.setPlainText('文件名为:'+self.ui.filename.text())
return filename
def localfile(self):
'''
返回local,即文件所在路径
:return:
'''
print(self.ui.filename.text())
filename = self.ui.filename.text()
localfile = 'D:/file/'+ filename
print(localfile)
return localfile
def path(self):
'''
返回填写的filepath 文件路径
'''
print(self.ui.filepath.toPlainText())
filepath = self.ui.filepath.toPlainText()
return filepath
def needrestart(self):
'''
根据所选,返回Index,供下面使用是否需要重启
1 需要
2 不需要
'''
need = self.ui.restart.currentIndex()
print(need)
return need
def connect(self,Index):
'''
根据传入的Index连接服务器
:param Index: 1 231 2 233
:return: ssh
'''
if Index == 1:
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('172.31.3.231',22,self.username, self.password)
except Exception:
print (Exception)
return ssh
elif Index == 2:
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('172.31.3.233',22,self.username, self.password)
except Exception:
print (Exception)
return ssh
def trans(self,Index,localpath,remotepath):
'''
根据传入的Index连接服务器并上传文件
:param Index:
:return:
'''
if Index == 1:
try:
trans = paramiko.Transport(('172.31.3.231',22))
trans.connect(username=self.username,password=self.password)
except Exception as e:
print (e)
sftp = paramiko.SFTPClient.from_transport(trans)
sftp.put(localpath,remotepath)
time.sleep(2)
trans.close()
elif Index == 2 :
try:
trans = paramiko.Transport(('172.31.3.233',22))
trans.connect(username=self.username,password=self.password)
except Exception as e:
print (e)
sftp = paramiko.SFTPClient.from_transport(trans)
sftp.put(localpath,remotepath)
time.sleep(2)
trans.close()
def onefile(self,Index,type):
'''
一个文件,根据传入的type判断平台文件还是应用文件,拼接完整的路径
:param type: 1 应用文件 2 平台文件
:param type: Index 1 连接231 2 连接233
:return:
'''
if type == 1:
remotepath = '/home/iorder_appsvr/iorder_appsvr/web/WEB-INF/classes' + str(self.path())
self.ui.log.setPlainText('文件路径为:'+ remotepath)
filename = self.filename()
self.localfile()
bak = filename + time.strftime("%y%m%d") + 'bak'
cmd = 'cd {0};mv {1} {2}'.format(remotepath,self.filename(),bak)
#备份并上传替换文件
print(cmd)
self.ui.log.setPlainText('备份替换文件')
# 根据Index 连接对应的环境
ssh = self.connect(Index)
stdin, stdout, stderr = ssh.exec_command(cmd,get_pty=True)
for line in stdout:
print (line.strip('\n'))
ssh.close()
remotefile = '/home/iorder_appsvr/iorder_appsvr/web/WEB-INF/classes' + str(self.path())+str(self.filename())
self.trans(Index,self.localfile(),remotefile)
self.ui.log.setPlainText('文件上传成功')
return cmd,filename,remotepath
elif type == 2:
remotepath = '/home/iorder_appsvr/iorder_appsvr/web' + str(self.path())
self.ui.log.setPlainText('文件路径为:'+ remotepath)
filename = self.filename()
bak = filename + time.strftime("%y%m%d") + 'bak'
cmd = 'cd {0} ;mv {1} {2}'.format(remotepath,self.filename(),bak)
print(cmd)
self.ui.log.setPlainText('备份替换文件')
ssh = self.connect(Index)
stdin, stdout, stderr = ssh.exec_command(cmd,get_pty=True)
for line in stdout:
print (line.strip('\n'))
ssh.close()
remotefile = '/home/iorder_appsvr/iorder_appsvr/web' + str(self.path())+str(self.filename())
self.trans(Index,self.localfile(),remotefile)
self.ui.log.setPlainText('文件上传成功')
return cmd,filename,remotepath
def zip(self,Index):
'''
web.zip包直接复制到opt后解压后复制
Index 用来判断231 还是 233
:return:
'''
cmd = 'cd /opt;rm -rf web;mkdir web;ls'
# cd 到opt下创建新的web目录
ssh = self.connect(Index)
stdin, stdout, stderr = ssh.exec_command(cmd,get_pty=True)
for line in stdout:
print (line.strip('\n'))
ssh.close()
#上传文件至opt/web 并解压
remotepath = '/opt/web/{}'.format(self.filename())
localpath = r'D:/file/{}'.format(self.filename())
print(remotepath,localpath)
self.trans(Index,localpath,remotepath)
self.ui.log.setPlainText('压缩包文件上传成功')
cmd1 = 'cd /opt/web;ls;unzip {};rm -rf {}'.format(self.filename(),self.filename())
ssh = self.connect(Index)
stdin, stdout, stderr = ssh.exec_command(cmd1,get_pty=True)
for line in stdout:
print (line.strip('\n'))
time.sleep(2)
#复制解压后的文件
cmd2 = '\cp -Rf /opt/web/* /home/iorder_appsvr/iorder_appsvr/'
stdin, stdout, stderr = ssh.exec_command(cmd2,get_pty=True)
for line in stdout:
print (line.strip('\n'))
ssh.close()
self.ui.log.setPlainText('复制zip替换文件成功')
def start(self):
'''
开始部署 1 部署231 2 部署233
:return:
'''
if self.printtomcat() == 1:
print('部署环境为:'+ self.ui.environment.currentText())
# 部署单个文件
if self.filenumber() == 1:
self.onefile(1,self.filetype())
#部署web.zip
elif self.filenumber() == 2:
#连接231 部署web.zip
self.zip(1)
# print('222')
#判断是否需要重启
if self.needrestart() == 1:
ssh = self.connect(1)
stdin, stdout, stderr = ssh.exec_command('service tomcat_iorder_appsvr restart',get_pty=False)
for line in stdout:
print (line.strip('\n'))
time.sleep(5)
ssh.close()
elif self.needrestart() == 2:
time.sleep(2)
elif self.printtomcat() == 2:
print('部署环境为:'+ self.ui.environment.currentText())
# 部署单个文件
if self.filenumber() == 1:
self.onefile(2,self.filetype())
# 部署web.zip
elif self.filenumber()== 2:
#连接231 部署web.zip
# print('222 222')
self.zip(2)
# 判断是否需要重启
if self.needrestart() == 1:
ssh = self.connect(2)
stdin, stdout, stderr = ssh.exec_command('service tomcat_iorder_appsvr restart',get_pty=False)
for line in stdout:
print (line.strip('\n'))
time.sleep(2)
ssh.close()
self.ui.log.setPlainText('部署完成,服务器重启中,请稍候')
elif self.needrestart() == 2:
time.sleep(2)
self.ui.log.setPlainText('部署完成')
if __name__=="__main__":
app = QApplication(sys.argv)
win = Main()
win.show()
sys.exit(app.exec_())