-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
autobrr.sh
executable file
·191 lines (175 loc) · 4.78 KB
/
autobrr.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
#!/bin/bash
# Refactored Swizzin version from Ze0s
user=$(whoami)
mkdir -p "$HOME/.logs/"
export log="$HOME/.logs/autobrr.log"
touch "$log"
function port() {
LOW_BOUND=$1
UPPER_BOUND=$2
comm -23 <(seq ${LOW_BOUND} ${UPPER_BOUND} | sort) <(ss -Htan | awk '{print $4}' | cut -d':' -f2 | sort -u) | shuf | head -n 1
}
function autobrr_download_latest() {
echo "Downloading autobrr release archive"
case "$(dpkg --print-architecture)" in
"amd64") arch='x86_64' ;;
"arm64") arch="arm64" ;;
"armhf") arch="armv6" ;;
*)
echo "Arch not supported"
exit 1
;;
esac
latest=$(curl -sL https://api.github.com/repos/autobrr/autobrr/releases/latest | grep "linux_$arch" | grep browser_download_url | cut -d \" -f4) || {
echo "Failed to query GitHub for latest version"
exit 1
}
mkdir -p "$HOME/.tmp/"
if ! curl "$latest" -L -o "$HOME/.tmp/autobrr.tar.gz" >> "$log" 2>&1; then
echo "Download failed, exiting"
exit 1
fi
echo "Archive downloaded"
echo "Extracting archive"
mkdir -p "$HOME/.local/bin/"
# the archive contains both autobrr and autobrrctl to easily setup the user
tar xfv "$HOME/.tmp/autobrr.tar.gz" --directory "$HOME/.local/bin/" >> "$log" 2>&1 || {
echo "Failed to extract"
exit 1
}
rm -rf "$HOME/.tmp/autobrr.tar.gz"
echo "Archive extracted"
}
_systemd() {
type=simple
if [[ $(systemctl --version | awk 'NR==1 {print $2}') -ge 240 ]]; then
type=exec
fi
echo "Installing Systemd service"
mkdir -p "$HOME/.config/systemd/user/"
cat > "$HOME/.config/systemd/user/autobrr.service" << EOF
[Unit]
Description=autobrr service
After=syslog.target network.target
[Service]
Type=${type}
ExecStart=$HOME/.local/bin/autobrr --config=$HOME/.config/autobrr/
[Install]
WantedBy=default.target
EOF
echo "Service installed"
}
function _add_user {
port=$(port 10000 12000)
# generate a sessionSecret
sessionSecret="$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c16)"
mkdir -p "$HOME/.config/autobrr/"
cat > "$HOME/.config/autobrr/config.toml" << CFG
# config.toml
# Hostname / IP
#
# Default: "localhost"
#
host = "0.0.0.0"
# Port
#
# Default: 8989
#
port = ${port}
# Base url
# Set custom baseUrl eg /autobrr/ to serve in subdirectory.
# Not needed for subdomain, or by accessing with the :port directly.
#
# Optional
#
# baseUrl = "/autobrr/"
# autobrr logs file
# If not defined, logs to stdout
#
# Optional
#
logPath = "$HOME/.config/autobrr/logs/autobrr.log"
# Log level
#
# Default: "DEBUG"
#
# Options: "ERROR", "DEBUG", "INFO", "WARN"
#
logLevel = "DEBUG"
# Session secret
#
sessionSecret = "${sessionSecret}"
CFG
read -rep "Please set a password for your autobrr user ${user}> " password
echo "${password}" | "$HOME/.local/bin/autobrrctl" --config "$HOME/.config/autobrr" create-user "$user" || {
echo "Failed to execute autobrrctl command"
exit 1
}
systemctl --user enable --now autobrr 2>&1 | tee -a "${log}"
touch "$HOME/.install/.autobrr.lock"
echo "autobrr is now installed and running at http://$(hostname -f):${port}/" | tee -a "${log}"
}
function _remove(){
if [[ ! -f $HOME/.install/.autobrr.lock ]]; then
echo "Autobrr not installed!"
exit 1
fi
systemctl stop --user autobrr
systemctl disable --user autobrr
rm "$HOME/.config/systemd/user/autobrr.service"
rm -rf "$HOME/.config/autobrr"
rm $HOME/.local/bin/*brr*
rm "$HOME/.install/.autobrr.lock"
}
function _upgrade {
if [[ ! -f $HOME/.install/.autobrr.lock ]]; then
echo "Autobrr not installed!"
exit 1
fi
autobrr_download_latest
systemctl try-restart --user autobrr
}
echo 'This is unsupported software. You will not get help with this, please answer `yes` if you understand and wish to proceed'
if [[ -z ${eula} ]]; then
read -r eula
fi
if ! [[ $eula =~ yes ]]; then
echo "You did not accept the above. Exiting..."
exit 1
else
echo "Proceeding with installation"
fi
echo "Welcome to the AutoBrr installer..."
echo ""
echo "What do you like to do?"
echo "Logs are stored at ${log}"
echo "install = Install autobrr"
echo "upgrade = upgrades autobrr to latest version"
echo "uninstall = Completely removes autobrr"
echo "exit = Exits Installer"
while true; do
read -r -p "Enter it here: " choice
case $choice in
"install")
autobrr_download_latest
_systemd
_add_user
break
;;
"uninstall")
_remove
break
;;
"upgrade")
_upgrade
break
;;
"exit")
break
;;
*)
echo "Unknown Option."
;;
esac
done
exit