From c01ba513bf3a29f5887eb1709fd0a117fc0959c3 Mon Sep 17 00:00:00 2001 From: Laurent Goderre Date: Thu, 5 Oct 2023 16:56:58 -0400 Subject: [PATCH] Implement jq templating --- .github/workflows/verify-templating.yml | 22 ++++ .gitignore | 1 + 6.0/{alpine => alpine3.18}/Dockerfile | 6 + .../docker-entrypoint.sh | 0 6.0/{ => bookworm}/Dockerfile | 6 + 6.0/{ => bookworm}/docker-entrypoint.sh | 0 6.2/{alpine => alpine3.18}/Dockerfile | 6 + .../docker-entrypoint.sh | 0 6.2/{ => bookworm}/Dockerfile | 6 + 6.2/{ => bookworm}/docker-entrypoint.sh | 0 7.0/{alpine => alpine3.18}/Dockerfile | 6 + .../docker-entrypoint.sh | 0 7.0/{ => bookworm}/Dockerfile | 6 + 7.0/{ => bookworm}/docker-entrypoint.sh | 0 7.2/{alpine => alpine3.18}/Dockerfile | 6 + .../docker-entrypoint.sh | 0 7.2/{ => bookworm}/Dockerfile | 6 + 7.2/{ => bookworm}/docker-entrypoint.sh | 0 Dockerfile-alpine.template | 8 +- Dockerfile.template | 10 +- apply-templates.sh | 73 +++++++++++ generate-stackbrew-library.sh | 85 ++++++------ update.sh | 68 +--------- versions.json | 58 ++++++++ versions.sh | 124 ++++++++++++++++++ 25 files changed, 380 insertions(+), 117 deletions(-) create mode 100644 .github/workflows/verify-templating.yml create mode 100644 .gitignore rename 6.0/{alpine => alpine3.18}/Dockerfile (97%) rename 6.0/{alpine => alpine3.18}/docker-entrypoint.sh (100%) rename 6.0/{ => bookworm}/Dockerfile (98%) rename 6.0/{ => bookworm}/docker-entrypoint.sh (100%) rename 6.2/{alpine => alpine3.18}/Dockerfile (97%) rename 6.2/{alpine => alpine3.18}/docker-entrypoint.sh (100%) rename 6.2/{ => bookworm}/Dockerfile (98%) rename 6.2/{ => bookworm}/docker-entrypoint.sh (100%) rename 7.0/{alpine => alpine3.18}/Dockerfile (97%) rename 7.0/{alpine => alpine3.18}/docker-entrypoint.sh (100%) rename 7.0/{ => bookworm}/Dockerfile (98%) rename 7.0/{ => bookworm}/docker-entrypoint.sh (100%) rename 7.2/{alpine => alpine3.18}/Dockerfile (97%) rename 7.2/{alpine => alpine3.18}/docker-entrypoint.sh (100%) rename 7.2/{ => bookworm}/Dockerfile (98%) rename 7.2/{ => bookworm}/docker-entrypoint.sh (100%) create mode 100755 apply-templates.sh create mode 100644 versions.json create mode 100755 versions.sh diff --git a/.github/workflows/verify-templating.yml b/.github/workflows/verify-templating.yml new file mode 100644 index 00000000..14497bec --- /dev/null +++ b/.github/workflows/verify-templating.yml @@ -0,0 +1,22 @@ +name: Verify Templating + +on: + pull_request: + push: + +defaults: + run: + shell: 'bash -Eeuo pipefail -x {0}' + +jobs: + apply-templates: + name: Check For Uncomitted Changes + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Apply Templates + run: ./apply-templates.sh + - name: Check Git Status + run: | + status="$(git status --short)" + [ -z "$status" ] diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..d548f66d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.jq-template.awk diff --git a/6.0/alpine/Dockerfile b/6.0/alpine3.18/Dockerfile similarity index 97% rename from 6.0/alpine/Dockerfile rename to 6.0/alpine3.18/Dockerfile index 83bbcd2b..0b49d622 100644 --- a/6.0/alpine/Dockerfile +++ b/6.0/alpine3.18/Dockerfile @@ -1,3 +1,9 @@ +# +# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh" +# +# PLEASE DO NOT EDIT IT DIRECTLY. +# + FROM alpine:3.18 # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added diff --git a/6.0/alpine/docker-entrypoint.sh b/6.0/alpine3.18/docker-entrypoint.sh similarity index 100% rename from 6.0/alpine/docker-entrypoint.sh rename to 6.0/alpine3.18/docker-entrypoint.sh diff --git a/6.0/Dockerfile b/6.0/bookworm/Dockerfile similarity index 98% rename from 6.0/Dockerfile rename to 6.0/bookworm/Dockerfile index 7eb87156..8bb14991 100644 --- a/6.0/Dockerfile +++ b/6.0/bookworm/Dockerfile @@ -1,3 +1,9 @@ +# +# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh" +# +# PLEASE DO NOT EDIT IT DIRECTLY. +# + FROM debian:bookworm-slim # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added diff --git a/6.0/docker-entrypoint.sh b/6.0/bookworm/docker-entrypoint.sh similarity index 100% rename from 6.0/docker-entrypoint.sh rename to 6.0/bookworm/docker-entrypoint.sh diff --git a/6.2/alpine/Dockerfile b/6.2/alpine3.18/Dockerfile similarity index 97% rename from 6.2/alpine/Dockerfile rename to 6.2/alpine3.18/Dockerfile index fd29b568..57a9d900 100644 --- a/6.2/alpine/Dockerfile +++ b/6.2/alpine3.18/Dockerfile @@ -1,3 +1,9 @@ +# +# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh" +# +# PLEASE DO NOT EDIT IT DIRECTLY. +# + FROM alpine:3.18 # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added diff --git a/6.2/alpine/docker-entrypoint.sh b/6.2/alpine3.18/docker-entrypoint.sh similarity index 100% rename from 6.2/alpine/docker-entrypoint.sh rename to 6.2/alpine3.18/docker-entrypoint.sh diff --git a/6.2/Dockerfile b/6.2/bookworm/Dockerfile similarity index 98% rename from 6.2/Dockerfile rename to 6.2/bookworm/Dockerfile index 8497d1be..a17a1928 100644 --- a/6.2/Dockerfile +++ b/6.2/bookworm/Dockerfile @@ -1,3 +1,9 @@ +# +# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh" +# +# PLEASE DO NOT EDIT IT DIRECTLY. +# + FROM debian:bookworm-slim # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added diff --git a/6.2/docker-entrypoint.sh b/6.2/bookworm/docker-entrypoint.sh similarity index 100% rename from 6.2/docker-entrypoint.sh rename to 6.2/bookworm/docker-entrypoint.sh diff --git a/7.0/alpine/Dockerfile b/7.0/alpine3.18/Dockerfile similarity index 97% rename from 7.0/alpine/Dockerfile rename to 7.0/alpine3.18/Dockerfile index 834cbceb..678127ed 100644 --- a/7.0/alpine/Dockerfile +++ b/7.0/alpine3.18/Dockerfile @@ -1,3 +1,9 @@ +# +# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh" +# +# PLEASE DO NOT EDIT IT DIRECTLY. +# + FROM alpine:3.18 # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added diff --git a/7.0/alpine/docker-entrypoint.sh b/7.0/alpine3.18/docker-entrypoint.sh similarity index 100% rename from 7.0/alpine/docker-entrypoint.sh rename to 7.0/alpine3.18/docker-entrypoint.sh diff --git a/7.0/Dockerfile b/7.0/bookworm/Dockerfile similarity index 98% rename from 7.0/Dockerfile rename to 7.0/bookworm/Dockerfile index 170e5242..34a4d590 100644 --- a/7.0/Dockerfile +++ b/7.0/bookworm/Dockerfile @@ -1,3 +1,9 @@ +# +# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh" +# +# PLEASE DO NOT EDIT IT DIRECTLY. +# + FROM debian:bookworm-slim # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added diff --git a/7.0/docker-entrypoint.sh b/7.0/bookworm/docker-entrypoint.sh similarity index 100% rename from 7.0/docker-entrypoint.sh rename to 7.0/bookworm/docker-entrypoint.sh diff --git a/7.2/alpine/Dockerfile b/7.2/alpine3.18/Dockerfile similarity index 97% rename from 7.2/alpine/Dockerfile rename to 7.2/alpine3.18/Dockerfile index d7a78959..9e18699b 100644 --- a/7.2/alpine/Dockerfile +++ b/7.2/alpine3.18/Dockerfile @@ -1,3 +1,9 @@ +# +# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh" +# +# PLEASE DO NOT EDIT IT DIRECTLY. +# + FROM alpine:3.18 # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added diff --git a/7.2/alpine/docker-entrypoint.sh b/7.2/alpine3.18/docker-entrypoint.sh similarity index 100% rename from 7.2/alpine/docker-entrypoint.sh rename to 7.2/alpine3.18/docker-entrypoint.sh diff --git a/7.2/Dockerfile b/7.2/bookworm/Dockerfile similarity index 98% rename from 7.2/Dockerfile rename to 7.2/bookworm/Dockerfile index 7e45f211..b073f523 100644 --- a/7.2/Dockerfile +++ b/7.2/bookworm/Dockerfile @@ -1,3 +1,9 @@ +# +# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh" +# +# PLEASE DO NOT EDIT IT DIRECTLY. +# + FROM debian:bookworm-slim # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added diff --git a/7.2/docker-entrypoint.sh b/7.2/bookworm/docker-entrypoint.sh similarity index 100% rename from 7.2/docker-entrypoint.sh rename to 7.2/bookworm/docker-entrypoint.sh diff --git a/Dockerfile-alpine.template b/Dockerfile-alpine.template index 4762895e..a9e5efa9 100644 --- a/Dockerfile-alpine.template +++ b/Dockerfile-alpine.template @@ -1,4 +1,4 @@ -FROM alpine:3.18 +FROM alpine:{{ env.variant | ltrimstr("alpine") }} # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added RUN addgroup -S -g 1000 redis && adduser -S -G redis -u 999 redis @@ -10,9 +10,9 @@ RUN apk add --no-cache \ # add tzdata for https://github.com/docker-library/redis/issues/138 tzdata -ENV REDIS_VERSION placeholder -ENV REDIS_DOWNLOAD_URL placeholder -ENV REDIS_DOWNLOAD_SHA placeholder +ENV REDIS_VERSION {{ .version }} +ENV REDIS_DOWNLOAD_URL {{ .downloadUrl }} +ENV REDIS_DOWNLOAD_SHA {{ .sha256 }} RUN set -eux; \ \ diff --git a/Dockerfile.template b/Dockerfile.template index 5f0e9db2..31f2a065 100644 --- a/Dockerfile.template +++ b/Dockerfile.template @@ -1,11 +1,11 @@ -FROM debian:bookworm-slim +FROM debian:{{ env.variant }}-slim # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added RUN groupadd -r -g 999 redis && useradd -r -g redis -u 999 redis # grab gosu for easy step-down from root # https://github.com/tianon/gosu/releases -ENV GOSU_VERSION 1.16 +ENV GOSU_VERSION {{ .gosu.version }} RUN set -eux; \ savedAptMark="$(apt-mark showmanual)"; \ apt-get update; \ @@ -26,9 +26,9 @@ RUN set -eux; \ gosu --version; \ gosu nobody true -ENV REDIS_VERSION placeholder -ENV REDIS_DOWNLOAD_URL placeholder -ENV REDIS_DOWNLOAD_SHA placeholder +ENV REDIS_VERSION {{ .version }} +ENV REDIS_DOWNLOAD_URL {{ .downloadUrl }} +ENV REDIS_DOWNLOAD_SHA {{ .sha256 }} RUN set -eux; \ \ diff --git a/apply-templates.sh b/apply-templates.sh new file mode 100755 index 00000000..2edf95f8 --- /dev/null +++ b/apply-templates.sh @@ -0,0 +1,73 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +[ -f versions.json ] # run "versions.sh" first + +cd "$(dirname "$(readlink -f "$BASH_SOURCE")")" + +jqt='.jq-template.awk' +if [ -n "${BASHBREW_SCRIPTS:-}" ]; then + jqt="$BASHBREW_SCRIPTS/jq-template.awk" +elif [ "$BASH_SOURCE" -nt "$jqt" ]; then + # https://github.com/docker-library/bashbrew/blob/master/scripts/jq-template.awk + wget -qO "$jqt" 'https://github.com/docker-library/bashbrew/raw/9f6a35772ac863a0241f147c820354e4008edf38/scripts/jq-template.awk' +fi + +if [ "$#" -eq 0 ]; then + versions="$(jq -r 'keys | map(@sh) | join(" ")' versions.json)" + eval "set -- $versions" +fi + +generated_warning() { + cat <<-EOH + # + # NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh" + # + # PLEASE DO NOT EDIT IT DIRECTLY. + # + + EOH +} + +for version; do + export version + + if [ -d "$version" ]; then + rm -rf "$version" + fi + + if jq -e '.[env.version] | not' versions.json > /dev/null; then + echo "skipping $version ..." + continue + fi + + variants="$(jq -r '.[env.version].variants | map(@sh) | join(" ")' versions.json)" + eval "variants=( $variants )" + + for variant in "${variants[@]}"; do + export variant + + echo "processing $version/$variant ..." + + dir="$version${variant:+/$variant}" + + mkdir -p "$dir" + + cp -f docker-entrypoint.sh "$dir/" + + case "$variant" in + alpine*) + template='Dockerfile-alpine.template' + sed -i -e 's/gosu/su-exec/g' "$dir/docker-entrypoint.sh" + ;; + *) + template='Dockerfile.template' + ;; + esac + + { + generated_warning + gawk -f "$jqt" "$template" + } > "$dir/Dockerfile" + done +done diff --git a/generate-stackbrew-library.sh b/generate-stackbrew-library.sh index 42d47532..3ad3d694 100755 --- a/generate-stackbrew-library.sh +++ b/generate-stackbrew-library.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash set -eu declare -A aliases=( @@ -9,11 +9,13 @@ declare -A aliases=( self="$(basename "$BASH_SOURCE")" cd "$(dirname "$(readlink -f "$BASH_SOURCE")")" -versions=( */ ) -versions=( "${versions[@]%/}" ) +if [ "$#" -eq 0 ]; then + versions="$(jq -r 'keys | map(@sh) | join(" ")' versions.json)" + eval "set -- $versions" +fi # sort version numbers with highest first -IFS=$'\n'; versions=( $(echo "${versions[*]}" | sort -rV) ); unset IFS +IFS=$'\n'; set -- $(sort -rV <<<"$*"); unset IFS # get the most recent commit which modified any of "$@" fileCommit() { @@ -68,51 +70,50 @@ join() { echo "${out#$sep}" } -for version in "${versions[@]}"; do - for v in \ - '' alpine \ - ; do - dir="$version${v:+/$v}" - variant="$(basename "$v")" +for version; do + export version - [ -f "$dir/Dockerfile" ] || continue + variants="$(jq -r '.[env.version].variants | map(@sh) | join(" ")' versions.json)" + eval "variants=( $variants )" - commit="$(dirCommit "$dir")" + alpine="$(jq -r '.[env.version].alpine' versions.json)" + debian="$(jq -r '.[env.version].debian' versions.json)" - fullVersion="$(git show "$commit":"$dir/Dockerfile" | awk '$1 == "ENV" && $2 == "REDIS_VERSION" { print $3; exit }')" - - versionAliases=() - while [ "$fullVersion" != "$version" -a "${fullVersion%[.]*}" != "$fullVersion" ]; do - versionAliases+=( $fullVersion ) - fullVersion="${fullVersion%[.]*}" - done - versionAliases+=( - $version - ${aliases[$version]:-} - ) - - if [ -n "$variant" ]; then - variantAliases=( "${versionAliases[@]/%/-$variant}" ) - variantAliases=( "${variantAliases[@]//latest-/}" ) - else - variantAliases=( "${versionAliases[@]}" ) - fi + fullVersion="$(jq -r '.[env.version].version' versions.json)" - variantParent="$(awk 'toupper($1) == "FROM" { print $2 }' "$dir/Dockerfile")" + versionAliases=() + while [ "$fullVersion" != "$version" -a "${fullVersion%[.]*}" != "$fullVersion" ]; do + versionAliases+=( $fullVersion ) + fullVersion="${fullVersion%[.]*}" + done + versionAliases+=( + $version + ${aliases[$version]:-} + ) - suite="${variantParent#*:}" # "jessie-slim", "stretch" - suite="${suite%-slim}" # "jessie", "stretch" - - if [ "$v" = 'alpine' ]; then - suite="alpine$suite" # "alpine3.8" - suiteAliases=( "${versionAliases[@]/%/-$suite}" ) - else - suiteAliases=( "${variantAliases[@]/%/-$suite}" ) - fi - suiteAliases=( "${suiteAliases[@]//latest-/}" ) - variantAliases+=( "${suiteAliases[@]}" ) + for variant in "${variants[@]}"; do + dir="$version/$variant" + commit="$(dirCommit "$dir")" + + variantParent="$(awk 'toupper($1) == "FROM" { print $2 }' "$dir/Dockerfile")" variantArches="${parentRepoToArches[$variantParent]}" + variantAliases=( "${versionAliases[@]/%/-$variant}" ) + variantAliases=( "${variantAliases[@]//latest-/}" ) + + case "$variant" in + "$debian") + variantAliases=( + "${versionAliases[@]}" + "${variantAliases[@]}" + ) + ;; + alpine"$alpine") + variantAliases+=( "${versionAliases[@]/%/-alpine}" ) + variantAliases=( "${variantAliases[@]//latest-/}" ) + ;; + esac + echo cat <<-EOE Tags: $(join ', ' "${variantAliases[@]}") diff --git a/update.sh b/update.sh index 7a306d98..bac2d758 100755 --- a/update.sh +++ b/update.sh @@ -3,69 +3,5 @@ set -Eeuo pipefail cd "$(dirname "$(readlink -f "$BASH_SOURCE")")" -versions=( "$@" ) -if [ ${#versions[@]} -eq 0 ]; then - versions=( */ ) -fi -versions=( "${versions[@]%/}" ) - -packagesUrl='https://raw.githubusercontent.com/redis/redis-hashes/master/README' -packages="$(echo "$packagesUrl" | sed -r 's/[^a-zA-Z.-]+/-/g')" -trap "$(printf 'rm -f %q' "$packages")" EXIT -curl -fsSL "$packagesUrl" -o "$packages" - -for version in "${versions[@]}"; do - rcVersion="${version%-rc}" - - line="$( - awk ' - { gsub(/^redis-|[.]tar[.]gz$/, "", $2) } - $1 == "hash" && $2 ~ /^'"$rcVersion"'([.]|$)/ { print } - ' "$packages" \ - | sort -rV \ - | head -1 - )" - - if [ -n "$line" ]; then - fullVersion="$(cut -d' ' -f2 <<<"$line")" - downloadUrl="$(cut -d' ' -f5 <<<"$line")" - shaHash="$(cut -d' ' -f4 <<<"$line")" - shaType="$(cut -d' ' -f3 <<<"$line")" - elif [ "$version" != "$rcVersion" ] && fullVersion="$( - git ls-remote --tags https://github.com/redis/redis.git "refs/tags/$rcVersion*" \ - | cut -d/ -f3 \ - | cut -d^ -f1 \ - | sort -urV \ - | head -1 - )" && [ -n "$fullVersion" ]; then - downloadUrl="https://github.com/redis/redis/archive/$fullVersion.tar.gz" - shaType='sha256' - shaHash="$(curl -fsSL "$downloadUrl" | "${shaType}sum" | cut -d' ' -f1)" - else - echo >&2 "error: full version for $version cannot be determined" - exit 1 - fi - [ "$shaType" = 'sha256' ] || [ "$shaType" = 'sha1' ] - - echo "$version: $fullVersion" - - for variant in \ - alpine '' \ - ; do - dir="$version${variant:+/$variant}" - [ -d "$dir" ] || continue - template="Dockerfile${variant:+-$variant}.template" - - sed -r \ - -e 's/^(ENV REDIS_VERSION) .*/\1 '"$fullVersion"'/' \ - -e 's!^(ENV REDIS_DOWNLOAD_URL) .*!\1 '"$downloadUrl"'!' \ - -e 's/^(ENV REDIS_DOWNLOAD_SHA) .*/\1 '"$shaHash"'/' \ - -e 's!sha[0-9]+sum!'"$shaType"'sum!g' \ - "$template" > "$dir/Dockerfile" - - cp -a docker-entrypoint.sh "$dir/" - if [ "$variant" = 'alpine' ]; then - sed -i -e 's/gosu/su-exec/g' "$dir/docker-entrypoint.sh" - fi - done -done +./versions.sh "$@" +./apply-templates.sh "$@" diff --git a/versions.json b/versions.json new file mode 100644 index 00000000..832a3df3 --- /dev/null +++ b/versions.json @@ -0,0 +1,58 @@ +{ + "6.0": { + "alpine": "3.18", + "debian": "bookworm", + "downloadUrl": "http://download.redis.io/releases/redis-6.0.20.tar.gz", + "gosu": { + "version": "1.16" + }, + "sha256": "173d4c5f44b5d7186da96c4adc5cb20e8018b50ec3a8dfe0d191dbbab53952f0", + "variants": [ + "bookworm", + "alpine3.18" + ], + "version": "6.0.20" + }, + "6.2": { + "alpine": "3.18", + "debian": "bookworm", + "downloadUrl": "http://download.redis.io/releases/redis-6.2.14.tar.gz", + "gosu": { + "version": "1.16" + }, + "sha256": "34e74856cbd66fdb3a684fb349d93961d8c7aa668b06f81fd93ff267d09bc277", + "variants": [ + "bookworm", + "alpine3.18" + ], + "version": "6.2.14" + }, + "7.0": { + "alpine": "3.18", + "debian": "bookworm", + "downloadUrl": "http://download.redis.io/releases/redis-7.0.14.tar.gz", + "gosu": { + "version": "1.16" + }, + "sha256": "7e1cdf347f4970ea39d5b7fdb19aedec4c21942e202de65bdeb782d38d2f299f", + "variants": [ + "bookworm", + "alpine3.18" + ], + "version": "7.0.14" + }, + "7.2": { + "alpine": "3.18", + "debian": "bookworm", + "downloadUrl": "http://download.redis.io/releases/redis-7.2.2.tar.gz", + "gosu": { + "version": "1.16" + }, + "sha256": "ca999be08800edc6d265379c4c7aafad92f0ee400692e4e2d69829ab4b4c3d08", + "variants": [ + "bookworm", + "alpine3.18" + ], + "version": "7.2.2" + } +} diff --git a/versions.sh b/versions.sh new file mode 100755 index 00000000..3aabc536 --- /dev/null +++ b/versions.sh @@ -0,0 +1,124 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +# we will support at most two entries in each of these lists, and both should be in descending order +supportedDebianSuites=( + bookworm +) +supportedAlpineVersions=( + 3.18 +) +defaultDebianSuite="${supportedDebianSuites[0]}" +declare -A debianSuites=( + #[7.2]='3.17' +) +defaultAlpineVersion="${supportedAlpineVersions[0]}" +declare -A alpineVersions=( + #[14]='3.16' +) + +gosuVersion='1.16' + +cd "$(dirname "$(readlink -f "$BASH_SOURCE")")" + +versions=( "$@" ) +if [ ${#versions[@]} -eq 0 ]; then + versions=( */ ) + json='{}' +else + json="$(< versions.json)" +fi +versions=( "${versions[@]%/}" ) + +packagesBase='https://raw.githubusercontent.com/redis/redis-hashes/master/README' + +declare -A packages= + +fetch_package_list() { + local -; set +x # make sure running with "set -x" doesn't spam the terminal with the raw package lists + + # normal (GA) releases end up in the "main" component of upstream's repository + if [ -z "${packages}" ]; then + packages="$(curl -fsSL "$packagesBase")" + fi +} +get_version() { + local version="$1"; shift + + rcVersion="${version%-rc}" + + line="$( + awk ' + { gsub(/^redis-|[.]tar[.]gz$/, "", $2) } + $1 == "hash" && $2 ~ /^'"$rcVersion"'([.]|$)/ { print } + ' <<< "$packages" \ + | sort -rV \ + | head -1 + )" + + if [ -n "$line" ]; then + fullVersion="$(cut -d' ' -f2 <<<"$line")" + downloadUrl="$(cut -d' ' -f5 <<<"$line")" + shaHash="$(cut -d' ' -f4 <<<"$line")" + shaType="$(cut -d' ' -f3 <<<"$line")" + elif [ "$version" != "$rcVersion" ] && fullVersion="$( + git ls-remote --tags https://github.com/redis/redis.git "refs/tags/$rcVersion*" \ + | cut -d/ -f3 \ + | cut -d^ -f1 \ + | sort -urV \ + | head -1 + )" && [ -n "$fullVersion" ]; then + downloadUrl="https://github.com/redis/redis/archive/$fullVersion.tar.gz" + shaType='sha256' + shaHash="$(curl -fsSL "$downloadUrl" | "${shaType}sum" | cut -d' ' -f1)" + else + echo >&2 "error: full version for $version cannot be determined" + exit 1 + fi + [ "$shaType" = 'sha256' ] || [ "$shaType" = 'sha1' ] +} + +for version in "${versions[@]}"; do + export version + + versionAlpineVersion="${alpineVersions[$version]:-$defaultAlpineVersion}" + versionDebianSuite="${debianSuites[$version]:-$defaultDebianSuite}" + export versionAlpineVersion versionDebianSuite + + doc="$(jq -nc '{ + alpine: env.versionAlpineVersion, + debian: env.versionDebianSuite, + }')" + + fetch_package_list + get_version "$version" + + for suite in "${supportedDebianSuites[@]}"; do + export suite + doc="$(jq <<<"$doc" -c ' + .variants += [ env.suite ] + ')" + done + + for alpineVersion in "${supportedAlpineVersions[@]}"; do + doc="$(jq <<<"$doc" -c --arg v "$alpineVersion" ' + .variants += [ "alpine" + $v ] + ')" + done + + echo "$version: $fullVersion" + + export fullVersion shaType shaHash downloadUrl gosuVersion + json="$(jq <<<"$json" -c --argjson doc "$doc" ' + .[env.version] = ($doc + { + version: env.fullVersion, + downloadUrl: env.downloadUrl, + (env.shaType): env.shaHash, + "gosu": { + version: env.gosuVersion + } + }) + ')" +done + +jq <<<"$json" -S . > versions.json