Skip to content

Commit

Permalink
Add script in posix shell to remove old files and use in Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
psychomantys committed Nov 26, 2024
1 parent b0be9b7 commit 01b6c01
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 3 deletions.
9 changes: 6 additions & 3 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"]
Expand Down
114 changes: 114 additions & 0 deletions docker/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

0 comments on commit 01b6c01

Please sign in to comment.