Skip to content

Commit f58b86e

Browse files
committed
Enable s2i
1 parent 4ea239a commit f58b86e

File tree

13 files changed

+135
-4
lines changed

13 files changed

+135
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# create backup user
2+
3+
psql -U postgres -d $POSTGRESQL_DATABASE -c "CREATE USER $POSTGRESQL_BACKUP_USER SUPERUSER password '$POSTGRESQL_BACKUP_PASSWORD';"
4+
psql -U postgres -d $POSTGRESQL_DATABASE -c "ALTER USER $POSTGRESQL_BACKUP_USER set default_transaction_read_only = on;"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
log_destination = 'stderr'
2+
logging_collector = on
3+
log_directory = 'pg_log'
4+
log_filename = 'postgresql.log'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Check that user credentials for backup is set
2+
3+
[[ -v POSTGRESQL_BACKUP_USER && -v POSTGRESQL_BACKUP_PASSWORD ]] || usage "You have to set all variables for user for doing backup: POSTGRESQL_BACKUP_USER, POSTGRESQL_BACKUP_PASSWORD"

latest/Dockerfile

+5-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.6 \
1414
POSTGRESQL_PREV_VERSION=9.5 \
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). \
@@ -57,6 +58,7 @@ ENV CONTAINER_SCRIPTS_PATH=/usr/share/container-scripts/postgresql \
5758
ENABLED_COLLECTIONS=rh-postgresql96
5859

5960
COPY root /
61+
COPY ./s2i/bin/ $STI_SCRIPTS_PATH
6062

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

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

72+
RUN chown -R 26:26 ${APP_DATA}
7073
USER 26
7174

7275
ENTRYPOINT ["container-entrypoint"]

latest/Dockerfile.rhel7

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

33
# PostgreSQL image for OpenShift.
44
# Volumes:
@@ -13,7 +13,8 @@ FROM rhel7
1313
ENV POSTGRESQL_VERSION=9.6 \
1414
POSTGRESQL_PREV_VERSION=9.5 \
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-postgresql96
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,7 @@ ENV BASH_ENV=${CONTAINER_SCRIPTS_PATH}/scl_enable \
7072

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

75+
RUN chown -R 26:26 ${APP_DATA}
7376
USER 26
7477

7578
ENTRYPOINT ["container-entrypoint"]

latest/root/usr/bin/run-postgresql

+5
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ else
2222
try_pgupgrade
2323
fi
2424

25+
process_extending_files ${APP_DATA}/src/pre-init ${CONTAINER_SCRIPTS_PATH}/pre-init
26+
2527
pg_ctl -w start -o "-h ''"
2628
if $PG_INITIALIZED ; then
2729
migrate_db
2830
create_users
2931
fi
3032
set_passwords
33+
34+
process_extending_files ${APP_DATA}/src/init ${CONTAINER_SCRIPTS_PATH}/init
35+
3136
pg_ctl stop
3237

3338
unset_env_vars

latest/root/usr/bin/run-postgresql-slave

+4
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ check_env_vars
2828
generate_passwd_file
2929
generate_postgresql_config
3030

31+
process_extending_files ${APP_DATA}/src/pre-init ${CONTAINER_SCRIPTS_PATH}/pre-init
32+
3133
wait_for_postgresql_master
3234
export MASTER_FQDN=$(postgresql_master_addr)
3335
initialize_replica
3436

37+
process_extending_files ${APP_DATA}/src/init ${CONTAINER_SCRIPTS_PATH}/init
38+
3539
unset_env_vars
3640
exec postgres "$@"

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

+46
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,52 @@ 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 files will be included at the end of image postgresql.conf file
258+
259+
##### `pre-init/`
260+
261+
contained shell scripts (`*.sh`) are sourced before `service` is started
262+
263+
##### `init/`
264+
265+
contained shell scripts (`*.sh`) are sourced after `service` is
266+
started
267+
268+
----------------------------------------------
269+
270+
During `s2i build` all provided files are copied into `/opt/app-root/src`
271+
directory in the new image. If some configuration files are present in
272+
destination directory, files with the same name are overwritten. Also only one
273+
file with the same name can be used for customization and user provided files
274+
are preferred over default files in `/usr/share/container-scripts/`-
275+
so it is possible to overwrite them.
276+
277+
Same configuration directory structure can be used to customize the image
278+
every time the image is started using `docker run`. The directory have to be
279+
mounted into `/opt/app-root/src/` in the image (`-v
280+
./image-configuration/:/opt/app-root/src/`). This overwrites customization
281+
built into the image.
282+
283+
238284
Troubleshooting
239285
---------------
240286
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:

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

+29
Original file line numberDiff line numberDiff line change
@@ -415,3 +415,32 @@ try_pgupgrade ()
415415

416416
run_pgupgrade
417417
}
418+
419+
# get_matched_files finds file for image extending
420+
function get_matched_files() {
421+
local custom_dir default_dir
422+
custom_dir="$1"
423+
default_dir="$2"
424+
files_matched="$3"
425+
find "$default_dir" -maxdepth 1 -type f -name "$files_matched" -printf "%f\n"
426+
[ -d "$custom_dir" ] && find "$custom_dir" -maxdepth 1 -type f -name "$files_matched" -printf "%f\n"
427+
}
428+
429+
# process_extending_files process extending files in $1 and $2 directories
430+
# - source all *.sh files
431+
# (if there are files with same name source only file from $1)
432+
function process_extending_files() {
433+
local custom_dir default_dir
434+
custom_dir=$1
435+
default_dir=$2
436+
437+
while read filename ; do
438+
echo "=> sourcing $filename ..."
439+
# Custom file is prefered
440+
if [ -f $custom_dir/$filename ]; then
441+
source $custom_dir/$filename
442+
elif [ -f $default_dir/$filename ]; then
443+
source $default_dir/$filename
444+
fi
445+
done <<<"$(get_matched_files "$custom_dir" "$default_dir" '*.sh' | sort -u)"
446+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
cat >> "$PGDATA/postgresql.conf" <<EOF
4+
5+
# Custom extending configuration
6+
EOF
7+
8+
extending_cfg_dir="${APP_DATA}/src/postgresql-config"
9+
10+
for conf in $(ls ${extending_cfg_dir}); do
11+
cat >> "$PGDATA/postgresql.conf" <<EOF
12+
include '$extending_cfg_dir/${conf}'
13+
EOF
14+
done

latest/s2i/bin/assemble

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
mv /tmp/src/* ./
10+
11+
# Fix source directory permissions
12+
/usr/libexec/fix-permissions ./

latest/s2i/bin/run

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
/usr/bin/run-postgresql

latest/s2i/bin/usage

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

0 commit comments

Comments
 (0)