forked from Taxel/PlexTraktSync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·67 lines (53 loc) · 1.28 KB
/
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
#!/bin/sh
: ${TRACE:=}
: ${APP_USER:=plextraktsync}
: ${APP_GROUP:=plextraktsync}
ensure_dir() {
install -o "$APP_USER" -g "$APP_GROUP" -d "$@"
}
ensure_owner() {
chown "$APP_USER:$APP_GROUP" "$@"
}
# change uid/gid of app user if requested
setup_user() {
local uid=${PUID:-}
local gid=${PGID:-}
if [ -n "$uid" ] && [ "$(id -u $APP_USER)" != "$uid" ]; then
usermod -o -u "$uid" "$APP_USER"
fi
if [ -n "$gid" ] && [ "$(id -g $APP_GROUP)" != "$gid" ]; then
groupmod -o -g "$gid" "$APP_GROUP"
fi
}
# Run command as app user
# https://github.com/karelzak/util-linux/issues/325
switch_user() {
local uid=$(id -u "$APP_USER")
local gid=$(id -g "$APP_GROUP")
exec setpriv --euid "$uid" --ruid "$uid" --clear-groups --egid "$gid" --rgid "$gid" -- "$@"
}
fix_permissions() {
ensure_dir /app/config
ensure_owner /app/config -R
}
needs_switch_user() {
local uid=${PUID:-0}
local gid=${PGID:-0}
# configured to run as non-root
if [ "$uid" -eq 0 ] && [ "$gid" -eq 0 ]; then
return 1
fi
# must be root to be able to switch user
[ "$(id -u)" -eq 0 ]
}
set -eu
test -n "$TRACE" && set -x
# prepend default command
set -- python -m plextraktsync "$@"
# fix permissions and switch user if configured
if needs_switch_user; then
setup_user
fix_permissions
switch_user "$@"
fi
exec "$@"