forked from dperson/transmission
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transmission.sh
executable file
·115 lines (105 loc) · 4.26 KB
/
transmission.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
#!/usr/bin/env bash
#===============================================================================
# FILE: transmission.sh
#
# USAGE: ./transmission.sh
#
# DESCRIPTION: Entrypoint for transmission docker container
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: David Personette ([email protected]),
# ORGANIZATION:
# CREATED: 09/28/2014 12:11
# REVISION: 1.0
#===============================================================================
set -o nounset # Treat unset variables as an error
dir="/var/lib/transmission-daemon"
### timezone: Set the timezone for the container
# Arguments:
# timezone) for example EST5EDT
# Return: the correct zoneinfo file will be symlinked into place
timezone() { local timezone="${1:-EST5EDT}"
[[ -e /usr/share/zoneinfo/$timezone ]] || {
echo "ERROR: invalid timezone specified: $timezone" >&2
return
}
if [[ -w /etc/timezone && $(cat /etc/timezone) != $timezone ]]; then
echo "$timezone" >/etc/timezone
ln -sf /usr/share/zoneinfo/$timezone /etc/localtime
dpkg-reconfigure -f noninteractive tzdata >/dev/null 2>&1
fi
}
### usage: Help
# Arguments:
# none)
# Return: Help text
usage() { local RC="${1:-0}"
echo "Usage: ${0##*/} [-opt] [command]
Options (fields in '[]' are optional, '<>' are required):
-h This help
-n No auth config; don't configure authentication at runtime
-t \"\" Configure timezone
possible arg: \"[timezone]\" - zoneinfo timezone for container
The 'command' (if provided and valid) will be run instead of transmission
" >&2
exit $RC
}
while getopts ":hnt:" opt; do
case "$opt" in
h) usage ;;
n) export NOAUTH=true ;;
t) timezone $OPTARG ;;
"?") echo "Unknown option: -$OPTARG"; usage 1 ;;
":") echo "No argument value for option: -$OPTARG"; usage 2 ;;
esac
done
shift $(( OPTIND - 1 ))
[[ "${USERID:-""}" =~ ^[0-9]+$ ]] && usermod -u $USERID -o transmission
[[ "${GROUPID:-""}" =~ ^[0-9]+$ ]]&& groupmod -g $GROUPID -o transmission
[[ "${TZ:-""}" ]] && timezone $TZ
for env in $(printenv | grep '^TR_'); do
name="$(cut -c4- <<< ${env%%=*} | tr '_A-Z' '-a-z')"
val="\"${env##*=}\""
[[ "$val" =~ ^\"([0-9]+|false|true)\"$ ]] && val="$(sed 's|"||g' <<< $val)"
sed -i 's|\([0-9A-Za-z"]\)$|\1,|' $dir/info/settings.json
if grep -q "\"$name\"" $dir/info/settings.json; then
sed -i "/\"$name\"/s|:.*|: $val,|" $dir/info/settings.json
else
sed -i "/^}/i\ \"$name\": $val," $dir/info/settings.json
fi
sed -rzi 's/,([^,]*)$/\1/' $dir/info/settings.json
done
watchdir="$(awk -F'=' '/"watch-dir"/ {print $2}' $dir/info/settings.json |
sed 's/[,"]//g')"
[[ -d $dir/downloads ]] || mkdir -p $dir/downloads
[[ -d $dir/incomplete ]] || mkdir -p $dir/incomplete
[[ -d $dir/info/blocklists ]] || mkdir -p $dir/info/blocklists
[[ $watchdir && ! -d $watchdir ]] && mkdir -p $watchdir
chown -Rh transmission. $dir 2>&1 | grep -iv 'Read-only' || :
# Ensure su is owned by root to avoid setgid issues when using user namespaces in daemon
chown root:root /bin/su
if [[ $# -ge 1 && -x $(which $1 2>&-) ]]; then
exec "$@"
elif [[ $# -ge 1 ]]; then
echo "ERROR: command not found: $1"
exit 13
elif ps -ef | egrep -v 'grep|transmission.sh' | grep -q transmission; then
echo "Service already running, please restart container to apply changes"
else
if [[ -z $(find $dir/info/blocklists/bt_level1 -mmin -1080 2>&-) && \
"${BLOCKLIST:-""}" != "no" ]]; then
# Initialize blocklist
url='http://list.iblocklist.com'
curl -Ls "$url"'/?list=bt_level1&fileformat=p2p&archiveformat=gz' |
gzip -cd >$dir/info/blocklists/bt_level1
chown transmission. $dir/info/blocklists/bt_level1
fi
exec su -l transmission -s /bin/bash -c "exec env "CURL_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt" transmission-daemon \
--allowed \\* --blocklist --config-dir $dir/info \
--foreground --log-info --no-portmap \
$([[ ${NOAUTH:-""} ]] && echo '--no-auth' || echo "--auth \
--username ${TRUSER:-admin} --password ${TRPASSWD:-admin}")"
fi