-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker-run.sh
executable file
·316 lines (277 loc) · 10.3 KB
/
docker-run.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/bin/bash
VERSION=V4
################################## ################################ #### # ##
# >> DOCKER-RUN-GLFTPD :: WEBUI
################################## ################################ #### # ##
# ENVIRONMENT VARIABLES:
#
# GL_DATA="<path>" basedir for gl bind mounts (default=./glftpd)
# gl/bot config and data is stored here
# GL_DIR="<path>" optional path to (existing) gl install
# WEBUI_LOCAL=1 run commands on same host, no gl docker [0|1]
# WEBUI_AUTH_MODE="<mode>" auth mode [basic|glftpd|both|none] (basic)
# NETWORK="<network>" docker network mode [host|bridge] (bridge)
# FORCE=1 remove any existing container first [0|1]
#
# WEBUI_ARGS+= " --any-other-flags " add any other docker run options
#
# (*) to reuse existing install set GL_DATA to /glftpd
#
################################################################### #### # ##
#DEBUG=0
#GL_DATA="./glftpd"
GLFTPD=0
WEBUI=1
#WEBUI_LOCAL=1
#WEBUI_DBUS=0
#WEBUI_AUTH_MODE="basic"
#NETWORK="host"
DOCKER_REGISTRY="ghcr.io/silv3rr"
DOCKER_IMAGE_GLFTPD="docker-glftpd"
DOCKER_IMAGE_WEBUI="docker-glftpd-web:latest"
DOCKER_TAGS="full latest"
SCRIPTDIR="$(dirname "$0")"
GLFTPD_ARGS+="$*"
WEBUI_ARGS+="$*"
REMOVE_CT=1
# check existing images. if we already have 'full' tag, keep using it
for t in $DOCKER_TAGS; do
if [ -z "$GLFTPD_IMAGE" ]; then
for i in "${DOCKER_IMAGE_GLFTPD}:$t" "${DOCKER_REGISTRY}/${DOCKER_IMAGE_GLFTPD}:$t"; do
GLFTPD_IMAGE="$(docker image ls --format='{{.Repository}}{{if .Tag}}:{{.Tag}}{{end}}' --filter reference="$i")"
TAG="$t"
break
done
fi
done
if [ -z "$GLFTPD_IMAGE" ] && [ "${USE_FULL:-0}" -eq 1 ]; then
GLFTPD_IMAGE="${DOCKER_REGISTRY}/${DOCKER_IMAGE_GLFTPD}:full"
fi
# get external/public ip
if [ -z "$IP_ADDR" ]; then
GET_IP="$( ip route get "$(ip route show 0.0.0.0/0 | grep -oP 'via \K\S+')" | grep -oP 'src \K\S+' )"
IP_ADDR="${GET_IP:-127.0.0.1}"
fi
ZS_STATUS="$(
docker image inspect --format='{{ index .Config.Labels "gl.zipscript.setup" }}' "$DOCKER_IMAGE_GLFTPD" \
2>/dev/null
)"
BOT_STATUS="$(
docker image inspect --format='{{ index .Config.Labels "gl.sitebot.setup" }}' "$DOCKER_IMAGE_GLFTPD" \
2>/dev/null
)"
#WEBUI="$(
# docker image inspect --format='{{ index .Config.Labels "gl.web.setup" }}' "$DOCKER_IMAGE_GLFTPD" \
# 2>/dev/null
#)"
if [ -s "$SCRIPTDIR/customizer.sh" ]; then
IP_ADDR=$IP_ADDR ZS_STATUS=$ZS_STATUS BOT_STATUS=$BOT_STATUS \
GLFTPD_CONF=$GLFTPD_CONF GLFTPD_PERM_UDB=$GLFTPD_PERM_UDB GLFTPD_PORT=$GLFTPD_PORT \
GLFTPD_PASV_PORTS=$GLFTPD_PASV_PORTS GLFTPD_PASV_ADDR=$GLFTPD_PASV_ADDR \
IRC_SERVERS=$IRC_SERVERS IRC_CHANNELS=$IRC_CHANNELS \
"$SCRIPTDIR/customizer.sh"
else
echo "Skipping custom config, 'customizer.sh' not found"
fi
echo "----------------------------------------------"
echo "DOCKER-GLFTPD-RUN-${VERSION}"
echo "----------------------------------------------"
# set runtime docker args
if [ "${DEBUG:-0}" -eq 0 ]; then
GLFTPD_ARGS+=" --detach "
WEBUI_ARGS+=" --detach "
fi
#WEBUI_ARGS+=" --add-host glftpd:127.0.0.1 "
if [ -z "$NETWORK" ]; then
DOCKER_NETWORK="$(docker network ls --format '{{.Name}}' --filter 'Name=shit')"
if [ -n "$DOCKER_NETWORK" ] && [ "$DOCKER_NETWORK" = "shit" ]; then
NETWORK="shit"
fi
fi
# local: check for existing glftpd install on host
if [ "${WEBUI_LOCAL:-0}" -eq 1 ]; then
if [ -z "$GL_DIR" ]; then
for i in /jail/glftpd /glftpd; do
if [ -d "$i/site" ] && [ -f "$i/bin/glftpd" ]; then
GL_DIR="$i"
echo "Found glftpd on host: $i (GL_DIR set)"
break
fi
done
fi
if [ -n "$GL_DIR" ]; then
WEBUI_ARGS+=" --ipc=host "
echo "* Using hosts IPC namespace"
NETWORK="host"
WEBUI_ARGS+=" --mount type=bind,src=${GL_DIR:-/glftpd},dst=/glftpd "
echo "* Mounting \$GL_DIR as /glftpd"
fi
fi
# local: exception, systemd dbus broker (debian)
if [ "${WEBUI_DBUS:-0}" -eq 1 ]; then
DOCKER_IMAGE_WEBUI="docker-glftpd-web:debian"
WEBUI_ARGS+=" --privileged -v /run/systemd:/run/systemd -v /run/dbus:/run/dbus "
echo "* Using systemd and dbus broker to start/stop glftpd"
sed -i -r "s|^(.*'env_bus'\s*=>\s*\")(.*)(\",.*)$|\1/usr/bin/env SYSTEMCTL_FORCE_BUS=1\3|" /app/config.php
fi
# set port
if [ "${NETWORK:-"bridge"}" = "host" ]; then
WEBUI_ARGS+=" --env WEBUI_PORT=4444 "
echo "* Running webui on host network: https://localhost:4444"
else
WEBUI_ARGS+=" --publish "${IP_ADDR:-127.0.0.1}:4444:443" "
fi
# set max open files to prevent high cpu usage by some procs
GLFTPD_ARGS+=" --ulimit nofile=1024:1024 "
WEBUI_ARGS+=" --ulimit nofile=1024:1024 "
# mount glftpd.conf
if [ "${GLFTPD_CONF:-0}" -eq 1 ] || [ "${ZS_STATUS:-0}" -eq 1 ]; then
REMOVE_CT=0
if [ -d glftpd/glftpd.conf ]; then
rmdir glftpd/glftpd.conf 2>/dev/null || { echo "! ERROR: \"glftpd.conf\" is a directory, remove it manually"; }
fi
if [ -f glftpd/glftpd.conf ]; then
GLFTPD_ARGS+=" --mount type=bind,src=${GL_DATA:-./glftpd}/glftpd.conf,dst=/glftpd/glftpd.conf "
WEBUI_ARGS+=" --mount type=bind,src=${GL_DATA:-./glftpd}/glftpd.conf,dst=/app/glftpd/glftpd.conf"
fi
fi
if [ "${GLFTPD_CONF:-0}" -eq 1 ]; then
REMOVE_CT=0
echo "* Set docker ip:port"
#GLFTPD_PASV_PORTS="$(sed -n -E 's/^pasv_addr (.*)/\1/p' glftpd/glftpd.conf)"
if grep -Eq "^pasv_ports.*" glftpd/glftpd.conf; then
GLFTPD_ARGS+=" --publish ${IP_ADDR}:${GLFTPD_PASV_PORTS:-5000-5100}:${GLFTPD_PASV_PORTS:-5000-5100} "
fi
fi
if [ -n "$GLFTPD_PASSWD" ]; then
GLFTPD_ARGS+=" --env GLFTPD_PASSWD=$GLFTPD_PASSWD "
fi
if [ "${GLFTPD_PERM_UDB:-0}" -eq 1 ]; then
REMOVE_CT=0
GLFTPD_ARGS+=" --mount type=bind,src=${GL_DATA:-./glftpd}/ftp-data/users,dst=/glftpd/ftp-data/users "
GLFTPD_ARGS+=" --mount type=bind,src=${GL_DATA:-./glftpd}/ftp-data/groups,dst=/glftpd/ftp-data/groups"
GLFTPD_ARGS+=" --mount type=bind,src=${GL_DATA:-./glftpd}/etc,dst=/glftpd/etc "
fi
# shellcheck disable=SC2174
if [ "${GLFTPD_SITE:-0}" -eq 1 ]; then
GLFTPD_ARGS+=" --mount type=bind,src=${GL_DATA:-./glftpd}/site,dst=/glftpd/site:rw "
WEBUI_ARGS+=" --mount type=bind,src=${GL_DATA:-./glftpd}/site,dst=/app/glftpd/site "
else
WEBUI_ARGS+=" --mount type=tmpfs,dst=/app/glftpd/site/NO_BIND_MOUNT "
fi
if [ "${BOT_STATUS:-0}" -eq 1 ]; then
REMOVE_CT=0
GLFTPD_ARGS+=" --mount type=bind,src=${GL_DATA:-./glftpd}/sitebot,dst=/glftpd/sitebot "
GLFTPD_ARGS+=" --publish ${IP_ADDR}:3333:3333 "
for i in glftpd/sitebot/eggdrop.conf glftpd/sitebot/pzs-ng/ngBot.conf ; do
if [ -d "$i" ]; then
rmdir "$i" 2>/dev/null || { echo "! ERROR: \"$i\" is a directory, remove it manually"; }
fi
done
if [ -f glftpd/sitebot/eggdrop.conf ]; then
WEBUI_ARGS+=" --mount type=bind,src=${GL_DATA:-./glftpd}/sitebot/eggdrop.conf,dst=/app/glftpd/sitebot/eggdrop.conf "
fi
if [ -f glftpd/sitebot/pzs-ng/ngBot.conf ]; then
WEBUI_ARGS+=" --mount type=bind,src=${GL_DATA:-./glftpd}/sitebot/pzs-ng/ngBot.conf,dst=/app/glftpd/sitebot/pzs-ng/ngBot.conf "
fi
fi
WEBUI_ARGS+=" --mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock "
WEBUI_ARGS+=" --env WEBUI_AUTH_MODE=${WEBUI_AUTH_MODE:-basic} "
# custom runtime scripts and glftpd commands
if [ -d entrypoint.d ]; then
REMOVE_CT=0
GLFTPD_ARGS+=" --mount type=bind,src=$(pwd)/entrypoint.d,dst=/entrypoint.d "
echo "* Mount 'entrypoint.d' dir for custom scripts"
fi
if [ -d custom ]; then
REMOVE_CT=0
if find custom/* >/dev/null 2>&1; then
GLFTPD_ARGS+=" --mount type=bind,src=$(pwd)/custom,dst=/custom "
echo "* Found files in 'custom', mounting dir"
fi
fi
if [ "${REMOVE_CT:-1}" -eq 1 ]; then
GLFTPD_ARGS+=" --rm "
WEBUI_ARGS+=" --rm "
fi
# remove existing container(s) which use local and/or registry images
for i in "${DOCKER_IMAGE_GLFTPD}" "${DOCKER_REGISTRY}/${DOCKER_IMAGE_GLFTPD}"; do
for j in $(docker image ls --format='{{.Repository}}' --filter reference="$i" | sort -u); do
REGEX_PAT_GLFTPD+=" ${j}|"
done
done
REGEX_GLFTPD="(glftpd|${REGEX_PAT_GLFTPD/%|/})$"
REGEX_WEBUI="(glftpd-web| ${DOCKER_IMAGE_WEBUI:-'docker-glftpd-web'})$"
if [ "${GLFTPD:-0}" -eq 1 ]; then
REGEX="$REGEX_GLFTPD"
fi
if [ "${WEBUI:-0}" -eq 1 ]; then
REGEX="$REGEX_WEBUI"
fi
if [ "${GLFTPD:-0}" -eq 1 ] && [ "${WEBUI:-0}" -eq 1 ]; then
REGEX="(${REGEX_GLFTPD}|${REGEX_WEBUI}|${DOCKER_REGISTRY}/docker-glftpd.*)"
fi
if [ -n "$REGEX" ]; then
docker ps -a --format '{{.ID}} {{.Image}} {{.Names}}'| grep -E "$REGEX" | while read -r i; do
CONTAINER="$(echo "$i"|cut -d' ' -f1)"
if [ -n "$CONTAINER" ] && [ "${FORCE:-0}" -eq 1 ]; then
printf "* Removing existing container '%s'... " "$i"
docker rm -f -v "$CONTAINER" 2>/dev/null
else
echo "! WARNING: container '$i' already exists, to remove it try: 'FORCE=1 ./docker-run.sh'"
fi
done
fi
# run docker with glftpd image and GLFTPD_ARGS
# shellcheck disable=SC2086
if [ "${GLFTPD:-1}" -eq 1 ]; then
if ! echo "$GLFTPD_IMAGE" | grep -Eq "$DOCKER_REGISTRY"; then
echo "* Found local '${TAG}' image"
else
echo "* Pulling '${TAG}' image from registry"
docker pull "$GLFTPD_IMAGE"
fi
if [ -n "$GLFTPD_IMAGE" ]; then
printf "* Docker run '%s'... " "$GLFTPD_IMAGE"
docker run \
$GLFTPD_ARGS \
--name glftpd \
--hostname glftpd \
--publish "${IP_ADDR}:${GLFTPD_PORT:-1337}:1337" \
--network "${NETWORK:-bridge}" \
--workdir /glftpd \
$DOCKER_IMAGE_GLFTPD
echo "* For logs run 'docker logs glftpd'"
else
echo "! Docker image not found"
exit 1
fi
fi
# run web interface image with WEBUI_ARGS
if [ "${WEBUI:-1}" -eq 1 ]; then
LOCAL_IMAGE_WEBUI=$(
docker image ls --format='{{.Repository}}' --filter reference="$DOCKER_IMAGE_WEBUI"
)
if [ -n "$LOCAL_IMAGE_WEBUI" ]; then
echo "* Using local docker image for webui"
else
echo "* Pulling image '${DOCKER_IMAGE_WEBUI}' from registry '$DOCKER_REGISTRY'"
DOCKER_IMAGE_WEBUI="${DOCKER_REGISTRY}/${DOCKER_IMAGE_WEBUI}"
docker pull $DOCKER_IMAGE_WEBUI
fi
# shellcheck disable=SC2086
if [ -n "$DOCKER_IMAGE_WEBUI" ]; then
printf "* Docker run '%s'... " "$DOCKER_IMAGE_WEBUI"
docker run \
$WEBUI_ARGS \
--hostname glftpd-web \
--name glftpd-web \
--network "${NETWORK:-bridge}" \
$DOCKER_IMAGE_WEBUI
echo "* For logs run 'docker logs glftpd-web'"
else
echo "! Docker image not found"
exit 1
fi
fi
echo "* All done."