Arrs: remote host cards via SSH cache, vv_arrs_local_node(), 2h refresh schedule
This commit is contained in:
Executable
+122
@@ -0,0 +1,122 @@
|
||||
#!/bin/bash
|
||||
# ==============================================================================================
|
||||
# ============================= Remote Arr Cache Writer ========================================
|
||||
# ==============================================================================================
|
||||
#
|
||||
# PURPOSE
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
# SSHes to each remote partner host, calls vv_arrs_local_node() on their PHP
|
||||
# stack, and caches the result locally in /tmp/vv_cache/arrs_remote_hostN.json.
|
||||
#
|
||||
# The arrs page reads these files for instant initial load without hitting the
|
||||
# remote arr APIs on every page view. This script runs every 2 hours so remote
|
||||
# library counts stay reasonably current without hammering the network.
|
||||
#
|
||||
# Accepts --host=HOST2 to refresh a single host (used by the UI refresh button).
|
||||
#
|
||||
# ==============================================================================================
|
||||
# USAGE
|
||||
# ==============================================================================================
|
||||
#
|
||||
# remote_arr_cache_writer.sh — refresh all remote hosts
|
||||
# remote_arr_cache_writer.sh --host=HOST2 — refresh one host only
|
||||
# remote_arr_cache_writer.sh --dry-run — show what would happen
|
||||
# remote_arr_cache_writer.sh --log — verbose output
|
||||
#
|
||||
# ==============================================================================================
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
source "$SCRIPT_DIR/../load_config.sh"
|
||||
|
||||
parse_args "$@"
|
||||
acquire_lock
|
||||
detect_hosts
|
||||
|
||||
# Parse --host= from raw args
|
||||
TARGET_HOST=""
|
||||
for arg in "$@"; do
|
||||
case "$arg" in --host=*) TARGET_HOST="${arg#--host=}" ;; esac
|
||||
done
|
||||
|
||||
mkdir -p /tmp/vv_cache
|
||||
|
||||
FETCH_OK=0
|
||||
FETCH_FAIL=0
|
||||
FETCH_SKIP=0
|
||||
|
||||
echo ""
|
||||
echo "━━━ $ICON_SYNC Remote Arr Cache Writer ━━━"
|
||||
[[ "$DRY_RUN" == true ]] && warn "DRY RUN — no changes will be made"
|
||||
[[ -n "$TARGET_HOST" ]] && echo " Target: $TARGET_HOST"
|
||||
echo ""
|
||||
|
||||
for host_var in HOST1 HOST2 HOST3 HOST4 HOST5 HOST6 HOST7 HOST8; do
|
||||
[[ "$host_var" == "$MY_ID" ]] && continue
|
||||
|
||||
hostname="${!host_var:-}"
|
||||
[[ -z "$hostname" ]] && continue
|
||||
|
||||
# If targeting a specific host, skip others
|
||||
if [[ -n "$TARGET_HOST" ]]; then
|
||||
[[ "${host_var,,}" != "${TARGET_HOST,,}" && "$host_var" != "$TARGET_HOST" ]] && continue
|
||||
fi
|
||||
|
||||
host_id="${host_var,,}" # host1, host2, …
|
||||
cache_file="/tmp/vv_cache/arrs_remote_${host_id}.json"
|
||||
|
||||
echo " $host_var ($hostname)…"
|
||||
|
||||
if [[ "$DRY_RUN" == true ]]; then
|
||||
warn " DRY RUN — would SSH to $hostname and cache arr data"
|
||||
(( FETCH_SKIP++ ))
|
||||
continue
|
||||
fi
|
||||
|
||||
# Resolve Tailscale IP
|
||||
REMOTE_IP=$(resolve_tailscale_ip "$hostname")
|
||||
if [[ -z "$REMOTE_IP" ]]; then
|
||||
warn " $host_var: cannot resolve Tailscale IP for $hostname — skipping"
|
||||
(( FETCH_FAIL++ ))
|
||||
continue
|
||||
fi
|
||||
log " $host_var: resolved $hostname → $REMOTE_IP"
|
||||
|
||||
# SSH and call vv_arrs_local_node() on the remote
|
||||
RESULT=$(ssh -i "$SSH_KEY" \
|
||||
-o ConnectTimeout=10 \
|
||||
-o StrictHostKeyChecking=no \
|
||||
-o BatchMode=yes \
|
||||
"root@${REMOTE_IP}" \
|
||||
"php -r \"require_once '/usr/local/emhttp/plugins/varaverk/include/arrs.php'; echo json_encode(vv_arrs_local_node());\"" \
|
||||
2>/dev/null)
|
||||
|
||||
if [[ -z "$RESULT" ]]; then
|
||||
warn " $host_var: empty response from SSH — skipping"
|
||||
(( FETCH_FAIL++ ))
|
||||
continue
|
||||
fi
|
||||
|
||||
# Validate JSON
|
||||
if ! echo "$RESULT" | php -r "
|
||||
\$d = json_decode(file_get_contents('php://stdin'), true);
|
||||
exit(!is_array(\$d) || !isset(\$d['arrs']) ? 1 : 0);
|
||||
" 2>/dev/null; then
|
||||
warn " $host_var: invalid or unexpected JSON — skipping"
|
||||
log " Response: ${RESULT:0:200}"
|
||||
(( FETCH_FAIL++ ))
|
||||
continue
|
||||
fi
|
||||
|
||||
echo "$RESULT" > "$cache_file"
|
||||
CACHED_TYPES=$(echo "$RESULT" | php -r "
|
||||
\$d = json_decode(file_get_contents('php://stdin'), true);
|
||||
echo implode(', ', array_column(\$d['arrs'] ?? [], 'type'));
|
||||
" 2>/dev/null)
|
||||
echo " $host_var: cached ✅ — ${CACHED_TYPES:-no arrs}"
|
||||
(( FETCH_OK++ ))
|
||||
done
|
||||
|
||||
echo ""
|
||||
echo "━━━━━ $ICON_SUMMARY Cache Writer Summary ━━━━━"
|
||||
echo " $FETCH_OK updated · $FETCH_FAIL failed · $FETCH_SKIP skipped"
|
||||
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
Reference in New Issue
Block a user