From 5abe6397c422d63e0db86f54a2e8166f6dec83de Mon Sep 17 00:00:00 2001 From: Rene Fichtmueller Date: Thu, 2 Apr 2026 01:42:25 +0200 Subject: [PATCH] feat: add logger utility + WireGuard setup in pi-scraper-setup.sh MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - utils/logger.ts: minimal console-based logger (debug/info/warn/error) used by community-issues and ebay-enricher scrapers - scripts/pi-scraper-setup.sh: step 7 adds optional WireGuard setup (pass WG_PRIVKEY + WG_ADDR env vars) — connects Pi to Erik for DB access auto-detects dead ethernet and routes WG traffic via working interface --- packages/scraper/src/utils/logger.ts | 22 +++++++++++++++ scripts/pi-scraper-setup.sh | 40 +++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 packages/scraper/src/utils/logger.ts diff --git a/packages/scraper/src/utils/logger.ts b/packages/scraper/src/utils/logger.ts new file mode 100644 index 0000000..9eb8a58 --- /dev/null +++ b/packages/scraper/src/utils/logger.ts @@ -0,0 +1,22 @@ +/** + * Simple logger utility — wraps console with consistent formatting + */ + +const ts = () => new Date().toISOString(); + +export const logger = { + debug: (msg: string, ctx?: Record) => { + if (process.env.LOG_LEVEL === "debug") { + console.debug(`[DEBUG] ${ts()} ${msg}`, ctx ?? ""); + } + }, + info: (msg: string, ctx?: Record) => { + console.log(`[INFO] ${ts()} ${msg}`, ctx ?? ""); + }, + warn: (msg: string, ctx?: Record) => { + console.warn(`[WARN] ${ts()} ${msg}`, ctx ?? ""); + }, + error: (msg: string, ctx?: Record) => { + console.error(`[ERROR] ${ts()} ${msg}`, ctx ?? ""); + }, +}; diff --git a/scripts/pi-scraper-setup.sh b/scripts/pi-scraper-setup.sh index b42a198..5bcdcb1 100644 --- a/scripts/pi-scraper-setup.sh +++ b/scripts/pi-scraper-setup.sh @@ -184,7 +184,45 @@ async function main() { main().catch((e) => { console.error("Fatal:", e); process.exit(1); }); PIEOF -# ── 7. PM2 process ─────────────────────────────────────────────────────────── +# ── 7. WireGuard (connects to Erik 10.10.0.1 for DB access) ───────────────── +WG_PRIVKEY="${WG_PRIVKEY:-}" +ERIK_PUBKEY="nrh8xiPzUWwLDK4y6+Cu0V3ne56zobIHKtxMGb7BKQo=" +ERIK_ENDPOINT="217.154.82.179:51820" +WG_ADDR="${WG_ADDR:-10.10.0.9}" # override per Pi: WG_ADDR=10.10.0.6 + +if [ -n "$WG_PRIVKEY" ]; then + sudo apt-get install -y wireguard-tools 2>/dev/null | tail -1 || true + # Detect primary outgoing interface + OUTIF=$(ip route get 8.8.8.8 2>/dev/null | awk '{for(i=1;i<=NF;i++) if($i=="dev") print $(i+1)}' | head -1) + POSTUPCMD="" + if [ -n "$OUTIF" ] && ! ping -c1 -W2 8.8.8.8 &>/dev/null; then + # Fallback route for WG traffic if default interface has no internet + GW=$(ip route | awk '/default/{print $3; exit}') + POSTUPCMD="PostUp = ip route add $ERIK_ENDPOINT via $GW dev $OUTIF 2>/dev/null || true" + fi + cat > /tmp/wg0.conf </dev/null || true + sudo wg-quick up wg0 + sudo systemctl enable wg-quick@wg0 + echo "WireGuard: $(sudo wg show wg0 | grep 'latest handshake' || echo 'starting...')" +else + echo "WireGuard: skipped (set WG_PRIVKEY and WG_ADDR to enable)" +fi + +# ── 8. PM2 process ─────────────────────────────────────────────────────────── cd "$INSTALL_DIR" PI_NAME="$PI_NAME" pm2 start \ --name "tip-pi-scraper" \