How to Create a Rollback automation for Proxies Revision

close up photo of programming of codes
Reading Time: 6 minutes

Hello Folks !!! I hope you all are doing well ! So, Again I am here with a new post and in this we’ll see How to Create a Rollback automation for Proxies Revision in a jenkins pipeline . I hope you will learn something new from this post. Stay tuned !!!

So, Let’s get started !!!

Overview

As part of the NextGen API gateway platform, we need an efficient way to rollback automation for proxies revision, i.e. In this we will automate an approach for CI/CD pipeline to find latest stable revision of proxy in the case of Rollback.

So let’s see How to Create a Rollback automation for Proxies Revision

Creating a rollback automation for proxies revision in a CI/CD pipeline typically involves:

Defining rollback criteria

Creating a rollback script

Testing the script

Deploying the rollback Automation 

Prerequisites

Before creating a rollback automation script for proxy revision we need some prequisites that are as follows:

A json file with all the input of stable and unstable revision that is deployed using create proxy pipeline

how to create this file

A json file with all the input of stable & unstable revision deployed using create proxy pipeline : This file will contain all the stable and unstable proxy revision whenever the creation and deployment of proxy is success or failure. If success it will give the stable revision list and if the deployment is fail it went under the unstable revisions.

 To create this file follow the below points:

Add the code in Post Build Action section of Deployment Pipeline as :

For Stable Revision Listing : add the below code to the success block of post build actions.

So the below code will create one file in the workspace directory of this job and that will store the stable proxy revisions in the json format and further this will artifact the file and its content in the archive directory of the same Proxy Deployment jenkins Job.

The path for the archive is :

Archive Path

cd /var/jenkins_home/jobs/revision-proxy/builds/build_number/archive/file_namecat file_name

Post Build Action – Success

post('Post-build steps') {

  success {

    script {
 
def jsonfile = new File("$WORKSPACE/proxy-revision.json")

if(jsonfile.exists() == false){

    jsonfile.write("{}")

}
def jsonPayload = jsonfile.text

def slurper = new groovy.json.JsonSlurper()

def json = slurper.parseText(jsonPayload)

def builder = new groovy.json.JsonBuilder()
 
String field = "${env.PROXYNAME}"

String revision = "${params.APIGEE_REVISION}"

println field

println revision

proxy_exists = json.keySet().contains(field)

if (proxy_exists==false){

    String[] proxy_revision = new String[1]

    String[] failed_proxy_revision = new String[0]

    proxy_revision[0] = revision

    def newproxy_element = [

        'stable_proxy_revision' : proxy_revision,

        'unstable_proxy_revision' : failed_proxy_revision

    ]

    json."${field}" = newproxy_element

    def json_builder = new groovy.json.JsonBuilder(json)

    File file = new File("$WORKSPACE/proxy-revision.json")

    file.write(json_builder.toPrettyString())
}

else{

    println "proxy exists appending to the file"

    String[] proxy_revision_list = json."${field}"."stable_proxy_revision"

    String[] modified_proxy_revision_list = new 

String[proxy_revision_list.size()+1]
     
    int i = 0

    for(i = 0; i < proxy_revision_list.size(); i++){

      modified_proxy_revision_list[i] = proxy_revision_list[i]

    }

    modified_proxy_revision_list[i] = revision  

    json."${field}"."stable_proxy_revision" = modified_proxy_revision_list  

    def json_builder = new groovy.json.JsonBuilder(json)  

    File file = new File("$WORKSPACE/proxy-revision.json")

    file.write(json_builder.toPrettyString())

    }

      }
     archiveArtifacts artifacts: 'proxy-revision.json',

    onlyIfSuccessful: true

    }

For Unstable Revision Listing : add the below code to the failure block of post build actions.

The below code add the unstbale revisions of  proxy in the same file that was created from the above code whenever the deployment of proxy or the pipeline fails.

Post Build Actions – Failure

failure {

    script {

        def jsonfile = new File("$WORKSPACE/proxy-revision.json")

if(jsonfile.exists() == false){

    jsonfile.write("{}")

}

def jsonPayload = jsonfile.text

def slurper = new groovy.json.JsonSlurper()

def json = slurper.parseText(jsonPayload)

def builder = new groovy.json.JsonBuilder()

 
String field = "${env.PROXYNAME}"

String revision = "${params.APIGEE_REVISION}"

proxy_exists = json.keySet().contains(field)

if (proxy_exists==false){

    String[] proxy_revision = new String[1]

    String[] success_proxy_revision = new String[0]

    proxy_revision[0] = revision
 
    def newproxy_element = [

        'stable_proxy_revision' : success_proxy_revision,

        'unstable_proxy_revision' : proxy_revision

    ]
 
    json."${field}" = newproxy_element

    def json_builder = new groovy.json.JsonBuilder(json)

    File file = new File("$WORKSPACE/proxy-revision.json")

    file.write(json_builder.toPrettyString())
}

else{

    String[] proxy_revision_list = json."${field}"."unstable_proxy_revision"

    String[] modified_proxy_revision_list = new 

String[proxy_revision_list.size()+1]  
 
    int i = 0

    for(i = 0; i < proxy_revision_list.size(); i++){

       modified_proxy_revision_list[i] = proxy_revision_list[i]

    }

    modified_proxy_revision_list[i] = revision 

    json."${field}"."unstable_proxy_revision" = modified_proxy_revision_list  
 
    def json_builder = new groovy.json.JsonBuilder(json) 
  
    File file = new File("$WORKSPACE/proxy-revision.json")

    file.write(json_builder.toPrettyString())

}
    }
}

The above prerequisites will append an apigee revision in a job of deployment proxy Jenkinsfile whenever a deployment of proxy is done to find latest stable and unstable revision of proxy according to the name of the proxy in case of rollback.

Steps Involved in Whole Rollback Automation Process

To do the rollback automation process for the stable and unstable proxy revisions follow the below steps to do so.

Defining Rollback Criteria

The rollback automation criteria is defined when a new proxy configuration is causing issues, such as network outages or increased latency, and needs to be quickly rolled back to a previous stable state to minimize downtime and ensure system availability. Criteria Defines in this automation includes :

Fetching the details from the stable revision deployment file

Using the stable revision as an input for the users to be rollbacked to 

Creating a Rollback Script

Develop a script that can take input from the user as a parameter in which all the deployed proxy revision should be mentioned a san dropdown menu and the user can select.

Add the below line at the start of the rollback automation job as:

Json Slurper

import groovy.json.JsonSlurper

Add this step in the pipeline to use input parameter as a part of user input using those input parameters from file as a part of user input in rollback job as:

User Input

steps {

    script {

        def jsonDir = "${repoName}"

        def jsonFiles = findFiles glob: "${jsonDir}/*.json"

        for (jsonFile in jsonFiles) {

            proxyName(jsonDir, jsonFile)

            for (envVar in selectedEnvironments) {

                def curVer = getCurrentRevision(envVar)

                copyArtifacts filter: '**/proxy-revision.json', 

fingerprintArtifacts: true, projectName: 'revision-proxy', selector: 

lastSuccessful()

                def jsonPayload = readFile("${WORKSPACE}/proxy-

revision.json")

                def listt = readJSON text: jsonPayload

                def stableRevisions = 

listt."${env.PROXYNAME}"."stable_proxy_revision"

                def userInput = input message: 'These are the Stable 

Revisions for Proxy Deployment:', parameters: [choice(name: 'STABLE_VERSION',

 defaultValue: stableRevisions[0], choices: stableRevisions, description:

 'Choose your Revision to Deploy Proxy')]

                    sh "$APIGEE_CLI_DIR/apigeecli apis deploy -o 

${APIGEE_ORG} -n ${env.PROXYNAME} -e $envVar -v ${userInput} -t ${env.TOKEN} 

-r --wait"

            }
        }
    }
}

So the above code will use input parameter as a part of user input to select the revision to rollback proxies from the dropdown menu to deploy proxy to some other revisions as Choose your revision to deploy proxy and it also copying the artifacts that was generated in the proxy deployment job as a file.

Testing the Script 

Test the rollback script in a non-production environment to ensure that it works as expected. This could involve simulating issues with the current proxy configuration and verifying that the script can automatically roll back to a previous stable version.To test the rollback automation script we are simply running the rollback automation script that was written in the above step as:

Build the rollback pipeline as Build with Parameters enter the values for the parameters asked .

Build the piepline with parameters.

While running the pipeline tye pipeline  holds on user Input Requested, Click on Input Requested.

Input the stable revision in the user input dropdown box to deploy the proxy on the selected revision.

  • Once you click on proceeed the proxy will deployed on the selected Revision as user input.

Deploying the rollback Automation 

Once the rollback script has been tested and validated, implement it in a production environment using the testing rollback script and do the necessary changes if required.

This is how we can do the Rollback automation for proxies revision.

Conclusion

Here, In this blog we have seen How to Create a Rollback automation for Proxies Revision in a jenkins pipeline. I hope this is helpful and interesting to know how we can rollback revision in apigee. Please feel free to ask query if you have any.

Thank You !!!

Happy Learning 🙂

Reference

https://code-maven.com/groovy-files

Written by 

Deeksha Tripathi is a Software Consultant at Knoldus Inc Software. She has a keen interest toward learning new technologies. Her practice area is DevOps. When not working, she will be busy in listening music , and spending time with her family .