From a342aa8bb76a69aae326f8b7cb8af783ff035cd3 Mon Sep 17 00:00:00 2001 From: Stephen Kent Date: Fri, 4 Nov 2022 22:42:50 -0700 Subject: [PATCH 1/2] Add support for accounts configuration file in container image --- Dockerfile | 1 + README.md | 23 ++++++++++++++++++++--- docker-compose.yaml | 7 +++---- docker/entrypoint | 31 ++++++++++++++++++------------- 4 files changed, 42 insertions(+), 20 deletions(-) diff --git a/Dockerfile b/Dockerfile index 075ee32..28593c2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 / diff --git a/README.md b/README.md index b7a7152..d89f409 100644 --- a/README.md +++ b/README.md @@ -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" @@ -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 diff --git a/docker-compose.yaml b/docker-compose.yaml index 2901b6e..3ea7c62 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -6,8 +6,7 @@ services: environment: CRON_SCHEDULE: "0 2 * * *" SMTPHOST: your.smtp.host - SAFEWAY_ACCOUNT_USERNAME: your.safeway.account.email@example.com - SAFEWAY_ACCOUNT_PASSWORD: very_secret - SAFEWAY_ACCOUNT_MAIL_FROM: your.email@example.com - SAFEWAY_ACCOUNT_MAIL_TO: your.email@example.com + SAFEWAY_ACCOUNTS_FILE: /accounts_file restart: unless-stopped + volumes: + - path/to/safeway_accounts_file:/accounts_file:ro diff --git a/docker/entrypoint b/docker/entrypoint index d48ba74..c5d21ec 100755 --- a/docker/entrypoint +++ b/docker/entrypoint @@ -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 From 964307d8c262186412244a7239669b3d9daa9d62 Mon Sep 17 00:00:00 2001 From: Stephen Kent Date: Fri, 4 Nov 2022 22:45:52 -0700 Subject: [PATCH 2/2] Add second docker-compose example file for accounts-based config --- docker-compose.accounts-config.yaml | 12 ++++++++++++ docker-compose.yaml | 7 ++++--- 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 docker-compose.accounts-config.yaml diff --git a/docker-compose.accounts-config.yaml b/docker-compose.accounts-config.yaml new file mode 100644 index 0000000..3ea7c62 --- /dev/null +++ b/docker-compose.accounts-config.yaml @@ -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 diff --git a/docker-compose.yaml b/docker-compose.yaml index 3ea7c62..2901b6e 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -6,7 +6,8 @@ services: environment: CRON_SCHEDULE: "0 2 * * *" SMTPHOST: your.smtp.host - SAFEWAY_ACCOUNTS_FILE: /accounts_file + SAFEWAY_ACCOUNT_USERNAME: your.safeway.account.email@example.com + SAFEWAY_ACCOUNT_PASSWORD: very_secret + SAFEWAY_ACCOUNT_MAIL_FROM: your.email@example.com + SAFEWAY_ACCOUNT_MAIL_TO: your.email@example.com restart: unless-stopped - volumes: - - path/to/safeway_accounts_file:/accounts_file:ro