2018-01-24 12:13:51 +01:00
|
|
|
#!/usr/bin/env bash
|
2020-08-27 20:55:28 +01:00
|
|
|
set -eo pipefail
|
2018-01-24 12:13:51 +01:00
|
|
|
|
2020-11-02 21:44:54 +01:00
|
|
|
# `terraform validate` requires this env variable to be set
|
|
|
|
|
export AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-us-east-1}
|
|
|
|
|
|
2020-08-27 10:57:45 +01:00
|
|
|
main() {
|
|
|
|
|
initialize_
|
|
|
|
|
parse_cmdline_ "$@"
|
|
|
|
|
terraform_validate_
|
|
|
|
|
}
|
2018-01-24 13:34:34 +01:00
|
|
|
|
2020-08-27 10:57:45 +01:00
|
|
|
initialize_() {
|
|
|
|
|
# get directory containing this script
|
|
|
|
|
local dir
|
|
|
|
|
local source
|
|
|
|
|
source="${BASH_SOURCE[0]}"
|
|
|
|
|
while [[ -L $source ]]; do # resolve $source until the file is no longer a symlink
|
|
|
|
|
dir="$(cd -P "$(dirname "$source")" > /dev/null && pwd)"
|
|
|
|
|
source="$(readlink "$source")"
|
|
|
|
|
# if $source was a relative symlink, we need to resolve it relative to the path where the symlink file was located
|
|
|
|
|
[[ $source != /* ]] && source="$dir/$source"
|
|
|
|
|
done
|
|
|
|
|
_SCRIPT_DIR="$(dirname "$source")"
|
2018-04-21 11:22:47 +02:00
|
|
|
|
2020-08-27 10:57:45 +01:00
|
|
|
# source getopt function
|
|
|
|
|
# shellcheck source=lib_getopt
|
|
|
|
|
. "$_SCRIPT_DIR/lib_getopt"
|
|
|
|
|
}
|
2018-01-24 13:34:34 +01:00
|
|
|
|
2020-08-27 10:57:45 +01:00
|
|
|
parse_cmdline_() {
|
|
|
|
|
declare argv
|
2021-12-11 05:33:22 -08:00
|
|
|
argv=$(getopt -o e:i:a: --long envs:,init-args:,args: -- "$@") || return
|
2020-08-27 10:57:45 +01:00
|
|
|
eval "set -- $argv"
|
2018-04-21 11:22:47 +02:00
|
|
|
|
2020-08-27 10:57:45 +01:00
|
|
|
for argv; do
|
|
|
|
|
case $argv in
|
|
|
|
|
-a | --args)
|
|
|
|
|
shift
|
|
|
|
|
ARGS+=("$1")
|
|
|
|
|
shift
|
|
|
|
|
;;
|
2021-12-11 05:33:22 -08:00
|
|
|
-i | --init-args)
|
|
|
|
|
shift
|
|
|
|
|
INIT_ARGS+=("$1")
|
|
|
|
|
shift
|
|
|
|
|
;;
|
2020-08-27 10:57:45 +01:00
|
|
|
-e | --envs)
|
|
|
|
|
shift
|
|
|
|
|
ENVS+=("$1")
|
|
|
|
|
shift
|
|
|
|
|
;;
|
|
|
|
|
--)
|
|
|
|
|
shift
|
|
|
|
|
FILES=("$@")
|
|
|
|
|
break
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
}
|
2020-04-04 02:17:25 -04:00
|
|
|
|
2020-08-27 10:57:45 +01:00
|
|
|
terraform_validate_() {
|
2020-04-04 02:17:25 -04:00
|
|
|
|
2020-08-27 10:57:45 +01:00
|
|
|
# Setup environment variables
|
2020-08-27 20:55:28 +01:00
|
|
|
local var var_name var_value
|
2020-08-27 10:57:45 +01:00
|
|
|
for var in "${ENVS[@]}"; do
|
2020-08-27 20:55:28 +01:00
|
|
|
var_name="${var%%=*}"
|
|
|
|
|
var_value="${var#*=}"
|
|
|
|
|
# shellcheck disable=SC2086
|
|
|
|
|
export $var_name="$var_value"
|
2020-08-27 10:57:45 +01:00
|
|
|
done
|
|
|
|
|
|
|
|
|
|
declare -a paths
|
2020-08-27 20:55:28 +01:00
|
|
|
local index=0
|
|
|
|
|
local error=0
|
2020-08-27 10:57:45 +01:00
|
|
|
|
2020-08-27 20:55:28 +01:00
|
|
|
local file_with_path
|
2020-08-27 10:57:45 +01:00
|
|
|
for file_with_path in "${FILES[@]}"; do
|
|
|
|
|
file_with_path="${file_with_path// /__REPLACED__SPACE__}"
|
|
|
|
|
|
|
|
|
|
paths[index]=$(dirname "$file_with_path")
|
|
|
|
|
((index += 1))
|
|
|
|
|
done
|
|
|
|
|
|
2020-08-27 20:55:28 +01:00
|
|
|
local path_uniq
|
2020-08-27 10:57:45 +01:00
|
|
|
for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
|
|
|
|
|
path_uniq="${path_uniq//__REPLACED__SPACE__/ }"
|
|
|
|
|
|
|
|
|
|
if [[ -n "$(find "$path_uniq" -maxdepth 1 -name '*.tf' -print -quit)" ]]; then
|
|
|
|
|
|
2020-11-02 21:44:54 +01:00
|
|
|
pushd "$(realpath "$path_uniq")" > /dev/null
|
|
|
|
|
|
|
|
|
|
if [[ ! -d .terraform ]]; then
|
|
|
|
|
set +e
|
2021-12-11 05:33:22 -08:00
|
|
|
init_output=$(terraform init -backend=false "${INIT_ARGS[@]}" 2>&1)
|
2020-11-02 21:44:54 +01:00
|
|
|
init_code=$?
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
|
|
if [[ $init_code != 0 ]]; then
|
|
|
|
|
error=1
|
|
|
|
|
echo "Init before validation failed: $path_uniq"
|
|
|
|
|
echo "$init_output"
|
|
|
|
|
popd > /dev/null
|
|
|
|
|
continue
|
2020-08-27 10:57:45 +01:00
|
|
|
fi
|
2020-11-02 21:44:54 +01:00
|
|
|
fi
|
2020-08-27 10:57:45 +01:00
|
|
|
|
2020-11-02 21:44:54 +01:00
|
|
|
set +e
|
|
|
|
|
validate_output=$(terraform validate "${ARGS[@]}" 2>&1)
|
|
|
|
|
validate_code=$?
|
|
|
|
|
set -e
|
2020-08-27 10:57:45 +01:00
|
|
|
|
2020-11-02 21:44:54 +01:00
|
|
|
if [[ $validate_code != 0 ]]; then
|
2020-08-27 10:57:45 +01:00
|
|
|
error=1
|
2020-11-02 21:44:54 +01:00
|
|
|
echo "Validation failed: $path_uniq"
|
|
|
|
|
echo "$validate_output"
|
2020-08-27 10:57:45 +01:00
|
|
|
echo
|
2020-04-04 02:17:25 -04:00
|
|
|
fi
|
2020-11-02 21:44:54 +01:00
|
|
|
|
|
|
|
|
popd > /dev/null
|
2018-05-24 21:10:49 +01:00
|
|
|
fi
|
2020-08-27 10:57:45 +01:00
|
|
|
done
|
|
|
|
|
|
|
|
|
|
if [[ $error -ne 0 ]]; then
|
|
|
|
|
exit 1
|
2018-01-24 13:48:44 +01:00
|
|
|
fi
|
2020-08-27 10:57:45 +01:00
|
|
|
}
|
|
|
|
|
|
2020-11-02 21:44:54 +01:00
|
|
|
# global arrays
|
2020-08-27 10:57:45 +01:00
|
|
|
declare -a ARGS
|
2021-12-11 05:33:22 -08:00
|
|
|
declare -a INIT_ARGS
|
2020-08-27 10:57:45 +01:00
|
|
|
declare -a ENVS
|
|
|
|
|
declare -a FILES
|
2019-02-21 19:38:50 +11:00
|
|
|
|
2020-08-27 10:57:45 +01:00
|
|
|
[[ ${BASH_SOURCE[0]} != "$0" ]] || main "$@"
|