Skip to content

Commit

Permalink
Add healthchecks and scripts for stencil population during build
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkb committed Apr 30, 2019
1 parent a4f69c1 commit 082e8ec
Show file tree
Hide file tree
Showing 14 changed files with 250 additions and 17 deletions.
88 changes: 76 additions & 12 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
# The version of the docker-compose standard being followed here
version: '3.2'
version: '3.4'

# Services are groups of containers handling one aspect of the application
services:
database: # PostgreSQL
image: postgres:alpine
# Use the PostgreSQL image we made ourselves by running ./scripts/build/postgres.sh
image: omniport-postgres:latest

# No matter what, if the container stops, start it again
restart: always

# Check the health of the container periodically
healthcheck:
test: ['CMD', 'checkhealth']
retries: 4

interval: 16m
timeout: 16s
start_period: 2m

# Expose the port 5432 used by PostgreSQL to other containers
expose:
- "5432"
Expand All @@ -33,8 +43,17 @@ services:
- network

channel-layer: # Redis
# Use the Redis image from Alpine Linux as is
image: redis:alpine
# Use the Redis image we made ourselves by running ./scripts/build/redis.sh
image: omniport-redis:latest

# Check the health of the container periodically
healthcheck:
test: ['CMD', 'checkhealth']
retries: 4

interval: 16m
timeout: 16s
start_period: 2m

# No matter what, if the container stops, start it again
restart: always
Expand All @@ -59,8 +78,17 @@ services:
- network

session-store: # Redis
# Use the Redis image from Alpine Linux as is
image: redis:alpine
# Use the Redis image we made ourselves by running ./scripts/build/redis.sh
image: omniport-redis:latest

# Check the health of the container periodically
healthcheck:
test: ['CMD', 'checkhealth']
retries: 4

interval: 16m
timeout: 16s
start_period: 2m

# No matter what, if the container stops, start it again
restart: always
Expand All @@ -85,8 +113,17 @@ services:
- network

communication-store: # Redis
# Use the Redis image from Alpine Linux as is
image: redis:alpine
# Use the Redis image we made ourselves by running ./scripts/build/redis.sh
image: omniport-redis:latest

# Check the health of the container periodically
healthcheck:
test: ['CMD', 'checkhealth']
retries: 4

interval: 16m
timeout: 16s
start_period: 2m

# No matter what, if the container stops, start it again
restart: always
Expand Down Expand Up @@ -114,6 +151,15 @@ services:
# Use the Redis Commander image as is
image: rediscommander/redis-commander:latest

# Check the health of the container periodically
healthcheck:
test: ['CMD', 'nc', '-z', '127.0.0.1', '8081']
retries: 4

interval: 16m
timeout: 16s
start_period: 2m

# No matter what, if the container stops, start it again
restart: always

Expand All @@ -139,8 +185,17 @@ services:
- network

cache: # Memcached
# Use the Memcached image from Alpine Linux as is
image: memcached:alpine
# Use the Memcached image we made ourselves by running ./scripts/build/memcached.sh
image: omniport-memcached:latest

# Check the health of the container periodically
healthcheck:
test: ['CMD', 'netcat', '-z', '127.0.0.1', '11211']
retries: 4

interval: 16m
timeout: 16s
start_period: 2m

# No matter what, if the container stops, start it again
restart: always
Expand All @@ -157,8 +212,17 @@ services:
- network

message-broker: # RabbitMQ
# Use the RabbitMQ image from Alpine Linux as is
image: rabbitmq:management-alpine
# Use the RabbitMQ image we made ourselves by running ./scripts/build/rabbitmq.sh
image: omniport-rabbitmq:latest

# Check the health of the container periodically
healthcheck:
test: ['CMD', 'checkhealth']
retries: 4

interval: 16m
timeout: 16s
start_period: 2m

# No matter what, if the container stops, start it again
restart: always
Expand Down
16 changes: 16 additions & 0 deletions memcached/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Use Memcached from the Debian Linux project
FROM memcached:latest

# Need to undo the misdeeds of Memcached developers
USER root

# Add labels for metadata
LABEL maintainer="Dhruv Bhanushali <https://dhruvkb.github.io/>"

# Install dependencies
RUN apt-get update \
&& apt-get install -y netcat \
&& rm -rf /var/lib/apt/lists/*

# Undo our undoing of Memcache's misdeeds, just in case they are important
USER memcache
8 changes: 8 additions & 0 deletions postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Use Postgres from the Debian Linux project
FROM postgres:latest

# Add labels for metadata
LABEL maintainer="Dhruv Bhanushali <https://dhruvkb.github.io/>"

# Install the health check script
COPY checkhealth.sh /usr/local/bin/checkhealth
26 changes: 26 additions & 0 deletions postgres/checkhealth.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
set -eo pipefail

HOST="$(hostname -i || echo '127.0.0.1')"
USER="${POSTGRES_USER:-postgres}"
DB="${POSTGRES_DB:-postgres}"

export PGPASSWORD="${POSTGRES_PASSWORD:-}"

# A Postgres node is considered healthy if
# * it connects with the given username and password
# * it returns 1 for a 'SELECT 1' SQL query

args=(
# Force postgres to not use the local socket and test external connectivity
--host "$HOST"
--username "$USER"
--dbname "$DB"
--quiet --no-align --tuples-only
)

if select="$(echo 'SELECT 1' | psql "${args[@]}")" && [ "$select" = '1' ]; then
exit 0
fi

exit 1
6 changes: 3 additions & 3 deletions postgres/database_stencil.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
POSTGRES_DB=
POSTGRES_USER=
POSTGRES_PASSWORD=
POSTGRES_DB=[[db]]
POSTGRES_USER=[[user]]
POSTGRES_PASSWORD=[[password]]
8 changes: 8 additions & 0 deletions rabbitmq/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Use RabbitMQ from the Debian Linux project
FROM rabbitmq:management

# Add labels for metadata
LABEL maintainer="Dhruv Bhanushali <https://dhruvkb.github.io/>"

# Install the health check script
COPY checkhealth.sh /usr/local/bin/checkhealth
14 changes: 14 additions & 0 deletions rabbitmq/checkhealth.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
set -eo pipefail

# A RabbitMQ node is considered healthy if
# * the rabbit app finished booting & it's running
# * there are no alarms
# * there is at least 1 active listener

rabbitmqctl eval '
{ true, rabbit_app_booted_and_running } = { rabbit:is_booted(node()), rabbit_app_booted_and_running },
{ [], no_alarms } = { rabbit:alarms(), no_alarms },
[] /= rabbit_networking:active_listeners(),
rabbitmq_node_is_healthy.
' || exit 1
4 changes: 2 additions & 2 deletions rabbitmq/message_broker_stencil.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
RABBITMQ_DEFAULT_USER=
RABBITMQ_DEFAULT_PASS=
RABBITMQ_DEFAULT_USER=[[user]]
RABBITMQ_DEFAULT_PASS=[[pass]]
8 changes: 8 additions & 0 deletions redis/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Use Redis from the Debian Linux project
FROM redis:latest

# Add labels for metadata
LABEL maintainer="Dhruv Bhanushali <https://dhruvkb.github.io/>"

# Install the health check script
COPY checkhealth.sh /usr/local/bin/checkhealth
13 changes: 13 additions & 0 deletions redis/checkhealth.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
set -eo pipefail

host="$(hostname -i || echo '127.0.0.1')"

# A Redis node is considered healthy if
# * it responds 'PONG' to every ping

if ping="$(redis-cli -h "$host" ping)" && [ "$ping" = 'PONG' ]; then
exit 0
fi

exit 1
12 changes: 12 additions & 0 deletions scripts/build/memcached.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# Enter the Memcached Docker folder
cd memcached/

# Build the container from the Memcached folder and tag it
TIMESTAMP=$(date +"%s")

docker build \
--tag omniport-memcached:${TIMESTAMP} \
--tag omniport-memcached:latest \
.
27 changes: 27 additions & 0 deletions scripts/build/postgres.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

# Enter the Postgres Docker folder
cd postgres/

read -p "Rebuild database .env file? (y/N): " REBUILD
if [ $REBUILD == 'Y' -o $REBUILD == 'y' ]; then
read -p "Enter the name of the database: " DB
read -p "Enter the user of the database: " USER
read -p "Enter the password of the database: " PASSWORD

# Perform text substitution to generate the new .env file
printf "Writing database environment file... "
cp database_stencil.env database.env
sed -i "s/\[\[db\]\]/${DB}/g" database.env
sed -i "s/\[\[user\]\]/${USER}/g" database.env
sed -i "s/\[\[password\]\]/${PASSWORD}/g" database.env
printf "done\n"
fi

# Build the container from the Postgres folder and tag it
TIMESTAMP=$(date +"%s")

docker build \
--tag omniport-postgres:${TIMESTAMP} \
--tag omniport-postgres:latest \
.
25 changes: 25 additions & 0 deletions scripts/build/rabbitmq.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

# Enter the RabbitMQ Docker folder
cd rabbitmq/

read -p "Rebuild message broker .env file? (y/N): " REBUILD
if [ $REBUILD == 'Y' -o $REBUILD == 'y' ]; then
read -p "Enter the user of the message broker: " USER
read -p "Enter the pass of the message broker: " PASS

# Perform text substitution to generate the new .env file
printf "Writing message broker environment file... "
cp message_broker_stencil.env message_broker.env
sed -i "s/\[\[user\]\]/${USER}/g" message_broker.env
sed -i "s/\[\[pass\]\]/${PASS}/g" message_broker.env
printf "done\n"
fi

# Build the container from the RabbitMQ folder and tag it
TIMESTAMP=$(date +"%s")

docker build \
--tag omniport-rabbitmq:${TIMESTAMP} \
--tag omniport-rabbitmq:latest \
.
12 changes: 12 additions & 0 deletions scripts/build/redis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# Enter the Redis Docker folder
cd redis/

# Build the container from the Redis folder and tag it
TIMESTAMP=$(date +"%s")

docker build \
--tag omniport-redis:${TIMESTAMP} \
--tag omniport-redis:latest \
.

0 comments on commit 082e8ec

Please sign in to comment.