Merge pull request #153118 from qowoz/tf-scripts

terraform-providers: update-provider scripts - misc fixes
This commit is contained in:
Jonas Chevalier 2022-01-02 12:54:47 +01:00 committed by GitHub
commit a475c130e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 59 deletions

View file

@ -1,5 +1,5 @@
#!/usr/bin/env nix-shell #!/usr/bin/env nix-shell
#! nix-shell -i bash -p jq #! nix-shell -I nixpkgs=../../../../.. -i bash -p jq
# shellcheck shell=bash # shellcheck shell=bash
# Update all providers which have specified provider source address # Update all providers which have specified provider source address
@ -14,9 +14,9 @@ providers=$(
) )
echo "Will update providers:" echo "Will update providers:"
echo "$providers" echo "${providers}"
for provider in $providers; do for provider in ${providers}; do
echo "Updating $provider" echo "Updating ${provider}"
./update-provider "$provider" ./update-provider "${provider}"
done done

View file

@ -1,5 +1,5 @@
#!/usr/bin/env nix-shell #!/usr/bin/env nix-shell
#! nix-shell -I nixpkgs=../../../../.. -i bash -p coreutils curl jq moreutils nix #! nix-shell -I nixpkgs=../../../../.. -i bash -p coreutils curl jq moreutils nix nix-prefetch
# shellcheck shell=bash # shellcheck shell=bash
# vim: ft=sh # vim: ft=sh
# #
@ -7,6 +7,7 @@
# provider source address. # provider source address.
# #
set -euo pipefail set -euo pipefail
shopt -s inherit_errexit
show_usage() { show_usage() {
cat <<DOC cat <<DOC
@ -57,36 +58,37 @@ while [[ $# -gt 0 ]]; do
shift 2 shift 2
;; ;;
*) *)
if [[ -n "$provider" ]]; then if [[ -n ${provider} ]]; then
echo "ERROR: provider name was passed two times: '$provider' and '$1'" echo "ERROR: provider name was passed two times: '${provider}' and '$1'"
echo "Use --help for more info" echo "Use --help for more info"
exit 1 exit 1
fi fi
provider=$1 provider=$1
shift shift
;;
esac esac
done done
if [[ -z "$provider" ]]; then if [[ -z ${provider} ]]; then
echo "ERROR: No providers specified!" echo "ERROR: No providers specified!"
echo echo
show_usage show_usage
exit 1 exit 1
fi fi
provider_name=$(basename "$provider") provider_name=$(basename "${provider}")
# Usage: read_attr <key> # Usage: read_attr <key>
read_attr() { read_attr() {
jq -r ".\"$provider_name\".\"$1\"" providers.json jq -r ".\"${provider_name}\".\"$1\"" providers.json
} }
# Usage: update_attr <key> <value> # Usage: update_attr <key> <value>
update_attr() { update_attr() {
if [[ "$2" == "null" ]]; then if [[ $2 == "null" ]]; then
jq -S ".\"$provider_name\".\"$1\" = null" providers.json | sponge providers.json jq -S ".\"${provider_name}\".\"$1\" = null" providers.json | sponge providers.json
else else
jq -S ".\"$provider_name\".\"$1\" = \"$2\"" providers.json | sponge providers.json jq -S ".\"${provider_name}\".\"$1\" = \"$2\"" providers.json | sponge providers.json
fi fi
} }
@ -96,23 +98,23 @@ prefetch_github() {
local owner=$1 local owner=$1
local repo=$2 local repo=$2
local rev=$3 local rev=$3
nix-prefetch-url --unpack "https://github.com/$owner/$repo/archive/$rev.tar.gz" nix-prefetch-url --unpack "https://github.com/${owner}/${repo}/archive/${rev}.tar.gz"
} }
old_source_address="$(read_attr provider-source-address)" old_source_address="$(read_attr provider-source-address)"
old_vendor_sha256=$(read_attr vendorSha256) old_vendor_sha256=$(read_attr vendorSha256)
old_version=$(read_attr version) old_version=$(read_attr version)
if [[ $provider =~ ^[^/]+/[^/]+$ ]]; then if [[ ${provider} =~ ^[^/]+/[^/]+$ ]]; then
source_address=registry.terraform.io/$provider source_address=registry.terraform.io/${provider}
else else
source_address=$old_source_address source_address=${old_source_address}
fi fi
if [[ "$source_address" == "null" ]]; then if [[ ${source_address} == "null" ]]; then
echo "Could not find the source address for provider: $provider" echo "Could not find the source address for provider: ${provider}"
exit 1 exit 1
fi fi
update_attr "provider-source-address" "$source_address" update_attr "provider-source-address" "${source_address}"
# The provider source address (used inside Terraform `required_providers` block) is # The provider source address (used inside Terraform `required_providers` block) is
# used to compute the registry API endpoint # used to compute the registry API endpoint
@ -122,58 +124,43 @@ update_attr "provider-source-address" "$source_address"
# registry.terraform.io/v1/providers/hashicorp/aws (provider URL for the JSON API) # registry.terraform.io/v1/providers/hashicorp/aws (provider URL for the JSON API)
registry_response=$(curl -s https://"${source_address/\///v1/providers/}") registry_response=$(curl -s https://"${source_address/\///v1/providers/}")
version="$(jq -r '.version' <<< "$registry_response")" version="$(jq -r '.version' <<<"${registry_response}")"
if [[ "$old_version" = "$version" && "$force" != 1 && -z "$vendorSha256" && "$old_vendor_sha256" != "$vendorSha256" ]]; then if [[ ${old_version} == "${version}" && ${force} != 1 && -z ${vendorSha256} && ${old_vendor_sha256} != "${vendorSha256}" ]]; then
echo "$provider_name is already at version $version" echo "${provider_name} is already at version ${version}"
exit exit
fi fi
update_attr version "$version" update_attr version "${version}"
provider_source_url="$(jq -r '.source' <<< "$registry_response")" provider_source_url="$(jq -r '.source' <<<"${registry_response}")"
org="$(echo "$provider_source_url" | cut -d '/' -f 4)" org="$(echo "${provider_source_url}" | cut -d '/' -f 4)"
update_attr owner "$org" update_attr owner "${org}"
repo="$(echo "$provider_source_url" | cut -d '/' -f 5)" repo="$(echo "${provider_source_url}" | cut -d '/' -f 5)"
update_attr repo "$repo" update_attr repo "${repo}"
rev="$(jq -r '.tag' <<< "$registry_response")" rev="$(jq -r '.tag' <<<"${registry_response}")"
update_attr rev "$rev" update_attr rev "${rev}"
sha256=$(prefetch_github "$org" "$repo" "$rev") sha256=$(prefetch_github "${org}" "${repo}" "${rev}")
update_attr sha256 "$sha256" update_attr sha256 "${sha256}"
repo_root=$(git rev-parse --show-toplevel) repo_root=$(git rev-parse --show-toplevel)
if [[ -z "$vendorSha256" ]]; then if [[ -z ${vendorSha256} ]]; then
if [[ "$old_vendor_sha256" == null ]]; then if [[ ${old_vendor_sha256} == null ]]; then
vendorSha256=null vendorSha256=null
elif [[ -n "$old_vendor_sha256" || "$vendor" = 1 ]]; then elif [[ -n ${old_vendor_sha256} || ${vendor} == 1 ]]; then
echo "=== Calculating vendorSha256 ===" echo "=== Calculating vendorSha256 ==="
update_attr vendorSha256 "0000000000000000000000000000000000000000000000000000000000000000" vendorSha256=$(nix-prefetch "{ sha256 }: (import ../../../../.. {}).terraform-providers.${provider_name}.go-modules.overrideAttrs (_: { vendorSha256 = sha256; })")
# Hackish way to find out the desired sha256. First build, then extract the
# error message from the logs.
set +e
nix-build --no-out-link "$repo_root" -A "terraform-providers.$provider_name.go-modules" 2>vendor_log.txt
set -e
logs=$(< vendor_log.txt)
if ! [[ $logs =~ got:\ +([^\ ]+) ]]; then
echo "ERROR: could not find new hash in output:"
cat vendor_log.txt
rm -f vendor_log.txt
exit 1
fi
rm -f vendor_log.txt
# trim the results in case it they have a sha256: prefix or contain more than one line
vendorSha256=$(echo "${BASH_REMATCH[1]#sha256:}" | head -n 1)
# Deal with nix unstable # Deal with nix unstable
if [[ $vendorSha256 = sha256-* ]]; then if [[ ${vendorSha256} == sha256-* ]]; then
vendorSha256=$(nix --extra-experimental-features nix-command hash to-base32 "$vendorSha256") vendorSha256=$(nix --extra-experimental-features nix-command hash to-base32 "${vendorSha256}")
fi fi
fi fi
fi fi
if [[ -n "$vendorSha256" ]]; then if [[ -n ${vendorSha256} ]]; then
update_attr vendorSha256 "$vendorSha256" update_attr vendorSha256 "${vendorSha256}"
fi fi
# Check that the provider builds # Check that the provider builds
echo "=== Building terraform-providers.$provider_name ===" echo "=== Building terraform-providers.${provider_name} ==="
nix-build "$repo_root" -A "terraform-providers.$provider_name" nix-build --no-out-link "${repo_root}" -A "terraform-providers.${provider_name}"