From 01b6c01dda248168843e727e8d8a7f6f8da8ac5b Mon Sep 17 00:00:00 2001 From: Psycho Mantys Date: Tue, 26 Nov 2024 09:23:48 -0300 Subject: [PATCH] Add script in posix shell to remove old files and use in Dockerfile --- docker/Dockerfile | 9 ++-- docker/file_handler.sh | 114 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 3 deletions(-) create mode 100755 docker/file_handler.sh diff --git a/docker/Dockerfile b/docker/Dockerfile index f3ff77b4..2da8a006 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -5,7 +5,7 @@ ARG BASE_VERSION="alpine3.17" ARG OLD_IMAGE="chat-webapp:latest" ARG KEEP_DAYS=60 -FROM node:${NODE_VERSION}-${BASE_VERSION} as builder +FROM node:${NODE_VERSION}-${BASE_VERSION} AS builder WORKDIR /app @@ -32,15 +32,18 @@ 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/file_handler.sh / COPY --from=builder --chown=nginx:nginx /app/dist /usr/share/nginx/html/chats/ -COPY --from=old_css --chown=nginx:nginx /usr/share/nginx/html/chats/assets/*.css /usr/share/nginx/html/chats/assets/ +COPY --from=old_css --chown=nginx:nginx /usr/share/nginx/html/chats/assets/all.tx[t] /usr/share/nginx/html/chats/assets/*.css /usr/share/nginx/html/chats/assets/ #COPY --from=old_css --chown=nginx:nginx /tmp/old_css/*.css /usr/share/nginx/html/chats/assets/ COPY docker-entrypoint.sh / + RUN mv /usr/share/nginx/html/chats/index.html /usr/share/nginx/html/chats/index.html.tmpl \ && cd /usr/share/nginx/html/chats/ \ && ln -s /tmp/index.html \ - && find "/usr/share/nginx/html/chats/assets/" -maxdepth 1 -mtime +30 -name "*.css" -exec rm -rf '{}' ';' + && /file_handler.sh css + #&& find "/usr/share/nginx/html/chats/assets/" -maxdepth 1 -mtime +30 -name "*.css" -exec rm -rf '{}' ';' EXPOSE 8080 ENTRYPOINT ["/docker-entrypoint.sh"] diff --git a/docker/file_handler.sh b/docker/file_handler.sh new file mode 100755 index 00000000..c4f68bac --- /dev/null +++ b/docker/file_handler.sh @@ -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<&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 < assets/all.txt +fi