Skip to content

Commit

Permalink
Merge pull request #61 from smkent/docker-with-accounts-file
Browse files Browse the repository at this point in the history
Add support for accounts configuration file in container image
  • Loading branch information
smkent authored Nov 5, 2022
2 parents 44612c4 + 964307d commit 0d7bb90
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ ENV SAFEWAY_ACCOUNT_USERNAME=
ENV SAFEWAY_ACCOUNT_PASSWORD=
ENV SAFEWAY_ACCOUNT_MAIL_FROM=
ENV SAFEWAY_ACCOUNT_MAIL_TO=
ENV SAFEWAY_ACCOUNTS_FILE=

RUN apk add --no-cache tini
COPY docker/entrypoint /
Expand Down
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ so they don't have to each be clicked manually.
## Installation and usage with Docker

A Docker container is provided which runs safeway-coupons with cron. The cron
schedule and your Safeway account details should be configured using environment
variables.
schedule and your Safeway account details may be configured using environment
variables, or with an accounts file.

Example `docker-compose.yaml`:
Example `docker-compose.yaml` with configuration via environment variables:

```yaml
version: "3.7"
Expand All @@ -34,6 +34,23 @@ services:
restart: unless-stopped
```
Example `docker-compose.yaml` with configuration via accounts file:

```yaml
version: "3.7"
services:
safeway-coupons:
image: ghcr.io/smkent/safeway-coupons:latest
environment:
CRON_SCHEDULE: "0 2 * * *" # Run at 2:00 AM each day
SMTPHOST: your.smtp.host
SAFEWAY_ACCOUNTS_FILE: /accounts_file
restart: unless-stopped
volumes:
- path/to/safeway_accounts_file:/accounts_file:ro
```

Start the container by running:

```console
Expand Down
12 changes: 12 additions & 0 deletions docker-compose.accounts-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: "3.7"

services:
safeway-coupons:
image: ghcr.io/smkent/safeway-coupons:latest
environment:
CRON_SCHEDULE: "0 2 * * *"
SMTPHOST: your.smtp.host
SAFEWAY_ACCOUNTS_FILE: /accounts_file
restart: unless-stopped
volumes:
- path/to/safeway_accounts_file:/accounts_file:ro
31 changes: 18 additions & 13 deletions docker/entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@

set -ex

if [ -z "${SAFEWAY_ACCOUNT_MAIL_TO}" ] \
&& [ -n "${SAFEWAY_ACCOUNT_USERNAME}" ]; then
export SAFEWAY_ACCOUNT_MAIL_TO="${SAFEWAY_ACCOUNT_USERNAME}"
fi

if [ -n "${SAFEWAY_ACCOUNT_MAIL_FROM}" ]; then
export MAILFROM="${SAFEWAY_ACCOUNT_MAIL_FROM}"
fi
if [ -n "${SAFEWAY_ACCOUNT_MAIL_TO}" ]; then
export MAILFROM="${SAFEWAY_ACCOUNT_MAIL_TO}"
args=
if [ -n "${SAFEWAY_ACCOUNTS_FILE}" ]; then
echo "Using accounts file at ${SAFEWAY_ACCOUNTS_FILE} for configuration"
args="--accounts-config ${SAFEWAY_ACCOUNTS_FILE}"
else
echo "Using environment variables for configuration"
if [ -z "${SAFEWAY_ACCOUNT_MAIL_TO}" ] \
&& [ -n "${SAFEWAY_ACCOUNT_USERNAME}" ]; then
export SAFEWAY_ACCOUNT_MAIL_TO="${SAFEWAY_ACCOUNT_USERNAME}"
fi
if [ -n "${SAFEWAY_ACCOUNT_MAIL_FROM}" ]; then
export MAILFROM="${SAFEWAY_ACCOUNT_MAIL_FROM}"
fi
if [ -n "${SAFEWAY_ACCOUNT_MAIL_TO}" ]; then
export MAILFROM="${SAFEWAY_ACCOUNT_MAIL_TO}"
fi
env | grep -vie 'password' | grep -e '^SAFEWAY_' -e '^SMTPHOST='
fi

mkdir /etc/cron.d
(
echo "${CRON_SCHEDULE?} safeway-coupons >/proc/1/fd/1 2>/proc/1/fd/2"
echo "${CRON_SCHEDULE?} safeway-coupons ${args} >/proc/1/fd/1 2>/proc/1/fd/2"
) > /var/spool/cron/crontabs/root

env | grep -vie 'password' | grep -e '^SAFEWAY_' -e '^SMTPHOST='

crontab -l

exec crond -f -d 8

0 comments on commit 0d7bb90

Please sign in to comment.