#!/bin/sh # Start the NS1 Mongo SSH tunnel if nothing is already listening on 127.0.0.1:27017. set -eu ROOT="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)" TUNNEL="$ROOT/scripts/mongo-tunnel.sh" LOG="${ZAPIER_TUNNEL_LOG:-$HOME/.grok/zapier-mongo-tunnel.log}" if nc -z 127.0.0.1 27017 2>/dev/null; then echo "mongo tunnel: already up (127.0.0.1:27017)" exit 0 fi if [ ! -x "$TUNNEL" ]; then echo "missing $TUNNEL" >&2 exit 1 fi mkdir -p "$(dirname "$LOG")" nohup "$TUNNEL" >>"$LOG" 2>&1 & echo $! >"${ZAPIER_TUNNEL_PIDFILE:-$HOME/.grok/zapier-mongo-tunnel.pid}" i=0 while [ "$i" -lt 20 ]; do if nc -z 127.0.0.1 27017 2>/dev/null; then echo "mongo tunnel: started (pid $(cat "${ZAPIER_TUNNEL_PIDFILE:-$HOME/.grok/zapier-mongo-tunnel.pid}"))" exit 0 fi i=$((i + 1)) sleep 0.25 done echo "mongo tunnel: failed to bind 127.0.0.1:27017 — see $LOG" >&2 exit 1