-
Notifications
You must be signed in to change notification settings - Fork 217
Description
What I'm trying to achieve
- I want to deploy a postgres instance and have this instance prepared with some SQL statements (
CREATE TABLE ...
). - I need to provision this postgres instance directly from the
oc new-app
command, without creating a GIT repository and going through the S2I build.
Steps to reproduce
Deploy a postgres instance:
oc new-app --name=postgresql --template=postgresql-persistent
Provision the start hook script
cat <<EOF > start.sh
echo ">>> Starting database schema creation"
echo "Here I would call 'psql bla bla bla'..."
echo "<<< Finished database schema creation"
EOF
oc create configmap postgresql-init --from-file=start.sh
Mount it on /opt/app-root/src/postgresql-start
:
oc set volume dc/postgresql --add -t configmap --configmap-name=postgresql-init -m /opt/app-root/src/postgresql-start --name postgresql-init
Expected behavior
$ oc logs dc/postgresql
waiting for server to start....LOG: redirecting log output to logging collector process
HINT: Future log output will appear in directory "pg_log".
done
server started
/var/run/postgresql:5432 - accepting connections
=> sourcing /usr/share/container-scripts/postgresql/start/set_passwords.sh ...
ALTER ROLE
=> sourcing /opt/app-root/src/postgresql-start/start.sh ...
>>> Starting database schema creation
Here I would call 'psql bla bla bla'...
<<< Finished database schema creation
waiting for server to shut down.... done
server stopped
Starting server...
LOG: redirecting log output to logging collector process
HINT: Future log output will appear in directory "pg_log".
Current behavior
$ oc logs dc/postgresql
waiting for server to start....LOG: redirecting log output to logging collector process
HINT: Future log output will appear in directory "pg_log".
done
server started
/var/run/postgresql:5432 - accepting connections
=> sourcing /usr/share/container-scripts/postgresql/start/set_passwords.sh ...
ALTER ROLE
waiting for server to shut down.... done
server stopped
Starting server...
LOG: redirecting log output to logging collector process
HINT: Future log output will appear in directory "pg_log".
=> My start hook script is not loaded
Troubleshooting
In the container, I can see that my configmap has been correctly mounted:
$ ls /opt/app-root/src/postgresql-start/
start.sh
But the call to process_extending_files
does not include my script:
$ . /usr/share/container-scripts/postgresql/common.sh
$ process_extending_files "${APP_DATA}/src/postgresql-start" "${CONTAINER_SCRIPTS_PATH}/start"
=> sourcing /usr/share/container-scripts/postgresql/start/set_passwords.sh ...
The call to get_matched_files
does not include my script either:
$ get_matched_files '*.sh' "${APP_DATA}/src/postgresql-start" "${CONTAINER_SCRIPTS_PATH}/start" | sort -u
set_passwords.sh
After further investigation, it appears that the configmap creates symlinks instead of regular files:
$ ls /opt/app-root/src/postgresql-start/ -la
total 4
drwxrwsrwx. 3 root 1000770000 4096 Nov 15 18:19 .
drwxrwxr-x. 1 postgres root 41 Nov 15 18:10 ..
drwxr-sr-x. 2 root 1000770000 234 Nov 15 18:19 ..2019_11_15_18_19_11.168320828
lrwxrwxrwx. 1 root 1000770000 31 Nov 15 18:19 ..data -> ..2019_11_15_18_19_11.168320828
lrwxrwxrwx. 1 root root 15 Nov 15 18:10 start.sh -> ..data/start.sh
start.sh
is a symlink to the proper file:
$ cat /opt/app-root/src/postgresql-start/start.sh
echo ">>> Starting database schema creation"
echo "Here I would call 'psql bla bla bla'..."
echo "<<< Finished database schema creation"
And the find command in the get_matched_files function in common.sh is missing the -L
switch to follow symlinks. Hence, the test -type f
leads to false in my case.
Resolution
Add the -L
switch to the find command in the get_matched_files function in common.sh:
# get_matched_files PATTERN DIR [DIR ...]
# ---------------------------------------
# Print all basenames for files matching PATTERN in DIRs.
get_matched_files ()
{
local pattern=$1 dir
shift
for dir; do
test -d "$dir" || continue
find -L "$dir" -maxdepth 1 -type f -name "$pattern" -printf "%f\n"
done
}
And then it works:
$ get_matched_files '*.sh' "${APP_DATA}/src/postgresql-start" "${CONTAINER_SCRIPTS_PATH}/start" | sort -u
set_passwords.sh
start.sh
$ process_extending_files "${APP_DATA}/src/postgresql-start" "${CONTAINER_SCRIPTS_PATH}/start"
=> sourcing /usr/share/container-scripts/postgresql/start/set_passwords.sh ...
=> sourcing /opt/app-root/src/postgresql-start/start.sh ...
>>> Starting database schema creation
Here I would call 'psql bla bla bla'...
<<< Finished database schema creation