Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tyvsmith/c1b2d0c475502bea87f0f67cc4cd0a5b to your computer and use it in GitHub Desktop.
Save tyvsmith/c1b2d0c475502bea87f0f67cc4cd0a5b to your computer and use it in GitHub Desktop.
TrueCharts - Sync QBittorrent Gluetun Port Forwards
#!/bin/bash
# Run on a cron from the host.
NAMESPACE=ix-qbittorrent
CONTAINER_VPN=qbittorrent-vpn
CONTAINER_BITTORRENT=qbittorrent
LAST_PORT_FILE=.last_gluten_forwarded_port
# Fetch the name of the pod with the label "app.kubernetes.io/instance=qbittorrent"
POD=$(k3s kubectl -n $NAMESPACE get pod -l app.kubernetes.io/instance=qbittorrent -o jsonpath="{.items[0].metadata.name}")
# Check the status of the pod
POD_STATUS=$(k3s kubectl -n $NAMESPACE get pod $POD -o jsonpath="{.status.phase}")
if [[ $POD_STATUS != "Running" ]]; then
echo "Pod is not in the Running state, exiting..."
exit 1
fi
# Get the forwarded port from the file inside the qbittorrent-vpn container
NEW_PORT=$(k3s kubectl exec -n $NAMESPACE $POD -c $CONTAINER_VPN -- cat /tmp/gluetun/forwarded_port)
if [ -z "$NEW_PORT" ]; then
echo "No port found in /tmp/gluetun/forwarded_port"
exit 1
fi
# Compare the new port with the last port value
if [[ -f "$LAST_PORT_FILE" ]]; then
LAST_PORT=$(cat $LAST_PORT_FILE)
else
LAST_PORT=""
fi
if [[ $NEW_PORT == $LAST_PORT ]]; then
echo "Port has not changed, exiting..."
exit 0
else
# If the port has changed, store the new port value
echo $NEW_PORT > $LAST_PORT_FILE
# Execute the curl request in the qbittorrent container and check response
RESPONSE=$(k3s kubectl exec -n $NAMESPACE $POD -c $CONTAINER_BITTORRENT -- sh -c "/usr/bin/curl -s -o /dev/null -w '%{http_code}' 'http://localhost:8080/api/v2/app/setPreferences' -d 'json={\"listen_port\": \"$NEW_PORT\"}'")
if [ "$RESPONSE" -ne 200 ]; then
echo "Curl request failed with HTTP response code $RESPONSE"
exit 1
fi
echo "Port updated to $NEW_PORT"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment