Преглед изворни кода

walle 2.0.0 增加排除/包含上线模式

walle-web.io пре 6 година
родитељ
комит
415797d699
3 измењених фајлова са 71 додато и 35 уклоњено
  1. 25 19
      walle/service/deployer.py
  2. 36 13
      walle/service/utils.py
  3. 10 3
      walle/service/waller.py

+ 25 - 19
walle/service/deployer.py

@@ -18,8 +18,8 @@ from walle.model.record import RecordModel
 from walle.model.task import TaskModel
 from walle.service.code import Code
 from walle.service.error import WalleError
-from walle.service.utils import color_clean, suffix_format
-from walle.service.utils import excludes_format
+from walle.service.utils import color_clean
+from walle.service.utils import excludes_format, includes_format
 from walle.service.notice import Notice
 from walle.service.waller import Waller
 from flask_login import current_user
@@ -56,7 +56,7 @@ class Deployer:
     local = None
 
     def __init__(self, task_id=None, project_id=None, console=False):
-        self.local_codebase = current_app.config.get('CODE_BASE')
+        self.local_codebase = current_app.config.get('CODE_BASE').rstrip('/') + '/'
         self.localhost = Waller(host='127.0.0.1')
         self.TaskRecord = RecordModel()
 
@@ -182,8 +182,11 @@ class Deployer:
         # 排除文件发布
         self.release_version_tar = '%s.tgz' % (self.release_version)
         with self.localhost.cd(self.local_codebase):
-            excludes = excludes_format(self.project_info['excludes'])
-            command = 'tar zcf  %s %s %s' % (self.release_version_tar, excludes, self.release_version)
+            if self.project_info['is_include']:
+                files = includes_format(self.release_version, self.project_info['excludes'])
+            else:
+                files = excludes_format(self.release_version, self.project_info['excludes'])
+            command = 'tar zcf %s/%s %s' % (self.local_codebase.rstrip('/'), self.release_version_tar, files)
             result = self.localhost.local(command, wenv=self.config())
 
         # # 指定文件发布
@@ -220,13 +223,15 @@ class Deployer:
 
     def prev_release_custom(self, waller):
         # 用户自定义命令
-        command = self.project_info['prev_release']
-        if command:
-            current_app.logger.info(command)
-            # TODO
-            target_release_version = "%s/%s" % (self.project_info['target_releases'], self.release_version)
-            with waller.cd(target_release_version):
-                result = waller.run(command, wenv=self.config())
+        commands = self.project_info['prev_release']
+        if commands:
+            for command in commands.split('\n'):
+                if command.strip().startswith('#'):
+                    continue
+                # TODO
+                target_release_version = "%s/%s" % (self.project_info['target_releases'], self.release_version)
+                with waller.cd(target_release_version):
+                    result = waller.run(command, wenv=self.config())
 
     def release(self, waller):
         '''
@@ -304,14 +309,15 @@ class Deployer:
         '''
         self.stage = self.stage_post_release
         self.sequence = 6
-
         # 用户自定义命令
-        command = self.project_info['post_release']
-        if command:
-            current_app.logger.info(command)
-            with waller.cd(self.project_info['target_root']):
-                result = waller.run(command, wenv=self.config())
-
+        commands = self.project_info['post_release']
+        if commands:
+            for command in commands.split('\n'):
+                if command.strip().startswith('#'):
+                    continue
+                # TODO
+                with waller.cd(self.project_info['target_root']):
+                    result = waller.run(command, wenv=self.config())
         # 个性化,用户重启的不一定是NGINX,可能是tomcat, apache, php-fpm等
         # self.post_release_service(waller)
 

+ 36 - 13
walle/service/utils.py

@@ -81,16 +81,39 @@ def say_yes():
     )
 
 
-def excludes_format(excludes_string):
-    excludes = [i for i in excludes_string.split('\n') if i.strip()]
-    if not excludes:
-        return ''
-    excludes = ' --exclude='.join(excludes)
-    return ' --exclude=' + excludes
-
-
-# 指定发布文件,支持模糊匹配,如:*.war
-def suffix_format(path, suffix_file):
-    for suffix_file in os.listdir('%s' % path):
-        if fnmatch.fnmatch(suffix_file, '%s' % suffix_file):
-            return suffix_file
+def excludes_format(path, excludes_string=None):
+    '''
+    排除文件,支持正则匹配,支持多选字符串
+    @param path:
+    @param excludes_string:
+    @return:
+    '''
+    path = os.path.basename(path) + '/'
+    if not excludes_string:
+        return path
+
+    prefix = '--exclude='
+    excludes = [prefix + i for i in excludes_string.split('\n') if i.strip()]
+
+    return ' {excludes} {path} '.format(excludes=' '.join(excludes), path=path)
+
+
+def includes_format(path, includes_string=None):
+    '''
+    指定发布文件,支持正则匹配,如:*.war。支持多行字符串。
+
+    @param path: release目录,非路径
+    @param includes_string:
+    @return:
+    '''
+    path = os.path.basename(path) + '/'
+    if not includes_string:
+        return path
+
+    prefix = path
+    includes = [prefix + i for i in includes_string.split('\n') if i.strip()]
+
+    if not includes:
+        return path
+
+    return ' '.join(includes)

+ 10 - 3
walle/service/waller.py

@@ -174,17 +174,24 @@ class Waller(Connection):
         except Exception as e:
             # TODO 收尾下
             current_app.logger.info('put: %s, %s', e, dir(e))
+            if wtype == 'put':
+                op_user = pwd.getpwuid(os.getuid())[0]
+                op_host = '127.0.0.1'
+            else:
+                op_user = self.user
+                op_host = self.host
 
             # TODO command
             ws_dict = {
-                'user': self.user,
-                'host': self.host,
+                'user': op_user,
+                'host': op_host,
                 'cmd': command,
                 'status': 1,
                 'stage': wenv['stage'],
                 'sequence': wenv['sequence'],
                 'success': '',
-                'error': e.message,
+                'error': str(e),
             }
+            current_app.logger.info(ws_dict)
             if wenv['console']:
                 emit('console', {'event': 'task:console', 'data': ws_dict}, room=wenv['task_id'])