feat: add logger utility + WireGuard setup in pi-scraper-setup.sh
- 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
This commit is contained in:
parent
072978f1a4
commit
5abe6397c4
22
packages/scraper/src/utils/logger.ts
Normal file
22
packages/scraper/src/utils/logger.ts
Normal file
@ -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<string, unknown>) => {
|
||||
if (process.env.LOG_LEVEL === "debug") {
|
||||
console.debug(`[DEBUG] ${ts()} ${msg}`, ctx ?? "");
|
||||
}
|
||||
},
|
||||
info: (msg: string, ctx?: Record<string, unknown>) => {
|
||||
console.log(`[INFO] ${ts()} ${msg}`, ctx ?? "");
|
||||
},
|
||||
warn: (msg: string, ctx?: Record<string, unknown>) => {
|
||||
console.warn(`[WARN] ${ts()} ${msg}`, ctx ?? "");
|
||||
},
|
||||
error: (msg: string, ctx?: Record<string, unknown>) => {
|
||||
console.error(`[ERROR] ${ts()} ${msg}`, ctx ?? "");
|
||||
},
|
||||
};
|
||||
@ -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 <<WGEOF
|
||||
[Interface]
|
||||
PrivateKey = $WG_PRIVKEY
|
||||
Address = $WG_ADDR/24
|
||||
$POSTUPCMD
|
||||
|
||||
[Peer]
|
||||
PublicKey = $ERIK_PUBKEY
|
||||
Endpoint = $ERIK_ENDPOINT
|
||||
AllowedIPs = 10.10.0.1/32
|
||||
PersistentKeepalive = 25
|
||||
WGEOF
|
||||
sudo mv /tmp/wg0.conf /etc/wireguard/wg0.conf
|
||||
sudo chmod 600 /etc/wireguard/wg0.conf
|
||||
sudo wg-quick down wg0 2>/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" \
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user