|
@@ -82,9 +82,14 @@ class Deployer:
|
|
|
# start to deploy
|
|
|
self.console = console
|
|
|
|
|
|
- def config(self):
|
|
|
- return {'task_id': self.task_id, 'user_id': self.user_id, 'stage': self.stage, 'sequence': self.sequence,
|
|
|
- 'console': self.console}
|
|
|
+ def config(self, console=None):
|
|
|
+ return {
|
|
|
+ 'task_id': self.task_id,
|
|
|
+ 'user_id': self.user_id,
|
|
|
+ 'stage': self.stage,
|
|
|
+ 'sequence': self.sequence,
|
|
|
+ 'console': console if console is not None else self.console
|
|
|
+ }
|
|
|
|
|
|
def start(self):
|
|
|
RecordModel().query.filter_by(task_id=self.task_id).delete()
|
|
@@ -120,10 +125,6 @@ class Deployer:
|
|
|
# 检查 目录是否存在
|
|
|
self.init_repo()
|
|
|
|
|
|
- # TODO to be removed
|
|
|
- command = 'mkdir -p %s' % (self.dir_codebase_project)
|
|
|
- result = self.localhost.local(command, wenv=self.config())
|
|
|
-
|
|
|
# 用户自定义命令
|
|
|
command = self.project_info['prev_deploy']
|
|
|
if command:
|
|
@@ -200,13 +201,6 @@ class Deployer:
|
|
|
command = 'mkdir -p %s' % (self.project_info['target_releases'])
|
|
|
result = waller.run(command, wenv=self.config())
|
|
|
|
|
|
- # 用户自定义命令
|
|
|
- command = self.project_info['prev_release']
|
|
|
- if command:
|
|
|
- current_app.logger.info(command)
|
|
|
- with waller.cd(self.project_info['target_releases']):
|
|
|
- result = waller.run(command, wenv=self.config())
|
|
|
-
|
|
|
# TODO md5
|
|
|
# 传送到版本库 release
|
|
|
result = waller.put(self.local_codebase + self.release_version_tar,
|
|
@@ -216,6 +210,19 @@ class Deployer:
|
|
|
# 解压
|
|
|
self.release_untar(waller)
|
|
|
|
|
|
+ # 用户自定义命令
|
|
|
+ self.prev_release_custom(waller)
|
|
|
+
|
|
|
+ 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())
|
|
|
+
|
|
|
def release(self, waller):
|
|
|
'''
|
|
|
5.部署代码到目标机器做的任务
|
|
@@ -230,11 +237,36 @@ class Deployer:
|
|
|
with waller.cd(self.project_info['target_releases']):
|
|
|
# 0. get previous link
|
|
|
command = 'readlink ' + self.project_info['target_root']
|
|
|
+ result = waller.run(command, wenv=self.config(console=False))
|
|
|
+ self.previous_release_version = os.path.basename(result.stdout).strip()
|
|
|
+
|
|
|
+ # 1. create a tmp link dir
|
|
|
+ current_link_tmp_dir = '%s/current-tmp-%s' % (self.project_info['target_releases'], self.task_id)
|
|
|
+ command = 'ln -sfn %s/%s %s' % (
|
|
|
+ self.project_info['target_releases'], self.release_version, current_link_tmp_dir)
|
|
|
result = waller.run(command, wenv=self.config())
|
|
|
+
|
|
|
+ # 2. make a soft link from release to tmp link
|
|
|
+
|
|
|
+ # 3. move tmp link to webroot
|
|
|
+ current_link_tmp_dir = '%s/current-tmp-%s' % (self.project_info['target_releases'], self.task_id)
|
|
|
+ command = 'mv -fT %s %s' % (current_link_tmp_dir, self.project_info['target_root'])
|
|
|
+ result = waller.run(command, wenv=self.config())
|
|
|
+
|
|
|
+ def rollback(self, waller):
|
|
|
+ '''
|
|
|
+ 5.部署代码到目标机器做的任务
|
|
|
+ - 恢复旧版本
|
|
|
+ :return:
|
|
|
+ '''
|
|
|
+ self.stage = self.stage_release
|
|
|
+ self.sequence = 5
|
|
|
+
|
|
|
+ with waller.cd(self.project_info['target_releases']):
|
|
|
+ # 0. get previous link
|
|
|
+ command = 'readlink ' + self.project_info['target_root']
|
|
|
+ result = waller.run(command, wenv=self.config(console=False))
|
|
|
self.previous_release_version = os.path.basename(result.stdout)
|
|
|
- current_app.logger.info(command)
|
|
|
- current_app.logger.info(self.previous_release_version)
|
|
|
- current_app.logger.info(result.stdout)
|
|
|
|
|
|
# 1. create a tmp link dir
|
|
|
current_link_tmp_dir = '%s/current-tmp-%s' % (self.project_info['target_releases'], self.task_id)
|
|
@@ -270,12 +302,10 @@ class Deployer:
|
|
|
|
|
|
# 用户自定义命令
|
|
|
command = self.project_info['post_release']
|
|
|
- if not command:
|
|
|
- return None
|
|
|
-
|
|
|
- current_app.logger.info(command)
|
|
|
- with waller.cd(self.project_info['target_root']):
|
|
|
- result = waller.run(command, wenv=self.config())
|
|
|
+ if command:
|
|
|
+ current_app.logger.info(command)
|
|
|
+ with waller.cd(self.project_info['target_root']):
|
|
|
+ result = waller.run(command, wenv=self.config())
|
|
|
|
|
|
# self.post_release_service(waller)
|
|
|
|
|
@@ -291,19 +321,6 @@ class Deployer:
|
|
|
|
|
|
def project_detection(self):
|
|
|
errors = []
|
|
|
- # walle user => walle LOCAL_SERVER_USER
|
|
|
- # show ssh_rsa.pub (maybe not necessary)
|
|
|
- # command = 'whoami'
|
|
|
- # current_app.logger.info(command)
|
|
|
- # result = self.localhost.local(command, exception=False, wenv=self.config())
|
|
|
- # if result.failed:
|
|
|
- # errors.append({
|
|
|
- # 'title': u'本地免密码登录失败',
|
|
|
- # 'why': result.stdout,
|
|
|
- # 'how': u'在宿主机中配置免密码登录,把walle启动用户%s的~/.ssh/ssh_rsa.pub添加到LOCAL_SERVER_USER用户%s的~/.ssh/authorized_keys。了解更多:http://walle-web.io/docs/troubleshooting.html' % (
|
|
|
- # pwd.getpwuid(os.getuid())[0], current_app.config.get('LOCAL_SERVER_USER')),
|
|
|
- # })
|
|
|
-
|
|
|
# LOCAL_SERVER_USER => git
|
|
|
|
|
|
# LOCAL_SERVER_USER => target_servers
|
|
@@ -485,3 +502,27 @@ class Deployer:
|
|
|
self.end(False)
|
|
|
|
|
|
return {'success': self.success, 'errors': self.errors}
|
|
|
+
|
|
|
+ def walle_rollback(self):
|
|
|
+ self.start()
|
|
|
+
|
|
|
+ try:
|
|
|
+ is_all_servers_success = True
|
|
|
+ self.release_version = self.taskMdl.get('link_id')
|
|
|
+ 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'])
|
|
|
+ self.prev_release_custom(self.connections[host])
|
|
|
+ self.release(self.connections[host])
|
|
|
+ self.post_release(self.connections[host])
|
|
|
+ except Exception as e:
|
|
|
+ is_all_servers_success = False
|
|
|
+ current_app.logger.error(e)
|
|
|
+ self.errors[host] = e.message
|
|
|
+ self.end(is_all_servers_success)
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ self.end(False)
|
|
|
+
|
|
|
+ return {'success': self.success, 'errors': self.errors}
|