All Products
Search
Document Center

Enterprise Distributed Application Service:Call an API operation to roll back an application that is deployed in an ECS cluster to a historical version

Last Updated:Aug 07, 2023

This topic describes how to use Enterprise Distributed Application Service (EDAS) SDK for Java to roll back an application that is deployed in an Elastic Compute Service (ECS) cluster to a historical version by calling an API operation.

Prerequisites

Roll back an application to a historical version

The following sample code provides an example on how to roll back an application to a historical version:

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.edas.model.v20170801.RollbackApplicationRequest;
import com.aliyuncs.edas.model.v20170801.RollbackApplicationResponse;

public class RollbackApplication {

    public static void main(String[] args)  {
        // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in EDAS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the Resource Access Management (RAM) console. 
        // In this sample code, the AccessKey ID and AccessKey secret are stored in the environment variables. You can save your AccessKey ID and AccessKey secret to your configuration file. 
        // To prevent key leaks, we recommend that you do not save your AccessKey ID and AccessKey secret to the code file. 
        String aliyun_user_ak = System.getenv("ACCESS_KEY_ID");
        // The ID of the region in which the application resides. 
        String region_id = "cn-hangzhou";

        DefaultProfile defaultProfile = DefaultProfile.getProfile(region_id, aliyun_user_ak, aliyun_user_sk);
        DefaultAcsClient client = new DefaultAcsClient(defaultProfile);

        // Create an API request and configure parameters. 
        RollbackApplicationRequest request = new RollbackApplicationRequest();
        // The ID of the application that you want to roll back to a historical version. 
        request.setAppId("6bbc57a2-a017-4bec-b521-49a15bd3****");
        // The historical version to which you want to roll back the application. You can call the ListHistoryDeployVersion operation to obtain information about the historical version to which you want to roll back the application. 
        request.setHistoryVersion("20210417.154931");
        // The ID of the group to which the application is deployed. If you want to deploy the application to all instance groups, set the parameter to all. 
        // You can call the ListDeployGroup operation to obtain the ID of the specified instance group. 
        request.setGroupId("all");
        // The number of phases before the application for the specified instances is rolled back. Valid values: 1 to 5. The value 1 specifies that the application for the specified instances is to be rolled back at one time. The values 2 to 4 specify that the application for the specified instances is to be rolled back in phases. 
        request.setBatch(1);
        // The amount of time that the system waits between two phases. Unit: minutes. By default, the value is 0. This specifies no waiting time between phases. You can set the parameter to a value from 0 to 5. If the number of phases before the application for the specified instances is rolled back is 1, you do not need to configure this parameter. 
        request.setBatchWaitTime(0);

        try {
            RollbackApplicationResponse response = client.getAcsResponse(request);
            System.out.println("Message=" + response.getMessage() + "\nChangeOrderId=" + response.getChangeOrderId());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

Sample response:

Message=success
ChangeOrderId=b3496bf0-0b6b-42c3-921c-f5250af****

Check the result

After you roll back the application to a historical version, you can call the GetChangeOrderInfo operation to view the change details and obtain the change status of the application. For more information about the API parameters, see GetChangeOrderInfo.

Sample returned response after you call the GetChangeOrderInfo operation:

{
  "Message": "success",
  "RequestId": "EF09BCD3-3D37-4D76-9CF3-12B71A9EDE7C",
  "Code": 200,
  "changeOrderInfo": {
    "Status": 2,
    "Desc": "Version: 20210417.154931 | Package Name: sc-consumer-D-0.0.1-SNAPSHOT.jar?Expires=161864****&OSSAccessKeyId=TMP.3KgjU****fSsJ&Signature=LrUEz***X9c...",
    "PipelineInfoList": {
      "PipelineInfo": [
        {
          "PipelineStatus": 2,
          "PipelineName": "Batch 1 Change",
           ...... 
           // The sample response is incomplete and is only for reference.

To check whether the application is rolled back to the specified historical version, you can view the value of the changeOrderInfo.Status parameter. Valid values of the changeOrderInfo.Status parameter:

  • 0: Ready

  • 1: In progress

  • 2: Successful

  • 3: Failed

  • 6: Terminated

  • 8: Waiting for manual confirmation to trigger the next phase during a manual phased rollback

  • 9: Waiting to trigger the next phase during an automatic phased rollback

  • 10: Failed due to a system exception