Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support rollback to specified release (#2858) #3093

Merged
merged 6 commits into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,15 @@ public ReleaseDTO updateAndPublish(@PathVariable("appId") String appId,
@Transactional
@PutMapping("/releases/{releaseId}/rollback")
public void rollback(@PathVariable("releaseId") long releaseId,
@RequestParam(name="toReleaseId", defaultValue = "-1") long toReleaseId,
@RequestParam("operator") String operator) {

Release release = releaseService.rollback(releaseId, operator);
Release release;
if (toReleaseId > -1) {
release = releaseService.rollbackTo(releaseId, toReleaseId, operator);
} else {
release = releaseService.rollback(releaseId, operator);
}

String appId = release.getAppId();
String clusterName = release.getClusterName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Release findFirstByAppIdAndClusterNameAndNamespaceNameAndIsAbandonedFalseOrderBy

List<Release> findByAppIdAndClusterNameAndNamespaceNameAndIsAbandonedFalseOrderByIdDesc(String appId, String clusterName, String namespaceName, Pageable page);

List<Release> findByAppIdAndClusterNameAndNamespaceNameAndIsAbandonedFalseAndIdBetweenOrderByIdDesc(String appId, String clusterName, String namespaceName, long fromId, long toId);

List<Release> findByReleaseKeyIn(Set<String> releaseKey);

List<Release> findByIdIn(Set<Long> releaseIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,21 @@ public List<Release> findActiveReleases(String appId, String clusterName, String
return releases;
}

private List<Release> findActiveReleasesBetween(String appId, String clusterName, String namespaceName,
long fromReleaseId, long toReleaseId) {
List<Release>
releases =
releaseRepository.findByAppIdAndClusterNameAndNamespaceNameAndIsAbandonedFalseAndIdBetweenOrderByIdDesc(appId,
clusterName,
namespaceName,
fromReleaseId,
toReleaseId);
if (releases == null) {
return Collections.emptyList();
}
return releases;
}

@Transactional
public Release mergeBranchChangeSetsAndRelease(Namespace namespace, String branchName, String releaseName,
String releaseComment, boolean isEmergencyPublish,
Expand Down Expand Up @@ -454,6 +469,45 @@ public Release rollback(long releaseId, String operator) {
return release;
}

@Transactional
public Release rollbackTo(long releaseId, long toReleaseId, String operator) {
if (releaseId == toReleaseId) {
throw new BadRequestException("current release equal to target release");
}

Release release = findOne(releaseId);
Release toRelease = findOne(toReleaseId);
if (release == null || toRelease == null) {
throw new NotFoundException("release not found");
}
if (release.isAbandoned() || toRelease.isAbandoned()) {
throw new BadRequestException("release is not active");
}

String appId = release.getAppId();
String clusterName = release.getClusterName();
String namespaceName = release.getNamespaceName();

List<Release> releases = findActiveReleasesBetween(appId, clusterName, namespaceName,
toReleaseId, releaseId);

for (int i = 0; i < releases.size() - 1; i++) {
releases.get(i).setAbandoned(true);
cbcc marked this conversation as resolved.
Show resolved Hide resolved
releases.get(i).setDataChangeLastModifiedBy(operator);
}

releaseRepository.saveAll(releases);

releaseHistoryService.createReleaseHistory(appId, clusterName,
namespaceName, clusterName, toReleaseId,
release.getId(), ReleaseOperation.ROLLBACK, null, operator);

//publish child namespace if namespace has child
rollbackChildNamespace(appId, clusterName, namespaceName, Lists.newArrayList(release, toRelease), operator);

return release;
}

private void rollbackChildNamespace(String appId, String clusterName, String namespaceName,
List<Release> parentNamespaceTwoLatestActiveRelease, String operator) {
Namespace parentNamespace = namespaceService.findOne(appId, clusterName, namespaceName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.ctrip.framework.apollo.biz.repository.ReleaseRepository;
import com.ctrip.framework.apollo.common.exception.BadRequestException;

import java.util.ArrayList;
import java.util.Optional;
import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -108,6 +109,38 @@ public void testRollback() {
Assert.assertEquals(user, firstRelease.getDataChangeLastModifiedBy());
}

@Test
public void testRollbackTo() {
List<Release> releaseList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
Release release = new Release();
release.setId(3 - i);
release.setAppId(appId);
release.setClusterName(clusterName);
release.setNamespaceName(namespaceName);
release.setAbandoned(false);
releaseList.add(release);
}
long releaseId1 = 1;
long releaseId3 = 3;
when(releaseRepository.findById(releaseId1)).thenReturn(Optional.of(releaseList.get(2)));
when(releaseRepository.findById(releaseId3)).thenReturn(Optional.of(releaseList.get(0)));
when(releaseRepository.findByAppIdAndClusterNameAndNamespaceNameAndIsAbandonedFalseAndIdBetweenOrderByIdDesc(appId,
clusterName,
namespaceName,
releaseId1,
releaseId3))
.thenReturn(releaseList);

releaseService.rollbackTo(releaseId3, releaseId1, user);

verify(releaseRepository).saveAll(releaseList);
Assert.assertTrue(releaseList.get(0).isAbandoned());
Assert.assertTrue(releaseList.get(1).isAbandoned());
Assert.assertFalse(releaseList.get(2).isAbandoned());
Assert.assertEquals(user, releaseList.get(0).getDataChangeLastModifiedBy());
Assert.assertEquals(user, releaseList.get(1).getDataChangeLastModifiedBy());
}

@Test
public void testFindRelease() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,12 @@ public void rollback(Env env, long releaseId, String operator) {
"releases/{releaseId}/rollback?operator={operator}",
null, releaseId, operator);
}

public void rollbackTo(Env env, long releaseId, long toReleaseId, String operator) {
restTemplate.put(env,
"releases/{releaseId}/rollback?toReleaseId={toReleaseId}&operator={operator}",
null, releaseId, toReleaseId, operator);
}
}

@Service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ public ReleaseDTO createGrayRelease(@PathVariable String appId,
return createdRelease;
}

@GetMapping("/envs/{env}/releases/{releaseId}")
public ReleaseDTO get(@PathVariable String env,
@PathVariable long releaseId) {
ReleaseDTO release = releaseService.findReleaseById(Env.valueOf(env), releaseId);

if (release == null) {
throw new NotFoundException("release not found");
}
return release;
}

@GetMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/releases/all")
public List<ReleaseBO> findAllReleases(@PathVariable String appId,
Expand Down Expand Up @@ -153,7 +163,8 @@ public ReleaseCompareResult compareRelease(@PathVariable String env,

@PutMapping(path = "/envs/{env}/releases/{releaseId}/rollback")
public void rollback(@PathVariable String env,
@PathVariable long releaseId) {
@PathVariable long releaseId,
@RequestParam(defaultValue = "-1") long toReleaseId) {
ReleaseDTO release = releaseService.findReleaseById(Env.valueOf(env), releaseId);

if (release == null) {
Expand All @@ -164,7 +175,11 @@ public void rollback(@PathVariable String env,
throw new AccessDeniedException("Access is denied");
}

releaseService.rollback(Env.valueOf(env), releaseId, userInfoHolder.getUser().getUserId());
if (toReleaseId > -1) {
releaseService.rollbackTo(Env.valueOf(env), releaseId, toReleaseId, userInfoHolder.getUser().getUserId());
} else {
releaseService.rollback(Env.valueOf(env), releaseId, userInfoHolder.getUser().getUserId());
}

ConfigPublishEvent event = ConfigPublishEvent.instance();
event.withAppId(release.getAppId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class ReleaseHistoryBO {

private List<EntityPair<String>> configuration;

private boolean isReleaseAbandoned;

private long previousReleaseId;

private int operation;
Expand Down Expand Up @@ -159,4 +161,12 @@ public void setConfiguration(
List<EntityPair<String>> configuration) {
this.configuration = configuration;
}

public boolean isReleaseAbandoned() {
return isReleaseAbandoned;
}

public void setReleaseAbandoned(boolean releaseAbandoned) {
isReleaseAbandoned = releaseAbandoned;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ private void setReleaseInfoToReleaseHistoryBO(ReleaseHistoryBO bo, ReleaseDTO re
if (release != null) {
bo.setReleaseTitle(release.getName());
bo.setReleaseComment(release.getComment());
bo.setReleaseAbandoned(release.isAbandoned());

Map<String, String> configuration = gson.fromJson(release.getConfigurations(), GsonType.CONFIG);
List<EntityPair<String>> items = new ArrayList<>(configuration.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ public void rollback(Env env, long releaseId, String operator) {
releaseAPI.rollback(env, releaseId, operator);
}

public void rollbackTo(Env env, long releaseId, long toReleaseId, String operator) {
releaseAPI.rollbackTo(env, releaseId, toReleaseId, operator);
}

public ReleaseCompareResult compare(Env env, long baseReleaseId, long toCompareReleaseId) {

ReleaseDTO baseRelease = null;
Expand Down
20 changes: 20 additions & 0 deletions apollo-portal/src/main/resources/static/config/history.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,22 @@ <h6 class="col-md-5 text-right" ng-bind="releaseHistory.releaseTimeFormatted"></
<div class="panel-heading">

<span ng-bind="history.releaseTitle"></span>
<span class="label label-warning no-radius" ng-if="history.isReleaseAbandoned">{{'Config.History.Abandoned' | translate }}</span>
<span class="pull-right" ng-bind="history.releaseTime | date: 'yyyy-MM-dd HH:mm:ss'"></span>

<div class="row" style="padding-top: 10px;">
<div class="col-md-5">
<small ng-show="history.releaseComment" ng-bind="history.releaseComment"></small>
</div>
<div class="col-md-7 text-right">
<button type="button" class="btn btn-default btn-sm" data-tooltip="tooltip" data-placement="bottom"
title="{{'Config.History.RollbackToTips' | translate }}"
ng-show="namespace.hasReleasePermission && !history.isReleaseAbandoned
&& (history.operation == 0 || history.operation == 1 || history.operation == 4)"
ng-click="preRollback()">
<img src="../img/rollback.png">
{{'Config.History.RollbackTo' | translate }}
</button>
<div class="btn-group">
<button type="button" class="btn btn-default btn-sm"
ng-class="{'active':history.viewType == 'diff'}" data-tooltip="tooltip"
Expand Down Expand Up @@ -256,6 +265,15 @@ <h4 class="text-center empty-container" ng-show="!isConfigHidden">
</section>

<showtextmodal text="text"></showtextmodal>

<rollbackmodal app-id="pageContext.appId" env="pageContext.env" cluster="pageContext.clusterName">
</rollbackmodal>

<apolloconfirmdialog apollo-dialog-id="'rollbackAlertDialog'"
apollo-title="'Config.RollbackAlert.DialogTitle' | translate"
apollo-detail="'Config.RollbackAlert.DialogContent' | translate" apollo-show-cancel-btn="true"
apollo-confirm="rollback">
</apolloconfirmdialog>
</div>


Expand Down Expand Up @@ -291,6 +309,7 @@ <h4 class="text-center empty-container" ng-show="!isConfigHidden">
<script type="application/javascript" src="../scripts/services/ReleaseHistoryService.js"></script>
<script type="application/javascript" src="../scripts/services/ConfigService.js"></script>
<script type="application/javascript" src="../scripts/services/PermissionService.js"></script>
<script type="application/javascript" src="../scripts/services/EventManager.js"></script>

<script type="application/javascript" src="../scripts/AppUtils.js"></script>
<script type="application/javascript" src="../scripts/controller/config/ReleaseHistoryController.js"></script>
Expand All @@ -299,6 +318,7 @@ <h4 class="text-center empty-container" ng-show="!isConfigHidden">
<script type="application/javascript" src="../scripts/directive/directive.js"></script>
<script type="application/javascript" src="../scripts/directive/show-text-modal-directive.js"></script>
<script type="application/javascript" src="../scripts/directive/diff-directive.js"></script>
<script type="application/javascript" src="../scripts/directive/rollback-modal-directive.js"></script>
</body>

</html>
5 changes: 5 additions & 0 deletions apollo-portal/src/main/resources/static/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@
"Component.Publish.OpPublish": "Release",
"Component.Rollback.To": "roll back to",
"Component.Rollback.Tips": "This operation will roll back to the last released version, and the current version is abandoned, but there is no impact to the currently editing configurations. You may view the currently effective version in the release history page",
"Component.RollbackTo.Tips": "This operation will roll back to this released version, and the current version is abandoned, but there is no impact to the currently editing configurations",
cbcc marked this conversation as resolved.
Show resolved Hide resolved
"Component.Rollback.ClickToView": "Click to view",
"Component.Rollback.ItemType": "Type",
"Component.Rollback.ItemKey": "Key",
Expand Down Expand Up @@ -646,6 +647,9 @@
"Config.History.OperationType8": "Delete Grayscale(Full Release)",
"Config.History.UrgentPublish": "Emergency Release",
"Config.History.LoadMore": "Load more",
"Config.History.Abandoned": "Abandoned",
"Config.History.RollbackTo": "Rollback To This Release",
"Config.History.RollbackToTips": "Rollback released configuration to this release",
"Config.History.ChangedItem": "Changed Configuration",
"Config.History.ChangedItemTips": "View changes between this release and the previous release",
"Config.History.AllItem": "Full Configuration",
Expand Down Expand Up @@ -749,6 +753,7 @@
"ReleaseModal.AllPublished": "Full Release Successfully",
"ReleaseModal.AllPublishFailed": "Failed to Full Release",
"Rollback.NoRollbackList": "No released history to rollback",
"Rollback.SameAsCurrentRelease": "This release is the same as current release",
"Rollback.RollbackSuccessfully": "Rollback Successfully",
"Rollback.RollbackFailed": "Failed to Rollback",
"Revoke.RevokeFailed": "Failed to Revoke",
Expand Down
5 changes: 5 additions & 0 deletions apollo-portal/src/main/resources/static/i18n/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@
"Component.Publish.OpPublish": "发布",
"Component.Rollback.To": "回滚到",
"Component.Rollback.Tips": "此操作将会回滚到上一个发布版本,且当前版本作废,但不影响正在修改的配置。可在发布历史页面查看当前生效的版本",
"Component.RollbackTo.Tips":"此操作将会回滚到此发布版本,且当前版本作废,但不影响正在修改的配置",
cbcc marked this conversation as resolved.
Show resolved Hide resolved
"Component.Rollback.ClickToView": "点击查看",
"Component.Rollback.ItemType": "Type",
"Component.Rollback.ItemKey": "Key",
Expand Down Expand Up @@ -646,6 +647,9 @@
"Config.History.OperationType8": "删除灰度(全量发布)",
"Config.History.UrgentPublish": "紧急发布",
"Config.History.LoadMore": "加载更多",
"Config.History.Abandoned": "已废弃",
"Config.History.RollbackTo": "回滚到此版本",
"Config.History.RollbackToTips": "回滚已发布的配置到此版本",
"Config.History.ChangedItem": "变更的配置",
"Config.History.ChangedItemTips": "查看此次发布与上次版本的变更",
"Config.History.AllItem": "全部配置",
Expand Down Expand Up @@ -749,6 +753,7 @@
"ReleaseModal.AllPublished": "全量发布成功",
"ReleaseModal.AllPublishFailed": "全量发布失败",
"Rollback.NoRollbackList": "没有可以回滚的发布历史",
"Rollback.SameAsCurrentRelease": "该版本与当前版本相同",
"Rollback.RollbackSuccessfully": "回滚成功",
"Rollback.RollbackFailed": "回滚失败",
"Revoke.RevokeFailed": "撤销失败",
Expand Down
Loading