Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Marsu31/33555a21f5a5331618b6d85417f4e946 to your computer and use it in GitHub Desktop.

Select an option

Save Marsu31/33555a21f5a5331618b6d85417f4e946 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
# 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
LAST_PORT=$(k3s kubectl exec -n $NAMESPACE $POD -c $CONTAINER_BITTORRENT -- sh -c "/usr/bin/curl -s 'http://localhost:8080/api/v2/app/preferences' | jq .listen_port")
if [[ $NEW_PORT == $LAST_PORT ]]; then
echo "Port has not changed, exiting..."
exit 0
else
# 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
@Marsu31
Copy link
Author

Marsu31 commented Nov 6, 2023

Fork from https://gist.github.com/tyvsmith/c1b2d0c475502bea87f0f67cc4cd0a5b.
Added: call qbittorrent API to know the current listen port value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment