← back to Dw Fleet Refresh

run-weekly.sh

35 lines

#!/bin/bash
# Weekly driver (Mac2 launchd). Builds snapshot + refreshes+deploys all sister
# sites on Kamatera, then emails a summary via George + posts a CNCP card.
set -o pipefail
TS=$(date '+%Y-%m-%d %H:%M %Z')
LOGDIR="$HOME/Projects/dw-fleet-refresh/runs"; mkdir -p "$LOGDIR"
OUT="$LOGDIR/run-$(date +%Y%m%d-%H%M%S).json"
TOKEN=$(grep -E '^SHOPIFY_ADMIN_TOKEN=' "$HOME/Projects/secrets-manager/.env" | head -1 | cut -d= -f2- | tr -d '"'"'"' ')
ssh -o ConnectTimeout=12 root@45.61.58.125 "SHOPIFY_ADMIN_TOKEN='$TOKEN' node /tmp/fleet-weekly-refresh.js --all --deploy" > "$OUT" 2> "$LOGDIR/run-$(date +%Y%m%d-%H%M%S).stderr"
cp "$OUT" "$LOGDIR/last-run.json"
# summarize
SUMMARY=$(node -e '
const d=JSON.parse(require("fs").readFileSync(process.argv[1],"utf8"));
const L=d.ledger||[];
const deployed=L.filter(x=>x.deployed).length;
const failed=L.filter(x=>x.gate&&x.gate!=="PASS");
const totalAdded=L.reduce((s,x)=>s+(x.added||0),0), totalDropped=L.reduce((s,x)=>s+(x.dropped||0),0), totalRefresh=L.reduce((s,x)=>s+(x.refreshed||0),0);
let s=`snapshot=${d.snapshot} live products | sites=${d.sites} | deployed=${deployed} | refreshedURLs=${totalRefresh} droppedDead=${totalDropped} addedNew=${totalAdded}\n`;
if(failed.length){ s+=`\nGATE-BLOCKED (NOT deployed) ${failed.length}:\n`+failed.map(x=>`  ${x.site}: ${x.gate} (prior=${x.prior} final=${x.final})`).join("\n"); }
else s+="\nAll sites passed gates.";
console.log(s);
' "$OUT")
echo "$SUMMARY"
# email via George
BODY="DW weekly fleet refresh — $TS\n\n$SUMMARY\n\nLedger: $OUT"
curl -s -X POST http://127.0.0.1:9850/api/send \
  -H 'Content-Type: application/json' \
  -H "Authorization: Basic $(printf 'admin:%s' "${GEORGE_BASIC_AUTH_PASS:-}" | base64)" \
  -d "$(node -e 'console.log(JSON.stringify({to:"steve@designerwallcoverings.com",subject:"DW weekly fleet refresh",text:process.argv[1]}))' "$BODY")" \
  -o /dev/null -w "george email: HTTP %{http_code}\n" 2>/dev/null || echo "(george email skipped)"
# CNCP card
curl -s -X POST http://127.0.0.1:3333/api/parking-lot -H 'Content-Type: application/json' \
  -d "$(node -e 'console.log(JSON.stringify({title:"Weekly fleet refresh ran",note:process.argv[1]}))' "$SUMMARY")" \
  -o /dev/null -w "cncp card: HTTP %{http_code}\n" 2>/dev/null || echo "(cncp skipped)"