Search in sources :

Example 66 with Release

use of com.ctrip.framework.apollo.biz.entity.Release in project apollo by ctripcorp.

the class ReleaseService method rollbackChildNamespace.

private void rollbackChildNamespace(String appId, String clusterName, String namespaceName, List<Release> parentNamespaceTwoLatestActiveRelease, String operator) {
    Namespace parentNamespace = namespaceService.findOne(appId, clusterName, namespaceName);
    Namespace childNamespace = namespaceService.findChildNamespace(appId, clusterName, namespaceName);
    if (parentNamespace == null || childNamespace == null) {
        return;
    }
    Release childNamespaceLatestActiveRelease = findLatestActiveRelease(childNamespace);
    Map<String, String> childReleaseConfiguration;
    Collection<String> branchReleaseKeys;
    if (childNamespaceLatestActiveRelease != null) {
        childReleaseConfiguration = GSON.fromJson(childNamespaceLatestActiveRelease.getConfigurations(), GsonType.CONFIG);
        branchReleaseKeys = getBranchReleaseKeys(childNamespaceLatestActiveRelease.getId());
    } else {
        childReleaseConfiguration = Collections.emptyMap();
        branchReleaseKeys = null;
    }
    Release abandonedRelease = parentNamespaceTwoLatestActiveRelease.get(0);
    Release parentNamespaceNewLatestRelease = parentNamespaceTwoLatestActiveRelease.get(1);
    Map<String, String> parentNamespaceAbandonedConfiguration = GSON.fromJson(abandonedRelease.getConfigurations(), GsonType.CONFIG);
    Map<String, String> parentNamespaceNewLatestConfiguration = GSON.fromJson(parentNamespaceNewLatestRelease.getConfigurations(), GsonType.CONFIG);
    Map<String, String> childNamespaceNewConfiguration = calculateChildNamespaceToPublishConfiguration(parentNamespaceAbandonedConfiguration, parentNamespaceNewLatestConfiguration, childReleaseConfiguration, branchReleaseKeys);
    // compare
    if (!childNamespaceNewConfiguration.equals(childReleaseConfiguration)) {
        branchRelease(parentNamespace, childNamespace, TIMESTAMP_FORMAT.format(new Date()) + "-master-rollback-merge-to-gray", "", childNamespaceNewConfiguration, parentNamespaceNewLatestRelease.getId(), operator, ReleaseOperation.MATER_ROLLBACK_MERGE_TO_GRAY, false, branchReleaseKeys);
    }
}
Also used : Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) Release(com.ctrip.framework.apollo.biz.entity.Release)

Example 67 with Release

use of com.ctrip.framework.apollo.biz.entity.Release in project apollo by ctripcorp.

the class ReleaseService method createRelease.

private Release createRelease(Namespace namespace, String name, String comment, Map<String, String> configurations, String operator) {
    Release release = new Release();
    release.setReleaseKey(ReleaseKeyGenerator.generateReleaseKey(namespace));
    release.setDataChangeCreatedTime(new Date());
    release.setDataChangeCreatedBy(operator);
    release.setDataChangeLastModifiedBy(operator);
    release.setName(name);
    release.setComment(comment);
    release.setAppId(namespace.getAppId());
    release.setClusterName(namespace.getClusterName());
    release.setNamespaceName(namespace.getNamespaceName());
    release.setConfigurations(GSON.toJson(configurations));
    release = releaseRepository.save(release);
    namespaceLockService.unlock(namespace.getId());
    auditService.audit(Release.class.getSimpleName(), release.getId(), Audit.OP.INSERT, release.getDataChangeCreatedBy());
    return release;
}
Also used : Release(com.ctrip.framework.apollo.biz.entity.Release)

Example 68 with Release

use of com.ctrip.framework.apollo.biz.entity.Release in project apollo by ctripcorp.

the class ReleaseService method branchRelease.

private Release branchRelease(Namespace parentNamespace, Namespace childNamespace, String releaseName, String releaseComment, Map<String, String> configurations, long baseReleaseId, String operator, int releaseOperation, boolean isEmergencyPublish, Collection<String> branchReleaseKeys) {
    Release previousRelease = findLatestActiveRelease(childNamespace.getAppId(), childNamespace.getClusterName(), childNamespace.getNamespaceName());
    long previousReleaseId = previousRelease == null ? 0 : previousRelease.getId();
    Map<String, Object> releaseOperationContext = Maps.newLinkedHashMap();
    releaseOperationContext.put(ReleaseOperationContext.BASE_RELEASE_ID, baseReleaseId);
    releaseOperationContext.put(ReleaseOperationContext.IS_EMERGENCY_PUBLISH, isEmergencyPublish);
    releaseOperationContext.put(ReleaseOperationContext.BRANCH_RELEASE_KEYS, branchReleaseKeys);
    Release release = createRelease(childNamespace, releaseName, releaseComment, configurations, operator);
    // update gray release rules
    GrayReleaseRule grayReleaseRule = namespaceBranchService.updateRulesReleaseId(childNamespace.getAppId(), parentNamespace.getClusterName(), childNamespace.getNamespaceName(), childNamespace.getClusterName(), release.getId(), operator);
    if (grayReleaseRule != null) {
        releaseOperationContext.put(ReleaseOperationContext.RULES, GrayReleaseRuleItemTransformer.batchTransformFromJSON(grayReleaseRule.getRules()));
    }
    releaseHistoryService.createReleaseHistory(parentNamespace.getAppId(), parentNamespace.getClusterName(), parentNamespace.getNamespaceName(), childNamespace.getClusterName(), release.getId(), previousReleaseId, releaseOperation, releaseOperationContext, operator);
    return release;
}
Also used : GrayReleaseRule(com.ctrip.framework.apollo.biz.entity.GrayReleaseRule) Release(com.ctrip.framework.apollo.biz.entity.Release)

Example 69 with Release

use of com.ctrip.framework.apollo.biz.entity.Release in project apollo by ctripcorp.

the class ReleaseService method publish.

@Transactional
public Release publish(Namespace namespace, String releaseName, String releaseComment, String operator, boolean isEmergencyPublish) {
    checkLock(namespace, isEmergencyPublish, operator);
    Map<String, String> operateNamespaceItems = getNamespaceItems(namespace);
    Namespace parentNamespace = namespaceService.findParentNamespace(namespace);
    // branch release
    if (parentNamespace != null) {
        return publishBranchNamespace(parentNamespace, namespace, operateNamespaceItems, releaseName, releaseComment, operator, isEmergencyPublish);
    }
    Namespace childNamespace = namespaceService.findChildNamespace(namespace);
    Release previousRelease = null;
    if (childNamespace != null) {
        previousRelease = findLatestActiveRelease(namespace);
    }
    // master release
    Map<String, Object> operationContext = Maps.newLinkedHashMap();
    operationContext.put(ReleaseOperationContext.IS_EMERGENCY_PUBLISH, isEmergencyPublish);
    Release release = masterRelease(namespace, releaseName, releaseComment, operateNamespaceItems, operator, ReleaseOperation.NORMAL_RELEASE, operationContext);
    // merge to branch and auto release
    if (childNamespace != null) {
        mergeFromMasterAndPublishBranch(namespace, childNamespace, operateNamespaceItems, releaseName, releaseComment, operator, previousRelease, release, isEmergencyPublish);
    }
    return release;
}
Also used : Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) Release(com.ctrip.framework.apollo.biz.entity.Release) Transactional(org.springframework.transaction.annotation.Transactional)

Example 70 with Release

use of com.ctrip.framework.apollo.biz.entity.Release in project apollo by ctripcorp.

the class NamespaceBranchService method doUpdateBranchGrayRules.

private void doUpdateBranchGrayRules(String appId, String clusterName, String namespaceName, String branchName, GrayReleaseRule newRules, boolean recordReleaseHistory, int releaseOperation) {
    GrayReleaseRule oldRules = grayReleaseRuleRepository.findTopByAppIdAndClusterNameAndNamespaceNameAndBranchNameOrderByIdDesc(appId, clusterName, namespaceName, branchName);
    Release latestBranchRelease = releaseService.findLatestActiveRelease(appId, branchName, namespaceName);
    long latestBranchReleaseId = latestBranchRelease != null ? latestBranchRelease.getId() : 0;
    newRules.setReleaseId(latestBranchReleaseId);
    grayReleaseRuleRepository.save(newRules);
    // delete old rules
    if (oldRules != null) {
        grayReleaseRuleRepository.delete(oldRules);
    }
    if (recordReleaseHistory) {
        Map<String, Object> releaseOperationContext = Maps.newHashMap();
        releaseOperationContext.put(ReleaseOperationContext.RULES, GrayReleaseRuleItemTransformer.batchTransformFromJSON(newRules.getRules()));
        if (oldRules != null) {
            releaseOperationContext.put(ReleaseOperationContext.OLD_RULES, GrayReleaseRuleItemTransformer.batchTransformFromJSON(oldRules.getRules()));
        }
        releaseHistoryService.createReleaseHistory(appId, clusterName, namespaceName, branchName, latestBranchReleaseId, latestBranchReleaseId, releaseOperation, releaseOperationContext, newRules.getDataChangeLastModifiedBy());
    }
}
Also used : GrayReleaseRule(com.ctrip.framework.apollo.biz.entity.GrayReleaseRule) Release(com.ctrip.framework.apollo.biz.entity.Release)

Aggregations

Release (com.ctrip.framework.apollo.biz.entity.Release)71 Test (org.junit.Test)36 Namespace (com.ctrip.framework.apollo.biz.entity.Namespace)22 Item (com.ctrip.framework.apollo.biz.entity.Item)12 GrayReleaseRule (com.ctrip.framework.apollo.biz.entity.GrayReleaseRule)9 Transactional (org.springframework.transaction.annotation.Transactional)9 AbstractIntegrationTest (com.ctrip.framework.apollo.biz.AbstractIntegrationTest)7 AbstractUnitTest (com.ctrip.framework.apollo.biz.AbstractUnitTest)7 ReleaseHistory (com.ctrip.framework.apollo.biz.entity.ReleaseHistory)7 NotFoundException (com.ctrip.framework.apollo.common.exception.NotFoundException)7 Sql (org.springframework.test.context.jdbc.Sql)7 InstanceConfig (com.ctrip.framework.apollo.biz.entity.InstanceConfig)5 Instance (com.ctrip.framework.apollo.biz.entity.Instance)4 InstanceDTO (com.ctrip.framework.apollo.common.dto.InstanceDTO)4 Date (java.util.Date)4 GetMapping (org.springframework.web.bind.annotation.GetMapping)4 Cluster (com.ctrip.framework.apollo.biz.entity.Cluster)3 ReleaseMessage (com.ctrip.framework.apollo.biz.entity.ReleaseMessage)3 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)3 PageRequest (org.springframework.data.domain.PageRequest)3