Skip to content

Commit

Permalink
Merge pull request sclorg#4 from mnagy/config_customization
Browse files Browse the repository at this point in the history
PostgreSQL settings.
  • Loading branch information
Michal Fojtik committed Mar 26, 2015
2 parents 0670ec4 + 8c27b59 commit 230e98f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 13 deletions.
4 changes: 2 additions & 2 deletions 9.2/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ EXPOSE 5432
# to make sure of that.
RUN rpmkeys --import file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 && \
yum -y --setopt=tsflags=nodocs install https://www.softwarecollections.org/en/scls/rhscl/postgresql92/epel-7-x86_64/download/rhscl-postgresql92-epel-7-x86_64.noarch.rpm && \
yum -y --setopt=tsflags=nodocs install postgresql92 && \
yum -y --setopt=tsflags=nodocs install gettext postgresql92 && \
yum clean all && \
mkdir -p /var/lib/pgsql/data && chown postgres.postgres /var/lib/pgsql/data && \
test "$(id postgres)" = "uid=26(postgres) gid=26(postgres) groups=26(postgres)"

COPY run-postgresql.sh /usr/local/bin/
COPY contrib/.bashrc /var/lib/pgsql/
COPY contrib /var/lib/pgsql/

VOLUME ["/var/lib/pgsql/data"]

Expand Down
4 changes: 2 additions & 2 deletions 9.2/Dockerfile.rhel7
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ EXPOSE 5432
# This image must forever use UID 26 for postgres user so our volumes are
# safe in the future. This should *never* change, the last test is there
# to make sure of that.
RUN yum install -y yum-utils && \
RUN yum install -y yum-utils gettext && \
yum-config-manager --enable rhel-server-rhscl-7-rpms && \
yum-config-manager --enable rhel-7-server-optional-rpms && \
yum install -y --setopt=tsflags=nodocs postgresql92 && \
Expand All @@ -31,7 +31,7 @@ RUN yum install -y yum-utils && \
test "$(id postgres)" = "uid=26(postgres) gid=26(postgres) groups=26(postgres)"

COPY run-postgresql.sh /usr/local/bin/
COPY contrib/.bashrc /var/lib/pgsql/
COPY contrib /var/lib/pgsql/

VOLUME ["/var/lib/pgsql/data"]

Expand Down
15 changes: 15 additions & 0 deletions 9.2/contrib/openshift-custom-postgresql.conf.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#
# Custom OpenShift configuration.
#
# NOTE: This file is rewritten everytime the container is started!
# Changes to this file will be overrwritten.
#

# Listen on all interfaces.
listen_addresses = '*'

# Determines the maximum number of concurrent connections to the database server. Default: 100
max_connections = ${POSTGRESQL_MAX_CONNECTIONS}

# Sets the amount of memory the database server uses for shared memory buffers. Default: 32MB
shared_buffers = ${POSTGRESQL_SHARED_BUFFERS}
33 changes: 24 additions & 9 deletions 9.2/run-postgresql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ source $HOME/.bashrc
set -eu

# Data dir
export PGDATA=/var/lib/pgsql/data
export PGDATA=$HOME/data
POSTGRESQL_CONFIG_FILE=$HOME/openshift-custom-postgresql.conf

# Configuration settings.
export POSTGRESQL_MAX_CONNECTIONS=${POSTGRESQL_MAX_CONNECTIONS:-100}
export POSTGRESQL_SHARED_BUFFERS=${POSTGRESQL_SHARED_BUFFERS:-32MB}

# Be paranoid and stricter than we should be.
psql_identifier_regex='^[a-zA-Z_][a-zA-Z0-9_]*$'
Expand All @@ -22,10 +27,17 @@ function usage() {
echo " \$POSTGRESQL_DATABASE (regex: '$psql_identifier_regex')"
echo "Optional:"
echo " \$POSTGRESQL_ADMIN_PASSWORD (regex: '$psql_password_regex')"
echo "Settings:"
echo " \$POSTGRESQL_MAX_CONNECTIONS (default: 100)"
echo " \$POSTGRESQL_SHARED_BUFFERS (default: 32MB)"
exit 1
}

function check_env_vars() {
if ! [[ -v POSTGRESQL_USER && -v POSTGRESQL_PASSWORD && -v POSTGRESQL_DATABASE ]]; then
usage
fi

[[ "$POSTGRESQL_USER" =~ $psql_identifier_regex ]] || usage
[ ${#POSTGRESQL_USER} -le 63 ] || usage "PostgreSQL username too long (maximum 63 characters)"
[[ "$POSTGRESQL_PASSWORD" =~ $psql_password_regex ]] || usage
Expand All @@ -44,8 +56,7 @@ function unset_env_vars() {
unset POSTGRESQL_ADMIN_PASSWORD
}

if [ "$1" = "postgres" -a ! -f "$PGDATA/postgresql.conf" ]; then

function initialize_database() {
check_env_vars

# Initialize the database cluster with utf8 support enabled by default.
Expand All @@ -56,12 +67,8 @@ if [ "$1" = "postgres" -a ! -f "$PGDATA/postgresql.conf" ]; then
# PostgreSQL configuration.
cat >> "$PGDATA/postgresql.conf" <<-EOF
#
# Custom OpenShift configuration starting at this point.
#
# Listen on all interfaces.
listen_addresses = '*'
# Custom OpenShift configuration:
include '../openshift-custom-postgresql.conf'
EOF

# Access control configuration.
Expand All @@ -85,6 +92,14 @@ if [ "$1" = "postgres" -a ! -f "$PGDATA/postgresql.conf" ]; then
fi

pg_ctl stop
}

# New config is generated every time a container is created. It only contains
# additional custom settings and is included from $PGDATA/postgresql.conf.
envsubst < ${POSTGRESQL_CONFIG_FILE}.template > ${POSTGRESQL_CONFIG_FILE}

if [ "$1" = "postgres" -a ! -f "$PGDATA/postgresql.conf" ]; then
initialize_database
fi

unset_env_vars
Expand Down
18 changes: 18 additions & 0 deletions 9.2/test/run
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,23 @@ function run_container_creation_tests() {
echo " Success!"
}

function test_config_option() {
local env_var=$1 ; shift
local setting=$1 ; shift
local value=$1 ; shift

# If $value is a string, it needs to be in simple quotes ''.
# If nothing is found, grep returns 1 and test fails.
docker run --rm -e $env_var=${value//\'/} $IMAGE_NAME cat /var/lib/pgsql/openshift-custom-postgresql.conf | grep -q "$setting = $value"
}

function run_configuration_tests() {
echo " Testing image configuration settings"
test_config_option POSTGRESQL_MAX_CONNECTIONS max_connections 42
test_config_option POSTGRESQL_SHARED_BUFFERS shared_buffers 64MB
echo " Success!"
}

function run_tests() {
local name=$1 ; shift
envs="-e POSTGRESQL_USER=$USER -e POSTGRESQL_PASSWORD=$PASS -e POSTGRESQL_DATABASE=db"
Expand All @@ -164,5 +181,6 @@ function run_tests() {
# Tests.

run_container_creation_tests
run_configuration_tests
USER=user PASS=pass run_tests no_admin
USER=user1 PASS=pass1 ADMIN_PASS=r00t run_tests admin
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ The image recognizes following environment variables that you can set during ini
| `POSTGRESQL_DATABASE` | Database name |
| `POSTGRESQL_ADMIN_PASSWORD` | Password for the `postgres` admin account (optional) |

Following environment variables influence PostgreSQL configuration file. They are all optional.

| Variable name | Description | Default
| :---------------------------- | ----------------------------------------------------------------------- | -------------------------------
| `POSTGRESQL_MAX_CONNECTIONS` | The maximum number of client connections allowed | 100
| `POSTGRESQL_SHARED_BUFFERS` | Sets how much memory is dedicated to PostgreSQL to use for caching data | 32M

You can also set following mount points by passing `-v /host:/container` flag to docker.

| Volume mount point | Description |
Expand Down

0 comments on commit 230e98f

Please sign in to comment.