-
Notifications
You must be signed in to change notification settings - Fork 0
/
droplet
executable file
·203 lines (186 loc) · 6.07 KB
/
droplet
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env bash
# Create a DigitalOcean droplet
# Bugs:
# Only prints/finds the first 200 API results.
# If you've got more than 200 droplets you shouldn't be using this.
set -e
TAG=init # init tag for ansible init config
PTAG=all # default print all tags
SIZE=s-1vcpu-1gb
REGION=lon1
IMAGE=centos-8-x64
NAME=temp-$(TZ=UTC date +%Y%m%d%H%M%S)
KEYID=24135599
error() {
echo "$0: ERROR: $1" >&2
exit 1
}
info() {
echo "$0: INFO: $1" >&2
}
chkcomm() {
command -v "$1" > /dev/null
}
listregions() {
curl -s -f -H "Authorization: Bearer $DO_API_TOKEN" "https://api.digitalocean.com/v2/regions?per_page=200" | \
jq -r '.regions[] | select(.available == true) | "\(.name): \(.slug)"' | sort
}
listimages() {
curl -s -f -H "Authorization: Bearer $DO_API_TOKEN" "https://api.digitalocean.com/v2/images?per_page=200" | \
jq -r '.images[].slug' | grep -v '^null$' | sort
}
listsizes() {
curl -s -f -H "Authorization: Bearer $DO_API_TOKEN" "https://api.digitalocean.com/v2/sizes?per_page=200" | \
jq -r '.sizes[].slug' | sort
}
listtags() {
curl -s -f -H "Authorization: Bearer $DO_API_TOKEN" "https://api.digitalocean.com/v2/tags?per_page=200" | \
jq -r '.tags[] | select(.resources.count > 0) | "\(.name) \(.resources.droplets.count)"'
}
deletedroplet() {
[[ -z "$DELETE" ]] && error "No droplets specified to delete - use ID."
curl -s -f -X DELETE -H "Authorization: Bearer $DO_API_TOKEN" "https://api.digitalocean.com/v2/droplets/$DELETE" || \
error "Could not delete droplet $DELETE - check the DO dashboard to avoid expense"
echo "$DELETE"
}
printdroplets() {
[[ "$PTAG" == "all" ]] && URLEND="" || URLEND="&tag_name=$TAG"
curl -s -f -H "Authorization: Bearer $DO_API_TOKEN" "https://api.digitalocean.com/v2/droplets?per_page=200$URLEND" | \
jq -r '.droplets[] | "\(.id) \(.networks.v4[].ip_address) \(.name) \(.region.slug) \(.image.slug) \(.size.slug) \(.tags | join(" "))"' | sort
}
dropletpubips() {
curl -s -f -H "Authorization: Bearer $DO_API_TOKEN" "https://api.digitalocean.com/v2/droplets/$DROPLETID" | \
jq -r '.droplet.networks.v4[] | select(.type == "public") | .ip_address'
}
addtag() {
curl -s -f -X POST -o /dev/null \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $DO_API_TOKEN" \
"https://api.digitalocean.com/v2/tags" \
-d '{"name": "'"$TAG"'"}'
curl -s -f -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $DO_API_TOKEN" \
"https://api.digitalocean.com/v2/tags/$TAG/resources" \
-d '{"resources": [ { "resource_id": "'"$DROPLET"'", "resource_type": "droplet" } ] }'
curl -s -f -X DELETE \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $DO_API_TOKEN" \
"https://api.digitalocean.com/v2/tags/init/resources" \
-d '{"resources": [ { "resource_id": "'"$DROPLET"'", "resource_type": "droplet" } ] }'
echo "$DROPLET"
}
createdroplet() {
trap 'error "$0 exited unexpectedly. Check the DO dashboard to avoid expense."' EXIT
DROPLETID=$(curl -s -f -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $DO_API_TOKEN" \
-d \
'{
"name":"'"$NAME"'",
"region":"'"$REGION"'",
"size":"'"$SIZE"'",
"image":"'"$IMAGE"'",
"ssh_keys":['"$KEYID"'],
"backups":false,
"ipv6":true,
"user_data":null,
"private_networking":null,
"volumes": null,
"tags":["'"$TAG"'"]
}' \
"https://api.digitalocean.com/v2/droplets" | \
jq .droplet.id)
[[ -z "$DROPLETID" ]] && error "Could not get ID for new droplet."
info "Droplet ID: $DROPLETID"
i=0
while [[ $i -lt 6 ]]; do
DROPLETIP=$(dropletpubips | head -1)
[[ -n "$DROPLETIP" ]] && break
(( ++i ))
sleep 10
done
if [[ -z "$DROPLETIP" ]] || [[ "$DROPLETIP" != *.*.*.* ]]; then
error "Could not get IP for droplet $DROPLET."
fi
trap - EXIT
info "Droplet name: $NAME"
info "Droplet IP: $DROPLETIP"
echo "$DROPLETID"
}
# --- Go! --- #
unset COMMAND
while getopts ':hRISTP:pca:n:r:i:s:t:d:' opt; do
case $opt in
R)
COMMAND=listregions
;;
I)
COMMAND=listimages
;;
S)
COMMAND=listsizes
;;
T)
COMMAND=listtags
;;
P)
COMMAND=dropletpubips
DROPLETID="$OPTARG"
;;
p)
COMMAND=printdroplets
;;
c)
COMMAND=createdroplet
;;
a)
COMMAND=addtag
DROPLET="$OPTARG"
;;
n)
NAME="$OPTARG"
;;
r)
REGION="$OPTARG"
;;
i)
IMAGE="$OPTARG"
;;
s)
SIZE="$OPTARG"
;;
t)
TAG="$OPTARG"
PTAG="$OPTARG"
;;
d)
COMMAND=deletedroplet
DELETE="$OPTARG"
;;
\?|h)
cat <<_EOF >&2
Usage: $0 [-R|-I|-S|-T|-c|-a <ID>|-p|-d <ID>] [-n <name>] [-r <region>] [-i <image>] [-s <size>] [-t <tag>]
-R : list regions
-I : list images
-S : list sizes
-T : list active tags
-c : create droplet
-a <dropletid> : add tag (specify -t <tag>)
-p : print droplets (long listing)
-d <dropletid> : delete droplet
-n <name> : use name
-r <region> : use region
-i <image> : use image
-s <size> : use size
-t <tag> : use tag
_EOF
exit 1
;;
esac
done
chkcomm jq || error "jq command not found."
chkcomm curl || error "curl command not found."
[[ -z "$COMMAND" ]] && error "No command specified."
[[ -z "$DO_API_TOKEN" ]] && error "Environment variable DO_API_TOKEN not set."
$COMMAND