-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage-swarm
executable file
·507 lines (400 loc) · 11.8 KB
/
manage-swarm
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#! /usr/bin/env bash
# Check for basic commands which are required before this script
# can be useful.
for checkCommand in docker-machine docker jq curl; do
if [ ! -x "$(command -v "${checkCommand}")" ]; then
echo "\"${checkCommand}\" is required, but missing" >&2
exit 1
fi
done
# Providers
## DigitalOcean
### Prompt for a parameter which can only be answered by querying the DigitalOcean API
function create_digitalocean_ask() {
local what
local apiKey
local values value
what="$1"
apiKey="$2"
echo 'Loading...' >&3
case "${what}" in
size)
values=($(curl -sSL -X GET -H "Content-Type: application/json" -H "Authorization: Bearer ${apiKey}" "https://api.digitalocean.com/v2/sizes" | jq -crM '.sizes[] | .slug' | sort))
;;
region)
values=($(curl -sSL -X GET -H "Content-Type: application/json" -H "Authorization: Bearer ${apiKey}" "https://api.digitalocean.com/v2/regions" | jq -crM '.regions[] | .slug' | sort))
;;
*)
echo "internal error: unable to ask about ${what}" >&2
return 1
;;
esac
echo "Please select one of the following for the ${what}:" >&3
select value in "${values[@]}"; do
if [ -n "${value}" ]; then
echo "${value}"
return 0
fi
done
return 1
}
### Create a new remote Docker Swarm by creating new nodes on DigitalOcean
function create_digitalocean() {
local name token count
local idx startIdx
local pids
local node1 ip
local arg nextArg nextArgName docker_machine_args
local machineName
name="$1"
token="$2"
count='3'
shift; shift;
if [ -z "${name}" -o -z "${token}" ]; then
echo "Usage: manage-swarm create digitalocean <swarmName> <apiToken> [--count <n>]" >&2
echo " [--size <n>] [--region <name>]" >&2
return 1
fi
docker_machine_args=()
startIdx='1'
nextArg=''
docker_machine_args=(
--digitalocean-tags "docker-swarm,${name}"
)
for arg in "$@"; do
if [ -n "${nextArg}" ]; then
if [ "${nextArg}" = '+' ]; then
if [ "${arg}" = 'ask' ]; then
exec 3>&1
arg="$(create_digitalocean_ask "${nextArgName}" "${token}")" || return 1
exec 3>&-
fi
docker_machine_args+=("${arg}")
else
eval "${nextArg}="'"${arg}"'
fi
nextArg=''
continue
fi
case "${arg}" in
--size|--region)
docker_machine_args+=("--digitalocean-${arg:2}")
nextArg='+'
nextArgName="${arg:2}"
;;
--count)
nextArg='count'
;;
--startIndex)
nextArg='startIdx'
;;
esac
done
pids=()
for ((idx = 1; idx <= count; idx++)) {
machineName="${name}-$[${startIdx} + ${idx} - 1]"
docker-machine create --driver digitalocean --digitalocean-image=ubuntu-18-04-x64 \
--digitalocean-monitoring=true \
--digitalocean-access-token "${token}" "${docker_machine_args[@]}" "${machineName}" &
pids+=("$!")
}
wait "${pids[@]}"
node1="${name}-1"
ip="$(docker-machine inspect "${node1}" | jq -crM .Driver.IPAddress)" || return 1
if [ "${startIdx}" = '1' ]; then
docker-machine ssh "${node1}" docker swarm init --advertise-addr "${ip}" || return 1
fi
joinCommand="$(docker-machine ssh "${node1}" docker swarm join-token worker | grep ' --token ')"
if [ -z "${joinCommand}" ]; then
echo "Failed to create join command" >&2
return 1
fi
for ((idx = 1; idx <= count; idx++)) {
machineName="${name}-$[${startIdx} + ${idx} - 1]"
if [ "${machineName}" = "${node1}" ]; then
continue
fi
docker-machine ssh "${name}-$[${startIdx} + ${idx} - 1]" "${joinCommand}" || return 1
}
if [ "${startIdx}" = '1' ]; then
mkdir -p "${swarmEnvDir}"
docker-machine env "${name}-1" > "${swarmEnvDir}/${name}"
fi
echo "Complete !"
return 0
}
### Expand a Docker Swarm by spawning additional droplets on DigitalOcean and adding them
### to the Docker Swarm.
function expand_digitalocean() {
local name info startIdx count
local token size region
name="$1"
info="$2"
startIdx="$3"
count="$4"
shift; shift; shift; shift
token="$(echo "${info}" | jq -crM '.Driver.AccessToken')"
region="$(echo "${info}" | jq -crM '.Driver.Region')"
size="$(echo "${info}" | jq -crM '.Driver.Size')"
create_digitalocean "${name}" "${token}" --count "${count}" --size "${size}" --region "${region}" --startIndex "${startIdx}" "$@" || return $?
return 0
}
# CLI function to create a new remote Docker Swarm by calling the "create" command
# for the provider.
function create() {
local provider
local swarmFile
provider="$1"
name="$2"
shift
if [ -z "${provider}" ]; then
echo "Usage: manage-swarm create <provider> <swarmName> ..." >&2
return 1
fi
swarmFile="${swarmEnvDir}/${name}"
if [ -n "${name}" -a -f "${swarmFile}" ]; then
echo "Swarm already exists: ${swarmFile}" >&2
return 1
fi
create_${provider} "$@" || return "$?"
exportConfigToSwarm "${name}"
return 0
}
# CLI function to expand an existing remote Docker Swarm by calling the "expand" command
# for the provider.
function expand() {
local name count
local provider
local info
local highestID nextID
name="$1"
count="$2"
shift; shift
if [ -z "${count}" ]; then
count='1'
fi
if [ -z "${name}" ]; then
echo "Usage: manage-swarm expand <swarmName> [<count> [<args>...]]" >&2
return 1
fi
swarmFile="${swarmEnvDir}/${name}"
if [ ! -f "${swarmFile}" ]; then
echo "Swarm does not exist: ${swarmFile}" >&2
return 1
fi
info="$(docker-machine inspect "${name}-1")"
highestID="$(docker-machine ls --filter NAME="${name}-[0-9]" -q | sed 's@^.*-@@' | sort -n | tail -n 1)"
nextID=$[${highestID} + 1]
provider="$(echo "${info}" | jq -crM '.DriverName')"
expand_${provider} "${name}" "${info}" "${nextID}" "${count}" "$@" || return $?
exportConfigToSwarm "${name}"
return 0
}
# CLI function to destroy a remote Docker Swarm, this just calls "docker-machine"
# so it is agnostic of all providers, as long as they are supported by
# "docker-machine"
function destroy() {
local name
local swarmFile
name="$1"
if [ -z "${name}" ]; then
echo "Usage: manage-swarm destroy <swarmName>" >&2
return 1
fi
swarmFile="${swarmEnvDir}/${name}"
if [ ! -f "${swarmFile}" ]; then
echo "Swarm does not exist: ${swarmFile}" >&2
return 1
fi
(
. "${swarmFile}"
toDelete=( $(
docker node ls -q | while IFS='' read -r id; do
machine="$(docker node inspect "${id}" | jq -crM '.[0].Description.Hostname')"
echo "${machine}"
done
) )
retval='0'
for machine in "${toDelete[@]}"; do
docker-machine rm --force "${machine}" || retval='1'
done
exit "${retval}"
) || return 1
rm -f "${swarmFile}"
return 0
}
# CLI function to print the status of a given Docker Swarm, it will
# list the stacks running on that swarm.
function status() {
local name
local swarmFile
name="$1"
if [ -z "${name}" ]; then
echo "Usage: manage-swarm status <swarmName>" >&2
return 1
fi
swarmFile="${swarmEnvDir}/${name}"
if [ ! -f "${swarmFile}" ]; then
echo "Swarm does not exist: ${swarmFile}" >&2
return 1
fi
(
. "${swarmFile}"
docker node ls
docker stack ls
) || return 1
return 0
}
# CLI function to print a list of known remote swarms
function list() {
local file
cd "${swarmEnvDir}" 2>/dev/null || return 0
for file in *; do
if [ ! -s "${file}" ]; then
continue
fi
echo "${file}"
done
return 0
}
# Internal function to create an importable configuration from "docker-machine" for
# a remote swarm and store it on every node within the swarm. This is needed so that
# other users of the swarm can access each machine in the swarm when creating or
# managing stacks on the swarm. It needs to be done after every create or expand
# of the swarm. After either of those operations, all users will need to (re-)import
# the swarm.
function exportConfigToSwarm() {
local name
local swarmFile
local tmpdir
local retval
name="$1"
if [ -z "${name}" ]; then
echo "Usage: exportConfigToSwarm <swarmName>" >&2
return 1
fi
swarmFile="${swarmEnvDir}/${name}"
if [ ! -f "${swarmFile}" ]; then
echo "Swarm does not exist: ${swarmFile}" >&2
return 1
fi
tmpdir="$(mktemp -d)" || return 1
tarfile="$(mktemp)" || return 1
(
. "${swarmFile}"
dockerMachineBaseDir="${HOME}/.docker/machine/machines"
docker node ls --format '{{.Hostname}}' | while IFS='' read -r hostname; do
dockerMachineDir="${dockerMachineBaseDir}/${hostname}"
configDir="${tmpdir}/${hostname}"
cp -rp "${dockerMachineDir}" "${configDir}"
cp "${dockerMachineBaseDir}/../certs/key.pem" "${configDir}/key.pem"
cp "${dockerMachineBaseDir}/../certs/cert.pem" "${configDir}/cert.pem"
cp "${dockerMachineBaseDir}/../certs/ca.pem" "${configDir}/ca.pem"
cp "${dockerMachineBaseDir}/../certs/ca-key.pem" "${configDir}/ca-key.pem"
cat "${configDir}/config.json" | sed '
s|"/.*/\.docker/machine/machines/'"${hostname}"'\(/*\)|"@DOCKER_MACHINE_DIR@\1|g
s|"/.*/\.docker/machine/certs/key\.pem"|"@DOCKER_MACHINE_DIR@/key.pem"|g
s|"/.*/\.docker/machine/certs/cert\.pem"|"@DOCKER_MACHINE_DIR@/cert.pem"|g
s|"/.*/\.docker/machine/certs/ca\.pem"|"@DOCKER_MACHINE_DIR@/ca.pem"|g
s|"/.*/\.docker/machine/certs/ca-key\.pem"|"@DOCKER_MACHINE_DIR@/ca-key.pem"|g
s|"/.*/\.docker/machine/certs"|"@DOCKER_MACHINE_DIR@"|g
s|"/.*/\.docker/machine"|"@DOCKER_MACHINE_DIR@/../.."|g
' > "${configDir}/config.json.in"
rm -f "${configDir}/config.json"
done
(
set -e
cd "${tmpdir}"
tar -cf - * | gzip -9c > "${tarfile}"
gzip -dc "${tarfile}" | tar -tf - >/dev/null
) || exit 1
docker node ls --format '{{.Hostname}}' | while IFS='' read -r hostname; do
docker-machine scp "${tarfile}" "${hostname}:/etc/wavelet-swarm.tar.gz" </dev/null
done
)
retval="$?"
rm -rf "${tmpdir}"
rm -f "${tarfile}"
return "${retval}"
}
# CLI function to import the "docker-machine" configuration for a remote swarm.
# It operates by SSH'ing into the IP specified and pulling the stored configuration
# by exportConfigToSwarm.
function import() {
local ip
local swarmFile swarmName
local tmpdir
local retval
ip="$1"
if [ -z "${ip}" ]; then
echo "Usage: manage-swarm import <ip>" >&2
return 1
fi
tmpdir="$(mktemp -d)" || return 1
(
set -e
dockerMachineBaseDir="${HOME}/.docker/machine/machines"
mkdir -p "${dockerMachineBaseDir}"
cd "${tmpdir}"
scp "root@${ip}:/etc/wavelet-swarm.tar.gz" wavelet-swarm.tar.gz
gzip -dc wavelet-swarm.tar.gz | tar -xf - >/dev/null
rm -f wavelet-swarm.tar.gz
for hostname in *; do
dockerMachineDir="${dockerMachineBaseDir}/${hostname}"
if [ -d "${dockerMachineDir}" ]; then
continue
fi
cp -rp "${hostname}" "${dockerMachineDir}/"
sed "s|@DOCKER_MACHINE_DIR@|${dockerMachineDir}|g" "${dockerMachineDir}/config.json.in" > "${dockerMachineDir}/config.json"
rm -f "${dockerMachineDir}/config.json.in"
done
)
retval="$?"
if [ "${retval}" = '0' ]; then
swarmName="$(cd "${tmpdir}" && echo *-1 | sed 's@-1$@@')"
swarmFile="${swarmEnvDir}/${swarmName}"
mkdir -p "${swarmEnvDir}"
docker-machine env "${swarmName}-1" > "${swarmFile}"
fi
rm -rf "${tmpdir}"
return "${retval}"
}
# CLI function to print usage information
function usage() {
echo "Usage: manage-swarm create <provider> <swarmName> ..."
echo " manage-swarm import <ip>"
echo " manage-swarm expand <swarmName> [<count>]"
echo " manage-swarm destroy <swarmName>"
echo " manage-swarm status <swarmName>"
echo " manage-swarm list"
echo " manage-swarm help"
}
# To make operating easier, change directories to the directory this script is in
cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1
action="$1"
shift
configDir="$(dirname "${BASH_SOURCE[0]}")/config"
globalEnvFile="${configDir}/default"
if [ -f "${globalEnvFile}" ]; then
. "${globalEnvFile}"
fi
swarmEnvDir="${configDir}/swarms"
# Map the CLI commands to their functions
case "${action}" in
create|destroy|expand|status|list|import|exportConfigToSwarm)
"${action}" "$@"
exit "$?"
;;
ls)
list "$@"
exit "$?"
;;
help)
usage
exit 0
;;
*)
usage >&2
exit 1
;;
esac