def prev_release(self, waller): ''' 4.部署代码到目标机器前做的任务 - 检查 webroot 父目录是否存在 :return: ''' # TODO socketio.sleep(0.001) self.stage = self.stage_prev_release self.sequence = 4 # 检查 target_releases 父目录是否存在 command = 'mkdir -p %s' % (self.project_info['target_releases']) result = waller.run(command, wenv=self.config()) # TODO 检查 webroot 父目录是否存在,是否为软链 # command = 'mkdir -p %s' % (self.project_info['target_root']) # result = waller.run(command) # current_app.logger.info('command: %s', dir(result)) # 用户自定义命令 command = self.project_info['prev_release'] current_app.logger.info(command) with waller.cd(self.project_info['target_releases']): result = waller.run(command, wenv=self.config()) # TODO md5 # 传送到版本库 release current_app.logger.info('/tmp/walle/codebase/' + self.release_version_tar) result = waller.put('/tmp/walle/codebase/' + self.release_version_tar, remote=self.project_info['target_releases'], wenv=self.config()) current_app.logger.info('command: %s', dir(result)) # 解压 self.release_untar(waller)
def release(self, waller): ''' 5.部署代码到目标机器做的任务 - 打包代码 local - scp local => remote - 解压 remote :return: ''' # TODO socketio.sleep(0.001) self.stage = self.stage_release self.sequence = 5 with waller.cd(self.project_info['target_releases']): # 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 prev_release(self, waller): ''' 4.部署代码到目标机器前做的任务 - 检查 webroot 父目录是否存在 :return: ''' # TODO socketio.sleep(0.001) self.stage = self.stage_prev_release self.sequence = 4 # 检查 target_releases 父目录是否存在 command = 'mkdir -p %s' % (self.project_info['target_releases']) result = waller.run(command, wenv=self.config()) # TODO 检查 webroot 父目录是否存在,是否为软链 # command = 'mkdir -p %s' % (self.project_info['target_root']) # result = waller.run(command) # current_app.logger.info('command: %s', dir(result)) # TODO md5 # 传送到版本库 release current_app.logger.info('/tmp/walle/codebase/' + self.release_version_tar) result = waller.put('/tmp/walle/codebase/' + self.release_version_tar, remote=self.project_info['target_releases'], wenv=self.config()) current_app.logger.info('command: %s', dir(result)) # 解压 self.release_untar(waller)
def post_deploy(self): ''' 3.检出代码后要做的任务 - 用户自定义操作命令 - 代码编译 - 清除日志文件及无用文件 - - 压缩打包 - 传送到版本库 release :return: ''' # TODO socketio.sleep(0.001) self.stage = self.stage_post_deploy self.sequence = 3 # 用户自定义命令 command = self.project_info['post_deploy'] with self.local.cd(self.local_codebase + self.release_version): result = self.local.run(command, wenv=self.config()) # 压缩打包 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) result = self.local.run(command, wenv=self.config())
def post_deploy(self): ''' 3.检出代码后要做的任务 - 用户自定义操作命令 - 代码编译 - 清除日志文件及无用文件 - - 压缩打包 - 传送到版本库 release :return: ''' # TODO socketio.sleep(0.001) self.stage = self.stage_post_deploy self.sequence = 3 # 用户自定义命令 command = self.project_info['post_deploy'] current_app.logger.info(command) current_app.logger.info(self.dir_codebase) current_app.logger.info(self.release_version) with self.local.cd(self.dir_codebase + self.release_version): result = self.local.run(command, wenv=self.config()) # 压缩打包 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) result = self.local.run(command, wenv=self.config())
def list_branch(self): self.init_repo() socketio.sleep(61) with self.local.cd(self.dir_codebase_project): command = 'git pull' from flask import current_app from walle.service import utils current_app.logger.info(utils.detailtrace()) result = self.local.run(command, wenv=self.config()) current_app.logger.info(self.dir_codebase_project) command = 'git branch -r' result = self.local.run(command, wenv=self.config()) # TODO 三种可能: false, error, success branches = color_clean(result.stdout.strip()) branches = branches.split('\n') # 去除 origin/HEAD -> 当前指向 # 去除远端前缀 branches = [ branch.strip().lstrip('origin/') for branch in branches if not branch.strip().startswith('origin/HEAD') ] return branches return None
def deploy(self): ''' 2.检出代码 :param project_name: :return: ''' # TODO socketio.sleep(0.001) self.stage = self.stage_deploy self.sequence = 2 current_app.logger.info('git dir: %s', self.dir_codebase_project + '/.git') # 如果项目底下有 .git 目录则认为项目完整,可以直接检出代码 # TODO 不标准 if os.path.exists(self.dir_codebase_project + '/.git'): with self.local.cd(self.dir_codebase_project): command = 'pwd && git pull' result = self.local.run(command, wenv=self.config()) else: # 否则当作新项目检出完整代码 with self.local.cd(self.dir_codebase_project): command = 'pwd && git clone %s .' % (self.project_info['repo_url']) current_app.logger.info('cd %s command: %s ', self.dir_codebase_project, command) result = self.local.run(command, wenv=self.config()) # 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.local.cd(self.dir_codebase): 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) result = self.local.run(command, wenv=self.config()) # 更新到指定 commit_id with self.local.cd(self.dir_codebase + self.release_version): command = 'git reset -q --hard %s' % (self.taskMdl.get('commit_id')) result = self.local.run(command, wenv=self.config()) # SocketHandler.send_to_all({ # 'type': 'user', # 'id': 33, # 'host': env.host_string, # 'command': command, # 'message': result.stdout, # }) # 用户自定义命令 # command = self.project_info['deploy'] # current_app.logger.info(command) # with self.local.cd(self.dir_codebase): # result = self.local.run(command) pass
def test(self): # server = '172.20.95.43' server = '172.16.0.231' ws_dict = { 'user': '******', 'host': 'ip', 'cmd': 'Going to sleep !!!!!', 'status': 0, 'stage': 1, 'sequence': 1, 'success': 'Going to sleep !!!!!', 'error': '', } emit('console', { 'event': 'task:console', 'data': ws_dict }, room=self.task_id) socketio.sleep(60) ws_dict = { 'user': '******', 'host': 'ip', 'cmd': 'sleep 60', 'status': 0, 'stage': 1, 'sequence': 1, 'success': 'sleep 60....', 'error': '', } emit('console', { 'event': 'task:console', 'data': ws_dict }, room=self.task_id) socketio.sleep(10) ws_dict = { 'user': '******', 'host': 'ip', 'cmd': 'sleep 10', 'status': 0, 'stage': 1, 'sequence': 1, 'success': 'sleep 10....', 'error': '', } emit('console', { 'event': 'task:console', 'data': ws_dict }, room=self.task_id) try: self.connections[server] = Waller(host=server, user='******') self.post_release_service(self.connections[server]) except Exception as e: current_app.logger.exception(e) self.errors[server] = e.message
def release_untar(self, waller): ''' 解压版本包 :return: ''' # TODO socketio.sleep(0.001) with waller.cd(self.project_info['target_releases']): command = 'tar zxf %s' % (self.release_version_tar) result = waller.run(command, wenv=self.config())