From 0097ab8a1fde788a9a51357339fa5498855623e2 Mon Sep 17 00:00:00 2001
From: Tianon Gravi <admwiggin@gmail.com>
Date: Mon, 11 Dec 2023 14:11:00 -0800
Subject: [PATCH] Update tools repo handling and update innovation to
 mysql-shell 8.2.1

The "innovation" branch of MySQL appears to have a separate tools repository now with newer versions of MySQL Shell.
---
 5.7/Dockerfile.oracle        |  6 ++----
 8.0/Dockerfile.oracle        |  6 ++----
 apply-templates.sh           |  6 +++++-
 innovation/Dockerfile.oracle |  8 +++-----
 template/Dockerfile.oracle   |  6 ++----
 versions.json                |  8 +++++++-
 versions.sh                  | 20 +++++++++++++++-----
 7 files changed, 36 insertions(+), 24 deletions(-)

diff --git a/5.7/Dockerfile.oracle b/5.7/Dockerfile.oracle
index 34dd84877..9f1f8bb4a 100644
--- a/5.7/Dockerfile.oracle
+++ b/5.7/Dockerfile.oracle
@@ -56,12 +56,11 @@ ENV MYSQL_MAJOR 5.7
 ENV MYSQL_VERSION 5.7.44-1.el7
 
 RUN set -eu; \
-	. /etc/os-release; \
 	{ \
 		echo '[mysql5.7-server-minimal]'; \
 		echo 'name=MySQL 5.7 Server Minimal'; \
 		echo 'enabled=1'; \
-		echo "baseurl=https://repo.mysql.com/yum/mysql-5.7-community/docker/el/${VERSION_ID%%[.-]*}/\$basearch/"; \
+		echo 'baseurl=https://repo.mysql.com/yum/mysql-5.7-community/docker/el/7/$basearch/'; \
 		echo 'gpgcheck=1'; \
 		echo 'gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql'; \
 # https://github.com/docker-library/mysql/pull/680#issuecomment-825930524
@@ -104,11 +103,10 @@ RUN set -eux; \
 	mysql --version
 
 RUN set -eu; \
-	. /etc/os-release; \
 	{ \
 		echo '[mysql-tools-community]'; \
 		echo 'name=MySQL Tools Community'; \
-		echo "baseurl=https://repo.mysql.com/yum/mysql-tools-community/el/${VERSION_ID%%[.-]*}/\$basearch/"; \
+		echo 'baseurl=https://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/'; \
 		echo 'enabled=1'; \
 		echo 'gpgcheck=1'; \
 		echo 'gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql'; \
diff --git a/8.0/Dockerfile.oracle b/8.0/Dockerfile.oracle
index dedf8edbf..0bc9653e3 100644
--- a/8.0/Dockerfile.oracle
+++ b/8.0/Dockerfile.oracle
@@ -56,12 +56,11 @@ ENV MYSQL_MAJOR 8.0
 ENV MYSQL_VERSION 8.0.35-1.el8
 
 RUN set -eu; \
-	. /etc/os-release; \
 	{ \
 		echo '[mysql8.0-server-minimal]'; \
 		echo 'name=MySQL 8.0 Server Minimal'; \
 		echo 'enabled=1'; \
-		echo "baseurl=https://repo.mysql.com/yum/mysql-8.0-community/docker/el/${VERSION_ID%%[.-]*}/\$basearch/"; \
+		echo 'baseurl=https://repo.mysql.com/yum/mysql-8.0-community/docker/el/8/$basearch/'; \
 		echo 'gpgcheck=1'; \
 		echo 'gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql'; \
 # https://github.com/docker-library/mysql/pull/680#issuecomment-825930524
@@ -95,11 +94,10 @@ RUN set -eux; \
 	mysql --version
 
 RUN set -eu; \
-	. /etc/os-release; \
 	{ \
 		echo '[mysql-tools-community]'; \
 		echo 'name=MySQL Tools Community'; \
-		echo "baseurl=https://repo.mysql.com/yum/mysql-tools-community/el/${VERSION_ID%%[.-]*}/\$basearch/"; \
+		echo 'baseurl=https://repo.mysql.com/yum/mysql-tools-community/el/8/$basearch/'; \
 		echo 'enabled=1'; \
 		echo 'gpgcheck=1'; \
 		echo 'gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql'; \
diff --git a/apply-templates.sh b/apply-templates.sh
index ee553ff8f..3c84529ab 100755
--- a/apply-templates.sh
+++ b/apply-templates.sh
@@ -1,5 +1,6 @@
 #!/usr/bin/env bash
 set -Eeuo pipefail
+shopt -s extglob # support globs like !(foo)
 
 [ -f versions.json ] # run "versions.sh" first
 
@@ -32,11 +33,14 @@ generated_warning() {
 for version; do
 	export version
 
-	rm -f "$version"/Dockerfile.*
+	rm -f "$version"/!(config)
+	mkdir -p "$version"
 
 	for variant in oracle debian; do
 		export variant
 
+		echo "processing $version ($variant) ..."
+
 		variantVersion="$(jq -r '.[env.version][env.variant] // {} | .version // ""' versions.json)"
 		if [ -n "$variantVersion" ]; then
 			dockerfile="Dockerfile.$variant"
diff --git a/innovation/Dockerfile.oracle b/innovation/Dockerfile.oracle
index 2ae20b8f7..334a3d00a 100644
--- a/innovation/Dockerfile.oracle
+++ b/innovation/Dockerfile.oracle
@@ -56,12 +56,11 @@ ENV MYSQL_MAJOR innovation
 ENV MYSQL_VERSION 8.2.0-1.el8
 
 RUN set -eu; \
-	. /etc/os-release; \
 	{ \
 		echo '[mysqlinnovation-server-minimal]'; \
 		echo 'name=MySQL innovation Server Minimal'; \
 		echo 'enabled=1'; \
-		echo "baseurl=https://repo.mysql.com/yum/mysql-innovation-community/docker/el/${VERSION_ID%%[.-]*}/\$basearch/"; \
+		echo 'baseurl=https://repo.mysql.com/yum/mysql-innovation-community/docker/el/8/$basearch/'; \
 		echo 'gpgcheck=1'; \
 		echo 'gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql'; \
 # https://github.com/docker-library/mysql/pull/680#issuecomment-825930524
@@ -95,18 +94,17 @@ RUN set -eux; \
 	mysql --version
 
 RUN set -eu; \
-	. /etc/os-release; \
 	{ \
 		echo '[mysql-tools-community]'; \
 		echo 'name=MySQL Tools Community'; \
-		echo "baseurl=https://repo.mysql.com/yum/mysql-tools-community/el/${VERSION_ID%%[.-]*}/\$basearch/"; \
+		echo 'baseurl=https://repo.mysql.com/yum/mysql-tools-innovation-community/el/8/$basearch/'; \
 		echo 'enabled=1'; \
 		echo 'gpgcheck=1'; \
 		echo 'gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql'; \
 # https://github.com/docker-library/mysql/pull/680#issuecomment-825930524
 		echo 'module_hotfixes=true'; \
 	} | tee /etc/yum.repos.d/mysql-community-tools.repo
-ENV MYSQL_SHELL_VERSION 8.0.35-1.el8
+ENV MYSQL_SHELL_VERSION 8.2.1-1.el8
 RUN set -eux; \
 	microdnf install -y "mysql-shell-$MYSQL_SHELL_VERSION"; \
 	microdnf clean all; \
diff --git a/template/Dockerfile.oracle b/template/Dockerfile.oracle
index 1027d4466..c4e2c74de 100644
--- a/template/Dockerfile.oracle
+++ b/template/Dockerfile.oracle
@@ -71,12 +71,11 @@ ENV MYSQL_MAJOR {{ env.version }}
 ENV MYSQL_VERSION {{ .oracle.version }}
 
 RUN set -eu; \
-	. /etc/os-release; \
 	{ \
 		echo '[mysql{{ env.version }}-server-minimal]'; \
 		echo 'name=MySQL {{ env.version }} Server Minimal'; \
 		echo 'enabled=1'; \
-		echo "baseurl=https://repo.mysql.com/yum/mysql-{{ env.version }}-community/docker/el/${VERSION_ID%%[.-]*}/\$basearch/"; \
+		echo {{ "baseurl=" + .oracle.repo + "/$basearch/" | @sh }}; \
 		echo 'gpgcheck=1'; \
 		echo 'gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql'; \
 # https://github.com/docker-library/mysql/pull/680#issuecomment-825930524
@@ -121,11 +120,10 @@ RUN set -eux; \
 	mysql --version
 
 RUN set -eu; \
-	. /etc/os-release; \
 	{ \
 		echo '[mysql-tools-community]'; \
 		echo 'name=MySQL Tools Community'; \
-		echo "baseurl=https://repo.mysql.com/yum/mysql-tools-community/el/${VERSION_ID%%[.-]*}/\$basearch/"; \
+		echo {{ "baseurl=" + .["mysql-shell"].repo + "/$basearch/" | @sh }}; \
 		echo 'enabled=1'; \
 		echo 'gpgcheck=1'; \
 		echo 'gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql'; \
diff --git a/versions.json b/versions.json
index bca7bcd99..f4fba08cc 100644
--- a/versions.json
+++ b/versions.json
@@ -1,12 +1,14 @@
 {
   "5.7": {
     "mysql-shell": {
+      "repo": "https://repo.mysql.com/yum/mysql-tools-community/el/7",
       "version": "8.0.35-1.el7"
     },
     "oracle": {
       "architectures": [
         "amd64"
       ],
+      "repo": "https://repo.mysql.com/yum/mysql-5.7-community/docker/el/7",
       "variant": "7-slim",
       "version": "5.7.44-1.el7"
     },
@@ -21,6 +23,7 @@
       "version": "8.0.35-1debian11"
     },
     "mysql-shell": {
+      "repo": "https://repo.mysql.com/yum/mysql-tools-community/el/8",
       "version": "8.0.35-1.el8"
     },
     "oracle": {
@@ -28,6 +31,7 @@
         "amd64",
         "arm64v8"
       ],
+      "repo": "https://repo.mysql.com/yum/mysql-8.0-community/docker/el/8",
       "variant": "8-slim",
       "version": "8.0.35-1.el8"
     },
@@ -35,13 +39,15 @@
   },
   "innovation": {
     "mysql-shell": {
-      "version": "8.0.35-1.el8"
+      "repo": "https://repo.mysql.com/yum/mysql-tools-innovation-community/el/8",
+      "version": "8.2.1-1.el8"
     },
     "oracle": {
       "architectures": [
         "amd64",
         "arm64v8"
       ],
+      "repo": "https://repo.mysql.com/yum/mysql-innovation-community/docker/el/8",
       "variant": "8-slim",
       "version": "8.2.0-1.el8"
     },
diff --git a/versions.sh b/versions.sh
index a76f2b1a7..6fd12dfa9 100755
--- a/versions.sh
+++ b/versions.sh
@@ -103,12 +103,22 @@ for version in "${versions[@]}"; do
 
 	oracleVariant="${oracleVariants[$version]:-$defaultOracleVariant}"
 	oracleVersion="${oracleVariant%%-*}" # "7", etc
+
+	rpmRepo="https://repo.mysql.com/yum/mysql-$version-community/docker/el/$oracleVersion"
+	case "$version" in
+		innovation) toolsRepo="https://repo.mysql.com/yum/mysql-tools-innovation-community/el/$oracleVersion" ;;
+		*)          toolsRepo="https://repo.mysql.com/yum/mysql-tools-community/el/$oracleVersion" ;;
+	esac
+	export rpmRepo toolsRepo
+
 	rpmVersion=
 	shellVersion=
-	doc="$(jq <<<"$doc" -c '. += { oracle: { architectures: [] } }')"
+	doc="$(jq <<<"$doc" -c '. += {
+		oracle: { architectures: [], repo: env.rpmRepo },
+		"mysql-shell": { repo: env.toolsRepo },
+	}')"
 	for bashbrewArch in $(xargs -n1 <<<"${!bashbrewArchToRpmArch[*]}" | sort | xargs); do
 		rpmArch="${bashbrewArchToRpmArch[$bashbrewArch]}"
-		rpmRepo="https://repo.mysql.com/yum/mysql-$version-community/docker/el/$oracleVersion"
 		archVersions="$(
 			fetch_rpm_versions "$rpmRepo" "$rpmArch" "$oracleVersion" 'mysql-community-server-minimal' \
 				|| :
@@ -121,7 +131,7 @@ for version in "${versions[@]}"; do
 			echo >&2 "error: $version architecture version mismatch! ('$rpmVersion' vs '$archVersion' on '$rpmArch'/'$bashbrewArch')"
 			exit 1
 		fi
-		shellArchVersions="$(fetch_rpm_versions "https://repo.mysql.com/yum/mysql-tools-community/el/$oracleVersion" "$rpmArch" "$oracleVersion" 'mysql-shell')"
+		shellArchVersions="$(fetch_rpm_versions "$toolsRepo" "$rpmArch" "$oracleVersion" 'mysql-shell')"
 		shellArchVersion="$(head -1 <<<"$shellArchVersions")"
 		if [ -z "$shellVersion" ]; then
 			shellVersion="$shellArchVersion"
@@ -152,9 +162,9 @@ for version in "${versions[@]}"; do
 				version: env.rpmVersion,
 				variant: env.oracleVariant,
 			}),
-			"mysql-shell": {
+			"mysql-shell": (.["mysql-shell"] + {
 				version: env.shellVersion,
-			},
+			}),
 		}
 	')"