def disconnect(self, close_code): branch_tag = self.config.repo_model if self.release_name == self.release_desc else self.branch_tag try: if len(self.host_list) == 0: status = '所有主机均部署失败,主机:{}'.format(', '.join(self.host_list)) elif len(self.host_fail) == 0: status = '所有主机均部署成功,主机:{}'.format(', '.join(self.host_list)) else: status = '主机:{} 部署成功;主机:{} 部署失败'.format( ', '.join(self.host_list), ', '.join(self.host_fail)) d_type = '回滚' if self.d_type == 'rollback' else '部署' paras = (self.config.project.project_name, self.config.project.get_project_env_display(), d_type, branch_tag, self.release_name, self.scope['user'].username, status) if self.config.to_mail: deploy_mail(self.config.to_mail, self.config.cc_mail, *paras) if self.config.wx_notice: deploy_wx(*paras) except Exception as e: deploy_logger.error('部署通知操作失败!{}'.format(e)) finally: if '<p style="color: #FF0000">所有主机均部署失败!退出部署流程!</p>' in self.deploy_results: self.deploy_results = self.deploy_results[:self.deploy_results.index( '<p style="color: #FF0000">所有主机均部署失败!退出部署流程!</p>') + 1] deploy_log.delay(project_config=self.config, deploy_user=self.scope['user'], d_type=self.d_type, branch_tag=branch_tag, release_name=self.release_name, release_desc=self.release_desc, result=self.deploy_results)
def clone(self, prev_cmds, username='', password='', git_port=22): """ :param prev_cmds: 代码检出前的操作 :param username: git仓库用户名,当repo_url使用http(s)协议时需要指定 :param password: git仓库密码,当repo_url使用http(s)协议时需要指定 :param git_port: git仓库端口,默认是22.当repo_url使用ssh协议时需要指定 :return: """ try: if not os.path.exists(self.base_path): os.makedirs(self.base_path, mode=0o755) if os.path.exists(self.proj_path) and os.listdir(self.proj_path): shutil.rmtree(self.proj_path, ignore_errors=True) os.makedirs(self.proj_path, mode=0o755, exist_ok=True) if username and password and self.repo_url.startswith('http'): t = self.repo_url.split('://') self.repo_url = f'{t[0]}://{username}:{password}@{t[1]}' elif git_port != 22: t = self.repo_url.split(':') self.repo_url = f'ssh://{t[0]}:{git_port}/{t[1]}' if len(prev_cmds) == 0: git.Repo.clone_from(url=self.repo_url, to_path=self.proj_path, origin=self.remote_name) else: self.run_cmd(prev_cmds) git.Repo.clone_from(url=self.repo_url, to_path=self.proj_path, origin=self.remote_name) except Exception as e: deploy_logger.error('拉取代码失败!{}'.format(e))
def checkout(self, repo_model='trunk', model_name='', revision=None): try: real_path = os.path.join(self.repo_url, self.gen_model(repo_model), model_name) repo = self.remote_repo(real_path) if os.path.exists(self.proj_path): shutil.rmtree(self.proj_path, ignore_errors=True) repo.checkout(self.proj_path, revision=revision) except Exception as e: deploy_logger.error('拉取代码失败!{}'.format(e))
def deploy_log(project_config, deploy_user, d_type, branch_tag, release_name, release_desc, result): try: DeployLog.objects.create( project_config=project_config, deploy_user=deploy_user, d_type=d_type, branch_tag=branch_tag, release_name=release_name, release_desc=release_desc, result=result, ) except Exception as e: deploy_logger.error('添加部署操作记录失败,原因:{}'.format(e))
def clone(self, prev_cmds): """ :param prev_cmds: 代码检出前的操作 :return: """ try: if not os.path.exists(self.base_path): os.makedirs(self.base_path) if os.path.exists(self.proj_path) and os.listdir(self.proj_path): shutil.rmtree(self.proj_path, ignore_errors=True) if len(prev_cmds) == 0: git.Repo.clone_from(url=self.repo_url, to_path=self.proj_path, origin=self.remote_name) else: if self.run_cmd(prev_cmds) == 0: git.Repo.clone_from(url=self.repo_url, to_path=self.proj_path, origin=self.remote_name) except Exception as e: deploy_logger.error('拉取代码失败!{}'.format(e))