forked from benhutchins/docker-mediawiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-entrypoint.sh
executable file
·241 lines (208 loc) · 8.61 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
#!/bin/bash
set -e
: ${MEDIAWIKI_SLEEP:=0}
# Sleep because if --link was used, docker-compose, or similar
# we need to give the database time to start up before we try to connect
sleep $MEDIAWIKI_SLEEP
: ${MEDIAWIKI_SITE_NAME:=MediaWiki}
: ${MEDIAWIKI_SITE_LANG:=en}
: ${MEDIAWIKI_ADMIN_USER:=admin}
: ${MEDIAWIKI_ADMIN_PASS:=rosebud}
: ${MEDIAWIKI_DB_TYPE:=mysql}
: ${MEDIAWIKI_DB_SCHEMA:=mediawiki}
: ${MEDIAWIKI_ENABLE_SSL:=false}
: ${MEDIAWIKI_UPDATE:=false}
if [ -z "$MEDIAWIKI_DB_HOST" ]; then
if [ -n "$MYSQL_PORT_3306_TCP_ADDR" ]; then
MEDIAWIKI_DB_HOST=$MYSQL_PORT_3306_TCP_ADDR
elif [ -n "$POSTGRES_PORT_5432_TCP_ADDR" ]; then
MEDIAWIKI_DB_TYPE=postgres
MEDIAWIKI_DB_HOST=$POSTGRES_PORT_5432_TCP_ADDR
elif [ -n "$DB_PORT_3306_TCP_ADDR" ]; then
MEDIAWIKI_DB_HOST=$DB_PORT_3306_TCP_ADDR
elif [ -n "$DB_PORT_5432_TCP_ADDR" ]; then
MEDIAWIKI_DB_TYPE=postgres
MEDIAWIKI_DB_HOST=$DB_PORT_5432_TCP_ADDR
else
echo >&2 'error: missing MEDIAWIKI_DB_HOST environment variable'
echo >&2 ' Did you forget to --link your database?'
exit 1
fi
fi
if [ -z "$MEDIAWIKI_DB_USER" ]; then
if [ "$MEDIAWIKI_DB_TYPE" = "mysql" ]; then
echo >&2 'info: missing MEDIAWIKI_DB_USER environment variable, defaulting to "root"'
MEDIAWIKI_DB_USER=root
elif [ "$MEDIAWIKI_DB_TYPE" = "postgres" ]; then
echo >&2 'info: missing MEDIAWIKI_DB_USER environment variable, defaulting to "postgres"'
MEDIAWIKI_DB_USER=postgres
else
echo >&2 'error: missing required MEDIAWIKI_DB_USER environment variable'
exit 1
fi
fi
if [ -z "$MEDIAWIKI_DB_PASSWORD" ]; then
if [ -n "$MYSQL_ENV_MYSQL_ROOT_PASSWORD" ]; then
MEDIAWIKI_DB_PASSWORD=$MYSQL_ENV_MYSQL_ROOT_PASSWORD
elif [ -n "$POSTGRES_ENV_POSTGRES_PASSWORD" ]; then
MEDIAWIKI_DB_PASSWORD=$POSTGRES_ENV_POSTGRES_PASSWORD
elif [ -n "$DB_ENV_MYSQL_ROOT_PASSWORD" ]; then
MEDIAWIKI_DB_PASSWORD=$DB_ENV_MYSQL_ROOT_PASSWORD
elif [ -n "$DB_ENV_POSTGRES_PASSWORD" ]; then
MEDIAWIKI_DB_PASSWORD=$DB_ENV_POSTGRES_PASSWORD
else
echo >&2 'error: missing required MEDIAWIKI_DB_PASSWORD environment variable'
echo >&2 ' Did you forget to -e MEDIAWIKI_DB_PASSWORD=... ?'
echo >&2
echo >&2 ' (Also of interest might be MEDIAWIKI_DB_USER and MEDIAWIKI_DB_NAME)'
exit 1
fi
fi
: ${MEDIAWIKI_DB_NAME:=mediawiki}
if [ -z "$MEDIAWIKI_DB_PORT" ]; then
if [ -n "$MYSQL_PORT_3306_TCP_PORT" ]; then
MEDIAWIKI_DB_PORT=$MYSQL_PORT_3306_TCP_PORT
elif [ -n "$POSTGRES_PORT_5432_TCP_PORT" ]; then
MEDIAWIKI_DB_PORT=$POSTGRES_PORT_5432_TCP_PORT
elif [ -n "$DB_PORT_3306_TCP_PORT" ]; then
MEDIAWIKI_DB_PORT=$DB_PORT_3306_TCP_PORT
elif [ -n "$DB_PORT_5432_TCP_PORT" ]; then
MEDIAWIKI_DB_PORT=$DB_PORT_5432_TCP_PORT
elif [ "$MEDIAWIKI_DB_TYPE" = "mysql" ]; then
MEDIAWIKI_DB_PORT="3306"
elif [ "$MEDIAWIKI_DB_TYPE" = "postgres" ]; then
MEDIAWIKI_DB_PORT="5432"
fi
fi
export MEDIAWIKI_DB_TYPE MEDIAWIKI_DB_HOST MEDIAWIKI_DB_USER MEDIAWIKI_DB_PASSWORD MEDIAWIKI_DB_NAME
TERM=dumb php -- <<'EOPHP'
<?php
// database might not exist, so let's try creating it (just to be safe)
if ($_ENV['MEDIAWIKI_DB_TYPE'] == 'mysql') {
$mysql = new mysqli($_ENV['MEDIAWIKI_DB_HOST'], $_ENV['MEDIAWIKI_DB_USER'], $_ENV['MEDIAWIKI_DB_PASSWORD'], '', (int) $_ENV['MEDIAWIKI_DB_PORT']);
if ($mysql->connect_error) {
file_put_contents('php://stderr', 'MySQL Connection Error: (' . $mysql->connect_errno . ') ' . $mysql->connect_error . "\n");
exit(1);
}
if (!$mysql->query('CREATE DATABASE IF NOT EXISTS `' . $mysql->real_escape_string($_ENV['MEDIAWIKI_DB_NAME']) . '`')) {
file_put_contents('php://stderr', 'MySQL "CREATE DATABASE" Error: ' . $mysql->error . "\n");
$mysql->close();
exit(1);
}
$mysql->close();
}
EOPHP
if ! [ -e index.php -a -e includes/DefaultSettings.php ]; then
echo >&2 "MediaWiki not found in $(pwd) - copying now..."
if [ "$(ls -A)" ]; then
echo >&2 "WARNING: $(pwd) is not empty - press Ctrl+C now if this is an error!"
( set -x; ls -A; sleep 10 )
fi
tar cf - --one-file-system -C /usr/src/mediawiki . | tar xf -
echo >&2 "Complete! MediaWiki has been successfully copied to $(pwd)"
fi
: ${MEDIAWIKI_SHARED:=/data}
if [ -d "$MEDIAWIKI_SHARED" ]; then
# If there is no LocalSettings.php but we have one under the shared
# directory, symlink it
if [ -e "$MEDIAWIKI_SHARED/LocalSettings.php" -a ! -e LocalSettings.php ]; then
ln -s "$MEDIAWIKI_SHARED/LocalSettings.php" LocalSettings.php
fi
# If the images directory only contains a README, then link it to
# $MEDIAWIKI_SHARED/images, creating the shared directory if necessary
if [ "$(ls images)" = "README" -a ! -L images ]; then
rm -fr images
mkdir -p "$MEDIAWIKI_SHARED/images"
ln -s "$MEDIAWIKI_SHARED/images" images
fi
# If an extensions folder exists inside the shared directory, as long as
# /var/www/html/extensions is not already a symbolic link, then replace it
if [ -d "$MEDIAWIKI_SHARED/extensions" -a ! -h /var/www/html/extensions ]; then
echo >&2 "Found 'extensions' folder in data volume, creating symbolic link."
rm -rf /var/www/html/extensions
ln -s "$MEDIAWIKI_SHARED/extensions" /var/www/html/extensions
fi
# If a skins folder exists inside the shared directory, as long as
# /var/www/html/skins is not already a symbolic link, then replace it
if [ -d "$MEDIAWIKI_SHARED/skins" -a ! -h /var/www/html/skins ]; then
echo >&2 "Found 'skins' folder in data volume, creating symbolic link."
rm -rf /var/www/html/skins
ln -s "$MEDIAWIKI_SHARED/skins" /var/www/html/skins
fi
# If a vendor folder exists inside the shared directory, as long as
# /var/www/html/vendor is not already a symbolic link, then replace it
if [ -d "$MEDIAWIKI_SHARED/vendor" -a ! -h /var/www/html/vendor ]; then
echo >&2 "Found 'vendor' folder in data volume, creating symbolic link."
rm -rf /var/www/html/vendor
ln -s "$MEDIAWIKI_SHARED/vendor" /var/www/html/vendor
fi
# Attempt to enable SSL support if explicitly requested
if [ $MEDIAWIKI_ENABLE_SSL = true ]; then
echo >&2 'info: enabling ssl'
a2enmod ssl
cp "$MEDIAWIKI_SHARED/ssl.key" /etc/apache2/ssl.key
cp "$MEDIAWIKI_SHARED/ssl.crt" /etc/apache2/ssl.crt
cp "$MEDIAWIKI_SHARED/ssl.bundle.crt" /etc/apache2/ssl.bundle.crt
elif [ -e "/etc/apache2/mods-enabled/ssl.load" ]; then
echo >&2 'warning: disabling ssl'
a2dismod ssl
fi
elif [ $MEDIAWIKI_ENABLE_SSL = true ]; then
echo >&2 'error: Detected MEDIAWIKI_ENABLE_SSL flag but found no data volume';
echo >&2 ' Did you forget to mount the volume with -v?'
exit 1
fi
# If there is no LocalSettings.php, create one using maintenance/install.php
if [ ! -e "LocalSettings.php" -a ! -z "$MEDIAWIKI_SITE_SERVER" ]; then
php maintenance/install.php \
--confpath /var/www/html \
--dbname "$MEDIAWIKI_DB_NAME" \
--dbschema "$MEDIAWIKI_DB_SCHEMA" \
--dbport "$MEDIAWIKI_DB_PORT" \
--dbserver "$MEDIAWIKI_DB_HOST" \
--dbtype "$MEDIAWIKI_DB_TYPE" \
--dbuser "$MEDIAWIKI_DB_USER" \
--dbpass "$MEDIAWIKI_DB_PASSWORD" \
--installdbuser "$MEDIAWIKI_DB_USER" \
--installdbpass "$MEDIAWIKI_DB_PASSWORD" \
--server "$MEDIAWIKI_SITE_SERVER" \
--scriptpath "" \
--lang "$MEDIAWIKI_SITE_LANG" \
--pass "$MEDIAWIKI_ADMIN_PASS" \
"$MEDIAWIKI_SITE_NAME" \
"$MEDIAWIKI_ADMIN_USER"
# If we have a mounted share volume, move the LocalSettings.php to it
# so it can be restored if this container needs to be reinitiated
if [ -d "$MEDIAWIKI_SHARED" ]; then
# Append inclusion of /data/CustomSettings.php
if [ -e "$MEDIAWIKI_SHARED/CustomSettings.php" ]; then
chown www-data: "$MEDIAWIKI_SHARED/CustomSettings.php"
echo "include('$MEDIAWIKI_SHARED/CustomSettings.php');" >> LocalSettings.php
fi
# Move generated LocalSettings.php to share volume
mv LocalSettings.php "$MEDIAWIKI_SHARED/LocalSettings.php"
ln -s "$MEDIAWIKI_SHARED/LocalSettings.php" LocalSettings.php
fi
fi
# If a composer.lock and composer.json file exist, use them to install
# dependencies for MediaWiki and desired extensions, skins, etc.
if [ -e "$MEDIAWIKI_SHARED/composer.lock" -a -e "$MEDIAWIKI_SHARED/composer.json" ]; then
curl -sS https://getcomposer.org/installer | php
cp "$MEDIAWIKI_SHARED/composer.lock" composer.lock
cp "$MEDIAWIKI_SHARED/composer.json" composer.json
php composer.phar install --no-dev
fi
# If LocalSettings.php exists, then attempt to run the update.php maintenance
# script. If already up to date, it won't do anything, otherwise it will
# migrate the database if necessary on container startup. It also will
# verify the database connection is working.
if [ -e "LocalSettings.php" -a $MEDIAWIKI_UPDATE = true ]; then
echo >&2 'info: Running maintenance/update.php';
php maintenance/update.php --quick
fi
# Ensure images folder exists
mkdir -p images
# Fix file ownership and permissions
chown -R www-data: .
chmod 755 images
exec "$@"