Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Whisparr #853

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
117 changes: 117 additions & 0 deletions scripts/install/whisparr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/bin/bash
# Whisparr Installer
# Refactored from existing files by Bakerboy448, FlyingSausages and others
# By B, 2022 for swizzin
#shellcheck source=sources/functions/utils
. /etc/swizzin/sources/functions/utils

app_name="whisparr"
if [ -z "$WHISPARR_OWNER" ]; then
if ! WHISPARR_OWNER="$(swizdb get "$app_name/owner")"; then
WHISPARR_OWNER="$(_get_master_username)"
echo_info "Setting ${app_name^} owner = $WHISPARR_OWNER"
swizdb set "$app_name/owner" "$WHISPARR_OWNER"
fi
else
echo_info "Setting ${app_name^} owner = $WHISPARR_OWNER"
swizdb set "$app_name/owner" "$WHISPARR_OWNER"
fi
user="$WHISPARR_OWNER"
swiz_configdir="/home/$user/.config"
app_configdir="$swiz_configdir/${app_name^}"
app_group="$user"
app_port="6900"
app_reqs=("curl" "sqlite3")
app_servicefile="$app_name.service"
app_dir="/opt/${app_name^}"
app_binary="${app_name^}"
#Remove any dashes in appname per FS
app_lockname="${app_name//-/}"
app_branch="nightly"
#ToDo: Update branch

if [ ! -d "$swiz_configdir" ]; then
mkdir -p "$swiz_configdir"
fi
chown "$user":"$user" "$swiz_configdir"

_install_whisparr() {
if [ ! -d "$app_configdir" ]; then
mkdir -p "$app_configdir"
fi
chown -R "$user":"$user" "$app_configdir"

apt_install "${app_reqs[@]}"

echo_progress_start "Downloading release archive"

urlbase="https://$app_name.servarr.com/v1/update/$app_branch/updatefile?os=linux&runtime=netcore"
case "$(_os_arch)" in
"amd64") dlurl="${urlbase}&arch=x64" ;;
"armhf") dlurl="${urlbase}&arch=arm" ;;
"arm64") dlurl="${urlbase}&arch=arm64" ;;
*)
echo_error "Arch not supported"
exit 1
;;
esac

if ! curl "$dlurl" -L -o "/tmp/$app_name.tar.gz" >> "$log" 2>&1; then
echo_error "Download failed, exiting"
exit 1
fi
echo_progress_done "Archive downloaded"

echo_progress_start "Extracting archive"
tar xfv "/tmp/$app_name.tar.gz" --directory /opt/ >> "$log" 2>&1 || {
echo_error "Failed to extract"
exit 1
}
rm -rf "/tmp/$app_name.tar.gz"
chown -R "${user}": "$app_dir"
echo_progress_done "Archive extracted"
}
_systemd_whisparr() {

echo_progress_start "Installing Systemd service"
cat > "/etc/systemd/system/$app_servicefile" << EOF
[Unit]
Description=${app_name^} Daemon
After=syslog.target network.target
[Service]
# Change the user and group variables here.
User=${user}
Group=${app_group}
Type=simple
# Change the path to ${app_name^} here if it is in a different location for you.
ExecStart=$app_dir/$app_binary -nobrowser -data=$app_configdir
TimeoutStopSec=20
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

systemctl -q daemon-reload
systemctl enable --now -q "$app_servicefile"
sleep 1
echo_progress_done "${app_name^} service installed and enabled"
}

_nginx_whisparr() {
if [[ -f /install/.nginx.lock ]]; then
echo_progress_start "Configuring nginx"
bash /usr/local/bin/swizzin/nginx/"$app_name".sh
systemctl reload nginx
echo_progress_done "Nginx configured"
else
echo_info "$app_name will run on port $app_port"
fi
}

_install_whisparr
_systemd_whisparr
_nginx_whisparr

touch "/install/.$app_lockname.lock"
echo_success "${app_name^} installed"
73 changes: 73 additions & 0 deletions scripts/nginx/whisparr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/bin/bash
# Nginx conf for *Arr
# Flying sausages 2020 / Bakerboy448 2021
# Refactored by B 2022
master=$(_get_master_username)
app_name="whisparr"

if ! WHISPARR_OWNER="$(swizdb get $app_name/owner)"; then
WHISPARR_OWNER=$(_get_master_username)
else
WHISPARR_OWNER="$(swizdb get $app_name/owner)"
fi

app_port="6900"
app_sslport="9600"
user="$WHISPARR_OWNER"
app_servicefile="${app_name}.service"
app_configdir="/home/$user/.config/${app_name^}"
app_baseurl="$app_name"
app_branch="nightly"

cat > /etc/nginx/apps/$app_name.conf << WHISPARR
location /$app_baseurl {
proxy_pass http://127.0.0.1:$app_port/$app_baseurl;
proxy_set_header Host \$proxy_host;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_redirect off;
auth_basic "What's the password?";
auth_basic_user_file /etc/htpasswd.d/htpasswd.${master};

proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection \$http_connection;
}
# Allow the App API
location /$app_baseurl/api { auth_request off;
proxy_pass http://127.0.0.1:$app_port/$app_baseurl/api;
}
WHISPARR

wasActive=$(systemctl is-active $app_servicefile)

if [[ $wasActive == "active" ]]; then
echo_log_only "Stopping $app_name"
systemctl stop "$app_servicefile"
fi
apikey=$(grep -oPm1 "(?<=<ApiKey>)[^<]+" "$app_configdir"/config.xml)

# Set to Debug as this is beta software
# ToDo: Logs back to Info
cat > "$app_configdir"/config.xml << WHISPARR
<Config>
<LogLevel>debug</LogLevel>
<UpdateMechanism>BuiltIn</UpdateMechanism>
<BindAddress>127.0.0.1</BindAddress>
<Port>$app_port</Port>
<SslPort>$app_sslport</SslPort>
<EnableSsl>False</EnableSsl>
<LaunchBrowser>False</LaunchBrowser>
<ApiKey>${apikey}</ApiKey>
<AuthenticationMethod>None</AuthenticationMethod>
<UrlBase>$app_baseurl</UrlBase>
<Branch>$app_branch</Branch>
</Config>
WHISPARR

chown -R "$user":"$user" "$app_configdir"

# Switch app back off if it was dead before; otherwise start it
if [[ $wasActive == "active" ]]; then
systemctl start "$app_servicefile" -q
fi
36 changes: 36 additions & 0 deletions scripts/remove/whisparr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

app_name="whisparr"

if ! WHISPARR_OWNER="$(swizdb get $app_name/owner)"; then
WHISPARR_OWNER=$(_get_master_username)
fi

user="$WHISPARR_OWNER"
app_configdir="/home/$user/.config/${app_name^}"
brettpetch marked this conversation as resolved.
Show resolved Hide resolved
app_servicefile="${app_name}.service"
app_dir="/opt/${app_name^}"
app_lockname="$app_name"

if ask "Would you like to purge the configuration?" Y; then
purgeapp="True"
else
purgeapp="False"
fi

systemctl disable --now -q "$app_servicefile"
rm /etc/systemd/system/"$app_servicefile"
systemctl daemon-reload -q
rm -rf "$app_dir"

if [[ "$purgeapp" = "True" ]]; then
rm -rf "$app_configdir"
fi

if [[ -f /install/.nginx.lock ]]; then
rm "/etc/nginx/apps/$app_name.conf"
systemctl reload nginx >> "$log" 2>&1
fi

swizdb clear "$app_name/owner"
rm "/install/.$app_lockname.lock"