Skip to content

Commit 6d49b8b

Browse files
committed
Add 'postgresql-pre-start' hook
- also cleanup some stderr output caused by missing '../init' and '../pre-start' directory - add missing double-quotes
1 parent 5ee8403 commit 6d49b8b

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

src/root/usr/bin/run-postgresql

+11-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ export_vars=$(cgroup-limits) ; export $export_vars
88
source "${CONTAINER_SCRIPTS_PATH}/common.sh"
99

1010
set_pgdata
11+
12+
process_extending_files \
13+
"${APP_DATA}/src/postgresql-pre-start" \
14+
"${CONTAINER_SCRIPTS_PATH}/pre-start"
15+
1116
check_env_vars
1217
generate_passwd_file
1318
generate_postgresql_config
@@ -24,12 +29,16 @@ fi
2429

2530
pg_ctl -w start -o "-h ''"
2631
if $PG_INITIALIZED ; then
27-
process_extending_files ${APP_DATA}/src/postgresql-init ${CONTAINER_SCRIPTS_PATH}/init
32+
process_extending_files \
33+
"${APP_DATA}/src/postgresql-init" \
34+
"${CONTAINER_SCRIPTS_PATH}/init"
2835
migrate_db
2936
create_users
3037
fi
3138

32-
process_extending_files ${APP_DATA}/src/postgresql-start ${CONTAINER_SCRIPTS_PATH}/start
39+
process_extending_files \
40+
"${APP_DATA}/src/postgresql-start" \
41+
"${CONTAINER_SCRIPTS_PATH}/start"
3342

3443
pg_ctl stop
3544

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

+7
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,13 @@ $ s2i build ~/image-configuration/ postgresql new-postgresql
252252
The directory passed to `s2i build` should contain one or more of the
253253
following directories:
254254

255+
256+
##### `postgresql-pre-start/`
257+
258+
Source all `*.sh` files from this directory during early start of the
259+
container. There's no PostgreSQL daemon running on background.
260+
261+
255262
##### `postgresql-cfg/`
256263

257264
Contained configuration files (`*.conf`) will be included at the end of image

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

+27-24
Original file line numberDiff line numberDiff line change
@@ -408,31 +408,34 @@ try_pgupgrade ()
408408
run_pgupgrade
409409
}
410410

411-
# get_matched_files finds file for image extending
412-
function get_matched_files() {
413-
local custom_dir default_dir
414-
custom_dir="$1"
415-
default_dir="$2"
416-
files_matched="$3"
417-
find "$default_dir" -maxdepth 1 -type f -name "$files_matched" -printf "%f\n"
418-
[ -d "$custom_dir" ] && find "$custom_dir" -maxdepth 1 -type f -name "$files_matched" -printf "%f\n"
411+
# get_matched_files PATTERN DIR [DIR ...]
412+
# ---------------------------------------
413+
# Print all basenames for files matching PATTERN in DIRs.
414+
get_matched_files ()
415+
{
416+
local pattern=$1 dir
417+
shift
418+
for dir; do
419+
test -d "$dir" || continue
420+
find "$dir" -maxdepth 1 -type f -name "$pattern" -printf "%f\n"
421+
done
419422
}
420423

421-
# process_extending_files process extending files in $1 and $2 directories
422-
# - source all *.sh files
423-
# (if there are files with same name source only file from $1)
424-
function process_extending_files() {
425-
local custom_dir default_dir
426-
custom_dir=$1
427-
default_dir=$2
428-
424+
# process_extending_files DIR [DIR ...]
425+
# -------------------------------------
426+
# Source all *.sh files in DIRs in alphabetical order, but if the file exists in
427+
# more then one DIR, source only the first occurrence (first found wins).
428+
process_extending_files()
429+
{
430+
local filename dir
429431
while read filename ; do
430-
echo "=> sourcing $filename ..."
431-
# Custom file is prefered
432-
if [ -f $custom_dir/$filename ]; then
433-
source $custom_dir/$filename
434-
elif [ -f $default_dir/$filename ]; then
435-
source $default_dir/$filename
436-
fi
437-
done <<<"$(get_matched_files "$custom_dir" "$default_dir" '*.sh' | sort -u)"
432+
for dir in "$@"; do
433+
local file="$dir/$filename"
434+
if test -f "$file"; then
435+
echo "=> sourcing $file ..."
436+
source "$file"
437+
break
438+
fi
439+
done
440+
done <<<"$(get_matched_files '*.sh' "$@" | sort -u)"
438441
}

0 commit comments

Comments
 (0)