Skip to content

Commit

Permalink
docker: create new container with cronjob tar-ing result daily
Browse files Browse the repository at this point in the history
  • Loading branch information
nikromen committed Dec 15, 2023
1 parent 7e08d7d commit fc88a80
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docker-compose.prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ services:
- 8080:8080
volumes:
- persistent:/persistent:z
env_file:
- "files/.env"

volumes:
persistent:
20 changes: 18 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,25 @@ services:
volumes:
- .:/opt/log-detective-website:z
- persistent:/persistent
env_file:
- "files/env"
environment:
- ENV=devel
- FEEDBACK_DIR=/persistent/results
- PYTHONPATH=/opt/log-detective-website

cron:
build:
context: .
dockerfile: docker/cron/Dockerfile
hostname: cron
command: "crond -f"
stdin_open: true
tty: true
volumes:
- persistent:/persistent
env_file:
- "files/env"

# The frontend container is only a quality of life for development.
# For production, we will simply compile the ClojureScript into JavaScript
# and deploy it.
Expand All @@ -40,9 +55,10 @@ services:
- 3333:3333
volumes:
- .:/opt/log-detective-website:z
env_file:
- "files/env"
environment:
- ENV=devel
- FEEDBACK_DIR=/persistent/results

volumes:
persistent:
14 changes: 14 additions & 0 deletions docker/cron/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM registry.fedoraproject.org/fedora:38
MAINTAINER [email protected]

RUN dnf -y update
RUN dnf -y install cronie tar

RUN touch /var/log/cron.log

COPY files/cron/tar_persistent.sh /bin/tar_persistent
RUN chmod +x /bin/tar_persistent

COPY files/cron/crontab_daily /etc/cron.d/crontab_daily
RUN chmod +x /etc/cron.d/crontab_daily
RUN crontab /etc/cron.d/crontab_daily
17 changes: 13 additions & 4 deletions docker/production/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
FROM registry.fedoraproject.org/fedora:38
MAINTAINER [email protected]

ENV FEEDBACK_DIR=/persistent/results
ENV ENV=production

RUN dnf -y update && \
Expand All @@ -19,6 +18,8 @@ RUN dnf -y update && \
sudo \
python3-ipdb \
findutils \
cronie \
tar \
# Frontend
&& dnf -y install \
npm \
Expand All @@ -36,7 +37,6 @@ RUN npx shadow-cljs release app
FROM registry.fedoraproject.org/fedora:38
MAINTAINER [email protected]

ENV FEEDBACK_DIR=/persistent/results
ENV ENV=production

RUN dnf -y install python3-fastapi \
Expand All @@ -52,8 +52,17 @@ RUN dnf -y install python3-fastapi \
COPY --from=0 /src/frontend/public /src/frontend/public
COPY --from=0 /src/backend /src/backend

# According to the documentation, gunicorn is a valid production server
# https://www.uvicorn.org/deployment/
# set-up cron
RUN touch /var/log/cron.log

COPY files/cron/tar_persistent.sh /bin/tar_persistent
RUN chmod +x /bin/tar_persistent

COPY files/cron/crontab_daily /etc/cron.d/crontab_daily
RUN chmod +x /etc/cron.d/crontab_daily
RUN crontab /etc/cron.d/crontab_daily


WORKDIR /src/backend
ENV PYTHONPATH="${PYTHONPATH}:/src"

Expand Down
12 changes: 12 additions & 0 deletions docker/production/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

set -m

# According to the documentation, gunicorn is a valid production server
# https://www.uvicorn.org/deployment/
# TODO: We should put this into a config file
gunicorn -k uvicorn.workers.UvicornWorker --certfile /persistent/letsencrypt/live/log-detective.com/cert.pem --keyfile /persistent/letsencrypt/live/log-detective.com/privkey.pem --ca-certs /persistent/letsencrypt/live/log-detective.com/fullchain.pem api:app -b 0.0.0.0:8080

crond -f &

fg %1
5 changes: 5 additions & 0 deletions files/cron/crontab_daily
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin

# run every day at 1:05AM
5 1 * * * root tar_persistent >> /var/log/cron.log 2>&1
7 changes: 7 additions & 0 deletions files/cron/tar_persistent.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/sh

# archive results tar_persistent.sh and remove the old ones
FILE_NAME=data-$(date +%s).tar.gz
tar -czvf $STORAGE_DIR/new-$FILE_NAME -C $FEEDBACK_DIR . && \
rm -f $STORAGE_DIR/data-*.tar.gz && \
mv $STORAGE_DIR/new-$FILE_NAME $STORAGE_DIR/$FILE_NAME
2 changes: 2 additions & 0 deletions files/env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
STORAGE_DIR=/persistent
FEEDBACK_DIR=/persistent/results

0 comments on commit fc88a80

Please sign in to comment.