Skip to content
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

add scrip to remove old css files #205

Merged
merged 13 commits into from
Jan 14, 2025
18 changes: 16 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

ARG NODE_VERSION="18.18.0"
ARG BASE_VERSION="alpine3.17"
ARG OLD_IMAGE="insights-webapp:latest"
ARG KEEP_DAYS=60

FROM node:${NODE_VERSION}-${BASE_VERSION} as builder
FROM node:${NODE_VERSION}-${BASE_VERSION} AS builder

WORKDIR /app

ARG KEEP_DAYS=${KEEP_DAYS}

ARG VITE_FIREBASE_CONFIG

ENV VITE_FIREBASE_CONFIG $VITE_FIREBASE_CONFIG
Expand All @@ -31,14 +35,24 @@ COPY . ./

RUN yarn build

FROM ${OLD_IMAGE} AS old_css

FROM nginxinc/nginx-unprivileged:1.25-alpine
ARG OLD_IMAGE=${OLD_IMAGE}
ARG KEEP_DAYS=${KEEP_DAYS}

COPY --chown=nginx:nginx docker/nginx/nginx.conf /etc/nginx/nginx.conf
COPY --chown=nginx:nginx docker/nginx/headers /usr/share/nginx/html/headers
COPY --chown=nginx:nginx docker/file_handler.sh /
COPY --from=builder --chown=nginx:nginx /app/dist /usr/share/nginx/html/insights/
COPY --from=old_css --chown=nginx:nginx /usr/share/nginx/html/insights/assets/all.tx[t] /usr/share/nginx/html/insights/assets/*.css /usr/share/nginx/html/insights/assets/

COPY docker-entrypoint.sh /

RUN mv /usr/share/nginx/html/insights/index.html /usr/share/nginx/html/insights/index.html.tmpl \
&& cd /usr/share/nginx/html/insights/ \
&& ln -s /tmp/index.html
&& ln -s /tmp/index.html \
&& /file_handler.sh css

EXPOSE 8080
ENTRYPOINT ["/docker-entrypoint.sh"]
Expand Down
114 changes: 114 additions & 0 deletions docker/nginx/file_handler.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/usr/bin/env sh

FILES=""
CURRENT_TIME=$( date +%s )
EXPIRE_TIME=$(( CURRENT_TIME-(30*24*60*60) ))
#EXPIRE_TIME=$(( CURRENT_TIME-1 ))

dict_keys(){
IFS="$( printf '\t' )"
echo "$1" | while read -r KEY VALUE ; do
echo "${KEY}"
done
}

dict_values(){
IFS="$( printf '\t' )"
echo "$1" | while read -r KEY VALUE ; do
echo "${VALUE}"
done
}


dict_set(){
has_key="false"
IFS="$( printf '\t' )"

while read -r KEY VALUE ; do
if [ "${KEY}" = "$2" ] ; then
printf '%s\t%s\n' "$2" "$3"
has_key='true'
else
printf '%s\t%s\n' "${KEY}" "${VALUE}"
fi
done <<-EOFDICT
$1
EOFDICT
if [ "${has_key}" = 'false' ] ; then
printf '%s\t%s\n' "$2" "$3"
fi
}

dict_get(){
has_key="false"
IFS="$( printf '\t' )"

while read -r KEY VALUE ; do
if [ "${KEY}" = "$2" ] ; then
echo "${VALUE}"
has_key='true'
break
fi
done <<-EOFDICT
$1
EOFDICT

if [ "${has_key}" = 'false' ] ; then
echo "$3"
fi
}

load_files(){
while read -r file_ctime filename ; do
if [ "${file_ctime}" -le "$( dict_get "${FILES}" "${filename}" "3199999999" )" ] ; then
FILES=$( dict_set "${FILES}" "${filename}" "${file_ctime}" )
fi
done<<EOFDICT
$( cat "$@" )
EOFDICT

}

print_files(){
dict_keys "${FILES}" | while read -r filename ; do
if [ -r "${filename}" ] ; then
printf '%s\t%s\n' "$( dict_get "${FILES}" "${filename}" )" "${filename}"
fi
done
}

delete_old_files(){
for filename in $( dict_keys "${FILES}" ); do
if [ "${EXPIRE_TIME}" -gt "$( dict_get "${FILES}" "${filename}" )" ] ; then
echo "deleting: ${filename} $( dict_get "${FILES}" "${filename}" )" 1>&2
rm -rf "${filename}"
fi
done
}

generate_files(){
while read -r filename ; do
if [ -r "${filename}" ] ; then
printf '%s\t%s\n' "${CURRENT_TIME}" "${filename}"
fi
done
}

if [ "$1" = "delete_old" ]; then
shift
load_files "$@"
delete_old_files
elif [ "$1" = "print" ] ; then
shift
load_files "$@"
#echo "$FILES"
print_files
elif [ "$1" = "generate" ] ; then
generate_files
elif [ "$1" = "css" ] ; then
load_files <<EOFDICT
$( find . -iname "*.css" | generate_files ; [ -r assets/all.txt ] && cat assets/all.txt )
EOFDICT
delete_old_files
print_files > assets/all.txt
fi
Loading