From 3d69162f4e5195c855d3d505a1a5aed1d9f5a66f Mon Sep 17 00:00:00 2001 From: Tianon Gravi Date: Thu, 27 Aug 2020 16:31:53 -0700 Subject: [PATCH] Use jq-template.awk --- .gitattributes | 3 ++ .github/workflows/verify-templating.yml | 22 ++++++++++++ .gitignore | 1 + 4.1/Dockerfile | 6 ++++ 4.2/Dockerfile | 6 ++++ 5.0/Dockerfile | 6 ++++ 5.1/Dockerfile | 6 ++++ Dockerfile.template | 4 +-- apply-templates.sh | 40 +++++++++++++++++++++ generate-stackbrew-library.sh | 14 ++++---- update.sh | 45 ++--------------------- versions.json | 18 ++++++++++ versions.sh | 48 +++++++++++++++++++++++++ 13 files changed, 168 insertions(+), 51 deletions(-) create mode 100644 .gitattributes create mode 100644 .github/workflows/verify-templating.yml create mode 100644 .gitignore create mode 100755 apply-templates.sh create mode 100644 versions.json create mode 100755 versions.sh diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..f08de86 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +**/Dockerfile linguist-generated +**/start-qemu linguist-generated +Dockerfile.template linguist-language=Dockerfile diff --git a/.github/workflows/verify-templating.yml b/.github/workflows/verify-templating.yml new file mode 100644 index 0000000..7e833f1 --- /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@v2 + - 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 0000000..d548f66 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.jq-template.awk diff --git a/4.1/Dockerfile b/4.1/Dockerfile index 178928c..0a89236 100644 --- a/4.1/Dockerfile +++ b/4.1/Dockerfile @@ -1,3 +1,9 @@ +# +# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh" +# +# PLEASE DO NOT EDIT IT DIRECTLY. +# + FROM debian:buster-slim RUN apt-get update && apt-get install -y --no-install-recommends \ diff --git a/4.2/Dockerfile b/4.2/Dockerfile index 806a7f1..98ae973 100644 --- a/4.2/Dockerfile +++ b/4.2/Dockerfile @@ -1,3 +1,9 @@ +# +# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh" +# +# PLEASE DO NOT EDIT IT DIRECTLY. +# + FROM debian:buster-slim RUN apt-get update && apt-get install -y --no-install-recommends \ diff --git a/5.0/Dockerfile b/5.0/Dockerfile index fcbf136..e5f9070 100644 --- a/5.0/Dockerfile +++ b/5.0/Dockerfile @@ -1,3 +1,9 @@ +# +# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh" +# +# PLEASE DO NOT EDIT IT DIRECTLY. +# + FROM debian:buster-slim RUN apt-get update && apt-get install -y --no-install-recommends \ diff --git a/5.1/Dockerfile b/5.1/Dockerfile index 0995611..89d0b94 100644 --- a/5.1/Dockerfile +++ b/5.1/Dockerfile @@ -1,3 +1,9 @@ +# +# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh" +# +# PLEASE DO NOT EDIT IT DIRECTLY. +# + FROM debian:buster-slim RUN apt-get update && apt-get install -y --no-install-recommends \ diff --git a/Dockerfile.template b/Dockerfile.template index 67cbd48..6db5d42 100644 --- a/Dockerfile.template +++ b/Dockerfile.template @@ -14,8 +14,8 @@ ENV QEMU_KEYS \ # https://www.qemu.org/download/#source # https://download.qemu.org/?C=M;O=D -ENV QEMU_VERSION %%QEMU_VERSION%% -ENV QEMU_URL %%QEMU_URL%% +ENV QEMU_VERSION {{ .version }} +ENV QEMU_URL {{ .url }} RUN set -eux; \ \ diff --git a/apply-templates.sh b/apply-templates.sh new file mode 100755 index 0000000..b40fce5 --- /dev/null +++ b/apply-templates.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +[ -f versions.json ] # run "versions.sh" first + +jqt='.jq-template.awk' +if [ -n "${BASHBREW_SCRIPTS:-}" ]; then + jqt="$BASHBREW_SCRIPTS/jq-template.awk" +elif [ "$BASH_SOURCE" -nt "$jqt" ]; then + wget -qO "$jqt" 'https://github.com/docker-library/bashbrew/raw/5f0c26381fb7cc78b2d217d58007800bdcfbcfa1/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 + + echo "processing $version ..." + + { + generated_warning + gawk -f "$jqt" Dockerfile.template + } > "$version/Dockerfile" + + cp -a start-qemu "$version/" +done diff --git a/generate-stackbrew-library.sh b/generate-stackbrew-library.sh index ef2adb3..f7a6831 100755 --- a/generate-stackbrew-library.sh +++ b/generate-stackbrew-library.sh @@ -10,8 +10,10 @@ 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 @@ -52,10 +54,8 @@ join() { echo "${out#$sep}" } -for version in "${versions[@]}"; do - commit="$(dirCommit "$version")" - - fullVersion="$(git show "$commit":"$version/Dockerfile" | awk '$1 == "ENV" && $2 == "QEMU_VERSION" { print $3; exit }')" +for version; do + fullVersion="$(jq -r --arg version "$version" '.[$version].version' versions.json)" rcVersion="${version%-rc}" @@ -69,6 +69,8 @@ for version in "${versions[@]}"; do ${aliases[$version]:-} ) + commit="$(dirCommit "$version")" + echo cat <<-EOE Tags: $(join ', ' "${versionAliases[@]}") diff --git a/update.sh b/update.sh index 69483a3..bac2d75 100755 --- a/update.sh +++ b/update.sh @@ -3,46 +3,5 @@ set -Eeuo pipefail cd "$(dirname "$(readlink -f "$BASH_SOURCE")")" -versions=( */ ) -versions=( "${versions[@]%/}" ) - -# https://download.qemu.org/?C=M;O=D -urls="$( - wget -qO- 'https://www.qemu.org/download/' \ - | grep -oE 'https://download[.]qemu[.]org/qemu-([^"]+)[.]tar[.]xz' \ - | sort -ruV -)" - -for version in "${versions[@]}"; do - rcGrepV='-v' - rcVersion="${version%-rc}" - if [ "$rcVersion" != "$version" ]; then - rcGrepV= - fi - - url="$( - grep -E "qemu-$rcVersion([.-])" <<<"$urls" \ - | grep $rcGrepV -E -- '-rc' \ - | head -1 - )" - fullVersion="${url##*/qemu-}" - fullVersion="${fullVersion%%.tar.*}" - - echo "$version: $fullVersion" - - sed -r \ - -e 's/%%QEMU_VERSION%%/'"$fullVersion"'/g' \ - -e 's!%%QEMU_URL%%!'"$url"'!g' \ - Dockerfile.template > "$version/Dockerfile" - cp -a start-qemu "$version/" - - case "$rcVersion" in - # https://github.com/qemu/qemu/commit/b10d49d7619e4957b4b971f816661b57e5061d71 - 3.0 | 3.1 | 4.0) - sed -ri \ - -e 's/libssh-dev/libssh2-1-dev/g' \ - -e 's/--enable-libssh/--enable-libssh2/g' \ - "$version/Dockerfile" - ;; - esac -done +./versions.sh "$@" +./apply-templates.sh "$@" diff --git a/versions.json b/versions.json new file mode 100644 index 0000000..ebf26f9 --- /dev/null +++ b/versions.json @@ -0,0 +1,18 @@ +{ + "4.1": { + "url": "https://download.qemu.org/qemu-4.1.1.tar.xz", + "version": "4.1.1" + }, + "4.2": { + "url": "https://download.qemu.org/qemu-4.2.1.tar.xz", + "version": "4.2.1" + }, + "5.0": { + "url": "https://download.qemu.org/qemu-5.0.0.tar.xz", + "version": "5.0.0" + }, + "5.1": { + "url": "https://download.qemu.org/qemu-5.1.0.tar.xz", + "version": "5.1.0" + } +} diff --git a/versions.sh b/versions.sh new file mode 100755 index 0000000..ba80de6 --- /dev/null +++ b/versions.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +cd "$(dirname "$(readlink -f "$BASH_SOURCE")")" + +versions=( "$@" ) +if [ ${#versions[@]} -eq 0 ]; then + versions=( */ ) + json='{}' +else + json="$(< versions.json)" +fi +versions=( "${versions[@]%/}" ) + +# https://download.qemu.org/?C=M;O=D +urls="$( + wget -qO- 'https://www.qemu.org/download/' \ + | grep -oE 'https://download[.]qemu[.]org/qemu-([^"]+)[.]tar[.]xz' \ + | sort -ruV +)" + +for version in "${versions[@]}"; do + rcGrepV='-v' + rcVersion="${version%-rc}" + if [ "$rcVersion" != "$version" ]; then + rcGrepV= + fi + + url="$( + grep -E "qemu-$rcVersion([.-])" <<<"$urls" \ + | grep $rcGrepV -E -- '-rc' \ + | head -1 + )" + fullVersion="${url##*/qemu-}" + fullVersion="${fullVersion%%.tar.*}" + + echo "$version: $fullVersion" + + export version fullVersion url + json="$(jq <<<"$json" ' + .[env.version] = { + version: env.fullVersion, + url: env.url, + } + ')" +done + +jq <<<"$json" -S . > versions.json