Przeglądaj źródła

walle 2.0.0 增加自定义全局变量

walle-web.io 6 lat temu
rodzic
commit
a023591e14
2 zmienionych plików z 46 dodań i 19 usunięć
  1. 37 16
      walle/service/deployer.py
  2. 9 3
      walle/service/waller.py

+ 37 - 16
walle/service/deployer.py

@@ -45,6 +45,7 @@ class Deployer:
     TaskRecord = None
 
     console = False
+    custom_global_env = {}
 
     version = datetime.now().strftime('%Y%m%d%H%M%S')
 
@@ -69,6 +70,29 @@ class Deployer:
             self.servers = self.taskMdl.get('servers_info')
             self.project_info = self.taskMdl.get('project_info')
 
+            # copy to a local version
+            self.release_version = '{project_id}_{task_id}_{timestamp}'.format(
+                project_id=self.project_info['id'],
+                task_id=self.task_id,
+                timestamp=time.strftime('%Y%m%d_%H%M%S', time.localtime(time.time())),
+            )
+            current_app.logger.info(self.taskMdl)
+
+            self.custom_global_env = {
+                'WEBROOT': self.project_info['target_root'],
+                'CURRENT_RELEASE': self.release_version,
+                'BRANCH': self.taskMdl.get('branch'),
+                'TAG': self.taskMdl.get('tag'),
+                'COMMIT_ID': self.taskMdl.get('commit_id'),
+                'PROJECT_NAME': self.project_info['name'],
+                'PROJECT_ID': str(self.project_info['id']),
+                'TASK_NAME': self.taskMdl.get('name'),
+                'TASK_ID': str(self.task_id),
+                'DEPLOY_USER': self.taskMdl.get('user_name'),
+                'DEPLOY_TIME': time.strftime('%Y%m%d %H%M%S', time.localtime(time.time())),
+            }
+            self.localhost.init_env(env=self.custom_global_env)
+
         if project_id:
             self.project_id = project_id
             self.project_info = ProjectModel(id=project_id).item()
@@ -121,7 +145,7 @@ class Deployer:
         commands = self.project_info['prev_deploy']
         if commands:
             for command in commands.split('\n'):
-                if command.strip().startswith('#'):
+                if command.strip().startswith('#') or not command.strip():
                     continue
                 with self.localhost.cd(self.dir_codebase_project):
                     result = self.localhost.local(command, wenv=self.config())
@@ -135,10 +159,10 @@ class Deployer:
         '''
         self.stage = self.stage_deploy
         self.sequence = 2
-
-        # copy to a local version
-        self.release_version = '%s_%s_%s' % (
-            self.project_name, self.task_id, time.strftime('%Y%m%d_%H%M%S', time.localtime(time.time())))
+#
+#        # copy to a local version
+#        self.release_version = '%s_%s_%s' % (
+#            self.project_name, self.task_id, time.strftime('%Y%m%d_%H%M%S', time.localtime(time.time())))
 
         with self.localhost.cd(self.local_codebase):
             command = 'cp -rf %s %s' % (self.dir_codebase_project, self.release_version)
@@ -173,7 +197,7 @@ class Deployer:
         commands = self.project_info['post_deploy']
         if commands:
             for command in commands.split('\n'):
-                if command.strip().startswith('#'):
+                if command.strip().startswith('#') or not command.strip():
                     continue
                 with self.localhost.cd(self.local_codebase + self.release_version):
                     result = self.localhost.local(command, wenv=self.config())
@@ -189,13 +213,6 @@ class Deployer:
             command = 'tar zcf %s/%s %s' % (self.local_codebase.rstrip('/'), self.release_version_tar, files)
             result = self.localhost.local(command, wenv=self.config())
 
-        # # 指定文件发布
-        # self.release_version_tar = '%s.tgz' % (self.release_version)
-        # with self.localhost.cd(self.local_codebase):
-        #     excludes = suffix_format(self.dir_codebase_project, self.project_info['excludes'])
-        #     command = 'tar zcf  %s %s %s' % (self.release_version_tar, excludes, self.release_version)
-        #     result = self.local.run(command, wenv=self.config())
-
     def prev_release(self, waller):
         '''
         4.部署代码到目标机器前做的任务
@@ -226,7 +243,7 @@ class Deployer:
         commands = self.project_info['prev_release']
         if commands:
             for command in commands.split('\n'):
-                if command.strip().startswith('#'):
+                if command.strip().startswith('#') or not command.strip():
                     continue
                 # TODO
                 target_release_version = "%s/%s" % (self.project_info['target_releases'], self.release_version)
@@ -313,7 +330,7 @@ class Deployer:
         commands = self.project_info['post_release']
         if commands:
             for command in commands.split('\n'):
-                if command.strip().startswith('#'):
+                if command.strip().startswith('#') or not command.strip():
                     continue
                 # TODO
                 with waller.cd(self.project_info['target_root']):
@@ -506,7 +523,11 @@ class Deployer:
             for server_info in self.servers:
                 host = server_info['host']
                 try:
-                    self.connections[host] = Waller(host=host, user=server_info['user'], port=server_info['port'])
+                    waller = Waller(host=host, user=server_info['user'], port=server_info['port'])
+                    waller.init_env(env=self.custom_global_env)
+                    current_app.logger.info(self.custom_global_env)
+
+                    self.connections[host] = waller
                     self.prev_release(self.connections[host])
                     self.release(self.connections[host])
                     self.post_release(self.connections[host])

+ 9 - 3
walle/service/waller.py

@@ -23,6 +23,11 @@ class Waller(Connection):
     connections, success, errors = {}, {}, {}
     release_version_tar, release_version = None, None
 
+    custom_global_env = {}
+
+    def init_env(self, env):
+        self.custom_global_env = env
+
     def run(self, command, wenv=None, run_mode=run_mode_remote, pty=True, exception=True, **kwargs):
         '''
         pty=True/False是直接影响到输出.False较适合在获取文本,True更适合websocket
@@ -40,11 +45,12 @@ class Waller(Connection):
         current_app.logger.info(message)
         try:
             if run_mode == self.run_mode_sudo:
-                result = super(Waller, self).sudo(command, pty=pty, **kwargs)
+                result = super(Waller, self).sudo(command, pty=pty, env=self.custom_global_env, **kwargs)
             elif run_mode == self.run_mode_local:
-                result = super(Waller, self).local(command, pty=pty, warn=True, watchers=[say_yes()], **kwargs)
+                current_app.logger.info(self.custom_global_env)
+                result = super(Waller, self).local(command, pty=pty, warn=True, watchers=[say_yes()], env=self.custom_global_env, **kwargs)
             else:
-                result = super(Waller, self).run(command, pty=pty, warn=True, watchers=[say_yes()], **kwargs)
+                result = super(Waller, self).run(command, pty=pty, warn=True, watchers=[say_yes()], env=self.custom_global_env, **kwargs)
 
             if result.failed:
                 exitcode, stdout, stderr = result.exited, '', result.stdout