forked from crramirez/limesurvey
-
Notifications
You must be signed in to change notification settings - Fork 50
/
docker-entrypoint.sh
executable file
·266 lines (233 loc) · 11.7 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/bin/bash
set -eu
cd /var/www/html
# 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"
}
if [[ "$1" == apache2* ]] || [ "$1" == php-fpm ]; then
file_env 'LIMESURVEY_DB_TYPE' 'mysql'
file_env 'LIMESURVEY_DB_HOST' 'mysql'
file_env 'LIMESURVEY_DB_PORT' '3306'
file_env 'LIMESURVEY_TABLE_PREFIX' ''
file_env 'LIMESURVEY_ADMIN_NAME' 'Lime Administrator'
file_env 'LIMESURVEY_ADMIN_EMAIL' '[email protected]'
file_env 'LIMESURVEY_ADMIN_USER' ''
file_env 'LIMESURVEY_ADMIN_PASSWORD' ''
file_env 'LIMESURVEY_SMTP_HOST' ''
file_env 'LIMESURVEY_SMTP_USER' ''
file_env 'LIMESURVEY_SMTP_PASSWORD' ''
file_env 'LIMESURVEY_SMTP_SSL' ''
file_env 'LIMESURVEY_DEBUG' '0'
file_env 'LIMESURVEY_SMTP_DEBUG' ''
file_env 'LIMESURVEY_SQL_DEBUG' '0'
file_env 'MYSQL_SSL_CA' ''
file_env 'LIMESURVEY_USE_INNODB' ''
file_env 'LIMESURVEY_USE_DB_SESSIONS' ''
file_env 'LIMESURVEY_DONT_SHOW_SCRIPT_NAME' ''
file_env 'LIMESURVEY_PHP_SESSION_SAVE_HANDLER' ''
file_env 'LIMESURVEY_PHP_SESSION_SAVE_PATH' ''
file_env 'LIMESURVEY_DONT_UPDATE' ''
file_env 'LIMESURVEY_API_MODE' 'off'
if [ -z "$LIMESURVEY_DONT_UPDATE" ]; then
# if we're linked to MySQL and thus have credentials already, let's use them
file_env 'LIMESURVEY_DB_USER' "${MYSQL_ENV_MYSQL_USER:-root}"
if [ "$LIMESURVEY_DB_USER" = 'root' ]; then
file_env 'LIMESURVEY_DB_PASSWORD' "${MYSQL_ENV_MYSQL_ROOT_PASSWORD:-}"
else
file_env 'LIMESURVEY_DB_PASSWORD' "${MYSQL_ENV_MYSQL_PASSWORD:-}"
fi
file_env 'LIMESURVEY_DB_NAME' "${MYSQL_ENV_MYSQL_DATABASE:-limesurvey}"
if [ -z "$LIMESURVEY_DB_PASSWORD" ]; then
echo >&2 'error: missing required LIMESURVEY_DB_PASSWORD environment variable'
echo >&2 ' Did you forget to -e LIMESURVEY_DB_PASSWORD=... ?'
echo >&2
echo >&2 ' (Also of interest might be LIMESURVEY_DB_USER and LIMESURVEY_DB_NAME.)'
exit 1
fi
chmod ug+w -R application/config
echo >&2 "Copying default container default config files into config volume..."
cp -dpRf /var/lime/application/config/* application/config
if ! [ -e plugins/index.html ]; then
echo >&2 "No index.html file in plugins dir in $(pwd) Copying defaults..."
cp -dpR /var/lime/plugins/* plugins
fi
if ! [ -e tmp/index.html ]; then
echo >&2 "No index.html file in tmp dir in $(pwd) Copying defaults..."
cp -dpR /var/lime/tmp/* tmp
fi
if ! [ -e upload/index.html ]; then
echo >&2 "No index.html file upload dir in $(pwd) Copying defaults..."
cp -dpR /var/lime/upload/* upload
fi
if ! [ -e application/config/config.php ]; then
echo >&2 "No config file in $(pwd) Copying default config file..."
#Copy default config file but also allow for the addition of attributes
awk '/lime_/ && c == 0 { c = 1; system("cat") } { print }' application/config/config-sample-mysql.php > application/config/config.php <<'EOPHP'
'attributes' => array(),
EOPHP
# Add default email config, so it can be overriden later
sed -i "/'config'=>array/s/$/\n'siteadminemail' => '[email protected]',\n'siteadminbounce' => '[email protected]',\n'siteadminname' => 'Your Name',\n'emailmethod' => 'mail',\n'emailsmtphost' => 'localhost',\n'emailsmtpuser' => '',\n'emailsmtppassword' => '',\n'emailsmtpssl' => '',\n'emailsmtpdebug' => '',\n'RPCInterface' => 'off',/" application/config/config.php
fi
# see http://stackoverflow.com/a/2705678/433558
sed_escape_lhs() {
echo "$@" | sed -e 's/[]\/$*.^|[]/\\&/g'
}
sed_escape_rhs() {
echo "$@" | sed -e 's/[\/&]/\\&/g'
}
php_escape() {
php -r 'var_export(('$2') $argv[1]);' -- "$1"
}
set_config() {
key="$1"
value="$2"
sed -i "/'$key'/s>\(.*\)>$value,1" application/config/config.php
}
LIMESURVEY_DB_CHARSET="utf8mb4"
if [ $LIMESURVEY_DB_TYPE == "pgsql" ]; then
LIMESURVEY_DB_CHARSET="utf8"
fi
set_config 'connectionString' "'$LIMESURVEY_DB_TYPE:host=$LIMESURVEY_DB_HOST;port=$LIMESURVEY_DB_PORT;dbname=$LIMESURVEY_DB_NAME;'"
set_config 'charset' "'$LIMESURVEY_DB_CHARSET'"
set_config 'tablePrefix' "'$LIMESURVEY_TABLE_PREFIX'"
set_config 'username' "'$LIMESURVEY_DB_USER'"
set_config 'password' "'$LIMESURVEY_DB_PASSWORD'"
set_config 'urlFormat' "'path'"
set_config 'debug' "$LIMESURVEY_DEBUG"
set_config 'debugsql' "$LIMESURVEY_SQL_DEBUG"
set_config 'RPCInterface' "'$LIMESURVEY_API_MODE'"
set_config 'showScriptName' "true"
if [ -n "$LIMESURVEY_DONT_SHOW_SCRIPT_NAME" ]; then
set_config 'showScriptName' "false"
fi
if [ -n "$MYSQL_SSL_CA" ]; then
set_config 'attributes' "array(PDO::MYSQL_ATTR_SSL_CA => '\/var\/www\/html\/$MYSQL_SSL_CA', PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false)"
fi
#Remove session line if exists
sed -i "/^'session'/d" application/config/config.php
if [ -n "$LIMESURVEY_USE_DB_SESSIONS" ]; then
#Add session line
awk '/DbHttpSession/ && c == 0 { c = 1; system("cat") } { print }' application/config/config.php > application/config/config.tmp <<'EOPHP'
'session' => array ('class' => 'application.core.web.DbHttpSession', 'connectionID' => 'db', 'sessionTableName' => '{{sessions}}', 'autoCreateSessionTable' => false, ),
EOPHP
mv application/config/config.tmp application/config/config.php
fi
fi
# Install BaltimoreCyberTrustRoot.crt.pem if needed
if [ "$MYSQL_SSL_CA" == "BaltimoreCyberTrustRoot.crt.pem" ] && ! [ -e BaltimoreCyberTrustRoot.crt.pem ]; then
echo "Downloading BaltimoreCyberTrustroot.crt.pem"
if curl -o BaltimoreCyberTrustRoot.crt.pem -fsL "https://cacerts.digicert.com/DigiCertGlobalRootG2.crt.pem"; then
echo "Downloaded successfully"
else
echo "Failed to download certificate - continuing anyway"
fi
fi
# Install Amazon global-bundle.pem if needed
if [ "$MYSQL_SSL_CA" == "global-bundle.pem" ] && ! [ -e global-bundle.pem ]; then
echo "Downloading Amazon global-bundle.pem"
if curl -o global-bundle.pem -fsL "https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem"; then
echo "Downloaded successfully"
else
echo "Failed to download certificate - continuing anyway"
fi
fi
echo "" > /usr/local/etc/php/conf.d/sessions.ini
if [ -n "$LIMESURVEY_PHP_SESSION_SAVE_HANDLER" ] && [ -n "$LIMESURVEY_PHP_SESSION_SAVE_PATH" ]; then
echo "Configuring custom session handler for PHP"
echo -e "session.save_handler = $LIMESURVEY_PHP_SESSION_SAVE_HANDLER\nsession.save_path = $LIMESURVEY_PHP_SESSION_SAVE_PATH" > /usr/local/etc/php/conf.d/sessions.ini
fi
if [ -z "$LIMESURVEY_DONT_UPDATE" ]; then
DBENGINE='MyISAM'
if [ -n "$LIMESURVEY_USE_INNODB" ]; then
chmod ug+w application/core/db/MysqlSchema.php
#If you want to use INNODB - remove MyISAM specification from LimeSurvey code
sed -i "/ENGINE=MyISAM/s/\(ENGINE=MyISAM \)//1" application/core/db/MysqlSchema.php
#Also set mysqlEngine in config file
sed -i "/\/\/ Update default LimeSurvey config here/s//'mysqlEngine'=>'InnoDB',/" application/config/config.php
DBENGINE='InnoDB'
chmod ug-w application/core/db/MysqlSchema.php
fi
#Set SMTP settings if environment contains values for it
set_config 'emailmethod' "'mail'"
set_config 'emailsmtphost' "'localhost'"
set_config 'siteadminemail' "'[email protected]'"
set_config 'siteadminbounce' "'[email protected]'"
set_config 'siteadminname' "'Your Name'"
set_config 'emailsmtpuser' "''"
set_config 'emailsmtppassword' "''"
set_config 'emailsmtpssl' "''"
set_config 'emailsmtpdebug' "''"
if [ -n "$LIMESURVEY_SMTP_HOST" ]; then
set_config 'emailmethod' "'smtp'"
set_config 'emailsmtphost' "'$LIMESURVEY_SMTP_HOST'"
set_config 'siteadminemail' "'$LIMESURVEY_ADMIN_EMAIL'"
set_config 'siteadminbounce' "'$LIMESURVEY_FROM_EMAIL'"
set_config 'siteadminname' "'$LIMESURVEY_ADMIN_NAME'"
if [ -n "$LIMESURVEY_SMTP_USER" ] && [ -n "$LIMESURVEY_SMTP_PASSWORD" ]; then
set_config 'emailsmtpuser' "'$LIMESURVEY_SMTP_USER'"
set_config 'emailsmtppassword' "'$LIMESURVEY_SMTP_PASSWORD'"
fi
if [ -n "$LIMESURVEY_SMTP_SSL" ]; then
set_config 'emailsmtpssl' "'$LIMESURVEY_SMTP_SSL'"
fi
if [ -n "$LIMESURVEY_SMTP_DEBUG" ]; then
set_config 'emailsmtpdebug' "1"
fi
fi
#Set timezone based on environment to config file if not already there
grep -qF 'date_default_timezone_set' application/config/config.php || sed --in-place '/^}/a\$longName = exec("echo \\$TZ"); if (!empty($longName)) {date_default_timezone_set($longName);}' application/config/config.php
chmod ug-w -R application/config
chmod ug=rwx -R tmp
chmod ug=rwx -R upload
chown www-data:www-data -R tmp
chown www-data:www-data -R plugins
mkdir -p upload/surveys
chown www-data:www-data -R upload
chown www-data:www-data -R application/config
mkdir -p /var/lime/sessions
chown www-data:www-data -R /var/lime/sessions
chmod ug=rwx -R /var/lime/sessions
fi
# The following 12 lines are borrowed from martialblog's entrypoint.sh
# see: https://github.com/martialblog/docker-limesurvey/blob/master/6.0/apache/entrypoint.sh
# Check if LimeSurvey database is provisioned
# Check if database is available
until nc -z -v -w30 "$LIMESURVEY_DB_HOST" "$LIMESURVEY_DB_PORT"
do
echo "Info: Waiting for database connection..."
sleep 5
done
if [ -z "$LIMESURVEY_DONT_UPDATE" ]; then
echo 'Info: Check if database already provisioned. Nevermind the stack trace.'
php application/commands/console.php updatedb || MUST_CREATE_DB=true
if [ -v MUST_CREATE_DB ] && [ -n "$LIMESURVEY_ADMIN_USER" ] && [ -n "$LIMESURVEY_ADMIN_PASSWORD" ]; then
echo >&2 'Database not yet populated - installing Limesurvey database'
DBENGINE=$DBENGINE php application/commands/console.php install "$LIMESURVEY_ADMIN_USER" "$LIMESURVEY_ADMIN_PASSWORD" "$LIMESURVEY_ADMIN_NAME" "$LIMESURVEY_ADMIN_EMAIL" verbose
fi
if [ -n "$LIMESURVEY_ADMIN_USER" ] && [ -n "$LIMESURVEY_ADMIN_PASSWORD" ]; then
echo >&2 'Updating password for admin user'
php application/commands/console.php resetpassword "$LIMESURVEY_ADMIN_USER" "$LIMESURVEY_ADMIN_PASSWORD"
fi
#flush asssets (clear cache on restart)
php application/commands/console.php flushassets
fi
fi
exec "$@"