From 30f2f6be4211d9407c547873aab8a911d1ef69cc Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Thu, 27 Jul 2017 12:39:46 +0200 Subject: [PATCH] Comply with sclorg/container-common-scripts#4 (2. step) Copy 'latest' into '9.2'. We sacrifice git history of 9.2 to have better history in latest. --- 9.2/Dockerfile | 60 +++++ 9.2/Dockerfile.rhel7 | 66 ++++++ 9.2/README.md | 1 + 9.2/cccp.yml | 1 + 9.2/root/help.1 | 12 + 9.2/root/usr/bin/cgroup-limits | 92 ++++++++ 9.2/root/usr/bin/container-entrypoint | 3 + 9.2/root/usr/bin/run-postgresql | 28 +++ 9.2/root/usr/bin/run-postgresql-master | 5 + 9.2/root/usr/bin/run-postgresql-slave | 36 +++ 9.2/root/usr/libexec/fix-permissions | 7 + .../container-scripts/postgresql/README.md | 4 + .../container-scripts/postgresql/common.sh | 220 ++++++++++++++++++ ...ustom-postgresql-replication.conf.template | 7 + .../openshift-custom-postgresql.conf.template | 21 ++ .../openshift-custom-recovery.conf.template | 9 + .../container-scripts/postgresql/scl_enable | 3 + 9.2/test/run | 1 + 18 files changed, 576 insertions(+) create mode 100644 9.2/Dockerfile create mode 100644 9.2/Dockerfile.rhel7 create mode 120000 9.2/README.md create mode 100644 9.2/cccp.yml create mode 100644 9.2/root/help.1 create mode 100755 9.2/root/usr/bin/cgroup-limits create mode 100755 9.2/root/usr/bin/container-entrypoint create mode 100755 9.2/root/usr/bin/run-postgresql create mode 100755 9.2/root/usr/bin/run-postgresql-master create mode 100755 9.2/root/usr/bin/run-postgresql-slave create mode 100755 9.2/root/usr/libexec/fix-permissions create mode 100644 9.2/root/usr/share/container-scripts/postgresql/README.md create mode 100644 9.2/root/usr/share/container-scripts/postgresql/common.sh create mode 100644 9.2/root/usr/share/container-scripts/postgresql/openshift-custom-postgresql-replication.conf.template create mode 100644 9.2/root/usr/share/container-scripts/postgresql/openshift-custom-postgresql.conf.template create mode 100644 9.2/root/usr/share/container-scripts/postgresql/openshift-custom-recovery.conf.template create mode 100644 9.2/root/usr/share/container-scripts/postgresql/scl_enable create mode 120000 9.2/test/run diff --git a/9.2/Dockerfile b/9.2/Dockerfile new file mode 100644 index 00000000..9aa7020e --- /dev/null +++ b/9.2/Dockerfile @@ -0,0 +1,60 @@ +FROM centos:centos7 + +# PostgreSQL image for OpenShift. +# Volumes: +# * /var/lib/psql/data - Database cluster for PostgreSQL +# Environment: +# * $POSTGRESQL_USER - Database user name +# * $POSTGRESQL_PASSWORD - User's password +# * $POSTGRESQL_DATABASE - Name of the database to create +# * $POSTGRESQL_ADMIN_PASSWORD (Optional) - Password for the 'postgres' +# PostgreSQL administrative account + +MAINTAINER SoftwareCollections.org + +ENV POSTGRESQL_VERSION=9.2 \ + HOME=/var/lib/pgsql \ + PGUSER=postgres + +LABEL io.k8s.description="PostgreSQL is an advanced Object-Relational database management system" \ + io.k8s.display-name="PostgreSQL 9.2" \ + io.openshift.expose-services="5432:postgresql" \ + io.openshift.tags="database,postgresql,postgresql92" + +EXPOSE 5432 + +ADD README.md /help.md +ADD root / + +# 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 centos-release-scl && \ + INSTALL_PKGS="rsync tar gettext bind-utils postgresql92 postgresql92-postgresql-contrib nss_wrapper" && \ + yum -y --setopt=tsflags=nodocs install $INSTALL_PKGS && \ + rpm -V $INSTALL_PKGS && \ + yum clean all && \ + localedef -f UTF-8 -i en_US en_US.UTF-8 && \ + test "$(id postgres)" = "uid=26(postgres) gid=26(postgres) groups=26(postgres)" && \ + mkdir -p /var/lib/pgsql/data && \ + /usr/libexec/fix-permissions /var/lib/pgsql && \ + /usr/libexec/fix-permissions /var/run/postgresql + +# Get prefix path and path to scripts rather than hard-code them in scripts +ENV CONTAINER_SCRIPTS_PATH=/usr/share/container-scripts/postgresql \ + ENABLED_COLLECTIONS=postgresql92 + +# When bash is started non-interactively, to run a shell script, for example it +# looks for this variable and source the content of this file. This will enable +# the SCL for all scripts without need to do 'scl enable'. +ENV BASH_ENV=${CONTAINER_SCRIPTS_PATH}/scl_enable \ + ENV=${CONTAINER_SCRIPTS_PATH}/scl_enable \ + PROMPT_COMMAND=". ${CONTAINER_SCRIPTS_PATH}/scl_enable" + + +VOLUME ["/var/lib/pgsql/data"] + +USER 26 + +ENTRYPOINT ["container-entrypoint"] +CMD ["run-postgresql"] diff --git a/9.2/Dockerfile.rhel7 b/9.2/Dockerfile.rhel7 new file mode 100644 index 00000000..f274a90e --- /dev/null +++ b/9.2/Dockerfile.rhel7 @@ -0,0 +1,66 @@ +FROM rhel7 + +# PostgreSQL image for OpenShift. +# Volumes: +# * /var/lib/psql/data - Database cluster for PostgreSQL +# Environment: +# * $POSTGRESQL_USER - Database user name +# * $POSTGRESQL_PASSWORD - User's password +# * $POSTGRESQL_DATABASE - Name of the database to create +# * $POSTGRESQL_ADMIN_PASSWORD (Optional) - Password for the 'postgres' +# PostgreSQL administrative account + +ENV POSTGRESQL_VERSION=9.2 \ + HOME=/var/lib/pgsql \ + PGUSER=postgres + +LABEL io.k8s.description="PostgreSQL is an advanced Object-Relational database management system" \ + io.k8s.display-name="PostgreSQL 9.2" \ + io.openshift.expose-services="5432:postgresql" \ + io.openshift.tags="database,postgresql,postgresql92" + +# Labels consumed by Red Hat build service +LABEL name="openshift3/postgresql-92-rhel7" \ + com.redhat.component="openshift-postgresql-docker" \ + version="9.2" \ + release="1" \ + architecture="x86_64" + +EXPOSE 5432 + +ADD README.md /help.md +ADD root / + +# 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 && \ + yum-config-manager --enable rhel-server-rhscl-7-rpms && \ + yum-config-manager --enable rhel-7-server-optional-rpms && \ + INSTALL_PKGS="rsync tar gettext bind-utils postgresql92 postgresql92-postgresql-contrib nss_wrapper" && \ + yum install -y --disablerepo="epel" --setopt=tsflags=nodocs $INSTALL_PKGS && \ + rpm -V $INSTALL_PKGS && \ + yum clean all && \ + localedef -f UTF-8 -i en_US en_US.UTF-8 && \ + test "$(id postgres)" = "uid=26(postgres) gid=26(postgres) groups=26(postgres)" && \ + mkdir -p /var/lib/pgsql/data && \ + /usr/libexec/fix-permissions /var/lib/pgsql && \ + /usr/libexec/fix-permissions /var/run/postgresql + +# Get prefix path and path to scripts rather than hard-code them in scripts +ENV CONTAINER_SCRIPTS_PATH=/usr/share/container-scripts/postgresql \ + ENABLED_COLLECTIONS=postgresql92 + +# When bash is started non-interactively, to run a shell script, for example it +# looks for this variable and source the content of this file. This will enable +# the SCL for all scripts without need to do 'scl enable'. +ENV BASH_ENV=${CONTAINER_SCRIPTS_PATH}/scl_enable \ + ENV=${CONTAINER_SCRIPTS_PATH}/scl_enable \ + PROMPT_COMMAND=". ${CONTAINER_SCRIPTS_PATH}/scl_enable" + +VOLUME ["/var/lib/pgsql/data"] + +USER 26 + +ENTRYPOINT ["container-entrypoint"] +CMD ["run-postgresql"] diff --git a/9.2/README.md b/9.2/README.md new file mode 120000 index 00000000..d359f030 --- /dev/null +++ b/9.2/README.md @@ -0,0 +1 @@ +root/usr/share/container-scripts/postgresql/README.md \ No newline at end of file diff --git a/9.2/cccp.yml b/9.2/cccp.yml new file mode 100644 index 00000000..96f9f25c --- /dev/null +++ b/9.2/cccp.yml @@ -0,0 +1 @@ +job-id: postgresql-92-centos7 diff --git a/9.2/root/help.1 b/9.2/root/help.1 new file mode 100644 index 00000000..3c74aa50 --- /dev/null +++ b/9.2/root/help.1 @@ -0,0 +1,12 @@ +.\"t +.\" WARNING: Do not edit this file manually, it is generated from README.md automatically. +.\" +.\" Automatically generated by Pandoc 1.16.0.2 +.\" +.TH "POSTGRESQL\-92\-RHEL7" "1" "February 22, 2017" "Container Image Pages" "" +.hy +.SH PostgreSQL Docker image +.PP +\f[B]The PostgreSQL 9.2 image is deprecated.\f[] +.SH AUTHORS +Red Hat. diff --git a/9.2/root/usr/bin/cgroup-limits b/9.2/root/usr/bin/cgroup-limits new file mode 100755 index 00000000..b9d4edc2 --- /dev/null +++ b/9.2/root/usr/bin/cgroup-limits @@ -0,0 +1,92 @@ +#!/usr/bin/python + +""" +Script for parsing cgroup information + +This script will read some limits from the cgroup system and parse +them, printing out "VARIABLE=VALUE" on each line for every limit that is +successfully read. Output of this script can be directly fed into +bash's export command. Recommended usage from a bash script: + + set -o errexit + export_vars=$(cgroup-limits) ; export $export_vars + +Variables currently supported: + MAX_MEMORY_LIMIT_IN_BYTES + Maximum possible limit MEMORY_LIMIT_IN_BYTES can have. This is + currently constant value of 9223372036854775807. + MEMORY_LIMIT_IN_BYTES + Maximum amount of user memory in bytes. If this value is set + to the same value as MAX_MEMORY_LIMIT_IN_BYTES, it means that + there is no limit set. The value is taken from + /sys/fs/cgroup/memory/memory.limit_in_bytes + NUMBER_OF_CORES + Number of detected CPU cores that can be used. This value is + calculated from /sys/fs/cgroup/cpuset/cpuset.cpus + NO_MEMORY_LIMIT + Set to "true" if MEMORY_LIMIT_IN_BYTES is so high that the caller + can act as if no memory limit was set. Undefined otherwise. +""" + +from __future__ import print_function +import sys + + +def _read_file(path): + try: + with open(path, 'r') as f: + return f.read().strip() + except IOError: + return None + + +def get_memory_limit(): + """ + Read memory limit, in bytes. + """ + + limit = _read_file('/sys/fs/cgroup/memory/memory.limit_in_bytes') + if limit is None or not limit.isdigit(): + print("Warning: Can't detect memory limit from cgroups", + file=sys.stderr) + return None + return int(limit) + + +def get_number_of_cores(): + """ + Read number of CPU cores. + """ + + core_count = 0 + + line = _read_file('/sys/fs/cgroup/cpuset/cpuset.cpus') + if line is None: + print("Warning: Can't detect number of CPU cores from cgroups", + file=sys.stderr) + return None + + for group in line.split(','): + core_ids = list(map(int, group.split('-'))) + if len(core_ids) == 2: + core_count += core_ids[1] - core_ids[0] + 1 + else: + core_count += 1 + + return core_count + + +if __name__ == "__main__": + env_vars = { + "MAX_MEMORY_LIMIT_IN_BYTES": 9223372036854775807, + "MEMORY_LIMIT_IN_BYTES": get_memory_limit(), + "NUMBER_OF_CORES": get_number_of_cores() + } + + env_vars = {k: v for k, v in env_vars.items() if v is not None} + + if env_vars.get("MEMORY_LIMIT_IN_BYTES", 0) >= 92233720368547: + env_vars["NO_MEMORY_LIMIT"] = "true" + + for key, value in env_vars.items(): + print("{0}={1}".format(key, value)) diff --git a/9.2/root/usr/bin/container-entrypoint b/9.2/root/usr/bin/container-entrypoint new file mode 100755 index 00000000..5fc44481 --- /dev/null +++ b/9.2/root/usr/bin/container-entrypoint @@ -0,0 +1,3 @@ +#!/bin/bash + +exec "$@" diff --git a/9.2/root/usr/bin/run-postgresql b/9.2/root/usr/bin/run-postgresql new file mode 100755 index 00000000..1c8de780 --- /dev/null +++ b/9.2/root/usr/bin/run-postgresql @@ -0,0 +1,28 @@ +#!/bin/bash + +export ENABLE_REPLICATION=${ENABLE_REPLICATION:-false} + +set -eu +export_vars=$(cgroup-limits) ; export $export_vars + +source "${CONTAINER_SCRIPTS_PATH}/common.sh" + +set_pgdata +check_env_vars +generate_passwd_file +generate_postgresql_config + +if [ ! -f "$PGDATA/postgresql.conf" ]; then + initialize_database + NEED_TO_CREATE_USERS=yes +fi + +pg_ctl -w start -o "-h ''" +if [ "${NEED_TO_CREATE_USERS:-}" == "yes" ]; then + create_users +fi +set_passwords +pg_ctl stop + +unset_env_vars +exec postgres "$@" diff --git a/9.2/root/usr/bin/run-postgresql-master b/9.2/root/usr/bin/run-postgresql-master new file mode 100755 index 00000000..79e7cc24 --- /dev/null +++ b/9.2/root/usr/bin/run-postgresql-master @@ -0,0 +1,5 @@ +#!/bin/bash + +export ENABLE_REPLICATION=true + +exec run-postgresql "$@" diff --git a/9.2/root/usr/bin/run-postgresql-slave b/9.2/root/usr/bin/run-postgresql-slave new file mode 100755 index 00000000..5d42d0d4 --- /dev/null +++ b/9.2/root/usr/bin/run-postgresql-slave @@ -0,0 +1,36 @@ +#!/bin/bash + +export ENABLE_REPLICATION=true + +set -eu +export_vars=$(cgroup-limits) ; export $export_vars + +source "$CONTAINER_SCRIPTS_PATH"/common.sh + +set_pgdata + +function initialize_replica() { + echo "Initializing PostgreSQL slave ..." + # TODO: Validate and reuse existing data? + rm -rf $PGDATA + PGPASSWORD="${POSTGRESQL_MASTER_PASSWORD}" pg_basebackup -x --no-password --pgdata ${PGDATA} --host=${MASTER_FQDN} --port=5432 -U "${POSTGRESQL_MASTER_USER}" + + # PostgreSQL recovery configuration. + generate_postgresql_recovery_config + cat >> "$PGDATA/recovery.conf" <&2 "error: $1" + fi + + cat >&2 </dev/null) + # FIXME: This is for debugging (docker run) + if [ -v POSTGRESQL_MASTER_IP ]; then + endpoints=${POSTGRESQL_MASTER_IP:-} + fi + if [ -z "$endpoints" ]; then + >&2 echo "Failed to resolve PostgreSQL master IP address" + exit 3 + fi + echo -n "$(echo $endpoints | cut -d ' ' -f 1)" +} + +# New config is generated every time a container is created. It only contains +# additional custom settings and is included from $PGDATA/postgresql.conf. +function generate_postgresql_config() { + envsubst \ + < "${CONTAINER_SCRIPTS_PATH}/openshift-custom-postgresql.conf.template" \ + > "${POSTGRESQL_CONFIG_FILE}" + + if [ "${ENABLE_REPLICATION}" == "true" ]; then + envsubst \ + < "${CONTAINER_SCRIPTS_PATH}/openshift-custom-postgresql-replication.conf.template" \ + >> "${POSTGRESQL_CONFIG_FILE}" + fi +} + +function generate_postgresql_recovery_config() { + envsubst \ + < "${CONTAINER_SCRIPTS_PATH}/openshift-custom-recovery.conf.template" \ + > "${POSTGRESQL_RECOVERY_FILE}" +} + +# Generate passwd file based on current uid +function generate_passwd_file() { + export USER_ID=$(id -u) + export GROUP_ID=$(id -g) + grep -v ^postgres /etc/passwd > "$HOME/passwd" + echo "postgres:x:${USER_ID}:${GROUP_ID}:PostgreSQL Server:${HOME}:/bin/bash" >> "$HOME/passwd" + export LD_PRELOAD=libnss_wrapper.so + export NSS_WRAPPER_PASSWD=${HOME}/passwd + export NSS_WRAPPER_GROUP=/etc/group +} + +function initialize_database() { + # Initialize the database cluster with utf8 support enabled by default. + # This might affect performance, see: + # http://www.postgresql.org/docs/9.2/static/locale.html + LANG=${LANG:-en_US.utf8} initdb + + # PostgreSQL configuration. + cat >> "$PGDATA/postgresql.conf" <> "$PGDATA/pg_hba.conf" <