Last active
April 29, 2018 14:13
-
-
Save MartinAhrer/2d21b8dd73319f0ddbcffa4168125a20 to your computer and use it in GitHub Desktop.
Create DigitalOcean droplets
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
#!/usr/bin/env bash | |
SIZE=1gb | |
IMAGE=ubuntu-16-10-x64 | |
REGION=fra1 | |
TAG=droplet_sh | |
function createDroplet() { | |
: ${1?"Usage: createDroplet name sshkey-passphrase"} | |
: ${2?"Usage: createDroplet name sshkey-passphrase"} | |
set -x | |
ssh-keygen -t rsa -b 4096 -C "${1}" -f ${1}_id_rsa -P ${2} | |
FINGER_PRINT=$(doctl compute ssh-key import ${1} --public-key-file ${1}_id_rsa.pub --format FingerPrint --no-header) | |
DROPLET_ID=$(doctl compute droplet create ${1} --size ${SIZE} --image ${IMAGE} --region ${REGION} --ssh-keys ${FINGER_PRINT} --no-header --format ID --wait) | |
DROPLET_IP_ADDRESS=$(doctl compute droplet get $DROPLET_ID --no-header --format PublicIPv4) | |
echo "DROPLET_NAME=${1}" > ${1}.env | |
echo "DROPLET_ID=${DROPLET_ID}" >> ${1}.env | |
echo "DROPLET_IP_ADDRESS=${DROPLET_IP_ADDRESS}" >> ${1}.env | |
doctl compute droplet-action shutdown $ {DROPLET_ID} | |
set +x | |
#docker-machine create --driver generic --generic-ip-address $DROPLET_IP_ADDRESS --generic-ssh-key ${1}_id_rsa ${1} | |
} | |
function deleteDroplet() { | |
doctl compute droplet ${1} --force | |
} | |
function batchCreateDroplet() { | |
: ${1?"Usage: batchCreateDroplet names-file sshkey-passphrase"} | |
: ${2?"Usage: batchCreateDroplet names-file sshkey-passphrase"} | |
while read -r DROPLET_NAME; do | |
mkdir ${DROPLET_NAME} | |
pushd ${DROPLET_NAME} | |
createDroplet ${DROPLET_NAME} ${2} | |
#echo createDroplet ${DROPLET_NAME} ${2} | |
popd | |
done < ${1} | |
} | |
function md5Fingerprint () { | |
: ${1?"Usage: md5Fingerprint keyfile"} | |
ssh-keygen -l -E md5 -f ${1} | awk {'print substr($2,5)'} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment