← back to Dw Pitch Followup
deploy/finish-tunnel.sh
60 lines
#!/bin/bash
# Finish followup.designerwallcoverings.com once `cloudflared tunnel login`
# has written ~/.cloudflared/cert.pem. Idempotent. Run from anywhere.
set -euo pipefail
CFDIR="$HOME/.cloudflared"
NAME="dw-followup"
HOST="followup.designerwallcoverings.com"
APP_URL="http://127.0.0.1:9768"
[ -f "$CFDIR/cert.pem" ] || { echo "MISSING $CFDIR/cert.pem — run: cloudflared tunnel login"; exit 1; }
# 1. Create tunnel (idempotent)
if cloudflared tunnel list 2>/dev/null | grep -qw "$NAME"; then
echo "tunnel $NAME exists"
else
cloudflared tunnel create "$NAME"
fi
UUID=$(cloudflared tunnel list 2>/dev/null | awk -v n="$NAME" '$2==n{print $1}' | head -1)
[ -n "$UUID" ] || { echo "could not resolve tunnel UUID"; exit 2; }
echo "UUID=$UUID"
# 2. config.yml (local-managed ingress → the app)
cat > "$CFDIR/config.yml" <<YML
tunnel: $UUID
credentials-file: $CFDIR/$UUID.json
ingress:
- hostname: $HOST
service: $APP_URL
- service: http_status:404
YML
echo "wrote $CFDIR/config.yml"
# 3. DNS route (CNAME $HOST → tunnel)
cloudflared tunnel route dns "$NAME" "$HOST" || echo "(route dns: may already exist)"
# 4. launchd service so the tunnel survives reboot
PLIST="$HOME/Library/LaunchAgents/com.steve.dw-followup-tunnel.plist"
cat > "$PLIST" <<PL
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"><dict>
<key>Label</key><string>com.steve.dw-followup-tunnel</string>
<key>ProgramArguments</key><array>
<string>/opt/homebrew/bin/cloudflared</string>
<string>tunnel</string><string>--no-autoupdate</string>
<string>--config</string><string>$CFDIR/config.yml</string>
<string>run</string><string>$NAME</string>
</array>
<key>RunAtLoad</key><true/><key>KeepAlive</key><true/>
<key>StandardOutPath</key><string>$HOME/Projects/dw-pitch-followup/data/tunnel.log</string>
<key>StandardErrorPath</key><string>$HOME/Projects/dw-pitch-followup/data/tunnel.log</string>
</dict></plist>
PL
launchctl bootout "gui/$(id -u)/com.steve.dw-followup-tunnel" 2>/dev/null || true
launchctl bootstrap "gui/$(id -u)" "$PLIST"
echo "tunnel launchd service installed"
sleep 5
echo "=== verify ==="
curl -sI -m 12 "https://$HOST" | head -3 || echo "(give DNS+edge ~30s, retry)"