Skip to content

chore(provisioner/terraform): make testdata generate.sh parallel #16326

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

Merged
Merged
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
72 changes: 54 additions & 18 deletions provisioner/terraform/testdata/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,73 @@
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")"

for d in */; do
pushd "$d"
generate() {
local name="$1"

echo "=== BEGIN: $name"
terraform init -upgrade &&
terraform plan -out terraform.tfplan &&
terraform show -json ./terraform.tfplan | jq >"$name".tfplan.json &&
terraform graph -type=plan >"$name".tfplan.dot &&
rm terraform.tfplan &&
terraform apply -auto-approve &&
terraform show -json ./terraform.tfstate | jq >"$name".tfstate.json &&
rm terraform.tfstate &&
terraform graph -type=plan >"$name".tfstate.dot
Comment on lines +10 to +18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the &&'s necessary if we have -e set?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't be, but I was having trouble getting this working without them. I might've had some other issue before that caused it though so I can retry.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand, printing "end" is dependent on the chaining, which makes it easier to discern what output belongs where.

ret=$?
echo "=== END: $name"
if [[ $ret -ne 0 ]]; then
return $ret
fi
}

run() {
d="$1"
cd "$d"
name=$(basename "$(pwd)")

# This needs care to update correctly.
if [[ $name == "kubernetes-metadata" ]]; then
popd
continue
echo "== Skipping: $name"
return 0
fi

# This directory is used for a different purpose (quick workaround).
if [[ $name == "cleanup-stale-plugins" ]]; then
popd
continue
echo "== Skipping: $name"
return 0
fi

if [[ $name == "timings-aggregation" ]]; then
popd
continue
echo "== Skipping: $name"
return 0
fi

echo "== Generating test data for: $name"
if ! out="$(generate "$name" 2>&1)"; then
echo "$out"
echo "== Error generating test data for: $name"
return 1
fi
echo "== Done generating test data for: $name"
exit 0
}

terraform init -upgrade
terraform plan -out terraform.tfplan
terraform show -json ./terraform.tfplan | jq >"$name".tfplan.json
terraform graph -type=plan >"$name".tfplan.dot
rm terraform.tfplan
terraform apply -auto-approve
terraform show -json ./terraform.tfstate | jq >"$name".tfstate.json
rm terraform.tfstate
terraform graph -type=plan >"$name".tfstate.dot
popd
declare -a jobs=()
for d in */; do
run "$d" &
jobs+=($!)
done

err=0
for job in "${jobs[@]}"; do
if ! wait "$job"; then
err=$((err + 1))
fi
done
if [[ $err -ne 0 ]]; then
echo "ERROR: Failed to generate test data for $err modules"
exit 1
fi

terraform version -json | jq -r '.terraform_version' >version.txt
Loading