-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployAuto.py
166 lines (142 loc) · 5.54 KB
/
deployAuto.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
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name: deployAuto.py
Description : 自动部署tomcat
Author : HSH
date: 2017/11/1
-------------------------------------------------
Change Activity:
2017/11/1:
-------------------------------------------------
"""
__author__ = 'HSH'
import os
import configparser
import zipfile
import datetime
from functools import reduce
import thread_manager
# 解压
def unzip(src, dist):
print('正在解压缩' + src + "到" + dist + '......')
f = zipfile.ZipFile(src, 'r')
for file in f.namelist():
f.extract(file, dist)
print('解压缩' + src + "到" + dist + '完成!')
# 压缩
def zip_dfile(src, dist_zipfile):
if os.path.exists(src):
if os.path.isfile(src) or (os.path.isdir(src) and len(os.listdir(src)) == 0):
dist_zipfile.write(src, reduce(lambda x, y: x + os.sep + y, src.split(os.sep)[4:]))
else:
for d in os.listdir(src):
d = src + os.sep + d
zip_dfile(d, dist_zipfile)
def compressed(src, dist):
print("正在压缩" + src + "到" + dist + '......')
f = zipfile.ZipFile(dist, 'w')
zip_dfile(src, f)
f.close()
print("正在压缩" + src + "到" + dist + "完成!")
def clear_cache(src):
if os.path.isfile(src):
os.remove(src)
elif os.path.isdir(src) and len(os.listdir(src)) == 0:
os.rmdir(src)
else:
while os.path.isdir(src) and len(os.listdir(src)) > 0:
for d in os.listdir(src):
d = src + os.sep + d
clear_cache(d)
def clear_tomcat(tomcat_path):
print('清理' + tomcat_path + '路径下的tomcat日志')
path = tomcat_path + os.sep + 'bin'
for file in os.listdir(path):
if '.log' in file:
file = path + os.sep + file
clear_cache(file)
path = tomcat_path + os.sep + 'logs'
for file in os.listdir(path):
file = path + os.sep + file
clear_cache(file)
print('清理' + tomcat_path + '路径下的tomcat缓存')
path = tomcat_path + os.sep + 'work'
for file in os.listdir(path):
file = path + os.sep + file
clear_cache(file)
print('清理' + tomcat_path + '路径下的tomcat部署文件')
path = tomcat_path + os.sep + 'webapps'
for file in os.listdir(path):
file = path + os.sep + file
clear_cache(file)
def deploy_tomcat(zip_path, tomcat_path):
print('开始部署' + tomcat_path + '路径下的tomcat部署文件......')
src = zip_path + os.sep + os.listdir(zip_path)[0]
dist = tomcat_path + os.sep + 'webapps'
unzip(src, dist)
print('部署' + tomcat_path + '路径下的tomcat部署文件完成!')
def shut_down_tomcat(tomcat_path):
print('停止' + tomcat_path + '路径下的tomcat')
if os.path.exists(tomcat_path):
os.chdir(tomcat_path + os.sep + 'bin')
os.system('shutdown.bat')
else:
print("错误!" + tomcat_path + "不存在,请检查配置文件!")
def start_up_tomcat(tomcat_path):
if os.path.exists(tomcat_path):
os.chdir(tomcat_path + os.sep + 'bin')
os.system('startup.bat')
else:
print("错误!" + tomcat_path + "不存在,请检查配置文件!")
def backup_deploy_file(tomcat_path, back_path):
print("备份部署文件")
if not os.path.exists(back_path):
os.makedirs(back_path)
# for file in os.listdir(back_path):
# clear_cache(back_path + os.sep + file)
path = tomcat_path + os.sep + 'webapps'
for file in os.listdir(path):
dest = path + os.sep + file
compressed(dest, back_path + os.sep + file + datetime.datetime.now().strftime('%Y%m%d%H%M%S') + '备份.zip')
def check_conf(tomcat_path_dic, zip_path_dic, backup_path_dic):
for k in tomcat_path_dic.keys():
tomcat_path = tomcat_path_dic[k]
if k not in zip_path_dic.keys():
exit('请检查配置文件:未配置' + k + '部署文件路径')
else:
if not os.path.exists(zip_path_dic[k]):
exit('请检查配置文件:' + k + '部署文件路径不存在')
bin_path = tomcat_path + os.sep + 'bin'
if not os.path.exists(bin_path):
exit(k + '下文件缺失bin')
if 'startup.bat' not in os.listdir(bin_path):
exit(k + '下缺少启动脚本')
if 'shutdown.bat' not in os.listdir(bin_path):
exit(k + '下缺少关闭脚本')
def main():
# 读取配置文件
cf = configparser.ConfigParser()
cf.read('deploy.conf')
tomcat_path_dic, zip_path_dic, backup_path_dic = cf['tomcat_path'], cf['zip_path'], cf['backup']
# 检测配置文件有效性
check_conf(tomcat_path_dic, zip_path_dic, backup_path_dic)
# 关闭tomcat
for k in tomcat_path_dic.keys():
shut_down_tomcat(tomcat_path_dic[k])
# 备份tomcat
backup_deploy_file(tomcat_path_dic[backup_path_dic['backup_server']], backup_path_dic['path'])
# 部署并启动tomcat
for k in tomcat_path_dic.keys():
tomcat_path = tomcat_path_dic[k]
clear_tomcat(tomcat_path)
if input("是否开始部署?(y/n)") == 'y':
for k in tomcat_path_dic.keys():
tomcat_path = tomcat_path_dic[k]
deploy_tomcat(zip_path_dic[k], tomcat_path)
start_up_tomcat(tomcat_path)
is_start_thread = input("是否启动配置startThread中的线程?(y/n)")
if is_start_thread == 'y':
thread_manager.start_threads()
if __name__ == '__main__':
main()