forked from dogukancagatay/docker-pwless-pgadmin4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
primary_entrypoint.sh
72 lines (63 loc) · 2.15 KB
/
primary_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
#!/usr/bin/env sh
# set -x
set -e
## Create /var/lib/pgadmin/pgpass
# 1st database
echo "$POSTGRES_HOST:$POSTGRES_PORT:$POSTGRES_DB:$POSTGRES_USER:$POSTGRES_PASSWORD" | tee "/var/lib/pgadmin/pgpass" >/dev/null
POSTGRES_HOST_1=$POSTGRES_HOST
POSTGRES_PORT_1=$POSTGRES_PORT
POSTGRES_DB_1=$POSTGRES_DB
POSTGRES_USER_1=$POSTGRES_USER
POSTGRES_PASSWORD_1=$POSTGRES_PASSWORD
## Create servers.json
tee /pgadmin4/servers.json >/dev/null <<EOF
{
"Servers": {
"1": {
"Name": "$POSTGRES_HOST_1",
"Group": "Servers",
"Host": "$POSTGRES_HOST_1",
"Port": $POSTGRES_PORT_1,
"MaintenanceDB": "postgres",
"Username": "$POSTGRES_USER_1",
"SSLMode": "prefer",
"PassFile": "/var/lib/pgadmin/pgpass"
}
EOF
# if there are more than 1 database then
# loop through environment variables and create password files
COUNT=2
while [ ! -z "$(eval echo \"\$POSTGRES_HOST_$COUNT\")" ]; do
POSTGRES_HOST="$(eval echo \"\$POSTGRES_HOST_$COUNT\")"
POSTGRES_PORT="$(eval echo \"\$POSTGRES_PORT_$COUNT\")"
# if POSTGRES_DB, default is "*"
POSTGRES_DB="$(eval echo \"\$POSTGRES_DB_$COUNT\")"
if [ -z "$POSTGRES_DB" ]; then
POSTGRES_DB="*"
fi
POSTGRES_USER="$(eval echo \"\$POSTGRES_USER_$COUNT\")"
POSTGRES_PASSWORD="$(eval echo \"\$POSTGRES_PASSWORD_$COUNT\")"
echo "$POSTGRES_HOST:$POSTGRES_PORT:$POSTGRES_DB:$POSTGRES_USER:$POSTGRES_PASSWORD" | tee "/var/lib/pgadmin/pgpass_$COUNT" >/dev/null
tee -a /pgadmin4/servers.json >/dev/null <<EOF
,"$COUNT": {
"Name": "$POSTGRES_HOST",
"Group": "Servers",
"Host": "$POSTGRES_HOST",
"Port": $POSTGRES_PORT,
"MaintenanceDB": "postgres",
"Username": "$POSTGRES_USER",
"SSLMode": "prefer",
"PassFile": "/var/lib/pgadmin/pgpass_$COUNT"
}
EOF
COUNT=$((COUNT + 1))
done
# close servers.json
tee -a /pgadmin4/servers.json >/dev/null <<EOF
}
}
EOF
chmod 600 `ls /var/lib/pgadmin/pgpass*`
chown pgadmin:root `ls /var/lib/pgadmin/pgpass*`
chown pgadmin:root /pgadmin4/servers.json
exec /entrypoint.sh "$@"