2017-09-01 15:49:24 +01:00
|
|
|
#!/usr/bin/env nix-shell
|
|
|
|
#! nix-shell -i bash -p bash coreutils curl jq nix
|
|
|
|
# vim: ft=sh sw=2 et
|
|
|
|
#
|
|
|
|
# This scripts scans the github terraform-providers repo for new releases,
|
|
|
|
# generates the corresponding nix code and finally generates an index of
|
2018-09-19 04:24:21 +01:00
|
|
|
# all the providers given in ./providers.txt.
|
2017-09-01 15:49:24 +01:00
|
|
|
set -euo pipefail
|
|
|
|
|
2018-09-18 21:25:43 +01:00
|
|
|
# the maximum number of attempts before giving up inside of GET and prefetch_github
|
|
|
|
readonly maxAttempts=30
|
|
|
|
|
2017-09-01 15:49:24 +01:00
|
|
|
GET() {
|
|
|
|
local url=$1
|
2018-09-18 21:25:43 +01:00
|
|
|
local retry=1
|
2017-09-01 15:49:24 +01:00
|
|
|
echo "fetching $url" >&2
|
2018-09-18 21:25:43 +01:00
|
|
|
while ! curl -#fL -u "$GITHUB_AUTH" "$url"; do
|
|
|
|
echo "The curl command has failed. Attempt $retry/${maxAttempts}" >&2
|
|
|
|
if [[ "${retry}" -eq "${maxAttempts}" ]]; then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
retry=$(( retry + 1 ))
|
|
|
|
sleep 5
|
|
|
|
done
|
2017-09-01 15:49:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get_org_repos() {
|
|
|
|
local org=$1
|
2017-09-08 19:36:43 +01:00
|
|
|
local page=1
|
|
|
|
GET "https://api.github.com/orgs/$org/repos?per_page=100" | jq -r '.[].name'
|
2017-09-01 15:49:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get_repo_tags() {
|
|
|
|
local owner=$1
|
|
|
|
local repo=$2
|
2017-09-08 19:36:43 +01:00
|
|
|
GET "https://api.github.com/repos/$owner/$repo/git/refs/tags?per_page=100" | \
|
2017-09-01 15:49:24 +01:00
|
|
|
jq -r '.[].ref' | \
|
2018-09-18 21:25:43 +01:00
|
|
|
grep -v 'v\.' | \
|
2017-09-01 15:49:24 +01:00
|
|
|
cut -d '/' -f 3- | \
|
|
|
|
sort --version-sort
|
|
|
|
}
|
|
|
|
|
|
|
|
prefetch_github() {
|
|
|
|
local owner=$1
|
|
|
|
local repo=$2
|
|
|
|
local rev=$3
|
2018-09-18 21:25:43 +01:00
|
|
|
local retry=1
|
|
|
|
while ! nix-prefetch-url --unpack "https://github.com/$owner/$repo/archive/$rev.tar.gz"; do
|
|
|
|
echo "The nix-prefetch-url command has failed. Attempt $retry/${maxAttempts}" >&2
|
|
|
|
if [[ "${retry}" -eq "${maxAttempts}" ]]; then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
retry=$(( retry + 1 ))
|
|
|
|
sleep 5
|
|
|
|
done
|
2017-09-01 15:49:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
echo_entry() {
|
|
|
|
local owner=$1
|
|
|
|
local repo=$2
|
|
|
|
local version=${3:1}
|
2017-09-08 19:36:43 +01:00
|
|
|
local sha256=$4
|
2017-09-01 15:49:24 +01:00
|
|
|
cat <<EOF
|
|
|
|
{
|
2017-09-08 19:36:43 +01:00
|
|
|
owner = "$owner";
|
|
|
|
repo = "$repo";
|
2017-09-01 15:49:24 +01:00
|
|
|
version = "$version";
|
2017-09-08 19:36:43 +01:00
|
|
|
sha256 = "$sha256";
|
2017-09-01 15:49:24 +01:00
|
|
|
};
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
indent() { sed 's/^/ /'; }
|
|
|
|
|
2018-09-18 21:27:04 +01:00
|
|
|
add_repo() {
|
|
|
|
org="${1}"
|
|
|
|
repo="${2}"
|
|
|
|
|
|
|
|
echo "*** $repo ***"
|
|
|
|
name=$(echo "$repo" | cut -d - -f 3-)
|
|
|
|
last_tag=$(get_repo_tags "$org" "$repo" | tail -1)
|
|
|
|
last_tag_sha256=$(prefetch_github "$org" "$repo" "$last_tag")
|
|
|
|
|
|
|
|
{
|
|
|
|
echo " $name ="
|
|
|
|
echo_entry "$org" "$repo" "$last_tag" "$last_tag_sha256" | indent
|
|
|
|
} >> data.nix
|
|
|
|
}
|
|
|
|
|
2017-09-08 19:36:43 +01:00
|
|
|
## Main ##
|
|
|
|
|
|
|
|
cd "$(dirname "$0")"
|
|
|
|
|
|
|
|
if [[ -z "${GITHUB_AUTH:-}" ]]; then
|
|
|
|
cat <<'HELP'
|
2018-05-02 19:02:18 +01:00
|
|
|
Missing the GITHUB_AUTH env. This is required to work around the 60 request
|
2017-09-08 19:36:43 +01:00
|
|
|
per hour rate-limit.
|
|
|
|
|
|
|
|
Go to https://github.com/settings/tokens and create a new token with the
|
|
|
|
"public_repo" scope.
|
|
|
|
|
|
|
|
Then `export GITHUB_AUTH=<your user>:<your token>` and run this script again.
|
|
|
|
HELP
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
cat <<HEADER > data.nix
|
|
|
|
# Generated with ./update-all
|
|
|
|
{
|
|
|
|
HEADER
|
2017-09-01 15:49:24 +01:00
|
|
|
|
2018-09-19 04:24:21 +01:00
|
|
|
while read line; do
|
|
|
|
IFS=' ' read -r -a fields <<< "$line"
|
|
|
|
if [[ "${#fields[@]}" -eq 0 ]]; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "${fields[0]}" = *"/"* ]]; then
|
|
|
|
org="$(echo "${fields[0]}" | cut -d/ -f1)"
|
|
|
|
repo="$(echo "${fields[0]}" | cut -d/ -f2)"
|
|
|
|
add_repo "${org}" "${repo}"
|
|
|
|
else
|
|
|
|
org="${fields[0]}"
|
|
|
|
repos=$(get_org_repos "$org")
|
|
|
|
if [[ "${#fields[@]}" -ge 2 ]] && [[ -n "${fields[1]}" ]]; then
|
|
|
|
repos="$( echo "${repos[@]}" | grep "${fields[1]}" )"
|
|
|
|
fi
|
|
|
|
if [[ "${#fields[@]}" -eq 3 ]] && [[ -n "${fields[2]}" ]]; then
|
|
|
|
repos="$( echo "${repos[@]}" | grep -v "${fields[2]}" )"
|
|
|
|
fi
|
|
|
|
repos="$( echo "${repos[@]}" | sort )"
|
2017-09-08 19:36:43 +01:00
|
|
|
|
2018-09-19 04:24:21 +01:00
|
|
|
for repo in $repos; do
|
|
|
|
add_repo "$org" "$repo"
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
done < <(grep -v '^#\|^$' providers.txt)
|
2018-09-18 21:27:04 +01:00
|
|
|
|
2017-09-08 19:36:43 +01:00
|
|
|
cat <<FOOTER >> data.nix
|
|
|
|
}
|
|
|
|
FOOTER
|
2017-09-01 15:49:24 +01:00
|
|
|
|
|
|
|
echo Done.
|