Skip to content

Commit 0c9da02

Browse files
committed
Enable s2i
1 parent 8230ed1 commit 0c9da02

File tree

36 files changed

+484
-62
lines changed

36 files changed

+484
-62
lines changed

9.4/Dockerfile

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM centos:centos7
1+
FROM centos/s2i-core-centos7
22

33
# PostgreSQL image for OpenShift.
44
# Volumes:
@@ -13,7 +13,8 @@ FROM centos:centos7
1313
ENV POSTGRESQL_VERSION=9.4 \
1414
POSTGRESQL_PREV_VERSION=9.2 \
1515
HOME=/var/lib/pgsql \
16-
PGUSER=postgres
16+
PGUSER=postgres \
17+
APP_DATA=/opt/app-root
1718

1819
ENV SUMMARY="PostgreSQL is an advanced Object-Relational database management system" \
1920
DESCRIPTION="PostgreSQL is an advanced Object-Relational database management system (DBMS). \
@@ -55,6 +56,7 @@ ENV CONTAINER_SCRIPTS_PATH=/usr/share/container-scripts/postgresql \
5556
ENABLED_COLLECTIONS=rh-postgresql94
5657

5758
COPY root /
59+
COPY ./s2i/bin/ $STI_SCRIPTS_PATH
5860

5961
# When bash is started non-interactively, to run a shell script, for example it
6062
# looks for this variable and source the content of this file. This will enable
@@ -65,6 +67,11 @@ ENV BASH_ENV=${CONTAINER_SCRIPTS_PATH}/scl_enable \
6567

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

70+
# {APP_DATA} needs to be accessed by postgres user while s2i assembling
71+
# postgres user changes permissions of files in APP_DATA during assembling
72+
RUN /usr/libexec/fix-permissions ${APP_DATA} && \
73+
usermod -a -G root postgres
74+
6875
USER 26
6976

7077
ENTRYPOINT ["container-entrypoint"]

9.4/Dockerfile.rhel7

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM rhel7
1+
FROM rhscl/s2i-core-rhel7
22

33
# PostgreSQL image for OpenShift.
44
# Volumes:
@@ -13,7 +13,8 @@ FROM rhel7
1313
ENV POSTGRESQL_VERSION=9.4 \
1414
POSTGRESQL_PREV_VERSION=9.2 \
1515
HOME=/var/lib/pgsql \
16-
PGUSER=postgres
16+
PGUSER=postgres \
17+
APP_DATA=/opt/app-root
1718

1819
ENV SUMMARY="PostgreSQL is an advanced Object-Relational database management system" \
1920
DESCRIPTION="PostgreSQL is an advanced Object-Relational database management system (DBMS). \
@@ -60,6 +61,7 @@ ENV CONTAINER_SCRIPTS_PATH=/usr/share/container-scripts/postgresql \
6061
ENABLED_COLLECTIONS=rh-postgresql94
6162

6263
COPY root /
64+
COPY ./s2i/bin/ $STI_SCRIPTS_PATH
6365

6466
# When bash is started non-interactively, to run a shell script, for example it
6567
# looks for this variable and source the content of this file. This will enable
@@ -70,6 +72,11 @@ ENV BASH_ENV=${CONTAINER_SCRIPTS_PATH}/scl_enable \
7072

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

75+
# {APP_DATA} needs to be accessed by postgres user while s2i assembling
76+
# postgres user changes permissions of files in APP_DATA during assembling
77+
RUN /usr/libexec/fix-permissions ${APP_DATA} && \
78+
usermod -a -G root postgres
79+
7380
USER 26
7481

7582
ENTRYPOINT ["container-entrypoint"]

9.4/root/usr/bin/run-postgresql

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ fi
2424

2525
pg_ctl -w start -o "-h ''"
2626
if $PG_INITIALIZED ; then
27+
process_extending_files ${APP_DATA}/src/postgresql-init ${CONTAINER_SCRIPTS_PATH}/postgresql-init
2728
migrate_db
2829
create_users
2930
fi
30-
set_passwords
31+
32+
process_extending_files ${APP_DATA}/src/postgresql-start-hook ${CONTAINER_SCRIPTS_PATH}/postgresql-start-hook
33+
3134
pg_ctl stop
3235

3336
unset_env_vars

9.4/root/usr/share/container-scripts/postgresql/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,45 @@ enough space for the copy; upgrade failure because of not enough space might
235235
lead to data loss.
236236

237237

238+
Extending image
239+
----------------
240+
241+
This image can be extended using
242+
[source-to-image](https://github.com/openshift/source-to-image).
243+
244+
For example to build customized image `new-postgresql`
245+
with configuration in `~/image-configuration/` run:
246+
247+
248+
```
249+
$ s2i build ~/image-configuration/ postgresql new-postgresql
250+
```
251+
252+
The directory passed to `s2i build` should contain one or more of the
253+
following directories:
254+
255+
##### `postgresql-config/`
256+
257+
contained configuration files (`*.conf`) will be included at the end of image postgresql.conf file during database initialization
258+
259+
260+
##### `postgresql-init/`
261+
262+
contained shell scripts (`*.sh`) are sourced once, when database is initialized
263+
264+
##### `postgresql-start-hook/`
265+
266+
contained shell scripts (`*.sh`) are sourced before every start
267+
268+
----------------------------------------------
269+
270+
During `s2i build` all provided files are copied into `/opt/app-root/src`
271+
directory in the new image. Only one
272+
file with the same name can be used for customization and user provided files
273+
are preferred over default files in `/usr/share/container-scripts/`-
274+
so it is possible to overwrite them.
275+
276+
238277
Troubleshooting
239278
---------------
240279
At first the postgres daemon writes its logs to the standard output, so these are available in the container log. The log can be examined by running:

9.4/root/usr/share/container-scripts/postgresql/common.sh

+29-15
Original file line numberDiff line numberDiff line change
@@ -190,21 +190,6 @@ function create_users() {
190190
fi
191191
}
192192

193-
function set_passwords() {
194-
if [[ ",$postinitdb_actions," = *,simple_db,* ]]; then
195-
psql --command "ALTER USER \"${POSTGRESQL_USER}\" WITH ENCRYPTED PASSWORD '${POSTGRESQL_PASSWORD}';"
196-
fi
197-
198-
if [ -v POSTGRESQL_MASTER_USER ]; then
199-
psql --command "ALTER USER \"${POSTGRESQL_MASTER_USER}\" WITH REPLICATION;"
200-
psql --command "ALTER USER \"${POSTGRESQL_MASTER_USER}\" WITH ENCRYPTED PASSWORD '${POSTGRESQL_MASTER_PASSWORD}';"
201-
fi
202-
203-
if [ -v POSTGRESQL_ADMIN_PASSWORD ]; then
204-
psql --command "ALTER USER \"postgres\" WITH ENCRYPTED PASSWORD '${POSTGRESQL_ADMIN_PASSWORD}';"
205-
fi
206-
}
207-
208193
migrate_db ()
209194
{
210195
test "$postinitdb_actions" = ",migration" || return 0
@@ -413,3 +398,32 @@ try_pgupgrade ()
413398

414399
run_pgupgrade
415400
}
401+
402+
# get_matched_files finds file for image extending
403+
function get_matched_files() {
404+
local custom_dir default_dir
405+
custom_dir="$1"
406+
default_dir="$2"
407+
files_matched="$3"
408+
find "$default_dir" -maxdepth 1 -type f -name "$files_matched" -printf "%f\n"
409+
[ -d "$custom_dir" ] && find "$custom_dir" -maxdepth 1 -type f -name "$files_matched" -printf "%f\n"
410+
}
411+
412+
# process_extending_files process extending files in $1 and $2 directories
413+
# - source all *.sh files
414+
# (if there are files with same name source only file from $1)
415+
function process_extending_files() {
416+
local custom_dir default_dir
417+
custom_dir=$1
418+
default_dir=$2
419+
420+
while read filename ; do
421+
echo "=> sourcing $filename ..."
422+
# Custom file is prefered
423+
if [ -f $custom_dir/$filename ]; then
424+
source $custom_dir/$filename
425+
elif [ -f $default_dir/$filename ]; then
426+
source $default_dir/$filename
427+
fi
428+
done <<<"$(get_matched_files "$custom_dir" "$default_dir" '*.sh' | sort -u)"
429+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
shopt -s nullglob
4+
5+
cat >> "$PGDATA/postgresql.conf" <<EOF
6+
7+
# Custom extending configuration
8+
EOF
9+
10+
extending_cfg_dir="${APP_DATA}/src/postgresql-config"
11+
12+
for conf in "$extending_cfg_dir"/*.conf; do
13+
echo include \'${conf}\' >> $PGDATA/postgresql.conf
14+
done
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
if [[ ",$postinitdb_actions," = *,simple_db,* ]]; then
4+
psql --command "ALTER USER \"${POSTGRESQL_USER}\" WITH ENCRYPTED PASSWORD '${POSTGRESQL_PASSWORD}';"
5+
fi
6+
7+
if [ -v POSTGRESQL_MASTER_USER ]; then
8+
psql --command "ALTER USER \"${POSTGRESQL_MASTER_USER}\" WITH REPLICATION;"
9+
psql --command "ALTER USER \"${POSTGRESQL_MASTER_USER}\" WITH ENCRYPTED PASSWORD '${POSTGRESQL_MASTER_PASSWORD}';"
10+
fi
11+
12+
if [ -v POSTGRESQL_ADMIN_PASSWORD ]; then
13+
psql --command "ALTER USER \"postgres\" WITH ENCRYPTED PASSWORD '${POSTGRESQL_ADMIN_PASSWORD}';"
14+
fi

9.4/s2i/bin/assemble

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
set -o errexit
4+
set -o nounset
5+
set -o pipefail
6+
7+
shopt -s dotglob
8+
echo "---> Installing application source ..."
9+
10+
11+
mv /tmp/src/* ./
12+
13+
# Fix source directory permissions
14+
/usr/libexec/fix-permissions ./

9.4/s2i/bin/run

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/usr/bin/run-postgresql

9.4/s2i/bin/usage

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
groff -t -man -ETascii /help.1

9.5/Dockerfile

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM centos:centos7
1+
FROM centos/s2i-core-centos7
22

33
# PostgreSQL image for OpenShift.
44
# Volumes:
@@ -13,7 +13,8 @@ FROM centos:centos7
1313
ENV POSTGRESQL_VERSION=9.5 \
1414
POSTGRESQL_PREV_VERSION=9.4 \
1515
HOME=/var/lib/pgsql \
16-
PGUSER=postgres
16+
PGUSER=postgres \
17+
APP_DATA=/opt/app-root
1718

1819
ENV SUMMARY="PostgreSQL is an advanced Object-Relational database management system" \
1920
DESCRIPTION="PostgreSQL is an advanced Object-Relational database management system (DBMS). \
@@ -55,6 +56,7 @@ ENV CONTAINER_SCRIPTS_PATH=/usr/share/container-scripts/postgresql \
5556
ENABLED_COLLECTIONS=rh-postgresql95
5657

5758
COPY root /
59+
COPY ./s2i/bin/ $STI_SCRIPTS_PATH
5860

5961
# When bash is started non-interactively, to run a shell script, for example it
6062
# looks for this variable and source the content of this file. This will enable
@@ -65,6 +67,11 @@ ENV BASH_ENV=${CONTAINER_SCRIPTS_PATH}/scl_enable \
6567

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

70+
# {APP_DATA} needs to be accessed by postgres user while s2i assembling
71+
# postgres user changes permissions of files in APP_DATA during assembling
72+
RUN /usr/libexec/fix-permissions ${APP_DATA} && \
73+
usermod -a -G root postgres
74+
6875
USER 26
6976

7077
ENTRYPOINT ["container-entrypoint"]

9.5/Dockerfile.rhel7

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM rhel7
1+
FROM rhscl/s2i-core-rhel7
22

33
# PostgreSQL image for OpenShift.
44
# Volumes:
@@ -13,7 +13,8 @@ FROM rhel7
1313
ENV POSTGRESQL_VERSION=9.5 \
1414
POSTGRESQL_PREV_VERSION=9.4 \
1515
HOME=/var/lib/pgsql \
16-
PGUSER=postgres
16+
PGUSER=postgres \
17+
APP_DATA=/opt/app-root
1718

1819
ENV SUMMARY="PostgreSQL is an advanced Object-Relational database management system" \
1920
DESCRIPTION="PostgreSQL is an advanced Object-Relational database management system (DBMS). \
@@ -60,6 +61,7 @@ ENV CONTAINER_SCRIPTS_PATH=/usr/share/container-scripts/postgresql \
6061
ENABLED_COLLECTIONS=rh-postgresql95
6162

6263
COPY root /
64+
COPY ./s2i/bin/ $STI_SCRIPTS_PATH
6365

6466
# When bash is started non-interactively, to run a shell script, for example it
6567
# looks for this variable and source the content of this file. This will enable
@@ -70,6 +72,11 @@ ENV BASH_ENV=${CONTAINER_SCRIPTS_PATH}/scl_enable \
7072

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

75+
# {APP_DATA} needs to be accessed by postgres user while s2i assembling
76+
# postgres user changes permissions of files in APP_DATA during assembling
77+
RUN /usr/libexec/fix-permissions ${APP_DATA} && \
78+
usermod -a -G root postgres
79+
7380
USER 26
7481

7582
ENTRYPOINT ["container-entrypoint"]

9.5/root/usr/bin/run-postgresql

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ fi
2424

2525
pg_ctl -w start -o "-h ''"
2626
if $PG_INITIALIZED ; then
27+
process_extending_files ${APP_DATA}/src/postgresql-init ${CONTAINER_SCRIPTS_PATH}/postgresql-init
2728
migrate_db
2829
create_users
2930
fi
30-
set_passwords
31+
32+
process_extending_files ${APP_DATA}/src/postgresql-start-hook ${CONTAINER_SCRIPTS_PATH}/postgresql-start-hook
33+
3134
pg_ctl stop
3235

3336
unset_env_vars

9.5/root/usr/share/container-scripts/postgresql/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,45 @@ enough space for the copy; upgrade failure because of not enough space might
235235
lead to data loss.
236236

237237

238+
Extending image
239+
----------------
240+
241+
This image can be extended using
242+
[source-to-image](https://github.com/openshift/source-to-image).
243+
244+
For example to build customized image `new-postgresql`
245+
with configuration in `~/image-configuration/` run:
246+
247+
248+
```
249+
$ s2i build ~/image-configuration/ postgresql new-postgresql
250+
```
251+
252+
The directory passed to `s2i build` should contain one or more of the
253+
following directories:
254+
255+
##### `postgresql-config/`
256+
257+
contained configuration files (`*.conf`) will be included at the end of image postgresql.conf file during database initialization
258+
259+
260+
##### `postgresql-init/`
261+
262+
contained shell scripts (`*.sh`) are sourced once, when database is initialized
263+
264+
##### `postgresql-start-hook/`
265+
266+
contained shell scripts (`*.sh`) are sourced before every start
267+
268+
----------------------------------------------
269+
270+
During `s2i build` all provided files are copied into `/opt/app-root/src`
271+
directory in the new image. Only one
272+
file with the same name can be used for customization and user provided files
273+
are preferred over default files in `/usr/share/container-scripts/`-
274+
so it is possible to overwrite them.
275+
276+
238277
Troubleshooting
239278
---------------
240279
At first the postgres daemon writes its logs to the standard output, so these are available in the container log. The log can be examined by running:

0 commit comments

Comments
 (0)