Skip to content

Instantly share code, notes, and snippets.

@christianberg
Last active April 14, 2024 17:25
Show Gist options
  • Save christianberg/6082543 to your computer and use it in GitHub Desktop.
Save christianberg/6082543 to your computer and use it in GitHub Desktop.
Shell script to create a Digital Ocean Droplet and install docker
#!/bin/bash
set -e
SECRETFILE=~/.digitalocean
if [[ -z $DIGOCEAN_ID ]] || [[ -z $DIGOCEAN_KEY ]]; then
if [ -e $SECRETFILE ]; then
. $SECRETFILE
fi
fi
if [[ -z $DIGOCEAN_ID ]] || [[ -z $DIGOCEAN_KEY ]]; then
echo "You need to set the environment variables DIGOCEAN_ID and DIGOCEAN_KEY"
echo "or provide them in the file $SECRETFILE"
exit 1
fi
BASE_URL='https://api.digitalocean.com'
AUTH="client_id=$DIGOCEAN_ID&api_key=$DIGOCEAN_KEY"
REGION_NAME="Amsterdam 1"
SIZE_NAME="512MB"
IMAGE_NAME="Ubuntu 13.04 x64"
REGION_ID=`curl -s "$BASE_URL/regions?$AUTH" | jq ".regions | map(select(.name==\"$REGION_NAME\"))[0].id"`
echo "ID of Region $REGION_NAME is $REGION_ID"
SIZE_ID=`curl -s "$BASE_URL/sizes?$AUTH" | jq ".sizes | map(select(.name==\"$SIZE_NAME\"))[0].id"`
echo "ID of Size $SIZE_NAME is $SIZE_ID"
IMAGE_ID=`curl -s "$BASE_URL/images?$AUTH" | jq ".images | map(select(.name==\"$IMAGE_NAME\"))[0].id"`
echo "ID of Image $IMAGE_NAME is $IMAGE_ID"
SSH_KEY_ID=`curl -s "$BASE_URL/ssh_keys?$AUTH" | jq '.ssh_keys[0].id'`
echo "Activating SSH Key with ID $SSH_KEY_ID"
TIMESTAMP=`date '+%Y%m%d%H%M%S'`
DROPLET_NAME="droplet-$TIMESTAMP"
echo "Creating new Droplet $DROPLET_NAME with these specifications..."
RESULT=`curl -s "$BASE_URL/droplets/new?$AUTH&name=$DROPLET_NAME&size_id=$SIZE_ID&image_id=$IMAGE_ID&region_id=$REGION_ID&ssh_key_ids=$SSH_KEY_ID"`
STATUS=`echo $RESULT | jq -r '.status'`
echo "Status: $STATUS"
if [ "$STATUS" != "OK" ]; then
echo "Something went wrong:"
echo $RESULT | jq .
exit 1
fi
DROPLET_ID=`echo $RESULT | jq '.droplet.id'`
echo "Droplet with ID $DROPLET_ID created!"
echo "Waiting for droplet to boot"
for i in {1..60}; do
DROPLET_STATUS=`curl -s "$BASE_URL/droplets/$DROPLET_ID?$AUTH" | jq -r '.droplet.status'`
[ "$DROPLET_STATUS" == 'active' ] && break
echo -n '.'
sleep 5
done
echo
if [ "$DROPLET_STATUS" != 'active' ]; then
echo "Droplet did not boot in time. Status: $DROPLET_STATUS"
exit 1
fi
IP_ADDRESS=`curl -s "$BASE_URL/droplets/$DROPLET_ID?$AUTH" | jq -r '.droplet.ip_address'`
echo "Execute bootstrap script"
BOOTSTRAP_URL="https://gist.github.com/christianberg/6082234/raw/bootstrap.sh"
ssh-keygen -R $IP_ADDRESS
SSH_OPTIONS="-o StrictHostKeyChecking=no"
ssh $SSH_OPTIONS root@$IP_ADDRESS "curl -s $BOOTSTRAP_URL | bash"
echo "*****************************"
echo "* Droplet is ready to use!"
echo "* IP address: $IP_ADDRESS"
echo "*****************************"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment