2020-09-30 20:21:16 +00:00
|
|
|
#!/usr/bin/env nix-shell
|
2023-05-19 23:44:08 +00:00
|
|
|
#! nix-shell -i bash -p coreutils curl git jq moreutils nix nurl
|
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
|
2022-01-31 04:48:36 +00:00
|
|
|
Usage: ./update-provider [--force] [--no-build] [<owner>/]<provider>
|
2020-12-01 10:05:00 +00:00
|
|
|
|
|
|
|
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.
|
2022-01-31 04:48:36 +00:00
|
|
|
* --no-build: Don't build provider
|
2020-09-30 20:21:16 +00:00
|
|
|
DOC
|
2020-12-01 10:05:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-26 20:54:00 +00:00
|
|
|
build=1
|
2020-12-01 10:05:00 +00:00
|
|
|
force=
|
|
|
|
provider=
|
2022-11-26 22:36:57 +00:00
|
|
|
spdx=1
|
2020-12-01 10:05:00 +00:00
|
|
|
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
|
|
case "$1" in
|
|
|
|
-h | --help)
|
|
|
|
show_usage
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
--force)
|
|
|
|
force=1
|
|
|
|
shift
|
|
|
|
;;
|
2022-01-31 04:48:36 +00:00
|
|
|
--no-build)
|
|
|
|
build=0
|
2020-12-01 10:05:00 +00:00
|
|
|
shift
|
|
|
|
;;
|
2022-11-26 22:36:57 +00:00
|
|
|
--no-spdx)
|
|
|
|
spdx=0
|
|
|
|
shift
|
|
|
|
;;
|
2020-12-01 10:05:00 +00:00
|
|
|
*)
|
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
|
|
|
|
|
2020-12-01 10:05:00 +00:00
|
|
|
# Usage: read_attr <key>
|
|
|
|
read_attr() {
|
2022-03-13 02:21:21 +00:00
|
|
|
jq -r ".\"${provider}\".\"$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
|
2022-03-13 02:21:21 +00:00
|
|
|
jq -S ".\"${provider}\".\"$1\" = null" providers.json | sponge providers.json
|
2020-12-01 10:05:00 +00:00
|
|
|
else
|
2022-03-13 02:21:21 +00:00
|
|
|
jq -S ".\"${provider}\".\"$1\" = \"$2\"" providers.json | sponge providers.json
|
2020-12-01 10:05:00 +00:00
|
|
|
fi
|
|
|
|
}
|
2020-09-30 20:21:16 +00:00
|
|
|
|
2022-03-13 02:21:21 +00:00
|
|
|
repo_root=$(git rev-parse --show-toplevel)
|
|
|
|
|
|
|
|
generate_hash() {
|
2023-05-19 23:44:08 +00:00
|
|
|
nurl --expr "(import ${repo_root} {}).terraform-providers.${provider}.$1"
|
2020-09-30 20:21:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-13 02:21:21 +00:00
|
|
|
echo_provider() {
|
|
|
|
echo "== terraform-providers.${provider}: $* =="
|
|
|
|
}
|
2020-12-01 10:05:00 +00:00
|
|
|
|
2022-09-21 04:59:14 +00:00
|
|
|
pushd "$(dirname "$0")" >/dev/null
|
2022-09-09 01:25:31 +00:00
|
|
|
|
2022-01-01 22:13:51 +00:00
|
|
|
if [[ ${provider} =~ ^[^/]+/[^/]+$ ]]; then
|
2022-11-26 08:42:21 +00:00
|
|
|
homepage="https://registry.terraform.io/providers/${provider}"
|
2022-03-13 02:21:21 +00:00
|
|
|
provider=$(basename "${provider}")
|
2022-11-26 08:42:21 +00:00
|
|
|
echo_provider "init"
|
|
|
|
update_attr homepage "${homepage}"
|
2022-03-13 02:21:21 +00:00
|
|
|
# create empty stings so nix-prefetch works
|
2022-09-21 04:59:14 +00:00
|
|
|
update_attr hash ""
|
|
|
|
update_attr vendorHash ""
|
2020-12-01 10:05:00 +00:00
|
|
|
fi
|
2022-03-13 02:21:21 +00:00
|
|
|
|
2022-11-26 08:42:21 +00:00
|
|
|
homepage="$(read_attr homepage)"
|
|
|
|
|
|
|
|
registry_response=$(curl -s "${homepage//providers/v1/providers}")
|
2020-12-01 10:05:00 +00:00
|
|
|
|
2022-11-26 20:54:00 +00:00
|
|
|
old_rev="$(read_attr rev)"
|
2022-11-26 05:27:43 +00:00
|
|
|
rev="$(jq -r '.tag' <<<"${registry_response}")"
|
2022-11-26 20:54:00 +00:00
|
|
|
if [[ ${force} != 1 ]]; then
|
|
|
|
if [[ ${old_rev} == "${rev}" ]]; then
|
|
|
|
echo_provider "already at version ${rev}"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
if [[ ${rev//v/} =~ [[:alpha:]] ]]; then
|
|
|
|
echo_provider "not updating to unstable version ${rev}"
|
|
|
|
exit
|
|
|
|
fi
|
2022-05-16 21:20:14 +00:00
|
|
|
fi
|
2022-11-26 05:27:43 +00:00
|
|
|
echo_provider "updating from ${old_rev} to ${rev}"
|
|
|
|
update_attr rev "${rev}"
|
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}"
|
2022-11-26 22:36:57 +00:00
|
|
|
|
|
|
|
if [[ ${spdx} == 1 ]]; then
|
2023-06-16 03:49:11 +00:00
|
|
|
old_spdx="$(read_attr spdx)"
|
|
|
|
if [[ ${old_spdx} != null ]]; then
|
|
|
|
spdx="$(curl -L -s ${GITHUB_TOKEN:+-u ":${GITHUB_TOKEN}"} "https://api.github.com/repos/${org}/${repo}/license" | jq -r '.license.spdx_id')"
|
|
|
|
update_attr spdx "${spdx}"
|
|
|
|
fi
|
2022-11-26 22:36:57 +00:00
|
|
|
fi
|
|
|
|
|
2022-09-21 04:59:14 +00:00
|
|
|
echo_provider "calculating hash"
|
|
|
|
hash=$(generate_hash src)
|
|
|
|
update_attr hash "${hash}"
|
2020-09-30 20:21:16 +00:00
|
|
|
|
2022-11-26 20:54:00 +00:00
|
|
|
old_vendor_hash="$(read_attr vendorHash)"
|
|
|
|
if [[ ${old_vendor_hash} != null ]]; then
|
|
|
|
echo_provider "calculating vendorHash"
|
|
|
|
vendorHash=$(generate_hash go-modules)
|
|
|
|
update_attr vendorHash "${vendorHash}"
|
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-31 04:48:36 +00:00
|
|
|
if [[ ${build} == 1 ]]; then
|
2022-03-13 02:21:21 +00:00
|
|
|
echo_provider "building"
|
|
|
|
nix-build --no-out-link "${repo_root}" -A "terraform-providers.${provider}"
|
2022-01-31 04:48:36 +00:00
|
|
|
fi
|
2022-09-09 01:25:31 +00:00
|
|
|
|
2022-09-21 04:59:14 +00:00
|
|
|
popd >/dev/null
|