#!/bin/bash #set -x #File: redeployProxy.sh #Author: Vishnu Balan #Date: 22 Mar 2017 #Notes: # # This script creates a new revision of a deployed proxy in the given environment. # The new revision is 1 increment from the last revision deployed at the ORG level. # [The latest revision at the ORG level may or may not be the same as the deployed revision at the environment level.] # The deployment is executed seamlessly so that in-flight transactions are not affected, # by inducing a delay of 10 sec in un-deploying the existing running version, once the new version is deployed. # #Change Log: # v1.0 VBALAN (04-12-2017) - Initial version. # help_menu() { echo "" echo " Help Menu : " echo " redeployProxy.sh: update the Apigee proxy version" echo " usage:" echo " ./redeployProxy.sh -h displays this message" echo " ./redeployProxy.sh -e -a -u username -p password" } deployNewRevision() { printf "\n Un-deploying ${PROXY} revision ${REVISION} and deploying revision ${NEXTREVISION} ...\n" CURL_CMD="curl -X POST -H 'Content-Type: application/x-www-form-urlencoded' 'https://api.enterprise.apigee.com/v1/o/${ORG}/environments/${ENV}/apis/${PROXY}/revisions/${NEXTREVISION}/deployments?delay=10&override=true' -u ${USERNAME}:${PASSWORD} --insecure -s" #echo ${CURL_CMD} RESPONSE=`eval ${CURL_CMD}` if [[ ${RESPONSE} == *'"status" : "deployed"'* ]]; then printf "\n Deployed ${PROXY} revision number ${NEXTREVISION} successfully in the environment ${ENV}.\n" else printf "Deployment failed ...\n" printf ${RESPONSE} fi } uploadNewRevision() { printf "\n Uploading ${PROXY} as revision number ${NEXTREVISION} ...\n" CURL_CMD="curl --fail -X POST -u '${USERNAME}:${PASSWORD}' -F 'file=@${TEMPDIR}/${PROXY}.zip' 'https://api.enterprise.apigee.com/v1/o/${ORG}/apis?action=import&name=apiProxy-vbalan' --insecure -s" RESPONSE=`eval ${CURL_CMD}` #echo ${RESPONSE} if [ "$?" -gt 0 ]; then printf "Upload failed for proxy ${PROXY} revision ${NEXTREVISION}, in the environment ${ENV}!\n" exit fi } downloadProxy() { printf "\n Downloading ${PROXY} revision number ${REVISION} to temp directory ${TEMPDIR} ...\n" CURL_CMD="curl --fail 'https://api.enterprise.apigee.com/v1/o/${ORG}/apis/${PROXY}/revisions/${REVISION}?format=bundle' -u ${USERNAME}:${PASSWORD} -o ${TEMPDIR}/${PROXY}.zip --insecure -s" #echo ${CURL_CMD} RESPONSE=`eval ${CURL_CMD}` if [ "$?" -gt 0 ]; then printf "Download failed for revision ${REVISION} of proxy ${PROXY}, in the environment ${ENV}!\n" exit fi } getRevision() { # This function retrieves 2 revision numbers. # The latest uploaded version of the proxy in the given ORG # and the latest deployed version of that proxy in the given environment under the same ORG. # These 2 revision numbers may or may not be the same... printf "\n Retrieving latest uploaded {PROXY} revision number from ${ORG} ...\n" CURL_CMD="curl 'https://api.enterprise.apigee.com/v1/o/${ORG}/apis/${PROXY}' -u ${USERNAME}:${PASSWORD} -H 'Accept: application/json' --insecure -s" RESPONSE=`eval ${CURL_CMD}` #echo ${RESPONSE} if [[ $RESPONSE == *"ApplicationDoesNotExist"* ]]; then #Read and set the error message ERRMSG=$(echo ${RESPONSE} | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["message"]') exit 1 else REVISION=$(echo ${RESPONSE} | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["revision"][len(obj["revision"])-1]') fi printf "REVISION=${REVISION}" NEXTREVISION=$((${REVISION}+1)) printf "\n Retrieving latest deployed {PROXY} revision number from ${ENV} ...\n" CURL_CMD="curl 'https://api.enterprise.apigee.com/v1/o/${ORG}/environments/${ENV}/apis/${PROXY}/deployments' -u ${USERNAME}:${PASSWORD} -H 'Accept: application/json' --insecure -s" RESPONSE=`eval ${CURL_CMD}` REVISION=$(echo ${RESPONSE} | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["revision"][0]["name"]') printf "REVISION=${REVISION}" } curlCalls() { # Calling function getRevision() that retrieves the latest revision number of the apiProxy and sets the variable "REVISION". getRevision printf "\n Current deployed revision of ${PROXY} is ${REVISION}\n" downloadProxy uploadNewRevision deployNewRevision } #Setup the user/pass and host setup() { if [ ${ENV} == 'prd' ]; then API_HOST=https://api.costco.com/ elif [ ${ENV} == 'sandbox' ]; then API_HOST=https://api-sandbox.costco.com/ else API_HOST=https://api-${ENV}.costco.com/ fi } # look for misfit command line arguments: # '-', '--', '---' are all unacceptable args in_option=0 for param in "$@" do if [ "$param" == '-' ] || [ "$param" == '--' ]; then echo "redeployProxy : $param is not recognised" help_menu exit 1 fi if [ `expr index $param '-'` -eq 1 ]; then in_option=1 else if [ $in_option -eq 0 ]; then echo "redeployProxy: Unrecognised option $param" help_menu exit 1 else in_option=0 fi fi done ENV= ORG= PROXY= USERNAME= PASSWORD= REVISION=0 NEXTREVISION= API_HOST= ERRMSG= PID=$$ TEMPDIRROOT=/var/tmp/ #clean-up existing temp dirs and files under /var/tmp/ rm -rf ${TEMPDIRROOT}* TEMPDIR=${TEMPDIRROOT}tempdir.${PID} (umask 077 && mkdir ${TEMPDIR}) || (echo "There was an error creating ${PROXY_DIR}." && exit 1) ENV_LIST="sandbox adt qat spt prd" while getopts "he:a:u:p:" opt; do case $opt in h) help_menu exit 0 ;; e) ENV=${OPTARG,,} ;; a) PROXY=$OPTARG ;; u) USERNAME=$OPTARG ;; p) PASSWORD=$OPTARG ;; *) echo "Invalid option $opt" ;; esac done #Check if environment is invalid if ! [[ " ${ENV_LIST} " =~ " ${ENV} " ]]; then echo "Invalid environment ${ENV}" exit 0 fi #Set the ORG based on the environment if [ ${ENV} == "sandbox" ]; then ORG="costco-sandbox" elif [ ${ENV} == "prd" ]; then ORG="costco" else ORG="costco-np" fi setup curlCalls