Skip to content

Commit

Permalink
Merge pull request #1096 from tfxor/feature/bash
Browse files Browse the repository at this point in the history
bugfix for bash
  • Loading branch information
eistrati authored Mar 30, 2022
2 parents 4b4763c + 8f700f6 commit 975a9ed
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 99 deletions.
10 changes: 5 additions & 5 deletions lib/scripts/aws/cloudfront_distribution/build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

if [[ -z "${TERRAHUB_VAR_S3_BUCKET_NAME}" ]]; then
if [ -z "${TERRAHUB_VAR_S3_BUCKET_NAME}" ]; then
echo "[ERROR] TERRAHUB_VAR_S3_BUCKET_NAME variable is empty. Aborting..."
exit 1
fi
Expand All @@ -9,22 +9,22 @@ fi
_CWD="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
export TERRAHUB_BUILD_TEMP_VARS="/tmp/.env-$(date '+%Y%m%d%H%M%S%N%Z')"

if [[ -f ${TERRAHUB_BUILD_TEMP_VARS} ]]; then
if [ -f "${TERRAHUB_BUILD_TEMP_VARS}" ]; then
rm -f ${TERRAHUB_BUILD_TEMP_VARS}
fi

touch ${TERRAHUB_BUILD_TEMP_VARS}

# Re-source terrahub build temporary file
if [[ -f ${TERRAHUB_BUILD_TEMP_VARS} ]]; then
source ${TERRAHUB_BUILD_TEMP_VARS}
if [ -f "${TERRAHUB_BUILD_TEMP_VARS}" ]; then
. ${TERRAHUB_BUILD_TEMP_VARS}
fi

# Invalidate CloudFront distribution
echo "[EXEC] ${_CWD}/invalidate.sh ${TERRAHUB_VAR_S3_BUCKET_NAME}"
${_CWD}/invalidate.sh ${TERRAHUB_VAR_S3_BUCKET_NAME}

# Cleanup terrahub build temporary file
if [[ -f ${TERRAHUB_BUILD_TEMP_VARS} ]]; then
if [ -f "${TERRAHUB_BUILD_TEMP_VARS}" ]; then
rm -f ${TERRAHUB_BUILD_TEMP_VARS}
fi
12 changes: 6 additions & 6 deletions lib/scripts/aws/cloudfront_distribution/invalidate.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
#!/bin/sh

# Validate if terrahub build temporary file exists
if [[ -z "${TERRAHUB_BUILD_TEMP_VARS}" ]]; then
if [ -z "${TERRAHUB_BUILD_TEMP_VARS}" ]; then
echo "[ERROR] TERRAHUB_BUILD_TEMP_VARS variable is empty. Aborting..."
exit 1
fi

# Re-source terrahub build temporary file
if [[ -f ${TERRAHUB_BUILD_TEMP_VARS} ]]; then
source ${TERRAHUB_BUILD_TEMP_VARS}
if [ -f "${TERRAHUB_BUILD_TEMP_VARS}" ]; then
. ${TERRAHUB_BUILD_TEMP_VARS}
fi

# Source path
if [[ -z "${TERRAHUB_VAR_S3_BUCKET_NAME}" ]] && [[ ! -z "${1}" ]]; then
if [ -z "${TERRAHUB_VAR_S3_BUCKET_NAME}" ] && [ ! -z "${1}" ]; then
TERRAHUB_VAR_S3_BUCKET_NAME=${1}
fi

if [[ -z "${TERRAHUB_VAR_S3_BUCKET_NAME}" ]]; then
if [ -z "${TERRAHUB_VAR_S3_BUCKET_NAME}" ]; then
echo "[ERROR] TERRAHUB_VAR_S3_BUCKET_NAME variable is empty. Aborting..."
exit 1
fi

aws --version > /dev/null 2>&1 || { echo >&2 '[ERROR] awscli is missing. Aborting...'; exit 1; }
_CF=$(aws cloudfront list-distributions --query "DistributionList.Items[?Comment=='${TERRAHUB_VAR_S3_BUCKET_NAME}' || AliasICPRecordals[0].CNAME=='${TERRAHUB_VAR_S3_BUCKET_NAME}'].Id" | jq ".[0]" || "null")

if [[ -z "${_CF}" ]] || [[ "${_CF}" == "null" ]]; then
if [ -z "${_CF}" ] || [ "${_CF}" == "null" ]; then
echo "[ERROR] No CloudFront distributions were found. Aborting..."
exit 0
fi
Expand Down
26 changes: 13 additions & 13 deletions lib/scripts/aws/lambda_function/build.sh
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
#!/bin/sh

if [[ -z "${TERRAHUB_BUILD_COMPILE_FILE}" ]]; then
if [ -z "${TERRAHUB_BUILD_COMPILE_FILE}" ]; then
echo "[ERROR] TERRAHUB_BUILD_COMPILE_FILE variable is empty. Aborting..."
exit 1
fi

if [[ -z "${TERRAHUB_BUILD_LOCAL_PATH}" ]]; then
if [ -z "${TERRAHUB_BUILD_LOCAL_PATH}" ]; then
echo "[ERROR] TERRAHUB_BUILD_LOCAL_PATH variable is empty. Aborting..."
exit 1
fi

if [[ -z "${TERRAHUB_BUILD_S3_DEPLOY}" ]]; then
if [ -z "${TERRAHUB_BUILD_S3_DEPLOY}" ]; then
echo "[ERROR] TERRAHUB_BUILD_S3_DEPLOY variable is empty. Aborting..."
exit 1
fi

if [[ -z "${TERRAHUB_BUILD_S3_PATH}" ]]; then
if [ -z "${TERRAHUB_BUILD_S3_PATH}" ]; then
echo "[ERROR] TERRAHUB_BUILD_S3_PATH variable is empty. Aborting..."
exit 1
fi

if [[ -z "${TERRAHUB_BUILD_INDEX_FILE}" ]]; then
if [ -z "${TERRAHUB_BUILD_INDEX_FILE}" ]; then
echo "[ERROR] TERRAHUB_BUILD_INDEX_FILE variable is empty. Aborting..."
exit 1
fi

if [[ -z "${TERRAHUB_BUILD_LAMBDA_FILE}" ]]; then
if [ -z "${TERRAHUB_BUILD_LAMBDA_FILE}" ]; then
echo "[ERROR] TERRAHUB_BUILD_LAMBDA_FILE variable is empty. Aborting..."
exit 1
fi

if [[ -z "${TERRAHUB_COMPONENT_HOME}" ]]; then
if [ -z "${TERRAHUB_COMPONENT_HOME}" ]; then
echo "[ERROR] TERRAHUB_COMPONENT_HOME variable is empty. Aborting..."
exit 1
fi

if [[ -z "${TERRAHUB_BUILD_OK}" ]]; then
if [ -z "${TERRAHUB_BUILD_OK}" ]; then
export TERRAHUB_BUILD_OK="false"
fi

# Initialize terrahub build temporary file
_CWD="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
export TERRAHUB_BUILD_TEMP_VARS="/tmp/.env-$(date '+%Y%m%d%H%M%S%N%Z')"

if [[ -f ${TERRAHUB_BUILD_TEMP_VARS} ]]; then
if [ -f "${TERRAHUB_BUILD_TEMP_VARS}" ]; then
rm -f ${TERRAHUB_BUILD_TEMP_VARS}
fi

Expand Down Expand Up @@ -72,8 +72,8 @@ echo "[EXEC] ${_CWD}/shasum.sh ${TERRAHUB_COMPONENT_HOME}/${TERRAHUB_BUILD_INDEX
${_CWD}/shasum.sh ${TERRAHUB_COMPONENT_HOME}/${TERRAHUB_BUILD_INDEX_FILE}

# Re-source terrahub build temporary file
if [[ -f ${TERRAHUB_BUILD_TEMP_VARS} ]]; then
source ${TERRAHUB_BUILD_TEMP_VARS}
if [ -f "${TERRAHUB_BUILD_TEMP_VARS}" ]; then
. ${TERRAHUB_BUILD_TEMP_VARS}
fi

# Upload index file to S3 storage
Expand All @@ -85,10 +85,10 @@ echo "[EXEC] ${_CWD}/upload.sh ${TERRAHUB_COMPONENT_HOME}/${TERRAHUB_BUILD_INDEX
${_CWD}/upload.sh ${TERRAHUB_COMPONENT_HOME}/${TERRAHUB_BUILD_INDEX_FILE} s3://${TERRAHUB_BUILD_S3_DEPLOY}/${TERRAHUB_BUILD_S3_PATH}/${TERRAHUB_BUILD_INDEX_FILE}

# Cleanup terrahub build temporary file
if [[ -f ${TERRAHUB_BUILD_TEMP_VARS} ]]; then
if [ -f "${TERRAHUB_BUILD_TEMP_VARS}" ]; then
rm -f ${TERRAHUB_BUILD_TEMP_VARS}
fi

if [[ -f ${TERRAHUB_COMPONENT_HOME}/${TERRAHUB_BUILD_INDEX_FILE} ]]; then
if [ -f "${TERRAHUB_COMPONENT_HOME}/${TERRAHUB_BUILD_INDEX_FILE}" ]; then
rm -f ${TERRAHUB_COMPONENT_HOME}/${TERRAHUB_BUILD_INDEX_FILE}
fi
14 changes: 7 additions & 7 deletions lib/scripts/aws/lambda_function/compare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

## Source path
_SRC=${1}
if [[ -z "${_SRC}" ]]; then
if [ -z "${_SRC}" ]; then
echo >&2 '[ERROR] _SRC variable is empty. Aborting...'
exit 1
fi

## Source files for build process
_COMPARE="${@:2}"
if [[ -z "${_COMPARE}" ]]; then
if [ -z "${_COMPARE}" ]; then
echo '[ERROR] _COMPARE variable is empty. Aborting...'
exit 1
fi

## Setup environmental variables
if [[ -f ${TERRAHUB_BUILD_TEMP_VARS} ]]; then
source ${TERRAHUB_BUILD_TEMP_VARS}
if [ -f "${TERRAHUB_BUILD_TEMP_VARS}" ]; then
. ${TERRAHUB_BUILD_TEMP_VARS}
fi

## Handle case where OS has sha1sum command instead of shasum
Expand All @@ -32,7 +32,7 @@ fi
## Compare SHA256 sums from _SRC file with files in _COMPARE
echo "[INFO] TERRAHUB_BUILD_OK='${TERRAHUB_BUILD_OK}' ==> Comparing SHA256 sums."
_EXCLUDE=" -path **/node_modules -o -path **/venv "
if [[ "$(uname)" == "Darwin" ]]; then
if [ "$(uname)" == "Darwin" ]; then
_SHASUM_ID=$(find -s ${_COMPARE} \( ${_EXCLUDE} \) -prune -o -type f -exec ${_SHASUM_CLI} {} \; | sort -k 2 | ${_SHASUM_CLI} | cut -f 1 -d " ")
else
_SHASUM_ID=$(find ${_COMPARE} \( ${_EXCLUDE} \) -prune -o -type f -exec ${_SHASUM_CLI} {} \; | sort -k 2 | ${_SHASUM_CLI} | cut -f 1 -d " ")
Expand All @@ -41,15 +41,15 @@ fi
## Checking if needs to skip SHA256 sums compare
echo "export TERRAHUB_SHA=\"${_SHASUM_ID}\"" >> ${TERRAHUB_BUILD_TEMP_VARS}
echo "[INFO] Current SHA256 => ${_SHASUM_ID}"
if [[ "${TERRAHUB_BUILD_OK}" == "true" ]]; then
if [ "${TERRAHUB_BUILD_OK}" == "true" ]; then
echo "[INFO] TERRAHUB_BUILD_OK='${TERRAHUB_BUILD_OK}' ==> Skipping comparing SHA256 sums."
exit 0
fi

## Checking if the project requires to be built
_SHASUM_CHK=$(head -n 1 ${_SRC})
echo "[INFO] S3 Object SHA256 => ${_SHASUM_CHK}"
if [[ "${_SHASUM_ID}" == "${_SHASUM_CHK}" ]]; then
if [ "${_SHASUM_ID}" == "${_SHASUM_CHK}" ]; then
echo '[INFO] Build is NOT required.'
exit 0
fi
Expand Down
2 changes: 1 addition & 1 deletion lib/scripts/aws/lambda_function/compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fi

## Setup environmental variables
if [ -f ${TERRAHUB_BUILD_TEMP_VARS} ]; then
source ${TERRAHUB_BUILD_TEMP_VARS}
. ${TERRAHUB_BUILD_TEMP_VARS}
fi

## Checking if TERRAHUB_BUILD_OK is true
Expand Down
10 changes: 5 additions & 5 deletions lib/scripts/aws/lambda_function/download.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

## Source path
_SRC=${1}
if [[ -z "${_SRC}" ]]; then
if [ -z "${_SRC}" ]; then
echo >&2 '[ERROR] _SRC variable is empty. Aborting...'
exit 1
fi

## S3 bucket name
_S3=${2-${TERRAHUB_BUILD_S3_DEPLOY}}
if [[ -z "${_S3}" ]]; then
if [ -z "${_S3}" ]; then
echo >&2 '[ERROR] _S3 variable is empty. Aborting...'
exit 1
fi
Expand All @@ -18,14 +18,14 @@ fi
_OPTIONS="${@:3}"

## Clean environmental variables
if [[ -f ${TERRAHUB_BUILD_TEMP_VARS} ]]; then
source ${TERRAHUB_BUILD_TEMP_VARS}
if [ -f "${TERRAHUB_BUILD_TEMP_VARS}" ]; then
. ${TERRAHUB_BUILD_TEMP_VARS}
fi

## Checking if _S3 file exists in S3
aws --version > /dev/null 2>&1 || { echo >&2 'awscli is missing. Aborting...'; exit 1; }
_COMPARE=$(aws s3 ls ${_S3} ${_OPTIONS} || echo "")
if [[ -z "${_COMPARE}" ]]; then
if [ -z "${_COMPARE}" ]; then
echo "[INFO] ${_S3} does NOT exist ==> First execution."
echo 'export TERRAHUB_BUILD_OK="true"' >> ${TERRAHUB_BUILD_TEMP_VARS}
exit 0
Expand Down
10 changes: 5 additions & 5 deletions lib/scripts/aws/lambda_function/shasum.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

## Source path
_SRC=${1}
if [[ -z "${_SRC}" ]]; then
if [ -z "${_SRC}" ]; then
echo >&2 '[ERROR] _SRC variable is empty. Aborting...'
exit 1
fi

## Setup environmental variables
if [[ -f ${TERRAHUB_BUILD_TEMP_VARS} ]]; then
source ${TERRAHUB_BUILD_TEMP_VARS}
if [ -f "${TERRAHUB_BUILD_TEMP_VARS}" ]; then
. ${TERRAHUB_BUILD_TEMP_VARS}
fi

## Checking if TERRAHUB_BUILD_OK is true
if [[ "$TERRAHUB_BUILD_OK" != "true" ]]; then
if [ "$TERRAHUB_BUILD_OK" != "true" ]; then
echo '[INFO] Build was NOT executed ==> SHA256 will NOT be updated.'
exit 0
fi

## Checking if SHA256 sums exists
if [[ -z "${TERRAHUB_SHA}" ]]; then
if [ -z "${TERRAHUB_SHA}" ]; then
echo >&2 '[ERROR] TERRAHUB_SHA variable is empty. Aborting...'
exit 1
fi
Expand Down
12 changes: 6 additions & 6 deletions lib/scripts/aws/lambda_function/upload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

## Source path
_SRC=${1}
if [[ -z "${_SRC}" ]]; then
if [ -z "${_SRC}" ]; then
echo >&2 '[ERROR] _SRC variable is empty. Aborting...'
exit 1
fi

## S3 bucket name
_S3=${2-${TERRAHUB_BUILD_S3_DEPLOY}}
if [[ -z "${_S3}" ]]; then
if [ -z "${_S3}" ]; then
echo >&2 '[ERROR] _S3 variable is empty. Aborting...'
exit 1
fi
Expand All @@ -18,8 +18,8 @@ fi
_OPTIONS="${@:3}"

## Setup environmental variables
if [[ -f ${TERRAHUB_BUILD_TEMP_VARS} ]]; then
source ${TERRAHUB_BUILD_TEMP_VARS}
if [ -f "${TERRAHUB_BUILD_TEMP_VARS}" ]; then
. ${TERRAHUB_BUILD_TEMP_VARS}
fi

## Checking if TERRAHUB_BUILD_OK is true
Expand All @@ -31,9 +31,9 @@ fi

## Sync _SRC to _S3
aws --version > /dev/null 2>&1 || { echo >&2 'awscli is missing. Aborting...'; exit 1; }
if [[ -d "${_SRC}" ]]; then
if [ -d "${_SRC}" ]; then
aws s3 sync ${_SRC} ${_S3} ${_OPTIONS}
elif [[ -f "${_SRC}" ]]; then
elif [ -f "${_SRC}" ]; then
aws s3 cp ${_SRC} ${_S3} ${_OPTIONS}
else
echo >&2 "[ERROR] ${_SRC} is not valid"
Expand Down
14 changes: 7 additions & 7 deletions lib/scripts/aws/lambda_function/zip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,38 @@

## zip option
_OPTION=${1}
if [[ "${_OPTION}" != "-r" ]] && [[ "${_OPTION}" != "-j" ]]; then
if [ "${_OPTION}" != "-r" ] && [ "${_OPTION}" != "-j" ]; then
echo >&2 '[ERROR] _OPTION variable must be "-r" or "-j". Aborting...'
exit 1
fi

## zip file
_FILE=${2}
if [[ -z "${_FILE}" ]]; then
if [ -z "${_FILE}" ]; then
echo >&2 '[ERROR] _FILE variable is empty. Aborting...'
exit 1
fi

## Source files or folders for zip process
_PATH="${@:3}"
if [[ -z "${_PATH}" ]]; then
if [ -z "${_PATH}" ]; then
echo >&2 '[ERROR] _PATH variable is empty. Aborting...'
exit 1
fi

## Setup environmental variables
if [[ -f ${TERRAHUB_BUILD_TEMP_VARS} ]]; then
source ${TERRAHUB_BUILD_TEMP_VARS}
if [ -f "${TERRAHUB_BUILD_TEMP_VARS}" ]; then
. ${TERRAHUB_BUILD_TEMP_VARS}
fi

## Checking if TERRAHUB_BUILD_OK is true
if [[ "${TERRAHUB_BUILD_OK}" != "true" ]]; then
if [ "${TERRAHUB_BUILD_OK}" != "true" ]; then
echo '[INFO] Build was NOT executed ==> zip file was NOT created.'
exit 0
fi

## Check if zip file exists and remove it
if [[ -f "${_FILE}" ]]; then
if [ -f "${_FILE}" ]; then
echo "[INFO] Removing existing ${_FILE}"
rm -f "${_FILE}"
fi
Expand Down
Loading

0 comments on commit 975a9ed

Please sign in to comment.