Skip to content

Commit 280bf67

Browse files
Add init-script (#14)
Signed-off-by: HiranmoyChowdhury <[email protected]>
1 parent 108a7d5 commit 280bf67

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ RUN set -x \
3131
ADD runit /runit
3232
ADD fsloader /fsloader
3333
ADD pgbouncer /pgbouncer
34+
ADD start /start
3435

3536
RUN chmod +x /fsloader/* \
3637
&& chmod +x /pgbouncer/* \
3738
&& chmod +x /runit/* \
39+
&& chmod +x /start/* \
3840
&& mkdir -p /etc/service/fsloader \
3941
&& mkdir -p /etc/service/pgbouncer \
40-
&& chown -R postgres /fsloader/* /etc/service/fsloader /pgbouncer/* /etc/service/pgbouncer /runit/*
42+
&& chown -R postgres /fsloader/* /etc/service/fsloader /pgbouncer/* /etc/service/pgbouncer /runit/* /start/*
4143

4244
RUN chgrp -R 0 /etc/service/fsloader && \
4345
chmod -R g=u /etc/service/fsloader
@@ -49,4 +51,4 @@ USER postgres
4951
RUN ln -s /fsloader/run /etc/service/fsloader/run \
5052
&& ln -s /pgbouncer/run /etc/service/pgbouncer/run
5153

52-
ENTRYPOINT ["/runit/run_runit.sh"]
54+
ENTRYPOINT ["/start/run.sh"]

start/run.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/sh -e
2+
3+
(
4+
if [ -d "/init" ]; then
5+
echo "INIT: Init Directory Exists"
6+
# Wait for PgBouncer to be ready
7+
SSLMODE=$(grep -i "client_tls_sslmode" /etc/config/pgbouncer.ini | cut -c 22-)
8+
AUTHTYPE=$(grep -i "auth_type" /etc/config/pgbouncer.ini | cut -c 13-)
9+
PASSWORD=$(cat /var/run/pgbouncer/secret/password)
10+
USERNAME=$(cat /var/run/pgbouncer/secret/username)
11+
if [[ "$SSLMODE" == "verify-full" ]] || [[ "$SSLMODE" == "verify-ca" ]] || [[ "$AUTHTYPE" == "cert" ]]; then
12+
args="host=localhost port=$PGBOUNCER_LISTEN_PORT user=$USERNAME password=$PASSWORD sslmode=$SSLMODE sslrootcert=/var/run/pgbouncer/tls/serving/client/ca.crt sslcert=/var/run/pgbouncer/tls/serving/client/tls.crt sslkey=/var/run/pgbouncer/tls/serving/client/tls.key dbname=pgbouncer"
13+
elif [[ "$SSLMODE" == "require" ]]; then
14+
args="host=localhost port=$PGBOUNCER_LISTEN_PORT user=$USERNAME password=$PASSWORD sslmode=$SSLMODE sslrootcert=/var/run/pgbouncer/tls/serving/client/ca.crt dbname=pgbouncer"
15+
else
16+
args="host=localhost port=$PGBOUNCER_LISTEN_PORT user=$USERNAME password=$PASSWORD dbname=pgbouncer"
17+
fi
18+
19+
until pg_isready -d "$args"; do
20+
echo "INIT: Waiting for PgBouncer to be ready..."
21+
sleep 2
22+
done
23+
echo "INIT: PgBouncer is ready!"
24+
25+
# Run initialization scripts
26+
cd /init-scripts || true
27+
for file in /init-scripts/*
28+
do
29+
case "$file" in
30+
*.sh)
31+
echo "INIT: Running user provided initialization shell script $file"
32+
sh "$file"
33+
;;
34+
*.lua)
35+
echo "INIT: Running user provided initialization lua script $file"
36+
# Adjust for PgBouncer if needed (e.g., psql commands)
37+
;;
38+
esac
39+
done
40+
fi
41+
) &
42+
# Start runit with exec to ensure it becomes PID 1
43+
exec /runit/run_runit.sh

0 commit comments

Comments
 (0)