Skip to content

Implement jq templating #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
76 changes: 76 additions & 0 deletions 1.6/alpine3.18/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#
# 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
RUN addgroup -g 11211 memcache && adduser -D -u 11211 -G memcache memcache

# ensure SASL's "libplain.so" is installed as per https://github.com/memcached/memcached/wiki/SASLHowto
RUN apk add --no-cache libsasl

ENV MEMCACHED_VERSION 1.6.22
ENV MEMCACHED_DOWNLOAD_URL https://memcached.org/files/memcached-1.6.22.tar.gz
ENV MEMCACHED_SHA1 7a691f390d59616dbebfc9e2e4942d499c39a338

RUN set -x \
\
&& apk add --no-cache --virtual .build-deps \
ca-certificates \
coreutils \
cyrus-sasl-dev \
gcc \
libc-dev \
libevent-dev \
linux-headers \
make \
openssl \
openssl-dev \
perl \
perl-io-socket-ssl \
perl-utils \
\
&& wget -O memcached.tar.gz "$MEMCACHED_DOWNLOAD_URL" \
&& echo "$MEMCACHED_SHA1 memcached.tar.gz" | sha1sum -c - \
&& mkdir -p /usr/src/memcached \
&& tar -xzf memcached.tar.gz -C /usr/src/memcached --strip-components=1 \
&& rm memcached.tar.gz \
\
&& cd /usr/src/memcached \
\
&& ./configure \
--build="$gnuArch" \
--enable-extstore \
--enable-sasl \
--enable-sasl-pwdb \
--enable-tls \
&& nproc="$(nproc)" \
&& make -j "$nproc" \
\
&& make test PARALLEL="$nproc" \
\
&& make install \
\
&& cd / && rm -rf /usr/src/memcached \
\
&& runDeps="$( \
scanelf --needed --nobanner --format '%n#p' --recursive /usr/local \
| tr ',' '\n' \
| sort -u \
| awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
)" \
&& apk add --no-network --virtual .memcached-rundeps $runDeps \
&& apk del --no-network .build-deps \
\
&& memcached -V

COPY docker-entrypoint.sh /usr/local/bin/
RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat
ENTRYPOINT ["docker-entrypoint.sh"]

USER memcache
EXPOSE 11211
CMD ["memcached"]
File renamed without changes.
96 changes: 96 additions & 0 deletions 1.6/bookworm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#
# 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
RUN groupadd --system --gid 11211 memcache && useradd --system --gid memcache --uid 11211 memcache

# ensure SASL's "libplain.so" is installed as per https://github.com/memcached/memcached/wiki/SASLHowto
RUN set -eux; \
apt-get update; \
apt-get install -y --no-install-recommends \
libsasl2-modules \
; \
rm -rf /var/lib/apt/lists/*

ENV MEMCACHED_VERSION 1.6.22
ENV MEMCACHED_DOWNLOAD_URL https://memcached.org/files/memcached-1.6.22.tar.gz
ENV MEMCACHED_SHA1 7a691f390d59616dbebfc9e2e4942d499c39a338

RUN set -x \
\
&& savedAptMark="$(apt-mark showmanual)" \
&& apt-get update \
&& apt-get install -y --no-install-recommends \
ca-certificates \
dpkg-dev \
gcc \
libc6-dev \
libevent-dev \
libio-socket-ssl-perl \
libsasl2-dev \
libssl-dev \
make \
perl \
wget \
&& rm -rf /var/lib/apt/lists/* \
\
&& wget -O memcached.tar.gz "$MEMCACHED_DOWNLOAD_URL" \
&& echo "$MEMCACHED_SHA1 memcached.tar.gz" | sha1sum -c - \
&& mkdir -p /usr/src/memcached \
&& tar -xzf memcached.tar.gz -C /usr/src/memcached --strip-components=1 \
&& rm memcached.tar.gz \
\
&& cd /usr/src/memcached \
\
&& gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)" \
&& enableExtstore="$( \
# https://github.com/docker-library/memcached/pull/38
case "$gnuArch" in \
# https://github.com/memcached/memcached/issues/381 "--enable-extstore on s390x (IBM System Z mainframe architecture) fails tests"
s390x-*) ;; \
*) echo '--enable-extstore' ;; \
esac \
)" \
&& ./configure \
--build="$gnuArch" \
--enable-sasl \
--enable-sasl-pwdb \
--enable-tls \
$enableExtstore \
&& nproc="$(nproc)" \
&& make -j "$nproc" \
\
# see https://github.com/docker-library/memcached/pull/54#issuecomment-562797748 and https://bugs.debian.org/927461 for why we have to munge openssl.cnf
&& sed -i.bak 's/SECLEVEL=2/SECLEVEL=1/g' /etc/ssl/openssl.cnf \
&& make test PARALLEL="$nproc" \
&& mv /etc/ssl/openssl.cnf.bak /etc/ssl/openssl.cnf \
\
&& make install \
\
&& cd / && rm -rf /usr/src/memcached \
\
&& apt-mark auto '.*' > /dev/null \
&& apt-mark manual $savedAptMark > /dev/null \
&& find /usr/local -type f -executable -exec ldd '{}' ';' \
| awk '/=>/ { so = $(NF-1); if (index(so, "/usr/local/") == 1) { next }; gsub("^/(usr/)?", "", so); print so }' \
| sort -u \
| xargs -r dpkg-query --search \
| cut -d: -f1 \
| sort -u \
| xargs -r apt-mark manual \
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
\
&& memcached -V

COPY docker-entrypoint.sh /usr/local/bin/
RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat
ENTRYPOINT ["docker-entrypoint.sh"]

USER memcache
EXPOSE 11211
CMD ["memcached"]
File renamed without changes.
9 changes: 5 additions & 4 deletions alpine/Dockerfile → Dockerfile-alpine.template
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
FROM alpine:3.18
FROM alpine:{{ env.variant | ltrimstr("alpine") }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a future goal would be to combine the Alpine and Debian templates into one to ensure that the common bits and flow remain in sync. This can be a future PR or added to this one 🤷.


# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
RUN addgroup -g 11211 memcache && adduser -D -u 11211 -G memcache memcache

# ensure SASL's "libplain.so" is installed as per https://github.com/memcached/memcached/wiki/SASLHowto
RUN apk add --no-cache libsasl

ENV MEMCACHED_VERSION 1.6.22
ENV MEMCACHED_SHA1 7a691f390d59616dbebfc9e2e4942d499c39a338
ENV MEMCACHED_VERSION {{ .version }}
ENV MEMCACHED_DOWNLOAD_URL {{ .downloadUrl }}
ENV MEMCACHED_SHA1 {{ .sha1 }}

RUN set -x \
\
Expand All @@ -26,7 +27,7 @@ RUN set -x \
perl-io-socket-ssl \
perl-utils \
\
&& wget -O memcached.tar.gz "https://memcached.org/files/memcached-$MEMCACHED_VERSION.tar.gz" \
&& wget -O memcached.tar.gz "$MEMCACHED_DOWNLOAD_URL" \
&& echo "$MEMCACHED_SHA1 memcached.tar.gz" | sha1sum -c - \
&& mkdir -p /usr/src/memcached \
&& tar -xzf memcached.tar.gz -C /usr/src/memcached --strip-components=1 \
Expand Down
9 changes: 5 additions & 4 deletions debian/Dockerfile → Dockerfile-debian.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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 --system --gid 11211 memcache && useradd --system --gid memcache --uid 11211 memcache
Expand All @@ -11,8 +11,9 @@ RUN set -eux; \
; \
rm -rf /var/lib/apt/lists/*

ENV MEMCACHED_VERSION 1.6.22
ENV MEMCACHED_SHA1 7a691f390d59616dbebfc9e2e4942d499c39a338
ENV MEMCACHED_VERSION {{ .version }}
ENV MEMCACHED_DOWNLOAD_URL {{ .downloadUrl }}
ENV MEMCACHED_SHA1 {{ .sha1 }}

RUN set -x \
\
Expand All @@ -32,7 +33,7 @@ RUN set -x \
wget \
&& rm -rf /var/lib/apt/lists/* \
\
&& wget -O memcached.tar.gz "https://memcached.org/files/memcached-$MEMCACHED_VERSION.tar.gz" \
&& wget -O memcached.tar.gz "$MEMCACHED_DOWNLOAD_URL" \
&& echo "$MEMCACHED_SHA1 memcached.tar.gz" | sha1sum -c - \
&& mkdir -p /usr/src/memcached \
&& tar -xzf memcached.tar.gz -C /usr/src/memcached --strip-components=1 \
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-debian.template'
;;
esac

{
generated_warning
gawk -f "$jqt" "$template"
} > "$dir/Dockerfile"
done
done
9 changes: 9 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh
set -e

# first arg is `-f` or `--some-option`
if [ "${1#-}" != "$1" ]; then
set -- memcached "$@"
fi

exec "$@"
Loading