1818from walle .model .task import TaskModel
1919from walle .service .code import Code
2020from walle .service .error import WalleError
21- from walle .service .utils import color_clean , suffix_format
22- from walle .service .utils import excludes_format
21+ from walle .service .utils import color_clean
22+ from walle .service .utils import excludes_format , includes_format
2323from walle .service .notice import Notice
2424from walle .service .waller import Waller
2525from flask_login import current_user
@@ -45,6 +45,7 @@ class Deployer:
4545 TaskRecord = None
4646
4747 console = False
48+ custom_global_env = {}
4849
4950 version = datetime .now ().strftime ('%Y%m%d%H%M%S' )
5051
@@ -56,7 +57,7 @@ class Deployer:
5657 local = None
5758
5859 def __init__ (self , task_id = None , project_id = None , console = False ):
59- self .local_codebase = current_app .config .get ('CODE_BASE' )
60+ self .local_codebase = current_app .config .get ('CODE_BASE' ). rstrip ( '/' ) + '/'
6061 self .localhost = Waller (host = '127.0.0.1' )
6162 self .TaskRecord = RecordModel ()
6263
@@ -69,6 +70,37 @@ def __init__(self, task_id=None, project_id=None, console=False):
6970 self .servers = self .taskMdl .get ('servers_info' )
7071 self .project_info = self .taskMdl .get ('project_info' )
7172
73+ # copy to a local version
74+ self .release_version = '{project_id}_{task_id}_{timestamp}' .format (
75+ project_id = self .project_info ['id' ],
76+ task_id = self .task_id ,
77+ timestamp = time .strftime ('%Y%m%d_%H%M%S' , time .localtime (time .time ())),
78+ )
79+ current_app .logger .info (self .taskMdl )
80+
81+ self .custom_global_env = {
82+ 'WEBROOT' : '"{}"' .format (self .project_info ['target_root' ]),
83+ 'CURRENT_RELEASE' : '"{}"' .format (self .release_version ),
84+ 'BRANCH' : '"{}"' .format (self .taskMdl .get ('branch' )),
85+ 'TAG' : '"{}"' .format (self .taskMdl .get ('tag' )),
86+ 'COMMIT_ID' : '"{}"' .format (self .taskMdl .get ('commit_id' )),
87+ 'PROJECT_NAME' : '"{}"' .format (self .project_info ['name' ]),
88+ 'PROJECT_ID' : '"{}"' .format (self .project_info ['id' ]),
89+ 'TASK_NAME' : '"{}"' .format (self .taskMdl .get ('name' )),
90+ 'TASK_ID' : '"{}"' .format (self .task_id ),
91+ 'DEPLOY_USER' : '"{}"' .format (self .taskMdl .get ('user_name' )),
92+ 'DEPLOY_TIME' : '"{}"' .format (time .strftime ('%Y%m%d-%H:%M:%S' , time .localtime (time .time ()))),
93+ }
94+ if self .project_info ['task_vars' ]:
95+ task_vars = [i .strip () for i in self .project_info ['task_vars' ].split ('\n ' ) if i .strip () and not i .strip ().startswith ('#' )]
96+ for var in task_vars :
97+ var_list = var .split ('=' , 1 )
98+ if len (var_list ) != 2 :
99+ continue
100+ self .custom_global_env [var_list [0 ]] = var_list [1 ]
101+
102+ self .localhost .init_env (env = self .custom_global_env )
103+
72104 if project_id :
73105 self .project_id = project_id
74106 self .project_info = ProjectModel (id = project_id ).item ()
@@ -118,11 +150,13 @@ def prev_deploy(self):
118150 self .init_repo ()
119151
120152 # 用户自定义命令
121- command = self .project_info ['prev_deploy' ]
122- if command :
123- current_app .logger .info (command )
124- with self .localhost .cd (self .dir_codebase_project ):
125- result = self .localhost .local (command , wenv = self .config ())
153+ commands = self .project_info ['prev_deploy' ]
154+ if commands :
155+ for command in commands .split ('\n ' ):
156+ if command .strip ().startswith ('#' ) or not command .strip ():
157+ continue
158+ with self .localhost .cd (self .dir_codebase_project ):
159+ result = self .localhost .local (command , wenv = self .config ())
126160
127161 def deploy (self ):
128162 '''
@@ -133,10 +167,10 @@ def deploy(self):
133167 '''
134168 self .stage = self .stage_deploy
135169 self .sequence = 2
136-
137- # copy to a local version
138- self .release_version = '%s_%s_%s' % (
139- self .project_name , self .task_id , time .strftime ('%Y%m%d_%H%M%S' , time .localtime (time .time ())))
170+ #
171+ # # copy to a local version
172+ # self.release_version = '%s_%s_%s' % (
173+ # self.project_name, self.task_id, time.strftime('%Y%m%d_%H%M%S', time.localtime(time.time())))
140174
141175 with self .localhost .cd (self .local_codebase ):
142176 command = 'cp -rf %s %s' % (self .dir_codebase_project , self .release_version )
@@ -168,26 +202,25 @@ def post_deploy(self):
168202 self .sequence = 3
169203
170204 # 用户自定义命令
171- command = self .project_info ['post_deploy' ]
172- if command :
173- with self .localhost .cd (self .local_codebase + self .release_version ):
174- result = self .localhost .local (command , wenv = self .config ())
205+ commands = self .project_info ['post_deploy' ]
206+ if commands :
207+ for command in commands .split ('\n ' ):
208+ if command .strip ().startswith ('#' ) or not command .strip ():
209+ continue
210+ with self .localhost .cd (self .local_codebase + self .release_version ):
211+ result = self .localhost .local (command , wenv = self .config ())
175212
176213 # 压缩打包
177214 # 排除文件发布
178215 self .release_version_tar = '%s.tgz' % (self .release_version )
179216 with self .localhost .cd (self .local_codebase ):
180- excludes = excludes_format (self .project_info ['excludes' ])
181- command = 'tar zcf %s %s %s' % (self .release_version_tar , excludes , self .release_version )
217+ if self .project_info ['is_include' ]:
218+ files = includes_format (self .release_version , self .project_info ['excludes' ])
219+ else :
220+ files = excludes_format (self .release_version , self .project_info ['excludes' ])
221+ command = 'tar zcf %s/%s %s' % (self .local_codebase .rstrip ('/' ), self .release_version_tar , files )
182222 result = self .localhost .local (command , wenv = self .config ())
183223
184- # # 指定文件发布
185- # self.release_version_tar = '%s.tgz' % (self.release_version)
186- # with self.localhost.cd(self.local_codebase):
187- # excludes = suffix_format(self.dir_codebase_project, self.project_info['excludes'])
188- # command = 'tar zcf %s %s %s' % (self.release_version_tar, excludes, self.release_version)
189- # result = self.local.run(command, wenv=self.config())
190-
191224 def prev_release (self , waller ):
192225 '''
193226 4.部署代码到目标机器前做的任务
@@ -215,13 +248,15 @@ def prev_release(self, waller):
215248
216249 def prev_release_custom (self , waller ):
217250 # 用户自定义命令
218- command = self .project_info ['prev_release' ]
219- if command :
220- current_app .logger .info (command )
221- # TODO
222- target_release_version = "%s/%s" % (self .project_info ['target_releases' ], self .release_version )
223- with waller .cd (target_release_version ):
224- result = waller .run (command , wenv = self .config ())
251+ commands = self .project_info ['prev_release' ]
252+ if commands :
253+ for command in commands .split ('\n ' ):
254+ if command .strip ().startswith ('#' ) or not command .strip ():
255+ continue
256+ # TODO
257+ target_release_version = "%s/%s" % (self .project_info ['target_releases' ], self .release_version )
258+ with waller .cd (target_release_version ):
259+ result = waller .run (command , wenv = self .config ())
225260
226261 def release (self , waller ):
227262 '''
@@ -299,14 +334,16 @@ def post_release(self, waller):
299334 '''
300335 self .stage = self .stage_post_release
301336 self .sequence = 6
302-
303337 # 用户自定义命令
304- command = self .project_info ['post_release' ]
305- if command :
306- current_app .logger .info (command )
307- with waller .cd (self .project_info ['target_root' ]):
308- result = waller .run (command , wenv = self .config ())
309-
338+ commands = self .project_info ['post_release' ]
339+ if commands :
340+ for command in commands .split ('\n ' ):
341+ if command .strip ().startswith ('#' ) or not command .strip ():
342+ continue
343+ # TODO
344+ with waller .cd (self .project_info ['target_root' ]):
345+ pty = False if command .find ('nohup' ) >= 0 else True
346+ result = waller .run (command , wenv = self .config (), pty = pty )
310347 # 个性化,用户重启的不一定是NGINX,可能是tomcat, apache, php-fpm等
311348 # self.post_release_service(waller)
312349
@@ -495,7 +532,10 @@ def walle_deploy(self):
495532 for server_info in self .servers :
496533 host = server_info ['host' ]
497534 try :
498- self .connections [host ] = Waller (host = host , user = server_info ['user' ], port = server_info ['port' ])
535+ waller = Waller (host = host , user = server_info ['user' ], port = server_info ['port' ], inline_ssh_env = True )
536+ waller .init_env (env = self .custom_global_env )
537+
538+ self .connections [host ] = waller
499539 self .prev_release (self .connections [host ])
500540 self .release (self .connections [host ])
501541 self .post_release (self .connections [host ])
@@ -527,7 +567,10 @@ def walle_rollback(self):
527567 for server_info in self .servers :
528568 host = server_info ['host' ]
529569 try :
530- self .connections [host ] = Waller (host = host , user = server_info ['user' ], port = server_info ['port' ])
570+ waller = Waller (host = host , user = server_info ['user' ], port = server_info ['port' ], inline_ssh_env = True )
571+ waller .init_env (env = self .custom_global_env )
572+
573+ self .connections [host ] = waller
531574 self .prev_release_custom (self .connections [host ])
532575 self .release (self .connections [host ])
533576 self .post_release (self .connections [host ])
0 commit comments