Skip to content

Commit

Permalink
Implement jq templating
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentGoderre committed Oct 11, 2023
1 parent 92c70b4 commit d56d096
Show file tree
Hide file tree
Showing 24 changed files with 337 additions and 75 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/verify-templating.yml
Original file line number Diff line number Diff line change
@@ -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" ]
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.jq-template.awk
6 changes: 6 additions & 0 deletions 6.0/alpine/Dockerfile → 6.0/alpine3.18/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions 6.0/Dockerfile → 6.0/bookworm/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions 6.2/alpine/Dockerfile → 6.2/alpine3.18/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions 6.2/Dockerfile → 6.2/bookworm/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions 7.0/alpine/Dockerfile → 7.0/alpine3.18/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions 7.0/Dockerfile → 7.0/bookworm/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions 7.2/alpine/Dockerfile → 7.2/alpine3.18/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions 7.2/Dockerfile → 7.2/bookworm/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions Dockerfile-alpine.template
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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; \
\
Expand Down
10 changes: 5 additions & 5 deletions Dockerfile.template
Original file line number Diff line number Diff line change
@@ -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; \
Expand All @@ -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; \
\
Expand Down
73 changes: 73 additions & 0 deletions apply-templates.sh
Original file line number Diff line number Diff line change
@@ -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
68 changes: 2 additions & 66 deletions update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$@"
58 changes: 58 additions & 0 deletions versions.json
Original file line number Diff line number Diff line change
@@ -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.13.tar.gz",
"gosu": {
"version": "1.16"
},
"sha256": "89ff27c80d420456a721ccfb3beb7cc628d883c53059803513749e13214a23d1",
"variants": [
"bookworm",
"alpine3.18"
],
"version": "6.2.13"
},
"7.0": {
"alpine": "3.18",
"debian": "bookworm",
"downloadUrl": "http://download.redis.io/releases/redis-7.0.13.tar.gz",
"gosu": {
"version": "1.16"
},
"sha256": "97065774d5fb8388eb0d8913458decfcb167d356e40d31dd01cd30c1cc391673",
"variants": [
"bookworm",
"alpine3.18"
],
"version": "7.0.13"
},
"7.2": {
"alpine": "3.18",
"debian": "bookworm",
"downloadUrl": "http://download.redis.io/releases/redis-7.2.1.tar.gz",
"gosu": {
"version": "1.16"
},
"sha256": "5c76d990a1b1c5f949bcd1eed90d0c8a4f70369bdbdcb40288c561ddf88967a4",
"variants": [
"bookworm",
"alpine3.18"
],
"version": "7.2.1"
}
}
Loading

0 comments on commit d56d096

Please sign in to comment.