2023-03-17 11:20:39 -04:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2023-03-29 08:13:26 -04:00
|
|
|
###### script header ######
|
|
|
|
|
2023-03-17 11:20:39 -04:00
|
|
|
lib=$(realpath @@BASE_LIB@@) || exit 1
|
2023-03-29 08:13:26 -04:00
|
|
|
stat "${lib}" >> /dev/null || exit 1
|
2023-03-17 11:20:39 -04:00
|
|
|
|
|
|
|
# shellcheck source=../sh/lib.bash
|
|
|
|
if ! source "${lib}"; then
|
|
|
|
echo "Error: could not find import"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-03-29 08:13:26 -04:00
|
|
|
terraform=$(realpath @@TERRAFORM@@)
|
|
|
|
stat "${terraform}" >> /dev/null
|
|
|
|
mode="@@MODE@@"
|
|
|
|
|
|
|
|
cd "${BUILD_WORKSPACE_DIRECTORY}"
|
|
|
|
|
|
|
|
###### script body ######
|
2023-03-17 11:20:39 -04:00
|
|
|
|
|
|
|
readarray -t <<< "$(
|
|
|
|
find "$(pwd)" -type f -name "*.tf" -exec dirname "{}" \; |
|
|
|
|
sort -ud
|
|
|
|
)"
|
|
|
|
terraformPaths=("${MAPFILE[@]}")
|
2023-11-24 09:58:21 -05:00
|
|
|
terraformFormatModules=()
|
|
|
|
terraformLockModules=()
|
|
|
|
terraformCheckModules=()
|
2023-03-17 11:20:39 -04:00
|
|
|
pathPrefix="${terraformPaths[0]}"
|
|
|
|
for ((i = 1; i < ${#terraformPaths[@]}; i++)); do
|
|
|
|
path="${terraformPaths[i]}"
|
|
|
|
if [[ ${path} == "${pathPrefix}"* ]]; then
|
|
|
|
continue
|
|
|
|
fi
|
2023-11-24 09:58:21 -05:00
|
|
|
terraformFormatModules+=("${pathPrefix}")
|
|
|
|
terraformLockModules+=("${pathPrefix}")
|
|
|
|
terraformCheckModules+=("${pathPrefix}")
|
2023-03-17 11:20:39 -04:00
|
|
|
pathPrefix="${path}"
|
|
|
|
done
|
|
|
|
|
|
|
|
excludeDirs=(
|
|
|
|
"build"
|
|
|
|
)
|
|
|
|
|
2023-11-24 09:58:21 -05:00
|
|
|
excludeLockDirs=(
|
|
|
|
"build"
|
|
|
|
"terraform-provider-constellation"
|
2023-12-12 10:00:03 -05:00
|
|
|
"terraform/legacy-module"
|
2023-11-24 09:58:21 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
excludeCheckDirs=(
|
|
|
|
"build"
|
|
|
|
"terraform-provider-constellation"
|
|
|
|
)
|
|
|
|
|
2023-06-14 07:58:21 -04:00
|
|
|
check() {
|
2023-11-24 09:58:21 -05:00
|
|
|
echo "The following Terraform modules are excluded and won't be formatted:"
|
2023-06-14 07:58:21 -04:00
|
|
|
for exclude in "${excludeDirs[@]}"; do
|
2023-11-24 09:58:21 -05:00
|
|
|
for i in "${!terraformFormatModules[@]}"; do
|
|
|
|
if [[ ${terraformFormatModules[i]} == "${BUILD_WORKSPACE_DIRECTORY}/${exclude}"* ]]; then
|
|
|
|
echo " ${terraformFormatModules[i]}"
|
|
|
|
unset 'terraformFormatModules[i]'
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "The following Terraform modules are excluded and their lockfiles won't be updated:"
|
|
|
|
for exclude in "${excludeLockDirs[@]}"; do
|
|
|
|
for i in "${!terraformLockModules[@]}"; do
|
|
|
|
if [[ ${terraformLockModules[i]} == "${BUILD_WORKSPACE_DIRECTORY}/${exclude}"* ]]; then
|
2023-11-28 11:30:11 -05:00
|
|
|
echo "${terraformLockModules[i]}"
|
2023-11-24 09:58:21 -05:00
|
|
|
unset 'terraformLockModules[i]'
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "The following Terraform modules are excluded and won't be checked:"
|
2023-11-27 03:00:08 -05:00
|
|
|
for exclude in "${excludeCheckDirs[@]}"; do
|
|
|
|
for i in "${!terraformCheckModules[@]}"; do
|
|
|
|
if [[ ${terraformCheckModules[i]} == "${BUILD_WORKSPACE_DIRECTORY}/${exclude}"* ]]; then
|
|
|
|
echo " ${terraformCheckModules[i]}"
|
|
|
|
unset 'terraformCheckModules[i]'
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "The following Terraform modules are excluded and won't be locked:"
|
|
|
|
for exclude in "${excludeLockDirs[@]}"; do
|
|
|
|
for i in "${!terraformLockModules[@]}"; do
|
|
|
|
if [[ ${terraformLockModules[i]} == "${BUILD_WORKSPACE_DIRECTORY}/${exclude}"* ]]; then
|
|
|
|
echo " ${terraformLockModules[i]}"
|
|
|
|
unset 'terraformLockModules[i]'
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "The following Terraform modules are excluded and won't be checked:"
|
2023-11-24 09:58:21 -05:00
|
|
|
for exclude in "${excludeCheckDirs[@]}"; do
|
|
|
|
for i in "${!terraformCheckModules[@]}"; do
|
|
|
|
if [[ ${terraformCheckModules[i]} == "${BUILD_WORKSPACE_DIRECTORY}/${exclude}"* ]]; then
|
|
|
|
echo " ${terraformCheckModules[i]}"
|
|
|
|
unset 'terraformCheckModules[i]'
|
2023-06-14 07:58:21 -04:00
|
|
|
fi
|
|
|
|
done
|
2023-03-17 11:20:39 -04:00
|
|
|
done
|
|
|
|
|
2023-06-14 07:58:21 -04:00
|
|
|
case ${mode} in
|
|
|
|
"check")
|
|
|
|
echo "Checking validity and format of the following Terraform modules:"
|
2023-11-24 09:58:21 -05:00
|
|
|
for script in "${terraformCheckModules[@]}"; do
|
2023-06-14 07:58:21 -04:00
|
|
|
echo " ${script}"
|
|
|
|
done
|
|
|
|
echo "This may take a minute..."
|
2023-11-24 09:58:21 -05:00
|
|
|
for module in "${terraformCheckModules[@]}"; do
|
2023-06-14 07:58:21 -04:00
|
|
|
${terraform} -chdir="${module}" init > /dev/null
|
2023-11-24 09:58:21 -05:00
|
|
|
${terraform} -chdir="${module}" fmt -recursive > /dev/null
|
2023-06-14 07:58:21 -04:00
|
|
|
${terraform} -chdir="${module}" validate > /dev/null
|
|
|
|
rm -rf "${module}/.terraform"
|
2023-11-28 11:30:11 -05:00
|
|
|
echo "Deleting lock files in the following directories:" # init generates lockfiles which should only be generated in the generate mode.
|
|
|
|
for dir in "${excludeLockDirs[@]}"; do
|
|
|
|
if [[ -d ${dir} ]]; then
|
|
|
|
find "${dir}" -name '*.lock.hcl' -type f -delete
|
|
|
|
else
|
|
|
|
echo " Directory ${dir} does not exist, skipping"
|
|
|
|
fi
|
|
|
|
done
|
2023-06-14 07:58:21 -04:00
|
|
|
done
|
|
|
|
;;
|
2023-03-17 11:20:39 -04:00
|
|
|
|
2023-06-14 07:58:21 -04:00
|
|
|
"format")
|
|
|
|
echo "Formatting the following Terraform modules:"
|
2023-11-24 09:58:21 -05:00
|
|
|
for module in "${terraformFormatModules[@]}"; do
|
2023-06-14 07:58:21 -04:00
|
|
|
echo " ${module}"
|
|
|
|
${terraform} -chdir="${module}" fmt -recursive > /dev/null
|
|
|
|
done
|
|
|
|
;;
|
2023-03-22 13:14:35 -04:00
|
|
|
|
2023-06-14 07:58:21 -04:00
|
|
|
"generate")
|
2023-11-24 09:58:21 -05:00
|
|
|
echo "Generating lock files for the following Terraform modules:"
|
|
|
|
for script in "${terraformLockModules[@]}"; do
|
2023-06-14 07:58:21 -04:00
|
|
|
echo " ${script}"
|
|
|
|
done
|
|
|
|
echo "This may take 5-10 min..."
|
2023-11-24 09:58:21 -05:00
|
|
|
for module in "${terraformLockModules[@]}"; do
|
2023-06-14 07:58:21 -04:00
|
|
|
${terraform} -chdir="${module}" init > /dev/null
|
|
|
|
${terraform} -chdir="${module}" providers lock -platform=linux_arm64 > /dev/null
|
|
|
|
${terraform} -chdir="${module}" providers lock -platform=linux_amd64 > /dev/null
|
|
|
|
${terraform} -chdir="${module}" providers lock -platform=darwin_arm64 > /dev/null
|
|
|
|
${terraform} -chdir="${module}" providers lock -platform=darwin_amd64 > /dev/null
|
|
|
|
${terraform} -chdir="${module}" providers lock -platform=windows_amd64 > /dev/null
|
|
|
|
rm -rf "${module}/.terraform"
|
|
|
|
done
|
|
|
|
;;
|
2023-03-17 11:20:39 -04:00
|
|
|
|
2023-06-14 07:58:21 -04:00
|
|
|
*)
|
|
|
|
echo "Error: unknown mode \"${mode}\""
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
if test -v SILENT; then
|
|
|
|
check > /dev/null
|
|
|
|
else
|
|
|
|
check
|
|
|
|
fi
|