Skip to content

Commit f8b5bf6

Browse files
author
李浩
committed
[IMP] 环境仓库deploy-key过期时尝试刷新
1 parent e5bb957 commit f8b5bf6

File tree

7 files changed

+105
-13
lines changed

7 files changed

+105
-13
lines changed

src/main/java/io/choerodon/devops/app/service/impl/DevopsGitServiceImpl.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ public void fileResourceSync(PushWebHookVO pushWebHookVO) {
771771
handleFiles(operationFiles, deletedFiles, devopsEnvironmentDTO, devopsEnvCommitDTO, path);
772772

773773
// 更新远程仓库的DevOps相关的tag
774-
handleTag(git, devopsEnvironmentDTO.getEnvIdRsa(), pushWebHookVO, devopsEnvCommitDTO, tagNotExist);
774+
handleTag(devopsEnvironmentDTO, git, devopsEnvironmentDTO.getEnvIdRsa(), pushWebHookVO, devopsEnvCommitDTO, tagNotExist);
775775

776776
devopsEnvironmentDTO.setDevopsSyncCommit(devopsEnvCommitDTO.getId());
777777
//更新环境 解释commit
@@ -858,17 +858,16 @@ private void handleFiles(List<String> operationFiles, List<String> deletedFiles,
858858
}
859859
}
860860

861-
private void handleTag(Git git, String sshKey, PushWebHookVO pushWebHookVO,
862-
DevopsEnvCommitDTO devopsEnvCommitDTO, Boolean tagNotExist) {
861+
private void handleTag(DevopsEnvironmentDTO devopsEnvironmentDTO, Git git, String sshKey, PushWebHookVO pushWebHookVO, DevopsEnvCommitDTO devopsEnvCommitDTO, Boolean tagNotExist) {
863862
if (Boolean.TRUE.equals(tagNotExist)) {
864-
GitUtil.createTagAndPush(git, sshKey, GitUtil.DEV_OPS_SYNC_TAG, devopsEnvCommitDTO.getCommitSha());
863+
GitUtil.createTagAndPush(devopsEnvironmentDTO, git, sshKey, GitUtil.DEV_OPS_SYNC_TAG, devopsEnvCommitDTO.getCommitSha());
865864
if (getDevopsSyncTag(pushWebHookVO)) {
866-
GitUtil.createTagAndPush(git, sshKey, GitUtil.DEV_OPS_SYNC_TAG, devopsEnvCommitDTO.getCommitSha());
865+
GitUtil.createTagAndPush(devopsEnvironmentDTO, git, sshKey, GitUtil.DEV_OPS_SYNC_TAG, devopsEnvCommitDTO.getCommitSha());
867866
}
868867
} else {
869-
GitUtil.pushTag(git, sshKey, GitUtil.DEV_OPS_SYNC_TAG, devopsEnvCommitDTO.getCommitSha());
868+
GitUtil.pushTag(devopsEnvironmentDTO, git, sshKey, GitUtil.DEV_OPS_SYNC_TAG, devopsEnvCommitDTO.getCommitSha());
870869
if (getDevopsSyncTag(pushWebHookVO)) {
871-
GitUtil.createTagAndPush(git, sshKey, GitUtil.DEV_OPS_SYNC_TAG, devopsEnvCommitDTO.getCommitSha());
870+
GitUtil.createTagAndPush(devopsEnvironmentDTO, git, sshKey, GitUtil.DEV_OPS_SYNC_TAG, devopsEnvCommitDTO.getCommitSha());
872871
}
873872
}
874873
}

src/main/java/io/choerodon/devops/infra/feign/GitlabServiceClient.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,4 +982,19 @@ ResponseEntity<Void> deletePipelineSchedule(
982982
@RequestParam(value = "username") String username,
983983
@RequestParam(value = "password") String password);
984984

985+
/**
986+
* 删除deployKeys
987+
*
988+
* @param projectId 项目Id
989+
* @param userId 用户Id
990+
* @Return List
991+
*/
992+
@ApiOperation(value = "删除deployKeys")
993+
@DeleteMapping(value = "/v1/projects/deploy_key")
994+
ResponseEntity<Void> deleteDeployKeys(
995+
@ApiParam(value = "项目ID", required = true)
996+
@RequestParam Integer projectId,
997+
@ApiParam(value = "用户Id")
998+
@RequestParam(required = false) Integer userId,
999+
@RequestParam Integer keyId);
9851000
}

src/main/java/io/choerodon/devops/infra/feign/fallback/GitlabServiceClientFallback.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,4 +616,9 @@ public ResponseEntity<Void> updatePipelineSchedule(Integer projectId, Integer us
616616
public ResponseEntity<Void> deletePipelineSchedule(Integer projectId, Integer userId, Integer pipelineScheduleId, String gitlabUrl, String authType, String accessToken, String username, String password) {
617617
throw new CommonException("error.delete.Pipeline.Schedule");
618618
}
619+
620+
@Override
621+
public ResponseEntity<Void> deleteDeployKeys(Integer projectId, Integer userId, Integer keyId) {
622+
throw new CommonException("devops.gitlab.project.deployKey.delete");
623+
}
619624
}

src/main/java/io/choerodon/devops/infra/feign/operator/GitlabServiceClientOperator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,4 +1841,7 @@ public void deletePipelineSchedule(Integer projectId,
18411841
}
18421842
}
18431843

1844+
public void deleteDeployKey(Integer projectId, Integer userId, Integer keyId) {
1845+
gitlabServiceClient.deleteDeployKeys(projectId, userId, keyId);
1846+
}
18441847
}

src/main/java/io/choerodon/devops/infra/util/GitUtil.java

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.util.ObjectUtils;
3333
import org.springframework.util.StringUtils;
3434

35+
import io.choerodon.core.convertor.ApplicationContextHelper;
3536
import io.choerodon.core.exception.CommonException;
3637
import io.choerodon.devops.api.vo.GitConfigVO;
3738
import io.choerodon.devops.api.vo.GitEnvConfigVO;
@@ -40,10 +41,12 @@
4041
import io.choerodon.devops.app.service.DevopsEnvironmentService;
4142
import io.choerodon.devops.infra.dto.DevopsClusterDTO;
4243
import io.choerodon.devops.infra.dto.DevopsEnvironmentDTO;
44+
import io.choerodon.devops.infra.dto.gitlab.DeployKeyDTO;
4345
import io.choerodon.devops.infra.dto.iam.ProjectDTO;
4446
import io.choerodon.devops.infra.dto.iam.Tenant;
4547
import io.choerodon.devops.infra.enums.EnvironmentType;
4648
import io.choerodon.devops.infra.feign.operator.BaseServiceClientOperator;
49+
import io.choerodon.devops.infra.feign.operator.GitlabServiceClientOperator;
4750
import io.choerodon.devops.infra.mapper.DevopsClusterMapper;
4851

4952
/**
@@ -75,7 +78,8 @@ public class GitUtil {
7578
private String gitlabSshUrl;
7679
@Value("${services.gitlab.internalsshUrl:}")
7780
private String gitlabInternalsshUrl;
78-
81+
@Autowired
82+
private GitlabServiceClientOperator gitlabServiceClientOperator;
7983

8084
public String getSshUrl() {
8185
if (org.apache.commons.lang3.StringUtils.isNotBlank(gitlabInternalsshUrl)) {
@@ -702,7 +706,7 @@ public String cloneAppMarket(String name, String commit, String remoteUrl, Strin
702706
* @param sha 要打tag的散列值
703707
* @throws CommonException push error
704708
*/
705-
public static void createTagAndPush(Git git, String sshKey, String tagName, String sha) {
709+
public static void createTagAndPush(DevopsEnvironmentDTO devopsEnvironmentDTO, Git git, String sshKey, String tagName, String sha) {
706710
try {
707711
// 创建之前删除,保证本地不存在要创建的tag
708712
deleteTag(git, tagName);
@@ -715,7 +719,71 @@ public static void createTagAndPush(Git git, String sshKey, String tagName, Stri
715719
pushCommand.add(tagName);
716720
pushCommand.setRemote("origin");
717721
pushCommand.setForce(true);
718-
pushCommand.setTransportConfigCallback(getTransportConfigCallback(sshKey)).call();
722+
Iterable<PushResult> call = pushCommand.setTransportConfigCallback(getTransportConfigCallback(sshKey)).call();
723+
Iterator<PushResult> iterator = call.iterator();
724+
while (iterator.hasNext()) {
725+
PushResult pushResult = iterator.next();
726+
RemoteRefUpdate remoteUpdate = pushResult.getRemoteUpdate("refs/tags/devops-sync");
727+
if (!remoteUpdate.getStatus().name().equals("OK")) {
728+
// 尝试刷新公钥,然后再重新推一次tag
729+
Integer gitlabAdminUserId = GitUserNameUtil.getAdminId();
730+
List<DeployKeyDTO> deployKeyDTOS = ApplicationContextHelper.getContext().getBean(GitlabServiceClientOperator.class).listDeployKey(devopsEnvironmentDTO.getGitlabEnvProjectId().intValue(), gitlabAdminUserId);
731+
deployKeyDTOS.forEach(key -> {
732+
if (key.getTitle().equals(devopsEnvironmentDTO.getCode())) {
733+
ApplicationContextHelper.getContext().getBean(GitlabServiceClientOperator.class).deleteDeployKey(devopsEnvironmentDTO.getGitlabEnvProjectId().intValue(), gitlabAdminUserId, key.getId());
734+
}
735+
});
736+
// 以管理员身份创建deploy key
737+
ApplicationContextHelper.getContext().getBean(GitlabServiceClientOperator.class).createDeployKey(
738+
devopsEnvironmentDTO.getGitlabEnvProjectId().intValue(),
739+
devopsEnvironmentDTO.getCode(),
740+
devopsEnvironmentDTO.getEnvIdRsaPub(),
741+
true,
742+
GitUserNameUtil.getAdminId()
743+
);
744+
// 尝试重新推送一次tag
745+
retryCreateTagAndPush(devopsEnvironmentDTO, git, sshKey, tagName, sha);
746+
break;
747+
}
748+
}
749+
} catch (Exception e) {
750+
throw new CommonException("create tag fail", e);
751+
}
752+
}
753+
754+
755+
/**
756+
* 重新尝试本地创建tag并推送远程仓库
757+
*
758+
* @param git git repo
759+
* @param sshKey ssh私钥
760+
* @param tagName tag名称
761+
* @param sha 要打tag的散列值
762+
* @throws CommonException push error
763+
*/
764+
public static void retryCreateTagAndPush(DevopsEnvironmentDTO devopsEnvironmentDTO, Git git, String sshKey, String tagName, String sha) {
765+
try {
766+
// 创建之前删除,保证本地不存在要创建的tag
767+
deleteTag(git, tagName);
768+
Repository repository = git.getRepository();
769+
ObjectId id = repository.resolve(sha);
770+
RevWalk walk = new RevWalk(repository);
771+
RevCommit commit = walk.parseCommit(id);
772+
git.tag().setObjectId(commit).setName(tagName).call();
773+
PushCommand pushCommand = git.push();
774+
pushCommand.add(tagName);
775+
pushCommand.setRemote("origin");
776+
pushCommand.setForce(true);
777+
Iterable<PushResult> call = pushCommand.setTransportConfigCallback(getTransportConfigCallback(sshKey)).call();
778+
Iterator<PushResult> iterator = call.iterator();
779+
while (iterator.hasNext()) {
780+
PushResult pushResult = iterator.next();
781+
RemoteRefUpdate remoteUpdate = pushResult.getRemoteUpdate("refs/tags/devops-sync");
782+
if (!remoteUpdate.getStatus().name().equals("OK")) {
783+
LOGGER.info("failed to push devops-sync tag of env:{}, id :{} ,err code :{}", devopsEnvironmentDTO.getCode(), devopsEnvironmentDTO.getId(), remoteUpdate.getStatus().name());
784+
throw new CommonException(remoteUpdate.getStatus().name());
785+
}
786+
}
719787
} catch (Exception e) {
720788
throw new CommonException("create tag fail", e);
721789
}
@@ -771,9 +839,9 @@ public static void deleteTag(Git git, String tagName) {
771839
* @param tagName tag名称
772840
* @param sha 要打tag的commit的散列值
773841
*/
774-
public static void pushTag(Git git, String sshKey, String tagName, String sha) {
842+
public static void pushTag(DevopsEnvironmentDTO devopsEnvironmentDTO, Git git, String sshKey, String tagName, String sha) {
775843
deleteTag(git, tagName);
776-
createTagAndPush(git, sshKey, tagName, sha);
844+
createTagAndPush(devopsEnvironmentDTO, git, sshKey, tagName, sha);
777845
}
778846

779847
/**

src/main/resources/messages/messages_en_US.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,3 +924,4 @@ error.load.host.upgrade.sh=Failed to read the host upgrade command template.
924924
error.host.app.name.length=The application name length should be in 0 to 128
925925
error.host.app.code.length=The application code length should be in 0 to 128
926926
error.middleware.code.exists=code exists
927+
devops.gitlab.project.deployKey.delete=Failed to delete gitlab project deployKey

src/main/resources/messages/messages_zh_CN.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,4 +920,5 @@ error.load.host.upgrade.sh=读取主机升级命令模版失败
920920
error.host.app.name.length=应用名称长度应该在0~128
921921
error.host.app.code.length=应用code长度应该在0~64
922922
error.middleware.code.exists=编码已存在
923-
error.key-encrypt.decrypt.abnormal_content=页面失效,请重新访问首页进入
923+
error.key-encrypt.decrypt.abnormal_content=页面失效,请重新访问首页进入
924+
devops.gitlab.project.deployKey.delete=删除gitlab项目的deploy key失败

0 commit comments

Comments
 (0)