forked from render-oss/docker-pgbouncer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·140 lines (113 loc) · 3.56 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/sh -e
# Based on https://raw.githubusercontent.com/brainsam/pgbouncer/master/entrypoint.sh
set -e
# Here are some parameters. See all on
# https://pgbouncer.github.io/config.html
PG_CONFIG_DIR=/etc/pgbouncer
if [ -n "$DATABASE_URL" ]; then
# Thanks to https://stackoverflow.com/a/17287984/146289
# Allow to pass values like dj-database-url / django-environ accept
proto="$(echo $DATABASE_URL | grep :// | sed -e's,^\(.*://\).*,\1,g')"
url="$(echo $DATABASE_URL | sed -e s,$proto,,g)"
# extract the user and password (if any)
userpass=$(echo $url | grep @ | sed -r 's/^(.*)@([^@]*)$/\1/')
DB_PASSWORD="$(echo $userpass | grep : | cut -d: -f2)"
if [ -n "$DB_PASSWORD" ]; then
DB_USER=$(echo $userpass | grep : | cut -d: -f1)
else
DB_USER=$userpass
fi
# extract the host -- updated
hostport=`echo $url | sed -e s,$userpass@,,g | cut -d/ -f1`
port=`echo $hostport | grep : | cut -d: -f2`
if [ -n "$port" ]; then
DB_HOST=`echo $hostport | grep : | cut -d: -f1`
DB_PORT=$port
else
DB_HOST=$hostport
fi
DB_NAME="$(echo $url | grep / | cut -d/ -f2-)"
fi
if [ -z "${POOL_MODE}" ]; then
echo "Error: POOL_MODE is not set"
exit 1
fi
if [ -z "${WEB_POOL_SIZE}" ]; then
echo "Error: WEB_POOL_SIZE is not set"
exit 1
fi
if [ -z "${JOB_POOL_SIZE}" ]; then
echo "Error: JOB_POOL_SIZE is not set"
exit 1
fi
if [ -z "${CONSOLE_POOL_SIZE}" ]; then
echo "Error: CONSOLE_POOL_SIZE is not set"
exit 1
fi
if [ -z "${UTIL_POOL_SIZE}" ]; then
echo "Error: UTIL_POOL_SIZE is not set"
exit 1
fi
if [ -z "${DB_HOST}" ]; then
echo "Error: DB_HOST is not set"
exit 1
fi
if [ -z "${DB_PORT}" ]; then
echo "Error: DB_PORT is not set"
exit 1
fi
if [ -z "${DB_USER}" ]; then
echo "Error: DB_USER is not set"
exit 1
fi
if [ -z "${DB_NAME}" ]; then
echo "Error: DB_NAME is not set"
exit 1
fi
if [ -z "${PORT}" ]; then
echo "Error: PORT is not set"
exit 1
fi
if [ -z "${STATS_USER}" ]; then
echo "Error: STATS_USER is not set"
exit 1
fi
if [ -z "${STATS_PASSWORD}" ]; then
echo "Error: STATS_PASSWORD is not set"
exit 1
fi
# Write the password with MD5 encryption, to avoid printing it during startup.
# Notice that `docker inspect` will show unencrypted env variables.
_AUTH_FILE="${AUTH_FILE:-$PG_CONFIG_DIR/userlist.txt}"
# Workaround userlist.txt missing issue
# https://github.com/edoburu/docker-pgbouncer/issues/33
if [ ! -e "${_AUTH_FILE}" ]; then
touch "${_AUTH_FILE}"
fi
if [ -n "$DB_USER" -a -n "$DB_PASSWORD" -a -e "${_AUTH_FILE}" ] && ! grep -q "^\"$DB_USER\"" "${_AUTH_FILE}"; then
if [ "$AUTH_TYPE" != "plain" ]; then
pass="md5$(echo -n "$DB_PASSWORD$DB_USER" | md5sum | cut -f 1 -d ' ')"
else
pass="$DB_PASSWORD"
fi
pass="$DB_PASSWORD"
echo "\"$DB_USER\" \"$pass\"" >> ${PG_CONFIG_DIR}/userlist.txt
echo "\"$STATS_USER\" \"$STATS_PASSWORD\"" >> ${PG_CONFIG_DIR}/userlist.txt
echo "Wrote authentication credentials to ${PG_CONFIG_DIR}/userlist.txt"
fi
sed -e "s/\${DB_HOST}/$DB_HOST/g" \
-e "s/\${DB_NAME}/$DB_NAME/g" \
-e "s/\${DB_PORT}/$DB_PORT/g" \
-e "s/\${DB_USER}/$DB_USER/g" \
-e "s/\${POOL_MODE}/$POOL_MODE/g" \
-e "s/\${WEB_POOL_SIZE}/$WEB_POOL_SIZE/g" \
-e "s/\${JOB_POOL_SIZE}/$JOB_POOL_SIZE/g" \
-e "s/\${CONSOLE_POOL_SIZE}/$CONSOLE_POOL_SIZE/g" \
-e "s/\${UTIL_POOL_SIZE}/$UTIL_POOL_SIZE/g" \
-e "s/\${PORT}/$PORT/g" \
-e "s/\${STATS_USER}/$STATS_USER/g" \
-e "s/\${STATS_PASSWORD}/$STATS_PASSWORD/g" \
${PG_CONFIG_DIR}/pgbouncer.ini.template > ${PG_CONFIG_DIR}/pgbouncer.ini
cat ${PG_CONFIG_DIR}/pgbouncer.ini
echo "Starting $*..."
exec "$@"