Created
June 3, 2016 07:54
-
-
Save lmduc/5dcb12d73ac936d6c840190c2cfb9219 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
trap 'excode=$?; cleanup; echo $excode; exit' EXIT HUP INT QUIT PIPE TERM | |
cleanup() { | |
# TODO: kill web | |
echo "inside cleanup for $1" | |
deregister_service | |
} | |
wait_till_port_is_open() { | |
echo "Waiting for $1 to launch on port $2..." | |
while ! nc -z $1 $2; do | |
sleep 0.1 # wait for 1/10 of the second before check again | |
done | |
echo "$1 UP!" | |
} | |
register_service() { | |
export CONTAINER_ID=$(cat /proc/self/cgroup | grep "pids:/" | sed 's/\([0-9]\):pids:\/docker\///g') | |
export CONTAINER_IP=$(hostname -i | awk '{ print $1}') | |
curl -XPUT -H "Content-type: application/json" -d "{ | |
\"ID\": \"$CONTAINER_ID\", | |
\"Address\": \"$CONTAINER_IP\", | |
\"Port\": $PORT, | |
\"Name\": \"web\" | |
}" "$CONSUL_SERVER/v1/agent/service/register" | |
} | |
deregister_service() { | |
echo "de-registering $CONTAINER_ID with consul server $CONSUL_SERVER" | |
curl -XPUT "$CONSUL_SERVER/v1/agent/service/deregister/$CONTAINER_ID" | |
} | |
main() { | |
bundle exec rake assets:precompile | |
bundle exec puma -C config/puma.rb config.ru & | |
wait_till_port_is_open localhost "$PORT" | |
register_service | |
echo "Finished and running" | |
} | |
main | |
wait $1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment