From a56785f0670ba10b1bca4147ae320e364ddc5a0e Mon Sep 17 00:00:00 2001 From: Joseph Hanson Date: Tue, 29 Oct 2024 17:12:17 -0500 Subject: [PATCH] add cross-seed to sabz --- .../default/sabnzbd/app/externalsecret.yaml | 7 + .../apps/default/sabnzbd/app/helmrelease.yaml | 33 +++-- .../default/sabnzbd/app/kustomization.yaml | 8 ++ .../sabnzbd/app/resources/post-process.sh | 121 ++++++++++++++++++ 4 files changed, 161 insertions(+), 8 deletions(-) create mode 100644 kubernetes/apps/default/sabnzbd/app/resources/post-process.sh diff --git a/kubernetes/apps/default/sabnzbd/app/externalsecret.yaml b/kubernetes/apps/default/sabnzbd/app/externalsecret.yaml index 6cd24098..e9b36cbc 100644 --- a/kubernetes/apps/default/sabnzbd/app/externalsecret.yaml +++ b/kubernetes/apps/default/sabnzbd/app/externalsecret.yaml @@ -13,8 +13,15 @@ spec: template: engineVersion: v2 data: + CROSS_SEED_API_KEY: "{{ .CROSS_SEED_API_KEY }}" + PUSHOVER_TOKEN: "{{ .sabz_token }}" + PUSHOVER_USER_KEY: "{{ .userkey_jahanson }}" SABNZBD__API_KEY: "{{ .api_key }}" SABNZBD__NZB_KEY: "{{ .nzb_key }}" dataFrom: + - extract: + key: cross-seed + - extract: + key: pushover - extract: key: sabnzbd diff --git a/kubernetes/apps/default/sabnzbd/app/helmrelease.yaml b/kubernetes/apps/default/sabnzbd/app/helmrelease.yaml index 7e52600d..f6c0cec4 100644 --- a/kubernetes/apps/default/sabnzbd/app/helmrelease.yaml +++ b/kubernetes/apps/default/sabnzbd/app/helmrelease.yaml @@ -22,6 +22,11 @@ spec: remediation: retries: 3 strategy: rollback + dependsOn: + - name: rook-ceph-cluster + namespace: rook-ceph + - name: volsync + namespace: volsync-system values: controllers: sabnzbd: @@ -43,6 +48,11 @@ spec: sabnzbd.default.svc.cluster.local, sabz.jahanson.tech, sabnzbd.jahanson.tech + CROSS_SEED_ENABLED: true + CROSS_SEED_HOST: cross-seed.qbittorrent.svc.cluster.local + CROSS_SEED_PORT: 80 + CROSS_SEED_SLEEP_INTERVAL: 30 + PUSHOVER_ENABLED: true envFrom: - secretRef: name: sabnzbd-secret @@ -70,14 +80,15 @@ spec: cpu: 100m limits: memory: 16Gi - pod: - securityContext: - runAsUser: 568 - runAsGroup: 568 - runAsNonRoot: true - fsGroup: 568 - fsGroupChangePolicy: OnRootMismatch - supplementalGroups: [10000] + defaultPodOptions: + securityContext: + runAsNonRoot: true + runAsUser: 568 + runAsGroup: 568 + fsGroup: 568 + fsGroupChangePolicy: OnRootMismatch + supplementalGroups: [10000] + seccompProfile: { type: RuntimeDefault } service: app: controller: sabnzbd @@ -104,6 +115,12 @@ spec: existingClaim: sabnzbd tmp: type: emptyDir + scripts: + type: configMap + name: sabnzbd-scripts + defaultMode: 0775 + globalMounts: + - readOnly: true media: type: nfs server: 10.1.1.13 diff --git a/kubernetes/apps/default/sabnzbd/app/kustomization.yaml b/kubernetes/apps/default/sabnzbd/app/kustomization.yaml index be13d2db..744f3931 100644 --- a/kubernetes/apps/default/sabnzbd/app/kustomization.yaml +++ b/kubernetes/apps/default/sabnzbd/app/kustomization.yaml @@ -6,3 +6,11 @@ resources: - ./externalsecret.yaml - ./helmrelease.yaml - ../../../../templates/volsync +configMapGenerator: + - name: sabnzbd-scripts + files: + - post-process.sh=./resources/post-process.sh +generatorOptions: + disableNameSuffixHash: true + annotations: + kustomize.toolkit.fluxcd.io/substitute: disabled diff --git a/kubernetes/apps/default/sabnzbd/app/resources/post-process.sh b/kubernetes/apps/default/sabnzbd/app/resources/post-process.sh new file mode 100644 index 00000000..03b6904c --- /dev/null +++ b/kubernetes/apps/default/sabnzbd/app/resources/post-process.sh @@ -0,0 +1,121 @@ +#!/usr/bin/env bash +# shellcheck disable=SC2154 + +set -euo pipefail + +# User-defined variables +CROSS_SEED_ENABLED="${CROSS_SEED_ENABLED:-false}" +CROSS_SEED_HOST="${CROSS_SEED_HOST:-required}" +CROSS_SEED_PORT="${CROSS_SEED_PORT:-required}" +CROSS_SEED_API_KEY="${CROSS_SEED_API_KEY:-required}" +CROSS_SEED_SLEEP_INTERVAL="${CROSS_SEED_SLEEP_INTERVAL:-30}" +PUSHOVER_ENABLED="${PUSHOVER_ENABLED:-false}" +PUSHOVER_USER_KEY="${PUSHOVER_USER_KEY:-required}" +PUSHOVER_TOKEN="${PUSHOVER_TOKEN:-required}" + +# Function to set release variables from SABnzbd +set_sab_vars() { + RELEASE_NAME="${SAB_FILENAME:-}" + RELEASE_DIR="${SAB_COMPLETE_DIR:-}" + RELEASE_CAT="${SAB_CAT:-}" + RELEASE_SIZE="${SAB_BYTES:-}" + RELEASE_STATUS="${SAB_PP_STATUS:-}" + RELEASE_INDEXER="${SAB_URL:-}" + RELEASE_TYPE="NZB" +} + +# Function to set release variables from qBittorrent +set_qb_vars() { + RELEASE_NAME="$1" # %N + RELEASE_DIR="$2" # %F + RELEASE_CAT="$3" # %L + RELEASE_SIZE="$4" # %Z + RELEASE_INDEXER="$5" # %T + RELEASE_STATUS=0 # Always 0 for qBittorrent + RELEASE_TYPE="Torrent" +} + +# Function to send pushover notification +send_pushover_notification() { + local pushover_message status_code json_data + printf -v pushover_message \ + "%s\nCategory: %s\nIndexer: %s\nSize: %s" \ + "${RELEASE_NAME%.*}" \ + "${RELEASE_CAT}" \ + "$(trurl --url "${RELEASE_INDEXER}" --get '{idn:host}')" \ + "$(numfmt --to iec --format "%8.2f" "${RELEASE_SIZE}")" + + json_data=$(jo \ + token="${PUSHOVER_TOKEN}" \ + user="${PUSHOVER_USER_KEY}" \ + title="${RELEASE_TYPE} Downloaded" \ + message="${pushover_message}" \ + priority="-2" \ + html="1" + ) + + status_code=$(curl \ + --silent \ + --write-out "%{http_code}" \ + --output /dev/null \ + --request POST \ + --header "Content-Type: application/json" \ + --data-binary "${json_data}" \ + "https://api.pushover.net/1/messages.json" + ) + + printf "pushover notification returned with HTTP status code %s and payload: %s\n" \ + "${status_code}" \ + "$(echo "${json_data}" | jq --compact-output)" >&2 +} + +# Function to search for cross-seed +search_cross_seed() { + local status_code + status_code=$(curl \ + --silent \ + --output /dev/null \ + --write-out "%{http_code}" \ + --request POST \ + --data-urlencode "path=${RELEASE_DIR}" \ + --header "X-Api-Key: ${CROSS_SEED_API_KEY}" \ + "http://${CROSS_SEED_HOST}:${CROSS_SEED_PORT}/api/webhook" + ) + + printf "cross-seed search returned with HTTP status code %s and path %s\n" \ + "${status_code}" \ + "${RELEASE_DIR}" >&2 + + sleep "${CROSS_SEED_SLEEP_INTERVAL}" +} + +main() { + # Determine the source and set release variables accordingly + if env | grep -q "^SAB_"; then + set_sab_vars + else + set_qb_vars "$@" + fi + + # Check if post-processing was successful + if [[ "${RELEASE_STATUS}" -ne 0 ]]; then + printf "post-processing failed with sabnzbd status code %s\n" \ + "${RELEASE_STATUS}" >&2 + exit 1 + fi + + # Update permissions on the release directory + chmod -R 750 "${RELEASE_DIR}" + + # Send pushover notification + if [[ "${PUSHOVER_ENABLED}" == "true" ]]; then + send_pushover_notification + fi + + # Search for cross-seed + if [[ "${CROSS_SEED_ENABLED}" == "true" ]]; then + search_cross_seed + fi +} + +main "$@"