diff --git a/9.2/Dockerfile b/9.2/Dockerfile index 5bac1373..70b39ebe 100644 --- a/9.2/Dockerfile +++ b/9.2/Dockerfile @@ -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"] diff --git a/9.2/Dockerfile.rhel7 b/9.2/Dockerfile.rhel7 index fc8c8d46..54d28f41 100644 --- a/9.2/Dockerfile.rhel7 +++ b/9.2/Dockerfile.rhel7 @@ -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 && \ @@ -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"] diff --git a/9.2/contrib/openshift-custom-postgresql.conf.template b/9.2/contrib/openshift-custom-postgresql.conf.template new file mode 100644 index 00000000..c8c975ad --- /dev/null +++ b/9.2/contrib/openshift-custom-postgresql.conf.template @@ -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} diff --git a/9.2/run-postgresql.sh b/9.2/run-postgresql.sh index 32571c11..204c524e 100755 --- a/9.2/run-postgresql.sh +++ b/9.2/run-postgresql.sh @@ -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_]*$' @@ -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 @@ -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. @@ -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. @@ -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 diff --git a/9.2/test/run b/9.2/test/run index 711da2e3..3c21ffa2 100755 --- a/9.2/test/run +++ b/9.2/test/run @@ -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" @@ -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 diff --git a/README.md b/README.md index af968716..f6ab835e 100644 --- a/README.md +++ b/README.md @@ -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 |