|
@@ -16,9 +16,9 @@ from walle.model.record import RecordModel
|
|
from walle.model.task import TaskModel
|
|
from walle.model.task import TaskModel
|
|
from walle.service.code import Code
|
|
from walle.service.code import Code
|
|
from walle.service.error import WalleError
|
|
from walle.service.error import WalleError
|
|
-from walle.service.extensions import socketio
|
|
|
|
from walle.service.utils import color_clean
|
|
from walle.service.utils import color_clean
|
|
from walle.service.waller import Waller
|
|
from walle.service.waller import Waller
|
|
|
|
+from flask_socketio import emit
|
|
|
|
|
|
|
|
|
|
class Deployer:
|
|
class Deployer:
|
|
@@ -77,6 +77,8 @@ class Deployer:
|
|
self.project_name = self.project_info['id']
|
|
self.project_name = self.project_info['id']
|
|
self.dir_codebase_project = self.local_codebase + str(self.project_name)
|
|
self.dir_codebase_project = self.local_codebase + str(self.project_name)
|
|
|
|
|
|
|
|
+ self.init_repo()
|
|
|
|
+
|
|
# start to deploy
|
|
# start to deploy
|
|
self.console = console
|
|
self.console = console
|
|
|
|
|
|
@@ -139,8 +141,6 @@ class Deployer:
|
|
:param project_name:
|
|
:param project_name:
|
|
:return:
|
|
:return:
|
|
'''
|
|
'''
|
|
- # TODO
|
|
|
|
- socketio.sleep(0.001)
|
|
|
|
self.stage = self.stage_deploy
|
|
self.stage = self.stage_deploy
|
|
self.sequence = 2
|
|
self.sequence = 2
|
|
|
|
|
|
@@ -163,7 +163,8 @@ class Deployer:
|
|
# copy to a local version
|
|
# copy to a local version
|
|
self.release_version = '%s_%s_%s' % (
|
|
self.release_version = '%s_%s_%s' % (
|
|
self.project_name, self.task_id, time.strftime('%Y%m%d_%H%M%S', time.localtime(time.time())))
|
|
self.project_name, self.task_id, time.strftime('%Y%m%d_%H%M%S', time.localtime(time.time())))
|
|
- with self.local.cd(self.dir_codebase):
|
|
|
|
|
|
+
|
|
|
|
+ with self.local.cd(self.local_codebase):
|
|
command = 'cp -rf %s %s' % (self.dir_codebase_project, self.release_version)
|
|
command = 'cp -rf %s %s' % (self.dir_codebase_project, self.release_version)
|
|
current_app.logger.info('cd %s command: %s ', self.dir_codebase_project, command)
|
|
current_app.logger.info('cd %s command: %s ', self.dir_codebase_project, command)
|
|
|
|
|
|
@@ -174,7 +175,8 @@ class Deployer:
|
|
command = 'git reset -q --hard %s' % (self.taskMdl.get('commit_id'))
|
|
command = 'git reset -q --hard %s' % (self.taskMdl.get('commit_id'))
|
|
result = self.local.run(command, wenv=self.config())
|
|
result = self.local.run(command, wenv=self.config())
|
|
|
|
|
|
- pass
|
|
|
|
|
|
+ if result.exited != Code.Ok:
|
|
|
|
+ raise WalleError(Code.shell_git_fail, message=result.stdout)
|
|
|
|
|
|
def post_deploy(self):
|
|
def post_deploy(self):
|
|
|
|
|
|
@@ -188,8 +190,6 @@ class Deployer:
|
|
- 传送到版本库 release
|
|
- 传送到版本库 release
|
|
:return:
|
|
:return:
|
|
'''
|
|
'''
|
|
- # TODO
|
|
|
|
- socketio.sleep(0.001)
|
|
|
|
self.stage = self.stage_post_deploy
|
|
self.stage = self.stage_post_deploy
|
|
self.sequence = 3
|
|
self.sequence = 3
|
|
|
|
|
|
@@ -200,8 +200,8 @@ class Deployer:
|
|
|
|
|
|
# 压缩打包
|
|
# 压缩打包
|
|
self.release_version_tar = '%s.tgz' % (self.release_version)
|
|
self.release_version_tar = '%s.tgz' % (self.release_version)
|
|
- with self.local.cd(self.dir_codebase):
|
|
|
|
- command = 'tar zcvf %s %s' % (self.release_version_tar, self.release_version)
|
|
|
|
|
|
+ with self.local.cd(self.local_codebase):
|
|
|
|
+ command = 'tar zcf %s %s' % (self.release_version_tar, self.release_version)
|
|
result = self.local.run(command, wenv=self.config())
|
|
result = self.local.run(command, wenv=self.config())
|
|
|
|
|
|
def prev_release(self, waller):
|
|
def prev_release(self, waller):
|
|
@@ -210,8 +210,6 @@ class Deployer:
|
|
- 检查 webroot 父目录是否存在
|
|
- 检查 webroot 父目录是否存在
|
|
:return:
|
|
:return:
|
|
'''
|
|
'''
|
|
- # TODO
|
|
|
|
- socketio.sleep(0.001)
|
|
|
|
self.stage = self.stage_prev_release
|
|
self.stage = self.stage_prev_release
|
|
self.sequence = 4
|
|
self.sequence = 4
|
|
|
|
|
|
@@ -248,8 +246,6 @@ class Deployer:
|
|
- 解压 remote
|
|
- 解压 remote
|
|
:return:
|
|
:return:
|
|
'''
|
|
'''
|
|
- # TODO
|
|
|
|
- socketio.sleep(0.001)
|
|
|
|
self.stage = self.stage_release
|
|
self.stage = self.stage_release
|
|
self.sequence = 5
|
|
self.sequence = 5
|
|
|
|
|
|
@@ -272,8 +268,6 @@ class Deployer:
|
|
解压版本包
|
|
解压版本包
|
|
:return:
|
|
:return:
|
|
'''
|
|
'''
|
|
- # TODO
|
|
|
|
- socketio.sleep(0.001)
|
|
|
|
with waller.cd(self.project_info['target_releases']):
|
|
with waller.cd(self.project_info['target_releases']):
|
|
command = 'tar zxf %s' % (self.release_version_tar)
|
|
command = 'tar zxf %s' % (self.release_version_tar)
|
|
result = waller.run(command, wenv=self.config())
|
|
result = waller.run(command, wenv=self.config())
|
|
@@ -307,37 +301,33 @@ class Deployer:
|
|
result = waller.run(command, wenv=self.config())
|
|
result = waller.run(command, wenv=self.config())
|
|
|
|
|
|
def list_tag(self):
|
|
def list_tag(self):
|
|
- self.init_repo()
|
|
|
|
-
|
|
|
|
with self.local.cd(self.dir_codebase_project):
|
|
with self.local.cd(self.dir_codebase_project):
|
|
command = 'git tag -l'
|
|
command = 'git tag -l'
|
|
- result = self.local.run(command, wenv=self.config())
|
|
|
|
- tags = color_clean(result.stdout.strip())
|
|
|
|
|
|
+ result = self.local.run(command, pty=False, wenv=self.config())
|
|
|
|
+ tags = result.stdout.strip()
|
|
tags = tags.split('\n')
|
|
tags = tags.split('\n')
|
|
return [color_clean(tag.strip()) for tag in tags]
|
|
return [color_clean(tag.strip()) for tag in tags]
|
|
|
|
|
|
return None
|
|
return None
|
|
|
|
|
|
def list_branch(self):
|
|
def list_branch(self):
|
|
- self.init_repo()
|
|
|
|
-
|
|
|
|
with self.local.cd(self.dir_codebase_project):
|
|
with self.local.cd(self.dir_codebase_project):
|
|
command = 'git pull'
|
|
command = 'git pull'
|
|
result = self.local.run(command, wenv=self.config())
|
|
result = self.local.run(command, wenv=self.config())
|
|
|
|
|
|
if result.exited != Code.Ok:
|
|
if result.exited != Code.Ok:
|
|
- raise WalleError(Code.shell_git_pull_fail)
|
|
|
|
|
|
+ raise WalleError(Code.shell_git_pull_fail, message=result.stdout)
|
|
|
|
|
|
current_app.logger.info(self.dir_codebase_project)
|
|
current_app.logger.info(self.dir_codebase_project)
|
|
|
|
|
|
command = 'git branch -r'
|
|
command = 'git branch -r'
|
|
- result = self.local.run(command, wenv=self.config())
|
|
|
|
|
|
+ result = self.local.run(command, pty=False, wenv=self.config())
|
|
|
|
|
|
# if result.exited != Code.Ok:
|
|
# if result.exited != Code.Ok:
|
|
# raise WalleError(Code.shell_run_fail)
|
|
# raise WalleError(Code.shell_run_fail)
|
|
|
|
|
|
# TODO 三种可能: false, error, success
|
|
# TODO 三种可能: false, error, success
|
|
- branches = color_clean(result.stdout.strip())
|
|
|
|
|
|
+ branches = result.stdout.strip()
|
|
branches = branches.split('\n')
|
|
branches = branches.split('\n')
|
|
# 去除 origin/HEAD -> 当前指向
|
|
# 去除 origin/HEAD -> 当前指向
|
|
# 去除远端前缀
|
|
# 去除远端前缀
|
|
@@ -348,25 +338,23 @@ class Deployer:
|
|
return None
|
|
return None
|
|
|
|
|
|
def list_commit(self, branch):
|
|
def list_commit(self, branch):
|
|
- self.init_repo()
|
|
|
|
-
|
|
|
|
with self.local.cd(self.dir_codebase_project):
|
|
with self.local.cd(self.dir_codebase_project):
|
|
command = 'git checkout %s && git pull' % (branch)
|
|
command = 'git checkout %s && git pull' % (branch)
|
|
self.local.run(command, wenv=self.config())
|
|
self.local.run(command, wenv=self.config())
|
|
|
|
|
|
- command = 'git log -35 --pretty="%h #_# %an #_# %s"'
|
|
|
|
- result = self.local.run(command, wenv=self.config())
|
|
|
|
|
|
+ command = 'git log -50 --pretty="%h #@_@# %an #@_@# %s"'
|
|
|
|
+ result = self.local.run(command, pty=False, wenv=self.config())
|
|
current_app.logger.info(result.stdout)
|
|
current_app.logger.info(result.stdout)
|
|
|
|
|
|
- commit_log = color_clean(result.stdout.strip())
|
|
|
|
|
|
+ commit_log = result.stdout.strip()
|
|
current_app.logger.info(commit_log)
|
|
current_app.logger.info(commit_log)
|
|
commit_list = commit_log.split('\n')
|
|
commit_list = commit_log.split('\n')
|
|
commits = []
|
|
commits = []
|
|
for commit in commit_list:
|
|
for commit in commit_list:
|
|
- if not re.search('^.+ #_# .+ #_# .*$', commit):
|
|
|
|
|
|
+ if not re.search('^.+ #@_@# .+ #@_@# .*$', commit):
|
|
continue
|
|
continue
|
|
|
|
|
|
- commit_dict = commit.split(' #_# ')
|
|
|
|
|
|
+ commit_dict = commit.split(' #@_@# ')
|
|
current_app.logger.info(commit_dict)
|
|
current_app.logger.info(commit_dict)
|
|
commits.append({
|
|
commits.append({
|
|
'id': commit_dict[0],
|
|
'id': commit_dict[0],
|
|
@@ -405,26 +393,33 @@ class Deployer:
|
|
|
|
|
|
def end(self, success=True):
|
|
def end(self, success=True):
|
|
status = TaskModel.status_success if success else TaskModel.status_fail
|
|
status = TaskModel.status_success if success else TaskModel.status_fail
|
|
|
|
+ current_app.logger.info('success:%s, status:%s' % (success, status))
|
|
TaskModel().get_by_id(self.task_id).update({'status': status})
|
|
TaskModel().get_by_id(self.task_id).update({'status': status})
|
|
|
|
|
|
def walle_deploy(self):
|
|
def walle_deploy(self):
|
|
self.start()
|
|
self.start()
|
|
- self.prev_deploy()
|
|
|
|
- self.deploy()
|
|
|
|
- self.post_deploy()
|
|
|
|
-
|
|
|
|
- all_servers_success = True
|
|
|
|
- for server_info in self.servers:
|
|
|
|
- server = server_info['host']
|
|
|
|
- try:
|
|
|
|
- self.connections[server] = Waller(host=server, user=self.project_info['target_user'])
|
|
|
|
- self.prev_release(self.connections[server])
|
|
|
|
- self.release(self.connections[server])
|
|
|
|
- self.post_release(self.connections[server])
|
|
|
|
- except Exception as e:
|
|
|
|
- current_app.logger.error(e)
|
|
|
|
- all_servers_success = False
|
|
|
|
- self.errors[server] = e.message
|
|
|
|
-
|
|
|
|
- self.end(all_servers_success)
|
|
|
|
|
|
+
|
|
|
|
+ try:
|
|
|
|
+ self.prev_deploy()
|
|
|
|
+ self.deploy()
|
|
|
|
+ self.post_deploy()
|
|
|
|
+
|
|
|
|
+ is_all_servers_success = True
|
|
|
|
+ for server_info in self.servers:
|
|
|
|
+ server = server_info['host']
|
|
|
|
+ try:
|
|
|
|
+ self.connections[server] = Waller(host=server, user=self.project_info['target_user'])
|
|
|
|
+ self.prev_release(self.connections[server])
|
|
|
|
+ self.release(self.connections[server])
|
|
|
|
+ self.post_release(self.connections[server])
|
|
|
|
+ except Exception as e:
|
|
|
|
+ is_all_servers_success = False
|
|
|
|
+ current_app.logger.error(e)
|
|
|
|
+ self.errors[server] = e.message
|
|
|
|
+ self.end(is_all_servers_success)
|
|
|
|
+
|
|
|
|
+ except Exception as e:
|
|
|
|
+ self.end(False)
|
|
|
|
+ emit('fail', {'event': 'console', 'data': {'message': Code.code_msg[Code.deploy_fail]}}, room=self.task_id)
|
|
|
|
+
|
|
return {'success': self.success, 'errors': self.errors}
|
|
return {'success': self.success, 'errors': self.errors}
|