Skip to content

Commit

Permalink
Fix .htaccess generation, add docker-based tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-beck committed May 17, 2020
1 parent 551a560 commit 594e79e
Show file tree
Hide file tree
Showing 6 changed files with 662 additions and 77 deletions.
36 changes: 20 additions & 16 deletions site/generate-htaccess.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ EOF
echo "# Version-specific rulesets generated by generate.sh"
n=$#
versions=( "$@" )
newestLTS=
oldestLTS=
newestStable=
oldestStaple=
oldestWeekly=

for (( i = n-1 ; i >= 0 ; i-- )) ; do
version="${versions[i]}"
Expand All @@ -36,31 +37,32 @@ for (( i = n-1 ; i >= 0 ; i-- )) ; do

if [[ "$version" =~ ^2[.][0-9]+[.][0-9]$ ]] ; then
# This is an LTS version
if [[ -z "$newestLTS" ]] ; then
newestLTS="$version"
if [[ -z "$newestStable" ]] ; then
newestStable="$version"
fi

cat <<EOF
# If major > ${major} or major = ${major} and minor >= ${minor} or major = ${major} and minor = ${minor} and patch >= ${patch}, use this LTS update site
RewriteCond %{QUERY_STRING} ^.*version=(\d)\.(\d+)\.(\d+)$ [NC]
RewriteCond %1 > ${major}
RewriteCond %1 >${major}
RewriteRule ^(update\-center.*\.(json|html)+) /stable-${major}\.${minor}\.${patch}%{REQUEST_URI}? [NC,L,R=301]
RewriteCond %{QUERY_STRING} ^.*version=(\d)\.(\d+)\.(\d+)$ [NC]
RewriteCond %1 = ${major}
RewriteCond %2 >= ${minor}
RewriteCond %1 =${major}
RewriteCond %2 >=${minor}
RewriteRule ^(update\-center.*\.(json|html)+) /stable-${major}\.${minor}\.${patch}%{REQUEST_URI}? [NC,L,R=301]
RewriteCond %{QUERY_STRING} ^.*version=(\d)\.(\d+)\.(\d+)$ [NC]
RewriteCond %1 = ${major}
RewriteCond %2 = ${minor}
RewriteCond %3 >= ${minor}
RewriteCond %1 =${major}
RewriteCond %2 =${minor}
RewriteCond %3 >=${minor}
RewriteRule ^(update\-center.*\.(json|html)+) /stable-${major}\.${minor}\.${patch}%{REQUEST_URI}? [NC,L,R=301]
EOF
oldestLTS=$version
oldestStaple="$version"
else
# This is a weekly version
# Split our version up into an array for rewriting
# 1.651 becomes (1 651)
oldestWeekly="$version"
cat <<EOF
# If major > ${major} or major = ${major} and minor >= ${minor}, use this weekly update site
Expand All @@ -74,23 +76,25 @@ RewriteRule ^(update\-center.*\.(json|html)+) /${major}\.${minor}%{REQUEST_URI}?
EOF

fi
v=${version%.1} # support args both as '1.234' and '1.234.1'.

done

cat <<EOF
# First LTS update site (stable-$oldestLTS) gets all older releases
# First LTS update site (stable-$oldestStaple) gets all older LTS releases
RewriteCond %{QUERY_STRING} ^.*version=\d\.(\d+)\.\d+$ [NC]
RewriteRule ^(update\-center.*\.(json|html)+) /stable-${oldestLTS}%{REQUEST_URI}? [NC,L,R=301]
RewriteRule ^(update\-center.*\.(json|html)+) /stable-${oldestStaple}%{REQUEST_URI}? [NC,L,R=301]
RewriteCond %{QUERY_STRING} ^.*version=\d\.(\d+)+$ [NC]
RewriteRule ^(update\-center.*\.(json|html)+) /${oldestWeekly}%{REQUEST_URI}? [NC,L,R=301]
EOF


echo "# Add a RewriteRule for /stable which will always rewrite to the last LTS site we have"
cat <<EOF
RewriteRule ^stable/(.+) "/stable-${newestLTS}/\$1" [NC,L,R=301]
RewriteRule ^stable/(.+) "/stable-${newestStable}/\$1" [NC,L,R=301]
EOF

Expand Down
1 change: 1 addition & 0 deletions site/test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/htaccess.tmp
7 changes: 7 additions & 0 deletions site/test/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Test image only containing the generated .htaccess file with redirects
FROM httpd:2.4

# It's the default file except we AllowOverride All, and enable mod_rewrite
COPY ./httpd.conf /usr/local/apache2/conf/httpd.conf
COPY ./htaccess.tmp /usr/local/apache2/htdocs/.htaccess
RUN chmod a+r /usr/local/apache2/htdocs/.htaccess
Loading

0 comments on commit 594e79e

Please sign in to comment.