|
| 1 | +#!/bin/bash |
| 2 | +KEY_LOCATION="" |
| 3 | +BUILDENV_LIST="" |
| 4 | +usage() |
| 5 | +{ |
| 6 | +cat << EOF |
| 7 | +usage: $0 options |
| 8 | +This script need to be executed with below option. |
| 9 | +OPTIONS: |
| 10 | + -e environment |
| 11 | + -b Security file location GIT|AWS |
| 12 | + -k key location |
| 13 | +EOF |
| 14 | +} |
| 15 | +#log Function - Used to provide information of execution information with date and time |
| 16 | +log() |
| 17 | +{ |
| 18 | + echo "`date +'%D %T'` : $1" |
| 19 | +} |
| 20 | +track_error() |
| 21 | +{ |
| 22 | + if [ $1 != "0" ]; then |
| 23 | + log "$2 exited with error code $1" |
| 24 | + log "completed execution IN ERROR at `date`" |
| 25 | + exit $1 |
| 26 | + fi |
| 27 | + |
| 28 | +} |
| 29 | +download_buildenvfile() |
| 30 | +{ |
| 31 | + if [ -z "$BUILDENV_LIST" ]; |
| 32 | + then |
| 33 | + if [ -z "$KEY_LOCATION" ]; |
| 34 | + then |
| 35 | + track_error $? "Please provide the file list using -b or file location -k or both -b and -k " |
| 36 | + else |
| 37 | + aws s3 sync s3://tc-buildproperties-${ENV_CONFIG}/$KEY_LOCATION . |
| 38 | + track_error $? "Environment setting" |
| 39 | + fi |
| 40 | + |
| 41 | + else |
| 42 | + Buffer_seclist=$(echo $BUILDENV_LIST | sed 's/,/ /g' ) |
| 43 | + for listname in $Buffer_seclist; |
| 44 | + do |
| 45 | + if [ -z "$KEY_LOCATION" ]; |
| 46 | + then |
| 47 | + aws s3 cp s3://tc-buildproperties-${ENV_CONFIG}/$listname . |
| 48 | + track_error $? "Environment setting" |
| 49 | + else |
| 50 | + aws s3 cp s3://tc-buildproperties-${ENV_CONFIG}/$KEY_LOCATION/$listname . |
| 51 | + track_error $? "Environment setting" |
| 52 | + fi |
| 53 | + done |
| 54 | + fi |
| 55 | + |
| 56 | +} |
| 57 | + |
| 58 | +configure_aws_cli() { |
| 59 | + aws --version |
| 60 | + aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID |
| 61 | + aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY |
| 62 | + aws configure set default.region $AWS_REGION |
| 63 | + aws configure set default.output json |
| 64 | + log "Configured AWS CLI." |
| 65 | +} |
| 66 | + |
| 67 | +while getopts .b:e:k:. OPTION |
| 68 | +do |
| 69 | + case $OPTION in |
| 70 | + e) |
| 71 | + ENV=$OPTARG |
| 72 | + ;; |
| 73 | + b) |
| 74 | + BUILDENV_LIST=$OPTARG |
| 75 | + ;; |
| 76 | + k) |
| 77 | + KEY_LOCATION=$OPTARG |
| 78 | + ;; |
| 79 | + ?) |
| 80 | + log "additional param required" |
| 81 | + usage |
| 82 | + exit |
| 83 | + ;; |
| 84 | + esac |
| 85 | +done |
| 86 | + |
| 87 | +AWS_ACCESS_KEY_ID=$(eval "echo \$${ENV}_AWS_ACCESS_KEY_ID") |
| 88 | +AWS_SECRET_ACCESS_KEY=$(eval "echo \$${ENV}_AWS_SECRET_ACCESS_KEY") |
| 89 | +AWS_REGION=$(eval "echo \$${ENV}_AWS_REGION") |
| 90 | +if [ -z $AWS_REGION ]; |
| 91 | +then |
| 92 | +AWS_REGION="us-east-1" |
| 93 | +fi |
| 94 | +if [ -z $AWS_ACCESS_KEY_ID ] || [ -z $AWS_SECRET_ACCESS_KEY ] ; |
| 95 | +then |
| 96 | + log "AWS Secret Parameters are not configured in circleci/environment" |
| 97 | + usage |
| 98 | + exit 1 |
| 99 | +else |
| 100 | + configure_aws_cli |
| 101 | +fi |
| 102 | +ENV_CONFIG=`echo "$ENV" | tr '[:upper:]' '[:lower:]'` |
| 103 | +download_buildenvfile |
0 commit comments