2020-09-30 20:21:16 +00:00
|
|
|
#!/usr/bin/env nix-shell
|
2022-01-01 22:19:54 +00:00
|
|
|
#! nix-shell -I nixpkgs=../../../../.. -i bash -p coreutils curl jq moreutils nix nix-prefetch
|
2020-09-30 20:21:16 +00:00
|
|
|
# shellcheck shell=bash
|
2020-12-01 10:05:00 +00:00
|
|
|
# vim: ft=sh
|
|
|
|
#
|
|
|
|
# Update a terraform provider to the latest version advertised at the
|
|
|
|
# provider source address.
|
2020-09-30 20:21:16 +00:00
|
|
|
#
|
|
|
|
set -euo pipefail
|
2022-01-01 22:13:51 +00:00
|
|
|
shopt -s inherit_errexit
|
2020-09-30 20:21:16 +00:00
|
|
|
|
2020-12-01 10:05:00 +00:00
|
|
|
show_usage() {
|
|
|
|
cat <<DOC
|
|
|
|
Usage: ./update-provider [--force] [--vendor] [<owner>/]<provider>
|
|
|
|
|
|
|
|
Update a single provider in the providers.json inventory file.
|
|
|
|
|
|
|
|
For example to update 'terraform-providers.aws' run:
|
|
|
|
|
|
|
|
./update-provider aws
|
|
|
|
|
|
|
|
If the provider is not in the list already, use the form '<owner>/<provider>'
|
|
|
|
to add the provider to the list:
|
2020-09-30 20:21:16 +00:00
|
|
|
|
2020-12-01 10:05:00 +00:00
|
|
|
./update-provider hetznercloud/hcloud
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
|
|
|
* --force: Force the update even if the version matches.
|
|
|
|
* --vendor: Switch from go package to go modules with vendor.
|
|
|
|
* --vendor-sha256 <sha256>: Override the SHA256 or "null".
|
2020-09-30 20:21:16 +00:00
|
|
|
DOC
|
2020-12-01 10:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
force=
|
|
|
|
provider=
|
|
|
|
vendor=
|
|
|
|
vendorSha256=
|
|
|
|
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
|
|
case "$1" in
|
|
|
|
-h | --help)
|
|
|
|
show_usage
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
--force)
|
|
|
|
force=1
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--vendor)
|
|
|
|
force=1
|
|
|
|
vendor=1
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--vendor-sha256)
|
|
|
|
force=1
|
|
|
|
vendorSha256=$2
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
*)
|
2022-01-01 22:13:51 +00:00
|
|
|
if [[ -n ${provider} ]]; then
|
|
|
|
echo "ERROR: provider name was passed two times: '${provider}' and '$1'"
|
2020-12-01 10:05:00 +00:00
|
|
|
echo "Use --help for more info"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
provider=$1
|
|
|
|
shift
|
2022-01-01 22:13:51 +00:00
|
|
|
;;
|
2020-12-01 10:05:00 +00:00
|
|
|
esac
|
|
|
|
done
|
2020-09-30 20:21:16 +00:00
|
|
|
|
2022-01-01 22:13:51 +00:00
|
|
|
if [[ -z ${provider} ]]; then
|
2020-12-01 10:05:00 +00:00
|
|
|
echo "ERROR: No providers specified!"
|
2020-09-30 20:21:16 +00:00
|
|
|
echo
|
2020-12-01 10:05:00 +00:00
|
|
|
show_usage
|
2020-09-30 20:21:16 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-01-01 22:13:51 +00:00
|
|
|
provider_name=$(basename "${provider}")
|
2020-09-30 20:21:16 +00:00
|
|
|
|
2020-12-01 10:05:00 +00:00
|
|
|
# Usage: read_attr <key>
|
|
|
|
read_attr() {
|
2022-01-01 22:13:51 +00:00
|
|
|
jq -r ".\"${provider_name}\".\"$1\"" providers.json
|
2020-12-01 10:05:00 +00:00
|
|
|
}
|
2020-09-30 20:21:16 +00:00
|
|
|
|
2020-12-01 10:05:00 +00:00
|
|
|
# Usage: update_attr <key> <value>
|
|
|
|
update_attr() {
|
2022-01-01 22:13:51 +00:00
|
|
|
if [[ $2 == "null" ]]; then
|
|
|
|
jq -S ".\"${provider_name}\".\"$1\" = null" providers.json | sponge providers.json
|
2020-12-01 10:05:00 +00:00
|
|
|
else
|
2022-01-01 22:13:51 +00:00
|
|
|
jq -S ".\"${provider_name}\".\"$1\" = \"$2\"" providers.json | sponge providers.json
|
2020-12-01 10:05:00 +00:00
|
|
|
fi
|
|
|
|
}
|
2020-09-30 20:21:16 +00:00
|
|
|
|
|
|
|
prefetch_github() {
|
|
|
|
# of a given owner, repo and rev, fetch the tarball and return the output of
|
|
|
|
# `nix-prefetch-url`
|
|
|
|
local owner=$1
|
|
|
|
local repo=$2
|
|
|
|
local rev=$3
|
2022-01-01 22:13:51 +00:00
|
|
|
nix-prefetch-url --unpack "https://github.com/${owner}/${repo}/archive/${rev}.tar.gz"
|
2020-09-30 20:21:16 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 10:05:00 +00:00
|
|
|
old_source_address="$(read_attr provider-source-address)"
|
|
|
|
old_vendor_sha256=$(read_attr vendorSha256)
|
|
|
|
old_version=$(read_attr version)
|
|
|
|
|
2022-01-01 22:13:51 +00:00
|
|
|
if [[ ${provider} =~ ^[^/]+/[^/]+$ ]]; then
|
|
|
|
source_address=registry.terraform.io/${provider}
|
2020-12-01 10:05:00 +00:00
|
|
|
else
|
2022-01-01 22:13:51 +00:00
|
|
|
source_address=${old_source_address}
|
2020-12-01 10:05:00 +00:00
|
|
|
fi
|
2022-01-01 22:13:51 +00:00
|
|
|
if [[ ${source_address} == "null" ]]; then
|
|
|
|
echo "Could not find the source address for provider: ${provider}"
|
2020-12-01 10:05:00 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2022-01-01 22:13:51 +00:00
|
|
|
update_attr "provider-source-address" "${source_address}"
|
2020-12-01 10:05:00 +00:00
|
|
|
|
|
|
|
# The provider source address (used inside Terraform `required_providers` block) is
|
|
|
|
# used to compute the registry API endpoint
|
|
|
|
#
|
|
|
|
# registry.terraform.io/hashicorp/aws (provider source address)
|
|
|
|
# registry.terraform.io/providers/hashicorp/aws (provider URL for the website)
|
|
|
|
# registry.terraform.io/v1/providers/hashicorp/aws (provider URL for the JSON API)
|
|
|
|
registry_response=$(curl -s https://"${source_address/\///v1/providers/}")
|
|
|
|
|
2022-01-01 22:13:51 +00:00
|
|
|
version="$(jq -r '.version' <<<"${registry_response}")"
|
|
|
|
if [[ ${old_version} == "${version}" && ${force} != 1 && -z ${vendorSha256} && ${old_vendor_sha256} != "${vendorSha256}" ]]; then
|
|
|
|
echo "${provider_name} is already at version ${version}"
|
2020-12-01 10:05:00 +00:00
|
|
|
exit
|
|
|
|
fi
|
2022-01-01 22:13:51 +00:00
|
|
|
update_attr version "${version}"
|
2020-12-01 10:05:00 +00:00
|
|
|
|
2022-01-01 22:13:51 +00:00
|
|
|
provider_source_url="$(jq -r '.source' <<<"${registry_response}")"
|
2020-09-30 20:21:16 +00:00
|
|
|
|
2022-01-01 22:13:51 +00:00
|
|
|
org="$(echo "${provider_source_url}" | cut -d '/' -f 4)"
|
|
|
|
update_attr owner "${org}"
|
|
|
|
repo="$(echo "${provider_source_url}" | cut -d '/' -f 5)"
|
|
|
|
update_attr repo "${repo}"
|
|
|
|
rev="$(jq -r '.tag' <<<"${registry_response}")"
|
|
|
|
update_attr rev "${rev}"
|
|
|
|
sha256=$(prefetch_github "${org}" "${repo}" "${rev}")
|
|
|
|
update_attr sha256 "${sha256}"
|
2020-09-30 20:21:16 +00:00
|
|
|
|
2020-12-01 10:05:00 +00:00
|
|
|
repo_root=$(git rev-parse --show-toplevel)
|
2020-09-30 20:21:16 +00:00
|
|
|
|
2022-01-01 22:13:51 +00:00
|
|
|
if [[ -z ${vendorSha256} ]]; then
|
|
|
|
if [[ ${old_vendor_sha256} == null ]]; then
|
2020-12-01 10:05:00 +00:00
|
|
|
vendorSha256=null
|
2022-01-01 22:13:51 +00:00
|
|
|
elif [[ -n ${old_vendor_sha256} || ${vendor} == 1 ]]; then
|
2020-12-01 10:05:00 +00:00
|
|
|
echo "=== Calculating vendorSha256 ==="
|
2022-01-01 22:19:54 +00:00
|
|
|
vendorSha256=$(nix-prefetch "{ sha256 }: (import ../../../../.. {}).terraform-providers.${provider_name}.go-modules.overrideAttrs (_: { vendorSha256 = sha256; })")
|
2020-12-01 10:05:00 +00:00
|
|
|
# Deal with nix unstable
|
2022-01-01 22:13:51 +00:00
|
|
|
if [[ ${vendorSha256} == sha256-* ]]; then
|
|
|
|
vendorSha256=$(nix --extra-experimental-features nix-command hash to-base32 "${vendorSha256}")
|
2020-12-01 10:05:00 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
2020-09-30 20:21:16 +00:00
|
|
|
|
2022-01-01 22:13:51 +00:00
|
|
|
if [[ -n ${vendorSha256} ]]; then
|
|
|
|
update_attr vendorSha256 "${vendorSha256}"
|
2020-12-01 10:05:00 +00:00
|
|
|
fi
|
2020-09-30 20:21:16 +00:00
|
|
|
|
2020-12-01 10:05:00 +00:00
|
|
|
# Check that the provider builds
|
2022-01-01 22:13:51 +00:00
|
|
|
echo "=== Building terraform-providers.${provider_name} ==="
|
2022-01-01 22:19:54 +00:00
|
|
|
nix-build --no-out-link "${repo_root}" -A "terraform-providers.${provider_name}"
|