-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_droplet.sh
executable file
·55 lines (49 loc) · 1.61 KB
/
create_droplet.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
usage() {
if [ "$1" ]; then
echo "--------------------------------------------------"
echo $1
fi
echo "--------------------------------------------------"
echo "Usage: $0 list|create|delete [name|id]"
echo
echo "Examples:"
echo " Create: $0 create myname"
echo " Delete: $0 delete 1234"
exit
}
list() {
if hash jq 2>/dev/null; then
http --body https://api.digitalocean.com/v2/droplets "Authorization: Bearer $DIGITAL_OCEAN_TOKEN" | jq '.droplets[] | {id, name, image: .image.slug, ip: .networks.v4[].ip_address, region: .region.slug, size: .size_slug}'
else
http --body https://api.digitalocean.com/v2/droplets "Authorization: Bearer $DIGITAL_OCEAN_TOKEN"
echo
echo "If you had jq installed (http://stedolan.github.io/jq/) this output would have been filtered for brevity..."
fi
}
create() {
if [ $# -lt 1 ]; then
usage "You must specify the name of the droplet to create"
fi
http POST https://api.digitalocean.com/v2/droplets "Authorization: Bearer $DIGITAL_OCEAN_TOKEN" name=$1 region=lon1 size=512mb image=ubuntu-14-04-x64 ipv6=false
}
delete() {
if [ $# -lt 1 ]; then
usage "You must specify the ID of the droplet to delete"
fi
http DELETE https://api.digitalocean.com/v2/droplets/$1 "Authorization: Bearer $DIGITAL_OCEAN_TOKEN"
}
if [ ! $DIGITAL_OCEAN_TOKEN ]; then
echo You must set an environment variable called DIGITAL_OCEAN_TOKEN
exit
fi
case "$1" in
list) list
;;
create) create $2
;;
delete) delete $2
;;
*) usage
;;
esac