Skip to content

Instantly share code, notes, and snippets.

@GeorgeDewar
Last active August 29, 2015 13:56
Show Gist options
  • Save GeorgeDewar/8994881 to your computer and use it in GitHub Desktop.
Save GeorgeDewar/8994881 to your computer and use it in GitHub Desktop.
Handy little script to shut down or boot up droplets on Digital Ocean - great for demonstrating redundant systems like Ceph, where similarly named nodes need to be killed and restarted during demonstrations.
#!/bin/bash
function usage {
echo "Shutdown and boot Digital Ocean droplets remotely, and simulate network disconnection"
echo
echo "Usage: $0 <kill/boot/killnet/resumenet> node1 [node2] [node3] [node...]"
exit 1
}
# Get settings and node IDs from files
source ~/.digitalocean_api_creds
source node_info
# Print usage if there are less than two arguments (operation + node)
if [ "$#" -lt "2" ]; then
usage
fi
# Use the first parameter as the operation, then shift
op=$1
shift 1
for ARG in $*
do
node=$ARG
fqdn=$node.$domain
case "$op" in
kill)
echo "Telling $fqdn to shut down..."
ssh root@$fqdn poweroff | sed "s/^/$fqdn: /"
echo
;;
boot)
id=${!node}
echo "Telling Digital Ocean to boot $fqdn (droplet $id)..."
curl https://api.digitalocean.com/droplets/$id/power_on/?client_id=$client_id\&api_key=$api_key
echo
;;
killnet)
echo "Simulating network disconnection on $fqdn..."
ssh root@$fqdn /usr/bin/killnet kill | sed "s/^/$fqdn: /"
echo
;;
resumenet)
echo "Resuming network connection on $fqdn..."
ssh root@$fqdn /usr/bin/killnet resume | sed "s/^/$fqdn: /"
echo
;;
*)
usage
;;
esac
done
@GeorgeDewar
Copy link
Author

It requires two supporting files:

~/.digitalocean_api_creds

client_id=<YOUR CLIENT ID>
api_key=<YOUR API KEY>

node_info

domain=<YOUR DOMAIN SUFFIX> (e.g. example.com)

node1=<DIGITAL OCEAN DROPLET ID>
node2=<DIGITAL OCEAN DROPLET ID>

etc

@GeorgeDewar
Copy link
Author

It now integrates with nodekill.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment