Monday, February 8, 2016

script to push to Amazon Elastic Beanstalk (EB) using the CLI and Git with downtime (swapping environments)

I wanted to automate pushing to my Elastic Beanstalk Amazon environment...

here is the simple script I came up with:

requirements:
- you should be able to upload a new version already using Git
- have the EB CLI 3.7.2 (Python 3.4.3) configured in the directory where you are running this script

- copy this script into the directory that you are going to upload
- give the script execution permissions
- eb list should have a current environment (depicted with a '*')

#!/bin/bash
set -e
#get the current environment
/usr/local/bin/eb list > temp_environment
temp_environment=$(grep \* temp_environment | cut -f2 -d" ")

echo
echo
echo "detected environment ->$temp_environment<-"
echo "subtitute this environment y/N"

read answer

if [ "$
answer" =  "y" ]; then
        a=1
else
        echo "your answer was $
answer. byee!"
        exit 1
fi

echo "substituting environmet with current version"

echo
echo

sufijo=`date +%a%y%m%d%H%M%S`

ambiente_sin_sufijo=$(echo $temp_environment | cut -d"-" -f1)
sufijo_ambiente=$(echo $temp_environment | cut -d"-" -f2)

dayname=$(echo $sufijo_ambiente | cut -c1-3)
year=$(echo $sufijo_ambiente | cut -c4-5)
mon=$(echo $sufijo_ambiente | cut -c6-7)
day=$(echo $sufijo_ambiente | cut -c8-9)
hour=$(echo $sufijo_ambiente | cut -c10-11)
min=$(echo $sufijo_ambiente | cut -c12-13)
sec=$(echo $sufijo_ambiente | cut -c14-15)

set -x

echo "New version uploaded to EB: `date`" > gitlog.temp
echo ""  >> gitlog.temp
echo "The files that changed since last time are:"   >> gitlog.temp
echo ""   >> gitlog.temp

set -f
git log --name-only --no-color --graph '--pretty=format:%h - %s' --abbrev-commit --since "20$year-$mon-$day $hour:$min:$sec" >> gitlog.temp


ambiente_final="prod"-"$sufijo"

echo "Deploying to: $ambiente_final and then swapping with: $temp_environment"
echo "do you wish to continue? s/N"

read respuesta

if [ "$respuesta" =  "s" ]; then
        a=1
else
        echo "su respuesta fue $respuesta, entonces salimos. chao!"
        exit 1
fi


set -x

eb clone "$temp_environment" -n "$ambiente_final" --timeout 15 || exit 1
eb deploy "$ambiente_final" || exit 1
eb swap "$temp_environment" -n "$ambiente_final" || exit 1
eb use "$ambiente_final" || exit 1

set +x

echo
echo
echo
echo FINISHED !!!
echo
echo
echo "Remember to execute -> eb terminate $temp_environment <-"

mail -s "New version" -r "Auto Uploader<no-reply@mail.com>" myemail@mail.com < gitlog.temp

rm gitlog.temp

No comments:

Post a Comment

cancel script completely on ctrl-c

I found this question interesting: basically how to cancel completely a script and all child processes : You do this by creating a subro...