-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
220 lines (194 loc) · 11.8 KB
/
main.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
#!/usr/bin/env python
# -- coding: utf-8 --
import sys
import os
import re
from imp import reload
import logging
import time
import shutil
now = int(time.time())
timeArray = time.localtime(now)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='myapp_main.log',
filemode='w')
def getText ( node ) :
if node.length == 0 :
return None
if node [ 0 ].childNodes.length == 0 :
return None;
return node [ 0 ].childNodes [ 0 ].nodeValue
default_encoding = 'utf-8'
if sys.getdefaultencoding ( ) != default_encoding :
reload ( sys )
sys.setdefaultencoding ( default_encoding )
from xml.dom import minidom
#############################################################################
# read config from xml
# this part is very suck will update in sometime
##############################################################################
def main ( configfile , sourcefile ,filename ) :
doc = minidom.parse ( configfile )
root = doc.documentElement
projects = root.getElementsByTagName ( 'project' )
for project in projects :
project_name = getText(project.getElementsByTagName ( "name" ))
operation_type = getText(project.getElementsByTagName ( "operation_type" ))
domain_name = getText(project.getElementsByTagName ( "domain_name" ))
if domain_name=='None':
domain_name=project_name+'.com'
project_type = getText(project.getElementsByTagName ( "project_type" ))
moblie_301_domain_name = getText(project.getElementsByTagName ( "moblie_301_domain_name" ))
moblie_301_project_name = getText(project.getElementsByTagName ( "moblie_301_project_name" ))
version = getText(project.getElementsByTagName ( "version" ))
second_level_domain=getText(project.getElementsByTagName ( "second_level_domain" ))
# connect_email=project.getElementsByTagName("connect_email")[0].childNodes[0].nodeValue
# note=project.getElementsByTagName("note")[0].childNodes[0].nodeValue
# send_mail_address=project.getElementsByTagName("send_mail_address")[0].childNodes[0].nodeValue
# send_mail_host=project.getElementsByTagName("send_mail_host")[0].childNodes[0].nodeValue
# send_mail_name=project.getElementsByTagName( "send_mail_name" )[0 ].childNodes[0 ].nodeValue
# send_mail_pass=project.getElementsByTagName("send_mail_pass")[0].childNodes[0].nodeValue
# auto_config_path=project.getElementsByTagName("auto_config_path")[0].childNodes[0].nodeValue
# nginx_config_dir= project.getElementsByTagName("nginx_config_dir")[0].childNodes[0].nodeValue
nginx_config_dir = '/config/nginx/'
# nginx_www_dir= project.getElementsByTagName("nginx_www_dir")[0].childNodes[0].nodeValue
nginx_www_dir = '/data/www/auto_deploy/'
# nginx_static_template_only_www=project.getElementsByTagName("nginx_static_template_only_www")[0].childNodes[0].nodeValue
nginx_static_template_only_www = './template/nginx_conf1.txt'
# nginx_static_template_301=project.getElementsByTagName("nginx_static_template_301")[0].childNodes[0].nodeValue
nginx_static_template_301 = './template/nginx_conf2.txt'
# rollback_path=project.getElementsByTagName("rollback_path")[0].childNodes[0].nodeValue
rollback_path = '/data/rollback/'
index_define='./'+getText(project.getElementsByTagName ( "index_path" ))
try :
#############################################################################
# new project on line
#############################################################################
if operation_type == 'update' :
print('starting update ' + project_name + ' '+ project_type + '.......')
logging.info('starting update ' + project_name +' '+ project_type + '.......')
import newproject
if project_type == 'other' :
nginx_static_template = nginx_static_template_only_www
project_type_dir='www'
if project_type == 'www' :
nginx_static_template = nginx_static_template_301
project_type_dir='mobile'
if newproject.CreateNginxConfigFile ( project_type , project_name , domain_name , nginx_static_template ,
nginx_config_dir , index_define , moblie_301_project_name ,
moblie_301_domain_name,second_level_domain ,filename) :
print('Create Nginx File is good')
logging.info(project_name+'Create Nginx File is good')
sourcefile2 = re.sub ( 'Auto_Deploy_(\w+).zip' , 'Auto_Deploy_\\1_' + project_type_dir + '.zip' , sourcefile )
os.rename ( sourcefile , sourcefile2 )
print('check dir exists '+nginx_www_dir+project_type_dir+'/'+filename)
logging.info('check dir exists '+nginx_www_dir+project_type_dir+'/'+filename)
if os.path.exists(nginx_www_dir+project_type_dir+'/'+filename):
logging.info('move files to rollback')
print('move files to rollback')
if os.path.exists('/data/rollback/'+otherStyleTime+'_'+filename):
logging.info('dir exists! move file to rollback /data/rollback/'+otherStyleTime+'_'+filename+otherStyleTime)
print('move files to rollback++++')
os.rename ( '/data/rollback/'+otherStyleTime+'_'+filename , '/data/rollback/'+otherStyleTime+'_'+filename+otherStyleTime )
shutil.move(nginx_www_dir+project_type_dir+'/'+filename,'/data/rollback/'+otherStyleTime+'_'+filename+otherStyleTime)
logging.info('remove success!')
else:
logging.info('exists not ture')
print('exists not ture')
print('start unzip file')
if newproject.UnzipSouceFile ( sourcefile2 , nginx_www_dir + project_type_dir ) :
logging.info(project_name+'Unzip Source File is good')
print(project_name+'Unzip Source File is good')
else :
print(project_name+'Unzip Err!!')
if newproject.RestartNginx ( ) :
print(project_name+'Restart is good')
logging.info(project_name+'Restart is good')
import mail
send_mail_address='[email protected]'
connect_email='[email protected]'
send_mail_host='smtp.mxhichina.com'
send_mail_name='[email protected]'
send_mail_pass='123qwe!@#QWE!@#QWE'
freeback = mail.pysendmail ( send_mail_address , connect_email , operation_type , project_name ,
domain_name , send_mail_host , send_mail_name , send_mail_pass );
print freeback
connect_email='[email protected]'
freeback = mail.pysendmail ( send_mail_address , connect_email , operation_type , project_name ,
domain_name , send_mail_host , send_mail_name , send_mail_pass );
print freeback
connect_email='[email protected]'
freeback = mail.pysendmail ( send_mail_address , connect_email , operation_type , project_name ,
domain_name , send_mail_host , send_mail_name , send_mail_pass );
print freeback
connect_email='[email protected]'
freeback = mail.pysendmail ( send_mail_address , connect_email , operation_type , project_name ,
domain_name , send_mail_host , send_mail_name , send_mail_pass );
print freeback
#############################################################################
# new project on line
#############################################################################
if operation_type == 'create' :
print('starting create ' + project_name + ' '+ project_type + '.......')
logging.info('starting create ' + project_name +' '+ project_type + '.......')
import newproject
if project_type == 'other' :
nginx_static_template = nginx_static_template_only_www
project_type_dir='www'
if project_type == 'www' :
nginx_static_template = nginx_static_template_301
project_type_dir='mobile'
if newproject.CreateNginxConfigFile ( project_type , project_name , domain_name , nginx_static_template ,
nginx_config_dir , index_define , moblie_301_project_name ,
moblie_301_domain_name,second_level_domain ,filename) :
print('Create Nginx File is good')
logging.info(project_name+'Create Nginx File is good')
sourcefile2 = re.sub ( 'Auto_Deploy_(\w+).zip' , 'Auto_Deploy_\\1_' + project_type_dir + '.zip' , sourcefile )
os.rename ( sourcefile , sourcefile2 )
if os.path.exists(nginx_www_dir+project_type_dir+'/'+filename):
shutil.move(nginx_www_dir+project_type_dir+'/'+filename,'/data/rollback/'+otherStyleTime+'_'+filename)
if newproject.UnzipSouceFile ( sourcefile2 , nginx_www_dir + project_type_dir ) :
print(project_name+'Unzip Source File is good')
logging.info(project_name+'Unzip Source File is good')
else :
print(project_name+'Unzip Err!!')
logging.debug(project_name+'Unzip Err!!')
if newproject.RestartNginx ( ) :
print(project_name+'Restart is good')
logging.info(project_name+'Restart is good')
import mail
send_mail_address='[email protected]'
send_mail_host='smtp.mxhichina.com'
send_mail_name='[email protected]'
send_mail_pass='123qwe!@#QWE!@#QWE'
connect_email='[email protected]'
freeback = mail.pysendmail ( send_mail_address , connect_email , operation_type , project_name ,
domain_name , send_mail_host , send_mail_name , send_mail_pass );
print freeback
connect_email='[email protected]'
freeback = mail.pysendmail ( send_mail_address , connect_email , operation_type , project_name ,
domain_name , send_mail_host , send_mail_name , send_mail_pass );
print freeback
connect_email='[email protected]'
freeback = mail.pysendmail ( send_mail_address , connect_email , operation_type , project_name ,
domain_name , send_mail_host , send_mail_name , send_mail_pass );
print freeback
connect_email='[email protected]'
freeback = mail.pysendmail ( send_mail_address , connect_email , operation_type , project_name ,
domain_name , send_mail_host , send_mail_name , send_mail_pass );
print freeback
except :
logging.info('err')
import mail
send_mail_address='[email protected]'
send_mail_host='smtp.mxhichina.com'
send_mail_name='[email protected]'
send_mail_pass='123qwe!@#QWE!@#QWE'
connect_email='[email protected]'
mail.pysendmail ( send_mail_address , connect_email , 'err!!!' , project_name ,
domain_name , send_mail_host , send_mail_name , send_mail_pass );
return project_name+'err'
return project_name+'success'