Skip to content

Commit

Permalink
Implement basic e-mail reports.
Browse files Browse the repository at this point in the history
  • Loading branch information
bovender committed Oct 9, 2020
1 parent 87a3330 commit bdb902f
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# dora changelog

## Version 3.0.0 (2020-10-09)

### Breaking change

- The app user and group were renamed from 'app' (passenger-docker's default)
Expand All @@ -9,6 +11,10 @@
README.md, you will need to adjust the paths in your mail server configuration
files, e.g. `master.cf` if you use Postfix.

### New

- Implemented basic status reporting.

### Fixed

- `rails-console.sh` now invokes `rails` as the `app` user.
Expand Down
13 changes: 12 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ ENV RAILS_PRECOMPILE_ASSETS "true"
ENV RAILS_SMTP_HOST ""
ENV RAILS_SMTP_USER ${APP_NAME}
ENV RAILS_SMTP_PASS ""
ENV RAILS_SMTP_FROM ""
ENV RAILS_DB_HOST ""
ENV RAILS_DB_NAME ${APP_NAME}
ENV RAILS_DB_USER ${APP_NAME}
ENV RAILS_DB_PASS ""
ENV EMAIL_REPORTS_TO ""
ENV TIMEZONE="UCT"
ENV WKHTMLTOPDF ""
ENV WKHTMLTOPDF_URL "https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.5/wkhtmltox_0.12.5-1.bionic_amd64.deb"
Expand All @@ -37,7 +39,7 @@ RUN /pd_build/nodejs.sh
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - &&\
echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list &&\
apt-get update &&\
apt-get install -y --no-install-recommends imagemagick tzdata yarn
apt-get install -y --no-install-recommends imagemagick msmtp tzdata yarn

ENV HOME /root
WORKDIR /home/dora
Expand Down Expand Up @@ -65,6 +67,15 @@ RUN chmod +x /usr/local/bin/rails-console.sh
ADD dora-banner.sh /usr/local/bin/dora-banner.sh
RUN chmod +x /usr/local/bin/dora-banner.sh

ADD configure-msmtp.sh /usr/local/bin/configure-msmtp.sh
RUN chmod +x /usr/local/bin/configure-msmtp.sh

ADD dora-status.sh /usr/local/bin/dora-status.sh
RUN chmod +x /usr/local/bin/dora-status.sh

ADD send-dora-status-mail.sh /usr/local/bin/send-dora-status-mail.sh
RUN chmod +x /usr/local/bin/send-dora-status-mail.sh

RUN mkdir -p /etc/my_init.d
ADD bootstrap-container.sh /etc/my_init.d/10_bootstrap_container.sh
RUN chmod +x /etc/my_init.d/10_bootstrap_container.sh
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# dora

<!-- TOC ignore:true -->
## *DO*cker container for *RA*ils

This is a little project that helps me to set up and operate [Docker][]
Expand All @@ -20,9 +21,11 @@ be more than happy though to take pull request to improve this.
An alternative and much more sophisticated approach to Dockerizing a Rails app
can be found at [Discourse][].

<!-- TOC ignore:true -->
## Outline
<!-- TOC -->

- [Current versions of third-party components](#current-versions-of-third-party-components)
- [Customization](#customization)
- [Environment variables](#environment-variables)
- [Build argument](#build-argument)
Expand All @@ -34,6 +37,7 @@ can be found at [Discourse][].
- [Data persistence](#data-persistence)
- [SSH access](#ssh-access)
- [wkhtmltopdf support](#wkhtmltopdf-support)
- [Status reports](#status-reports)
- [Development and testing](#development-and-testing)
- [MailHog](#mailhog)
- [Container time zone](#container-time-zone)
Expand All @@ -51,6 +55,16 @@ can be found at [Discourse][].

<!-- /TOC -->

## Current versions of third-party components

| Domain | Component | |
|------------------------|----------------------------------------------|--------:|
| Dockerfile | [phusion/passenger-ruby27][passenger-docker] | 1.0.11
| install-wkhtmltopdf.sh | [wkhtmltopdf][] | 0.12.5
| docker-compose.yml | Postgres | 11
| docker-compose.yml | Adminer | 4.7
| docker-compose.yml | [Mailhog][] | 1.0.0

## Customization

Customization is mostly done with environment variables.
Expand All @@ -75,6 +89,8 @@ Customization is mostly done with environment variables.
| `RAILS_SMTP_PORT` | SMTP port | 587
| `RAILS_SMTP_USER` | SMTP user name | `$APP_NAME`
| `RAILS_SMTP_PASS` | SMTP password |
| `RAILS_SMTP_FROM` | FROM address for [system messages](#status-reports) |
| `EMAIL_REPORTS_TO` | Optional e-mail recipient for daily [status reports](#status-reports) |
| `SECRET_KEY_BASE` | Rails' secret key base |
| `TIMEZONE` | Time zone of the container | `UCT`
| `NO_WKHTMLTOPDF` | Do not attempt to install [wkhtmltopdf][] | (empty)
Expand Down Expand Up @@ -351,6 +367,13 @@ Dora from installing [wkhtmltopdf][].
You can customize the download by overriding `$WKHTMLTOPDF_URL`. Just do not
forget to also place the SHA-256 checksum into `$WKHTMLTOPDF_SUM`.

## Status reports

If the environment variables `$RAILS_SMTP_FROM` and `$EMAIL_REPORTS_TO` are set,
dora will send a daily status e-mail that reports on the services inside the
container. Of course, this does not eliminate the need to properly monitor the
container in production.

## Development and testing

To use `dora` for development and testing, you may want to set `$GIT_PULL` to
Expand Down
7 changes: 7 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# dora's to-do list

A wish list for dora, currently without order of preference.

- Set up global environment variable that contains the application directory
(DRY).
- Better in-container monitoring.
4 changes: 4 additions & 0 deletions bootstrap-container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ set -x -e
# Since the above did not work, we modify /etc/environment directly
sed -i 's_^PATH=.\+$_PATH="'$APP_DIR'/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin"_' /etc/environment

configure-msmtp.sh
DAILY_STATUS_JOB=/etc/cron.daily/send-dora-status-mail
echo "/bin/bash -l /usr/local/bin/send-dora-status-mail.sh" > $DAILY_STATUS_JOB
chod +x $DAILY_STATUS_JOB

if [ "$GIT_PULL" != "false" ]; then
git clone -b $GIT_BRANCH https://${GIT_USER%% }${GIT_USER:+:}${GIT_PASS%% }${GIT_USER:+@}${GIT_REPO#https://} "$APP_DIR" ||
Expand Down
63 changes: 63 additions & 0 deletions configure-msmtp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env bash

# configure-msmtp.sh
# This script is part of dora -- Docker container for Rails
# https://github.com/bovender/dora


echo "# dora configuring msmtp"
MSMTPRC=/etc/msmtprc

if [[ $(id -u) != 0 ]]; then
echo "Script was invoked by user '$(id -u -n)'; re-invoking with sudo..."
echo
sudo $0
fi

function show_help() {
echo "Usage: $(basename $0) [options]"
echo "Options:"
echo " -f, --force: Force configuration even if $MSMTPRC exists"
echo " -h, --help: Show this help"
echo "This script creates $MSMTPRC with the content of the following variables:"
echo " \$RAILS_SMTP_HOST=$RAILS_SMTP_HOST"
echo " \$RAILS_SMTP_PORT=$RAILS_SMTP_PORT"
echo " \$RAILS_SMTP_USER=$RAILS_SMTP_USER"
echo " \$RAILS_SMTP_PASS=$RAILS_SMTP_PASS"
echo " \$RAILS_SMTP_FROM=$RAILS_SMTP_FROM"
}
while [[ $1 != "" ]]; do
case $1 in
-h | --help )
show_help
exit
;;
-f | --force )
FORCE=1
;;
esac
shift
done

if [[ $RAILS_SMTP_HOST == "" ]]; then
echo "FATAL: environment variable \$RAILS_SMTP_HOST is empty!"
exit 1
elif [[ -a $MSMTPRC && $FORCE != "1" ]]; then
echo "FATAL: $MSMTPRC exists; use -f or --force to overwrite"
exit 2
else
[[ -a $MSMTPRC ]] && echo "WARNING: $MSMTPRC exists; forcing overwrite!"
# Note that the heredoc lines must be preceded by true tabs
cat <<-EOF | tee $MSMTPRC
# msmtp configuration written by dora ($(basename $0)) on $(date -Is)
account default
host $RAILS_SMTP_HOST
port $RAILS_SMTP_PORT
user $RAILS_SMTP_USER
from $RAILS_SMTP_FROM
password $RAILS_SMTP_PASS
tls_starttls
EOF
chown root:root $MSMTPRC
chmod 0600 $MSMTPRC
fi
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ services:
- APP_NAME
- DORA_HOST_APP_DIR
- DORA_HOST_VOL_DIR
- EMAIL_REPORTS_TO
- GIT_PULL
- PASSENGER_APP_ENV
- RAILS_PRECOMPILE_ASSETS
Expand All @@ -30,6 +31,7 @@ services:
- RAILS_SMTP_PORT
- RAILS_SMTP_USER
- RAILS_SMTP_PASS
- RAILS_SMTP_FROM
- TIMEZONE
- WEBHOOK_SECRET
depends_on:
Expand Down Expand Up @@ -58,7 +60,7 @@ services:
mailhog:
image: mailhog/mailhog:v1.0.0
ports:
# Expost the UI's port on the host, but do not expose it to the world.
# Expose the UI's port on the host, but do not expose it to the world.
# MailHog's SMTP port 1025 is not listed here because it needs to be
# available on the Docker network only.
- "127.0.0.1:8025:8025"
Expand Down
2 changes: 2 additions & 0 deletions dora-banner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ echo "= RAILS_SMTP_HOST: $RAILS_SMTP_HOST"
echo "= RAILS_SMTP_PORT: $RAILS_SMTP_PORT"
echo "= RAILS_SMTP_USER: $RAILS_SMTP_USER"
echo "= RAILS_SMTP_PASS: $RAILS_SMTP_PASS"
echo "= RAILS_SMTP_FROM: $RAILS_SMTP_FROM"
echo "= EMAIL_REPORTS_TO: $EMAIL_REPORTS_TO"
echo "= TIMEZONE: $TIMEZONE"
echo "= NO_WKHTMLTOPDF: $NO_WKHTMLTOPDF"
echo "= \`which wkhtmltopdf\`: $(which wkhtmltopdf)"
Expand Down
39 changes: 39 additions & 0 deletions dora-status.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash

# dora-status.sh
# This script is part of dora -- Docker container for Rails
# https://github.com/bovender/dora

APP_DIR=/home/dora/rails
LOG_TAIL_LINES=12

if [[ $GIT_PULL == "true" ]]; then
GIT_DESCRIPTION=`git -C $APP_DIR describe 2>/dev/null`
else
GIT_DESCRIPTION="(not a Git repository)"
fi

echo -e "# dora status\n"
echo "- Application mame: $APP_NAME"
echo "- Repository version: $GIT_DESCRIPTION"
echo "- Container uptime: `uptime`"

echo -e "\n\n## nginx status\n"
# TODO: need to e sudo?
sv status nginx

echo -e "\n\n## sidekiq status\n"
sv status sidekiq

echo -e "\n\n## Passenger status\n"
passenger-status

echo -e "\n\n## Log tails"
for F in $APP_DIR/log/*.log; do
echo -e "\n### ${F##*/}"
echo "\`\`\`"
tail -n $LOG_TAIL_LINES $F
echo "\`\`\`"
done

echo -e "\n---"
4 changes: 4 additions & 0 deletions env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ RAILS_SMTP_HOST=mailhog
RAILS_SMTP_PORT=1025
RAILS_SMTP_USER=
RAILS_SMTP_PASS=
RAILS_SMTP_FROM=

# Set this to a valid e-mail address to receive a daily status report by e-mail.
EMAIL_REPORTS_TO=

# Rails' secret key. This really MUST be kept private!
SECRET_KEY_BASE=
Expand Down
2 changes: 2 additions & 0 deletions rails-env.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ env SECRET_KEY_BASE;
env RAILS_SMTP_HOST;
env RAILS_SMTP_USER;
env RAILS_SMTP_PASS;
env RAILS_SMTP_FROM;
env EMAIL_REPORTS_TO;
env RAILS_DB_HOST;
env RAILS_DB_NAME;
env RAILS_DB_USER;
Expand Down
25 changes: 25 additions & 0 deletions send-dora-status-mail.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

# send-dora-status-mail.sh
# This script is part of dora -- Docker container for Rails
# https://github.com/bovender/dora

TMP_FILE="/tmp/dora_mail_$(date -Ins).mbox"

echo "# dora sending status mail"

if [[ $RAILS_SMTP_FROM == "" ]]; then
echo "FATAL: Cannot send status mail because \$RAILS_SMTP_FROM is empty."
exit 1
elif [[ $EMAIL_REPORTS_TO == "" ]]; then
echo "FATAL: Cannot send status mail because \$EMAIL_REPORTS_TO is empty."
exit 2
else
# Note that the heredoc lines must be preceded by tabs, not spaces!
cat <<-EOF | msmtp $EMAIL_REPORTS_TO
To: $EMAIL_REPORTS_TO
Subject: [$APP_NAME] dora status report
$(dora-status.sh)
EOF
fi

0 comments on commit bdb902f

Please sign in to comment.