Browse Source

walle 2.1 - feature 发布流程切换gitpython,提速30%

walle 6 years ago
parent
commit
1b279807e8
3 changed files with 75 additions and 34 deletions
  1. 2 0
      walle/model/project.py
  2. 9 29
      walle/service/deployer.py
  3. 64 5
      walle/service/git/repo.py

+ 2 - 0
walle/model/project.py

@@ -26,6 +26,8 @@ class ProjectModel(SurrogatePK, Model):
 
     task_audit_true = 1
     task_audit_false = 0
+    repo_mode_branch = 'branch'
+    repo_mode_tag = 'tag'
 
     # 表的结构:
     id = db.Column(Integer, primary_key=True, autoincrement=True)

+ 9 - 29
walle/service/deployer.py

@@ -180,13 +180,12 @@ class Deployer:
 
             result = self.localhost.local(command, wenv=self.config())
 
-        # 更新到指定 commit_id
-        with self.localhost.cd(self.local_codebase + self.release_version):
-            command = 'git reset -q --hard %s' % (self.taskMdl.get('commit_id'))
-            result = self.localhost.local(command, wenv=self.config())
-
-            if result.exited != Code.Ok:
-                raise WalleError(Code.shell_git_fail, message=result.stdout)
+        # 更新到指定 branch/commit_id 或 tag
+        repo = Repo(self.local_codebase + self.release_version)
+        if self.project_info['repo_mode'] == ProjectModel.repo_mode_branch:
+            repo.checkout_2_commit(branch=self.taskMdl['branch'], commit=self.taskMdl['commit_id'])
+        else:
+            repo.checkout_2_tag(tag=self.taskMdl['tag'])
 
     def post_deploy(self):
 
@@ -413,29 +412,10 @@ class Deployer:
         repo.init(url=self.project_info['repo_url'])
         return repo.commits(branch)
 
-    # 待废弃,迁移到gitpython
     def init_repo(self):
-        if not os.path.exists(self.dir_codebase_project):
-            # 检查 目录是否存在
-            command = 'mkdir -p %s' % (self.dir_codebase_project)
-            self.localhost.local(command, wenv=self.config())
-
-        with self.localhost.cd(self.dir_codebase_project):
-            is_git_dir = self.localhost.local('[ -d ".git" ] && git status', exception=False, wenv=self.config())
-
-        if is_git_dir.exited != Code.Ok:
-            # 否则当作新项目检出完整代码
-            # 检查 目录是否存在
-            command = 'rm -rf %s' % (self.dir_codebase_project)
-            self.localhost.local(command, wenv=self.config())
-
-            # 切换到gitpython模式
-            command = 'git clone %s %s' % (self.project_info['repo_url'], self.dir_codebase_project)
-            current_app.logger.info('cd %s  command: %s  ' % (self.dir_codebase_project, command))
-
-            result = self.localhost.local(command, wenv=self.config())
-            if result.exited != Code.Ok:
-                raise WalleError(Code.shell_git_init_fail, message=result.stdout)
+        repo = Repo(self.dir_codebase_project)
+        repo.init(url=self.project_info['repo_url'])
+        # @todo 没有做emit
 
     def cleanup_local(self):
         # clean local package

+ 64 - 5
walle/service/git/repo.py

@@ -10,16 +10,64 @@
 import os
 import re
 import os.path as osp
+import getpass
 import git as PyGit
 from git import Repo as PyRepo
+from walle.model.record import RecordModel
 
 
 class Repo:
+
+    '''
+    @todo
+    需要把websocket接入
+    '''
     path = None
 
     def __init__(self, path=None):
         self.path = path
 
+
+    def log(self, wenv):
+        '''
+
+        @param wenv:
+
+            @param stage:
+            @param sequence:
+            @param user_id:
+            @param task_id:
+            @param status:
+            @param host:
+            @param user:
+            @param command:
+            @param success:
+            @param error:
+
+        @return:
+        '''
+        RecordModel().save_record(stage=wenv['stage'], sequence=wenv['sequence'], user_id=wenv['user_id'],
+                                  task_id=wenv['task_id'], status=exitcode, host='127.0.0.1', user=getpass.getuser(),
+                                  command=result.command, success=stdout,
+                                  error=stderr)
+
+    def websocket(self):
+        ws_dict = {
+            'user': getpass.getuser(),
+            'host': '127.0.0.1',
+            'cmd': command,
+            'status': exitcode,
+            'stage': wenv['stage'],
+            'sequence': wenv['sequence'],
+            'success': stdout,
+            'error': stderr,
+        }
+        if wenv['console']:
+            emit('console', {'event': 'task:console', 'data': ws_dict}, room=wenv['task_id'])
+
+        pass
+
+
     def is_git_dir(self):
         '''
         判断是否为git目录
@@ -73,21 +121,32 @@ class Repo:
         return repo.remote().pull()
 
     def checkout_2_branch(self, branch):
+        '''
+        切换到某个分支
+
+        @param branch:
+        @return:
+        '''
         PyRepo(self.path).git.checkout(branch)
 
     def checkout_2_commit(self, branch, commit):
         '''
-        @todo 未完成
+        切换分支的某个commit
+
         @param branch:
         @param commit:
         @return:
         '''
-        PyRepo(self.path).git.checkout(branch)
-        # PyRepo(self.path).head.set_reference(branch)
-        # 方法有问题,只是做了reset,没有checkout
-        PyRepo(self.path).head.set_commit(commit)
+        self.checkout_2_branch(branch=branch)
+        PyRepo(self.path).git.reset('--hard', commit)
 
     def checkout_2_tag(self, tag):
+        '''
+        切换到tag
+
+        @param tag:
+        @return:
+        '''
         PyRepo(self.path).git.checkout(tag)
 
     def branches(self):