This repository has been archived by the owner on Dec 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
droplet.sh
executable file
·313 lines (265 loc) · 9.94 KB
/
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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env bash
# droplet.sh - get info about or create a droplet with Vagrant & Ansible
# author : Chad Mayfield (chad@chd.my)
# license : gplv3
if [ ! $# -ge 1 ]; then
echo "ERROR: You must supply an argument! <get|create|help>"
exit 1
fi
avail_regions=
avail_sizes=
avail_distros=
fail() {
echo -e "ERROR:" "$@"
exit 1
}
check_curl() {
command -v curl >/dev/null 2>&1 || fail "curl is not installed! (Get started: https://curl.haxx.se)"
}
check_jq() {
command -v jq >/dev/null 2>&1 || fail "jq is not installed! (Get started: https://stedolan.github.io/jq/)"
}
check_vagrant() {
command -v vagrant >/dev/null 2>&1 || fail "vagrant is not installed! (Get started: https://www.vagrantup.com)"
}
check_key() {
# for security use API key set as env variable in .bashrc
if [ -z ${DO_KEY+x} ]; then
fail "A DigitalOcean API key must be set as \$DO_KEY in your .bashrc!";
fi
}
check_ssh_configd() {
ssh_config="${HOME}/.ssh/config"
directory="${HOME}/.ssh/config.d"
configlet="${HOME}/.ssh/config.d/digitalocean"
if [ ! -e "$ssh_config" ]; then
printf "ERROR: You aren't using a %s!\n" "$ssh_config"
fail "Please create one, run man 5 ssh_config for reference."
fi
if [ "$(grep -c "Include config.d" "$ssh_config")" -eq 0 ]; then
printf "ERROR: config.d is not enabled in %s!\n" "$ssh_config"
fail "You must enable it to proceed. See man 5 ssh_config for details."
fi
if [ -f "$configlet" ]; then
# check if we need to remove old settings
:
fi
}
check_deps_get() {
check_curl
check_jq
check_vagrant
check_key
}
check_deps_create() {
check_deps_get
check_ssh_configd
}
# TODO: Fix these, they don't work properly
check_args() {
# check that we have an available region
get_available_regions
if [ -z "${region}+x" ]; then
region="sfo2"
printf "No region specified, using default: %s\n" "$region"
else
if ! [[ $region =~ $avail_regions ]]; then
fail "Region '$region' is not available or invalid!"
fi
fi
# check that we have an available size in $region
get_available_sizes
if [ -z "${size}+x" ]; then
size="s-1vcpu-1gb"
printf "No size specified, using default: %s\n" "$size"
else
if ! [[ $region =~ $avail_sizes ]]; then
fail "Size '$size' is invalid or not available in region $region!"
fi
fi
# check that we have a distro (not community)
if [ -z "${distro}+x" ]; then
distro="ubuntu-18-04-x64"
printf "No size specified, using default: %s\n" "$distro"
else
if ! [[ $distro =~ x64|x32|stable|beta|alpha|rancher ]]; then
fail "Distro $distro is invalid! Check for valid distro using: $0 get distros"
fi
fi
# check that we have a machine name
if [ -z "${name}+x" ]; then
name="test-vps1"
printf "No size specified, using default: %s\n" "$name"
else
# if ! [[ $name =~ ^[a-zA-Z0-9-_]+$ ]]; then
# TODO: improve regexp
if ! [[ $name =~ ^[[:alnum:]]+$ ]]; then
fail "Name $name invalid! Please use only letters, numbers, dash, & underscores."
fi
fi
}
create_vps() {
machine="${region}-${size}-$(date "+%Y%m%d-%H%M")"
vagrantfile="deployments/${machine}/Vagrantfile"
printf "Deploying machine: %s\n" "$machine"
# make sure we don't kill another deployment
if [ -d "$machine" ]; then
fail "Directory $machine already exists in ./deployments!"
else
cp -R ./skeleton ./deployments/"${machine}"
fi
# modify Vagrantfile with user vars
sed -i -e "s/DROPLET_REGION/${region}/g" "$vagrantfile" || fail "Unable update region!"
sed -i -e "s/DROPLET_SIZE/${size}/g" "$vagrantfile" || fail "Unable to update size!"
sed -i -e "s/DROPLET_DISTRO/${distro}/g" "$vagrantfile" || fail "Unable to update distro!"
sed -i -e "s/DROPLET_NAME/${name}/g" "$vagrantfile" || fail "Unable to update name!"
rm -f "${vagrantfile}-e"
#run the machine
cwd="$(pwd)"
cd "./deployments/${machine}/" || fail "Change directory failed!"
vagrant --provider=digitalocean up || fail "Vagrant up failed!"
cd "$cwd" || fail "Failed to change directory to $cwd"
}
add_ssh_config() {
directory="${HOME}/.ssh/config.d"
if [ ! -d "$directory" ]; then
printf "Directory %s not found, creating it.\n" "$directory"
mkdir -p "$directory"
fi
cwd="$(pwd)"
cd "./deployments/${machine}/" || fail "Change directory failed!"
vagrant ssh-config >> "${directory}/digitalocean"
cd "$cwd" || fail "Failed to change directory to $cwd"
}
get_available_regions() {
# https://developers.digitalocean.com/documentation/v2/#regions
url_regions="https://api.digitalocean.com/v2/regions?page=1&per_page=999"
avail_regions="$(curl -s -H "Authorization: Bearer ${DO_KEY}" "$url_regions" | \
jq -r '.regions[].slug' 2>&1 | tr '\r\n' '|')"
avail_regions="${avail_regions%?}"
}
get_regions() {
# https://developers.digitalocean.com/documentation/v2/#regions
url_regions="https://api.digitalocean.com/v2/regions?page=1&per_page=999"
# compat for ancient version of bash on macOS
list=()
while IFS= read -r line; do
list+=( "$line" )
done < <( curl -s -H "Authorization: Bearer ${DO_KEY}" "$url_regions" | \
jq '.regions[] | "\(.slug)|\(.name)"' | sort )
# for newer versions of bash (v4.2+)
#mapfile -t regions < <( curl -H "Authorization: Bearer ${DO_KEY}" $url_regions | \
# jq '.regions[] | "\(.slug)|\(.name)"' | sort )
printf "==========================\n"
printf "%15s || %4s\n" "ACTIVE LOCATION" "SLUG"
printf "==========================\n"
for i in "${list[@]}"
do
# stip quotes
i=$(sed -e 's/^"//' -e 's/"$//' <<<"$i")
# strip prefix/suffix
printf "%15s => %4s\n" "${i#*|}" "${i%%|*}"
done
}
get_available_sizes() {
# https://developers.digitalocean.com/documentation/v2/#sizes
url_sizes="https://api.digitalocean.com/v2/sizes?page=1&per_page=999"
avail_sizes="$(curl -s -H "Authorization: Bearer ${DO_KEY}" "$url_sizes" | \
jq -c --arg size $size '.sizes[] | select(.available==true) | select(.slug == $size) | .regions' 2>&1 | \
sed 's/[]"[]//g' | sed 's/,/|/g')"
}
get_sizes() {
# https://developers.digitalocean.com/documentation/v2/#sizes
url_sizes="https://api.digitalocean.com/v2/sizes?page=1&per_page=999"
# compat for ancient version of bash on macOS
list=()
while IFS= read -r line; do
list+=( "$line" )
done < <(curl -s -H "Authorization: Bearer ${DO_KEY}" "$url_sizes" | \
jq '.sizes[] | "\(.slug)|\(.memory)|\(.vcpus)|\(.disk)|\(.transfer)|\(.price_monthly)"')
#jq --arg region $region .regions[] | select(.available==true) | select(.slug == $region) | .sizes
# for newer versions of bash (v4.2+)
#mapfile -t slist < <( curl -s -H "Authorization: Bearer ${DO_KEY}" "$url_sizes" | \
# jq '.sizes[] | "\(.slug)|\(.memory)|\(.vcpus)|\(.disk)|\(.transfer)|\(.price_monthly)"')
printf "=======================================================================\n"
printf "%15s : %5s %9s %11s %10s %14s\n" "DROPLET SLUG" "CPU" "MEM" "STORAGE" "BANDWIDTH" "PRICE"
printf "=======================================================================\n"
for i in "${list[@]}"
do
# stip quotes
i=$(sed -e 's/^"//' -e 's/"$//' <<<"$i")
read -r plug mem vcpus disk trans price <<< "$(echo "$i" | awk -F "|" '{ print $1, $2, $3, $4, $5, $6 }')"
printf "%15s : %2scpu %7sMB %5sGB SSD %3sTB Xfer %6s/monthly\n" "$plug" "$vcpus" "$mem" "$disk" "$trans" "\$$price"
done
}
get_distros() {
# https://developers.digitalocean.com/documentation/v2/#list-all-distribution-images
url_distros="https://api.digitalocean.com/v2/images?page=1&per_page=999&type=distribution"
printf "==============================================\n"
printf " %26s || %4s\n" "DISTRIBUTION NAME/VERSION" "SLUG"
printf "==============================================\n"
# compat for ancient version of bash on macOS
list=()
while IFS= read -r line; do
list+=( "$line" )
done < <( curl -s -H "Authorization: Bearer ${DO_KEY}" "$url_distros" | \
jq '.images[] | "\(.distribution) \(.name)|\(.slug)"' | sort )
# for newer versions of bash (v4.2+)
#mapfile -t list < <( curl -H "Authorization: Bearer ${DO_KEY}" $url_distros | \
# jq '.images[] | "\(.distribution) \(.name)|\(.slug)"' | sort )
for i in "${list[@]}"
do
# stip quotes
i=$(sed -e 's/^"//' -e 's/"$//' <<<"$i")
# strip prefix/suffix
printf "%27s => %s\n" "${i%%|*}" "${i#*|}"
done
}
case $1 in
get)
case $2 in
help|--help|-h|--h|-h)
echo "Help comming soon!"
;;
regions)
check_deps_get
get_regions
;;
sizes)
check_deps_get
get_sizes
;;
distros)
check_deps_get
get_distros
;;
*)
fail "You must supply a valid argument! $0 get <regions|sizes|distros>"
;;
esac
;;
create)
case $2 in
help|--help|-h|--h|-h)
echo "Help comming soon!"
;;
*)
region="$2"
size="$3"
distro="$4"
name="$5"
check_deps_create
check_args
create_vps
add_ssh_config
;;
esac
;;
help|--help|-h|--h|-h)
echo "Help comming soon!"
;;
*)
fail "You must supply an argument! <get|create|help>"
;;
esac
#EOF