Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add terraform-dependencies clean-tfvars-cache command #374

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions bin/terraform-dependencies/v2/clean-tfvars-cache
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/bin/bash

# exit on failures
set -e
set -o pipefail

usage() {
echo "Usage: $(basename "$0") [OPTIONS]" 1>&2
echo " -h - help"
exit 1
}

#DALMATIAN_ACCOUNT_DEFAULT_REGION="$(jq -r '.default_region' < "$CONFIG_SETUP_JSON_FILE")"
#PROJECT_NAME="$(jq -r '.project_name' < "$CONFIG_SETUP_JSON_FILE")"
#PROJECT_NAME_HASH="$(echo -n "$PROJECT_NAME" | sha1sum | head -c 6)"

while getopts "h" opt; do
case $opt in
h)
usage
;;
*)
usage
;;
esac
done

log_info -l "Checking for redundant files in tfvars cache ..." -q "$QUIET_MODE"

ACCOUNT_BOOTSTRAP_WORKSPACES=()
while IFS='' read -r workspace <&9
do
workspace=${workspace/\*/ }
workspace=$(echo "$workspace" | xargs)
if [[
"$workspace" != "default" &&
-n "$workspace"
]]
then
ACCOUNT_BOOTSTRAP_WORKSPACES+=("$workspace")
fi
done 9< <("$APP_ROOT/bin/dalmatian" terraform-dependencies run-terraform-command -c "workspace list" -a -q)

INFRASTRUCTURE_WORKSPACES=()
while IFS='' read -r workspace <&9
do
workspace=${workspace/\*/ }
workspace=$(echo "$workspace" | xargs)
if [[
"$workspace" != "default" &&
-n "$workspace"
]]
then
INFRASTRUCTURE_WORKSPACES+=("$workspace")
fi
done 9< <("$APP_ROOT/bin/dalmatian" terraform-dependencies run-terraform-command -c "workspace list" -i -q)

REMOVED_ACCOUNT_BOOTSTRAP_TFVARS=0
while IFS='' read -r tfvar_file <&9
do
ACCOUNT_EXISTS=0
for workspace in "${ACCOUNT_BOOTSTRAP_WORKSPACES[@]}"
do
if [ "100-$workspace.tfvars" == "$tfvar_file" ]
then
ACCOUNT_EXISTS=1
break
fi
done
if [ "$ACCOUNT_EXISTS" == 0 ]
then
log_info -l "Removing $tfvar_file account bootstrap tfvar file ..." -q "$QUIET_MODE"
rm "$CONFIG_TFVARS_DIR/$tfvar_file"
REMOVED_ACCOUNT_BOOTSTRAP_TFVARS=1
fi
done 9< <(find "$CONFIG_TFVARS_DIR"/100-* -maxdepth 1 -exec basename {} \;)

REMOVED_INFRASTRUCTURE_TFVARS=0
while IFS='' read -r tfvar_file <&9
do
INFRASTRUCTURE_EXISTS=0
for workspace in "${INFRASTRUCTURE_WORKSPACES[@]}"
do
if [ "200-$workspace.tfvars" == "$tfvar_file" ]
then
INFRASTRUCTURE_EXISTS=1
break
fi
done
if [ "$INFRASTRUCTURE_EXISTS" == 0 ]
then
log_info -l "Removing $tfvar_file infrastructure tfvar file ..." -q "$QUIET_MODE"
rm "$CONFIG_TFVARS_DIR/$tfvar_file"
REMOVED_INFRASTRUCTURE_TFVARS=1
fi
done 9< <(find "$CONFIG_TFVARS_DIR"/200-* -maxdepth 1 -exec basename {} \;)

if [[
"$REMOVED_ACCOUNT_BOOTSTRAP_TFVARS" == 0 &&
"$REMOVED_INFRASTRUCTURE_TFVARS" == 0
]]
then
log_info -l "tfvar cache clean complete - no files were removed" -q "$QUIET_MODE"
fi
2 changes: 2 additions & 0 deletions bin/terraform-dependencies/v2/get-tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,5 @@ do
done 9< <("$APP_ROOT/bin/dalmatian" terraform-dependencies run-terraform-command -c "workspace list" -i -q)

echo "$TFVARS_PATHS_JSON" > "$CONFIG_TFVARS_PATHS_FILE"

"$APP_ROOT/bin/dalmatian" terraform-dependencies clean-tfvars-cache