Skip to content

Instantly share code, notes, and snippets.

@sks
Last active December 22, 2015 23:54
Show Gist options
  • Save sks/c500328d3a0710543182 to your computer and use it in GitHub Desktop.
Save sks/c500328d3a0710543182 to your computer and use it in GitHub Desktop.
CI Code to do blue-green deployment with zero downtime
APP=<APP_NAME>
HOSTNAME=$APP
DOMAIN=run.aws-usw02-pr.ice.predix.io
SLEEP_BEFORE_DELETE_OLDER_APP=60
function status(){
echo $(date +"%m-%d-%y %H:%M:%S ") $*
}
function exit_if_error(){
if [ $1 -ne 0 ]
then
shift
status "[ERROR] : "$*
exit 1;
fi
}
function get_options(){
while getopts "b:h:" opt; do
case $opt in
b)
BUILD_NUMBER=$OPTARG
;;
h)
HOSTNAME=$APP-$OPTARG
;;
h)
DOMAIN=$OPTARG
;;
\?) echo "Invalid option -$OPTARG"
;;
esac
done
}
function validateOptions(){
if [ ${#BUILD_NUMBER} -eq 0 ]
then
exit_if_error 1 "BUILD_NUMBER not set using -b option"
fi
APP_NAME=$APP-$BUILD_NUMBER
status "[INFO] Pushing the app with the hostname set as $HOSTNAME"
}
function unmap_route_and_delete_older_apps(){
for app in $(cf a | grep $APP| awk '{print $1}')
do
if [ $app != $APP_NAME ]
then
status "unmapping route from older app"
cf unmap-route $app $DOMAIN -n $HOSTNAME
status "Deleting the app $app"
sleep $SLEEP_BEFORE_DELETE_OLDER_APP && cf delete $app -f # Just to make sure that any older process running are taken care of
exit_if_error $? "Error deleting the older app $app"
fi
done
}
function cleanup(){
echo "[INFO] deleting orphaned orphans if any"
cf delete-orphaned-routes -f
exit_if_error $? "Error cleaning up the unused routes"
}
function main(){
get_options $*
validateOptions
status "[INFO] pushing the app : $APP_NAME"
cf push $APP_NAME
exit_if_error $? "Could not push the application as expected"
cf map-route $APP_NAME $DOMAIN -n $HOSTNAME
exit_if_error $? "Could not map the route $HOSTNAME to the app $APP_NAME"
unmap_route_and_delete_older_apps
cleanup
}
main $*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment