diff --git a/Dockerfile b/Dockerfile index 237742e..f43ad80 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,13 +3,20 @@ FROM dastapov/hledger:1.26 as hledger FROM library/python:3.10-slim +RUN apt install cron && \ + apt-get clean && \ + rm -rf /var/lib/{apt,dpkg,cache,log} + COPY --from=hledger /usr/bin/hledger /usr/bin/hledger COPY src /code +COPY docker-entrypoint.sh /entrypoint.sh # TODO: Support more than one ledger file ENV LEDGER_FILE=/data/all.ledger ENV OUTPUT_FILE=/data/all.sqlite ENV OVERWRITE_OUTPUT=true +ENV CRONTAB_SCHEDULE="0 15 * * *" -ENTRYPOINT ["python", "/code/main.py"] +ENTRYPOINT ["sh", "/entrypoint.sh"] +CMD ["crond", "-f", "-l", "0"] diff --git a/README.md b/README.md index 1ac2d69..0fe286f 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,28 @@ Exports are not designed to be consistent across iterations. It is recommended t ## Usage -TBD +The default configuration is to run in a long-lived mode where the conversion will happen on a regular schedule defined by cron. You can use [crontab guru](https://crontab.guru) to build the value for the `CRONTAB_SCHEDULE` variable. -Currently planning on designing the container to be able to run as a long-lived job with a cron schedule for exporting. There will also be a method for running as a one-off. +``` +docker run \ + --name ledger-sqlite \ + --env "LEDGER_FILE=/data/journal.ledger" \ + --env "OUTPUT_FILE=/data/journal.sqlite" \ + --env "CRONTAB_SCHEDULE=0 15 * * *" \ + -v /path/to/data:/data \ + ghcr.io/tgrosinger/ledger-sqlite:latest +``` + +Alternatively, a single conversion can be performed and the container will exit when complete. This can also be useful for debugging as the logs are easier to retrieve. + +``` +docker run \ + --rm + --env "LEDGER_FILE=/data/journal.ledger" \ + --env "OUTPUT_FILE=/data/journal.sqlite" \ + -v /path/to/data:/data \ + --entrypoint python \ + ghcr.io/tgrosinger/ledger-sqlite:latest \ + /code/main.py + +``` diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100755 index 0000000..a50705f --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +# Inspired by https://github.com/dkruger/docker-cron + +cat << EOF > /var/spool/cron/crontabs/root +${CRONTAB_ENTRY} python /code/main.py +EOF + +exec "$@"