Skip to content

Instantly share code, notes, and snippets.

@bhyde
Created July 30, 2014 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhyde/f0ad822c760151df9331 to your computer and use it in GitHub Desktop.
Save bhyde/f0ad822c760151df9331 to your computer and use it in GitHub Desktop.
Create droplet using tugboat, and wait for it. Wait till we can ssh_keyscan can get it's host key. Update my known_hosts. Plus, a paranoid check that ssh is working reliability.
#!/bin/bash
# Usage: start-droplet-setup-ssh <name> london docker
NAME="$1"
REGION="$2"
IMAGE="$3"
function LOG() { echo "LOG: $*" ; }
function ERROR() { echo "ERROR: $*" ; exit 1 ;}
function wrapup(){ LOG install start for $NAME; }
trap wrapup EXIT
set -o nounset -o errexit
if tugboat droplets 2>/dev/null | grep --silent "$NAME" ; then
ERROR "Droplet $NAME already exists"
fi
LOG Enqueue creation request for $NAME in $REGION, a $IMAGE
case $REGION in
london) REGION=7 ;;
*) ERROR Unknown region $REGION ;;
esac
case $IMAGE in
docker) IMAGE=18494 ;;
*) ERROR unknown image $IMAGE ;;
esac
tugboat create -k "$IMAGE" -r "$REGION" -i 4970653 "$NAME"
LOG 'Wait for the end of creation.'
tugboat wait "--name=$NAME"
IP="$(tugboat info --name=$NAME | sed -n '/IP/s/^.* //p')"
LOG "Address of droplet $NAME is $IP"
LOG waiting for sshd and get hostkey
while echo ".." ; do
HOSTKEY="$( ssh-keyscan -T 20 $IP 2> /dev/null )"
if [[ ouch != "ouch$HOSTKEY" ]] ; then
echo "$HOSTKEY"
break
fi
done
LOG Add $NAME to known_hosts
cp ~/.ssh/known_hosts ~/.ssh/known_hosts.old
sed "/^$IP /d" ~/.ssh/known_hosts.old > ~/.ssh/known_hosts
echo "$HOSTKEY" >> ~/.ssh/known_hosts
LOG Check for reliablity
ID="root@$IP"
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 ; do
echo -n "$i --"
if ! ssh -o ConnectTimeout=3 "$ID" uptime 2>&1 ; then
traceroute "$IP"
:
fi
sleep 3
done
LOG "$NAME is available via ssh root@$IP"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment