Skip to content

Instantly share code, notes, and snippets.

@mikehwang
Created November 8, 2016 13:09
Show Gist options
  • Save mikehwang/a0b55c8170f01b941210c426eabdc1d7 to your computer and use it in GitHub Desktop.
Save mikehwang/a0b55c8170f01b941210c426eabdc1d7 to your computer and use it in GitHub Desktop.
Setting up upstart to run ngrok then to email tunnel info

How to add ngrok to upstart

Dependencies

  • upstart (tested on Ubuntu 14.04)
  • ngrok
  • ssmtp
  • jq
description "ngrok damon"
start on started docker
stop on runlevel [!2345]
respawn limit 10 5
umask 022
script
exec /usr/local/sbin/ngrok start --config /etc/ngrok/ngrok.yml --all
end script
post-start script
# TODO: How do I set this better?
DEST_EMAIL=<FILL IN>
OUTPUT="/tmp/cash-money.out"
# Magic sleep needed. There is some race condition where without this sleep, I never see the echos down
# below
sleep 0.5
while true; do
response=$(curl --write-out %{http_code} --silent --output /dev/null localhost:4040/api/tunnels)
if [ $response -eq 200 ]; then
num_tunnels=$(curl --silent localhost:4040/api/tunnels | jq '.tunnels | length')
# TODO: Assuming that number of tunnels is greater than 0. Need to verify with the config.
if [ $num_tunnels -gt 0 ]; then
echo "Subject: Ngrok arises $(date -u)" > $OUTPUT
curl --silent localhost:4040/api/tunnels | jq .tunnels | jq 'map(.public_url)' >> $OUTPUT
ssmtp $DEST_EMAIL < $OUTPUT
echo "Tunnels are up: $num_tunnels, sent ngrok email"
break
else
echo "No tunnels have been started. Will wait."
fi
fi
# Stole this from docker.conf
initctl status $UPSTART_JOB | grep -qE "(stop|respawn)/" && exit 1
echo "Waiting for ngrok to become fully available"
sleep 0.5
done
end script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment