-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_app.sh
executable file
·400 lines (330 loc) · 13.1 KB
/
docker_app.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/usr/bin/env bash
#
# Author: Stanislav Zhuk <[email protected]>
#
#{{{ bash settings
set -o errexit
set -o nounset
set -o pipefail
#}}}
#{{{ init logic
# shellcheck disable=SC1090
source "$(dirname "$(realpath "${BASH_SOURCE[0]}")")/output_helpers.sh"
# input arguments
readonly args=("${@}")
# shellcheck disable=SC2034
readonly script_name="Docker App"
# shellcheck disable=SC2034
readonly script_version="1.5.0"
#}}}
main() {
run_init
# these functions stop subsequent script execution
run_setup_desktop_shortcut
run_pull_image
run_container_logs
run_container_sh
run_metube_cookies
# these functions are executed sequentially
run_start_or_stop
info "Done."
}
#{{{ Functions
run_help() {
info "Usage: docker-app [arguments]
primary args:
buggregator # ultimate Debugging Server for PHP
lama-cleaner # image inpainting tool powered by SOTA AI Model
metube # youtube-dl web UI
mozhi # alternative-frontend for many translation engines
searxng # a privacy-respecting, hackable metasearch engine
rembg # tool to remove images background
asf # Steam cards farming
use env DOCKER_APP_MOUNT_DIR to mount another folder (default is ${HOME}/Downloads)
use env DOCKER_APP_PORT to bind non-default port for the service
use env DOCKER_APP_TAG to pull a specific image tag
use env DOCKER_ENV_FILE to pass specific env file to the container
use env DOCKER_NETWORK to connect the container to a specific network
use env BROWSER to open the specific browser
use env WAIT_UI_SEC to wait before opening the browser (default is 1 sec)
use env DOCKER_NO_STOP to prevent the container from stopping
metube env: METUBE_YTDL_OPTIONS, METUBE_OUTPUT_TEMPLATE
asf env: ASF_CRYPTKEY, ASF_ARGS
secondary args:
i, interactive (to run from desktop shortcut)
log, logs (view container logs)
p, pull (pull the newest image)
s, setup (create a desktop shortcut)
sh, bash (enter the container)
root (under root user)
cookies (show cookies instructions for metube)"
exit 0
}
has_arg() {
local param="${1}"
local params="${args[*]}"
# shortcuts for arguments
[[ " ${params} " =~ " i " ]] && params=" ${params} interactive "
[[ " ${params} " =~ " p " ]] && params=" ${params} pull "
[[ " ${params} " =~ " s " ]] && params=" ${params} setup "
# shellcheck disable=SC2076
if [[ " ${params} " =~ " ${param} " ]]; then
return 0
else
return 1
fi
}
run_init() {
readonly network="${DOCKER_NETWORK:-bridge}"
readonly env_file="${DOCKER_ENV_FILE:-}"
wait_ui_sec="${WAIT_UI_SEC:-1}"
# check for numeric value
[[ ! "${wait_ui_sec}" =~ ^[0-9]+$ ]] && wait_ui_sec=1
if has_arg "buggregator"; then
readonly app_name="Buggregator"
readonly app_comment="Ultimate Debugging Server for PHP"
readonly image_name="ghcr.io/buggregator/server:${DOCKER_APP_TAG:-latest}"
readonly container_name="buggregator"
readonly port="${DOCKER_APP_PORT:-8000}"
elif has_arg "lama-cleaner"; then
readonly app_name="Lama Cleaner"
readonly app_comment="Lama Cleaner (Image inpainting tool)"
readonly image_name="cwq1913/lama-cleaner:${DOCKER_APP_TAG:-cpu-1.2.5}"
readonly container_name="lama-cleaner"
readonly mount_dir="${DOCKER_APP_MOUNT_DIR:-${HOME}/Downloads/${container_name}}"
readonly port="${DOCKER_APP_PORT:-8080}"
[[ ${wait_ui_sec} == 1 ]] && wait_ui_sec=15
elif has_arg "metube"; then
readonly app_name="MeTube"
readonly app_comment="YouTube Downloader"
readonly image_name="alexta69/metube:${DOCKER_APP_TAG:-latest}"
readonly container_name="metube"
readonly mount_dir="${DOCKER_APP_MOUNT_DIR:-${HOME}/Downloads/${container_name}}"
readonly port="${DOCKER_APP_PORT:-8081}"
elif has_arg "mozhi"; then
readonly app_name="Mozhi"
readonly app_comment="Alternative-frontend for many translation engines"
readonly image_name="codeberg.org/aryak/mozhi:${DOCKER_APP_TAG:-latest}"
readonly container_name="mozhi"
readonly port="${DOCKER_APP_PORT:-8083}"
elif has_arg "searxng"; then
readonly app_name="SearXNG"
readonly app_comment="A privacy-respecting, hackable metasearch engine"
readonly image_name="searxng/searxng:${DOCKER_APP_TAG:-latest}"
readonly container_name="searxng"
readonly mount_dir="${DOCKER_APP_MOUNT_DIR:-${HOME}/Downloads/${container_name}}"
readonly port="${DOCKER_APP_PORT:-8082}"
elif has_arg "rembg"; then
readonly app_name="Rembg"
readonly app_comment="Tool to remove images background"
readonly image_name="danielgatis/rembg:${DOCKER_APP_TAG:-latest}"
readonly container_name="rembg"
readonly mount_dir="${DOCKER_APP_MOUNT_DIR:-${HOME}/Downloads/${container_name}}"
readonly port="${DOCKER_APP_PORT:-5000}"
[[ ${wait_ui_sec} == 1 ]] && wait_ui_sec=40
elif has_arg "asf"; then
readonly app_name="ASF"
readonly app_comment="Steam cards farming"
readonly image_name="justarchi/archisteamfarm:${DOCKER_APP_TAG:-released}"
readonly container_name="asf"
readonly mount_dir="${DOCKER_APP_MOUNT_DIR:-${HOME}/Downloads/${container_name}}"
readonly port="${DOCKER_APP_PORT:-1242}"
else
script_intro
run_help
fi
readonly desktop_entry="[Desktop Entry]
Categories=Utility;
Comment=${app_comment}
Exec=bash -ci \"docker-app interactive ${container_name}\"
Icon=utilities-terminal
Name=${app_name}
Terminal=false
Type=Application"
readonly desktop_shortcut="${HOME}/.local/share/applications/${container_name}.desktop"
}
run_metube_cookies() {
has_arg "metube" || return 0
has_arg "cookies" || return 0
info "How to send browser cookies:
1. Install in your browser an extension to extract cookies
Firefox https://addons.mozilla.org/en-US/firefox/addon/export-cookies-txt/
Chrome https://chrome.google.com/webstore/detail/get-cookiestxt/bgaddhkoddajcdgocldbbfleckgcbcid
2. Extract the cookies you need with the extension and rename the file cookies.txt
3. Drop the file here ${mount_dir}/cookies.txt
4. Restart the container"
exit 0
}
run_setup_desktop_shortcut() {
has_arg "setup" || return 0
if [[ -f "${desktop_shortcut}" ]]; then
info "Removing old shortcut ${desktop_shortcut}"
rm -f "${desktop_shortcut}"
fi
echo "${desktop_entry}" > "${desktop_shortcut}" && \
success "Added a new desktop shortcut ${desktop_shortcut}"
exit 0
}
run_pull_image() {
has_arg "pull" || return 0
docker pull "${image_name}"
success "Pulled image ${image_name}"
exit 0
}
run_container_logs() {
has_arg "log" || has_arg "logs" || return 0
docker logs -f "${container_name}"
exit 0
}
run_container_sh() {
has_arg "sh" || has_arg "bash" || return 0
if has_arg "bash"; then
local shell="bash"
else
local shell="sh"
fi
if has_arg "root"; then
docker exec -u root -it "${container_name}" "${shell}"
else
docker exec -it "${container_name}" "${shell}"
fi
exit 0
}
run_start_or_stop() {
if [[ ! "$(docker ps -q -f name="${container_name}")" ]]; then
if [[ "$(docker ps -aq -f status=exited -f name="${container_name}")" ]]; then
docker rm "${container_name}"
fi
if has_arg "interactive"; then
notify-send "${container_name}" "Started"
fi
success "Starting..."
local docker_opts=()
if has_arg "buggregator"; then
docker_opts=(
-p "127.0.0.1:${port}":8000
"${image_name}"
)
elif has_arg "lama-cleaner"; then
mkdir -p "${mount_dir}/"{torch_cache,huggingface_cache}
docker_opts=(
-p "127.0.0.1:${port}":8080
-v "${mount_dir}/torch_cache":/root/.cache/torch
-v "${mount_dir}/huggingface_cache":/root/.cache/huggingface
"${image_name}"
lama-cleaner --device=cpu --port=8080 --host=0.0.0.0
)
elif has_arg "metube"; then
mkdir -p "${mount_dir}/cache"
docker_opts=(
-p "127.0.0.1:${port}":8081
-v "${mount_dir}":/downloads
--user "$(id -u)":"$(id -g)"
"${image_name}"
)
if [[ "${METUBE_YTDL_OPTIONS:-}" != "" ]]; then
docker_opts=(-e YTDL_OPTIONS="${METUBE_YTDL_OPTIONS}" "${docker_opts[@]}")
else
docker_opts=(-e YTDL_OPTIONS='{"cachedir":"/downloads/cache","cookiefile":"/downloads/cookies.txt","subtitleslangs":["en.*","-live_chat"],"writesubtitles":true,"postprocessors":[{"key":"FFmpegEmbedSubtitle","already_have_subtitle":false},{"key":"SponsorBlock","categories":["sponsor","selfpromo","interaction","intro","outro","music_offtopic"],"when":"pre_process"},{"key":"ModifyChapters","remove_sponsor_segments":["sponsor","selfpromo","interaction","intro","outro","music_offtopic"]}],"verbose":true}' "${docker_opts[@]}")
fi
if [[ "${METUBE_OUTPUT_TEMPLATE:-}" != "" ]]; then
docker_opts=(-e OUTPUT_TEMPLATE="${METUBE_OUTPUT_TEMPLATE}" "${docker_opts[@]}")
else
docker_opts=(-e OUTPUT_TEMPLATE='%(upload_date>%Y-%m-%d)s [%(uploader|Unknown)s] %(title)s [%(resolution)s].%(ext)s' "${docker_opts[@]}")
fi
elif has_arg "mozhi"; then
docker_opts=(
-p "127.0.0.1:${port}":3000
"${image_name}"
)
elif has_arg "searxng"; then
mkdir -p "${mount_dir}"
docker_opts=(
-p "127.0.0.1:${port}":8080
-v "${mount_dir}:/etc/searxng"
-e "BASE_URL=http://localhost:${port}/"
"${image_name}"
)
elif has_arg "rembg"; then
docker_opts=(
-p "127.0.0.1:${port}":5000
"${image_name}"
s
)
elif has_arg "asf"; then
mkdir -p "${mount_dir}/"{config,logs,plugins}
if [[ ! -f "${mount_dir}/config/IPC.config" ]]; then
echo '{"Kestrel":{"Endpoints":{"HTTP":{"Url":"http://*:1242"}},"KnownNetworks":["10.0.0.0/8","172.16.0.0/12","192.168.0.0/16"]}}' | jq --indent 4 > "${mount_dir}/config/IPC.config"
fi
if [[ ! -f "${mount_dir}/config/ASF.json" ]]; then
echo '{"Headless": true}' | jq --indent 4 > "${mount_dir}/config/ASF.json"
fi
docker_opts=(
-p "127.0.0.1:${port}":1242
-v "${mount_dir}/config":/app/config
-v "${mount_dir}/logs":/app/logs
-v "${mount_dir}/plugins":/app/plugins
-e ASF_USER="$(id -u)"
--user "$(id -u)":"$(id -g)"
"${image_name}"
)
if [[ "${ASF_CRYPTKEY:-}" != "" ]]; then
docker_opts=(-e ASF_CRYPTKEY="${ASF_CRYPTKEY}" "${docker_opts[@]}")
fi
if [[ "${ASF_ARGS:-}" != "" ]]; then
docker_opts=(-e ASF_ARGS="${ASF_ARGS}" "${docker_opts[@]}")
fi
fi
if docker network inspect "${network}" >/dev/null 2>&1; then
docker_opts=(--network "${network}" "${docker_opts[@]}")
fi
if [[ "${env_file}" != "" ]]; then
mkdir -p "$(dirname "${env_file}")"
touch "${env_file}"
docker_opts=(--env-file "${env_file}" "${docker_opts[@]}")
fi
if has_arg "interactive" && [[ "$(docker images -q --filter=reference="${image_name}")" == "" ]]; then
notify-send "${container_name}" "Pulling image ${image_name}"
fi
docker run -d --rm --name "${container_name}" "${docker_opts[@]}"
local open_url="http://localhost:${port}"
local wait_message="Waiting ${wait_ui_sec} seconds before opening the browser..."
if has_arg "interactive"; then
[[ "${wait_ui_sec}" -gt 1 ]] && notify-send "${container_name}" "${wait_message}"
sleep "${wait_ui_sec}"
if [[ -n "${BROWSER:-}" ]]; then
${BROWSER} "${open_url}"
else
xdg-open "${open_url}"
fi
fi
[[ "${wait_ui_sec}" -gt 1 ]] && info "${wait_message}"
info "${open_url}"
success "${container_name} started."
else
if [[ "${DOCKER_NO_STOP:-}" != "" ]]; then
local open_url="http://localhost:${port}"
if has_arg "interactive"; then
if [[ -n "${BROWSER:-}" ]]; then
${BROWSER} "${open_url}"
else
xdg-open "${open_url}"
fi
fi
info "${open_url}"
exit 0
fi
if has_arg "interactive"; then
notify-send "${container_name}" "Stopped"
fi
success "Stopping..."
docker stop "${container_name}"
if has_arg "metube"; then
rm -f "${mount_dir}/cookies.txt"
rm -rf "${mount_dir}/cache"
fi
success "${container_name} stopped."
fi
}
#}}}
main "${@}"