forked from mieckert/keycloak-heroku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-entrypoint.sh
executable file
·192 lines (159 loc) · 5.05 KB
/
docker-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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/bin/bash
# Set database config from Heroku DATABASE_URL
if [ "$DATABASE_URL" != "" ] && [ "$DB_ADDR" == "" ]; then
echo "Found database configuration in DATABASE_URL=$DATABASE_URL"
regex='^postgres://([a-zA-Z0-9_-]+):([a-zA-Z0-9]+)@([a-z0-9.-]+):([[:digit:]]+)/([a-zA-Z0-9_-]+)$'
if [[ $DATABASE_URL =~ $regex ]]; then
export DB_ADDR=${BASH_REMATCH[3]}
export DB_PORT=${BASH_REMATCH[4]}
export DB_DATABASE=${BASH_REMATCH[5]}
export DB_USER=${BASH_REMATCH[1]}
export DB_PASSWORD=${BASH_REMATCH[2]}
echo "DB_ADDR=$DB_ADDR, DB_PORT=$DB_PORT, DB_DATABASE=$DB_DATABASE, DB_USER=$DB_USER, DB_PASSWORD=$DB_PASSWORD"
export DB_VENDOR=postgres
fi
fi
# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
exit 1
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}
##################
# Add admin user #
##################
file_env 'KEYCLOAK_USER'
file_env 'KEYCLOAK_PASSWORD'
if [ $KEYCLOAK_USER ] && [ $KEYCLOAK_PASSWORD ]; then
/opt/jboss/keycloak/bin/add-user-keycloak.sh --user $KEYCLOAK_USER --password $KEYCLOAK_PASSWORD
fi
############
# Hostname #
############
if [ "$KEYCLOAK_HOSTNAME" != "" ]; then
SYS_PROPS="-Dkeycloak.hostname.provider=fixed -Dkeycloak.hostname.fixed.hostname=$KEYCLOAK_HOSTNAME"
if [ "$KEYCLOAK_HTTP_PORT" != "" ]; then
SYS_PROPS+=" -Dkeycloak.hostname.fixed.httpPort=$KEYCLOAK_HTTP_PORT"
fi
if [ "$KEYCLOAK_HTTPS_PORT" != "" ]; then
SYS_PROPS+=" -Dkeycloak.hostname.fixed.httpsPort=$KEYCLOAK_HTTPS_PORT"
fi
fi
################
# Realm import #
################
if [ "$KEYCLOAK_IMPORT" ]; then
SYS_PROPS+=" -Dkeycloak.import=$KEYCLOAK_IMPORT"
fi
########################
# JGroups bind options #
########################
if [ -z "$BIND" ]; then
BIND=$(hostname -i)
fi
if [ -z "$BIND_OPTS" ]; then
for BIND_IP in $BIND
do
BIND_OPTS+=" -Djboss.bind.address=$BIND_IP -Djboss.bind.address.private=$BIND_IP "
done
fi
SYS_PROPS+=" $BIND_OPTS"
#################
# Configuration #
#################
# If the server configuration parameter is not present, append the HA profile.
if echo "$@" | egrep -v -- '-c |-c=|--server-config |--server-config='; then
SYS_PROPS+=" -c=standalone-ha.xml"
fi
############
# DB setup #
############
file_env 'DB_USER'
file_env 'DB_PASSWORD'
# Lower case DB_VENDOR
DB_VENDOR=`echo $DB_VENDOR | tr A-Z a-z`
# Detect DB vendor from default host names
if [ "$DB_VENDOR" == "" ]; then
if (getent hosts postgres &>/dev/null); then
export DB_VENDOR="postgres"
elif (getent hosts mysql &>/dev/null); then
export DB_VENDOR="mysql"
elif (getent hosts mariadb &>/dev/null); then
export DB_VENDOR="mariadb"
fi
fi
# Detect DB vendor from legacy `*_ADDR` environment variables
if [ "$DB_VENDOR" == "" ]; then
if (printenv | grep '^POSTGRES_ADDR=' &>/dev/null); then
export DB_VENDOR="postgres"
elif (printenv | grep '^MYSQL_ADDR=' &>/dev/null); then
export DB_VENDOR="mysql"
elif (printenv | grep '^MARIADB_ADDR=' &>/dev/null); then
export DB_VENDOR="mariadb"
fi
fi
# Default to H2 if DB type not detected
if [ "$DB_VENDOR" == "" ]; then
export DB_VENDOR="h2"
fi
# Set DB name
case "$DB_VENDOR" in
postgres)
DB_NAME="PostgreSQL";;
mysql)
DB_NAME="MySQL";;
mariadb)
DB_NAME="MariaDB";;
h2)
DB_NAME="Embedded H2";;
*)
echo "Unknown DB vendor $DB_VENDOR"
exit 1
esac
# Append '?' in the beggining of the string if JDBC_PARAMS value isn't empty
export JDBC_PARAMS=$(echo ${JDBC_PARAMS} | sed '/^$/! s/^/?/')
# Convert deprecated DB specific variables
function set_legacy_vars() {
local suffixes=(ADDR DATABASE USER PASSWORD PORT)
for suffix in "${suffixes[@]}"; do
local varname="$1_$suffix"
if [ ${!varname} ]; then
echo WARNING: $varname variable name is DEPRECATED replace with DB_$suffix
export DB_$suffix=${!varname}
fi
done
}
set_legacy_vars `echo $DB_VENDOR | tr a-z A-Z`
# Configure DB
echo "========================================================================="
echo ""
echo " Using $DB_NAME database"
echo ""
echo "========================================================================="
echo ""
if [ "$DB_VENDOR" != "h2" ]; then
/bin/sh /opt/jboss/tools/databases/change-database.sh $DB_VENDOR
fi
/opt/jboss/tools/x509.sh
/opt/jboss/tools/jgroups.sh $JGROUPS_DISCOVERY_PROTOCOL $JGROUPS_DISCOVERY_PROPERTIES
/opt/jboss/tools/autorun.sh
##################
# Start Keycloak #
##################
exec /opt/jboss/keycloak/bin/standalone.sh $SYS_PROPS $@ -Djboss.http.port=$PORT
exit $?