-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsamutev.sh
executable file
·456 lines (404 loc) · 21.6 KB
/
samutev.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
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
#!/bin/bash
#
# Script to launch or delaunch multipass (or gcp) vms with masterless minion(s)
# for easy testing of salt states
#
# 2020-10-21 RO
# 2020-11-18 TS
# 2021-02-10 TS added first poc for gcp capability
# default provide is multipass
PROV=${PROVIDER:-multipass}
PROV=${PROV,,}
# set default values for resource settings (applies to help text and code)
C_DEFAULT=2
M_DEFAULT=1
# gcp image minimum is 10G
GCP_MINIMUM_DISK=10
echo "${PROV}" | grep -q '^multipass$' && D_DEFAULT=4 || D_DEFAULT=${GCP_MINIMUM_DISK}
scriptpath=$(readlink -f "${0}")
# shellcheck source=samutev.conf.template
source "${scriptpath%.*}".conf
if [ ! -d "$salt_base" ]; then
echo
echo " configured salt_base=\"$salt_base\" not found!"
echo " please configure a valid directory"
echo
exit 23
fi
# MEMORY_GB filter is somehow not working -> resort to grep...
echo "${PROV}" | grep -q '^multipass$' || GCP_DEFAULT=$(gcloud compute machine-types list --filter="CPUS=${C_DEFAULT} AND zone=${DEFAULT_GCP_ZONE}" 2>&1 | grep "$(printf %.2f ${M_DEFAULT} | sed 's@,@.@g')"$ | awk '{ print $1 }')
if [ "${PROV}" != "multipass" ]; then
echo "$GCP_DEFAULT" | sed '/^$/d' | wc -c | grep -q '^0$' && echo "WARNING: Invalid default compute resource values for gcp, falling back to '${FALLBACK_GCP_MACH_TYPE}'."
echo "$GCP_DEFAULT" | sed '/^$/d' | wc -c | grep -q '^0$' && GCP_DEFAULT=${FALLBACK_GCP_MACH_TYPE}
fi
echo "${PROV}" | grep -q '^multipass$' && MAIN_DEP=multipass || MAIN_DEP=google-cloud-sdk
echo "${PROV}" | grep -q '^multipass$' && SNAP_PARA="" || SNAP_PARA=" --classic\n Run 'gcloud init' afterwards"
snap list ${MAIN_DEP} >/dev/null 2>&1
RET=$?
if [ ${RET} -ne 0 ]; then
echo
echo " please install ${MAIN_DEP} to use samutev with provider $PROV"
echo -e " => snap install ${MAIN_DEP}${SNAP_PARA}"
echo
exit 42
fi
# TBD check that gcloud init was done
if [ "${PROV}" != "multipass" ] && [ "$(apt list --installed 2>&1| grep -c -e nfs-kernel-server -e autossh)" != "2" ]; then
echo
echo " please install nfs-kernel-server and autossh to use samutev with provider $PROV"
echo " => apt install -y nfs-kernel-server portmap autossh"
echo
exit 255
fi
# default value for multipass, latest LTS, currently: "focal"
# default value for gcp, latest LTS, currently *hardcoded* below to focal,minimal,lts -> "ubuntu-minimal-2004-lts"
echo "${PROV}" | grep -q '^multipass$' && \
CODENAME_OF_LTS=$(multipass find | grep LTS | grep lts | awk '{ print $2 }' | sed 's@,@\n@g' | grep -v lts | tail -n1) \
|| \
CODENAME_OF_LTS=$(gcloud compute images list --filter="family=ubuntu" | grep lts | grep focal | grep minimal | awk '{ print $3}')
function help() {
scriptname="$(basename "${0}")"
echo "Usage:"
echo " ${scriptname} -h display this help message"
echo " ${scriptname} [-r <release>] -n <VM> new <VM> with masterless minion"
echo " ${scriptname} [-r <release>] -s <VM> new <VM> with minion and salt master, first vm => saltmaster, minimum of 2 vms"
echo " ${scriptname} -d <VM> delete <VM>"
echo " ${scriptname} -l list vms"
echo
echo " <release>: default is 'lts' aliased to '${CODENAME_OF_LTS}'"
echo " Other available options are:"
if echo "${PROV}" | grep -q '^multipass$'; then \
multipass find | grep LTS | awk '{ print $1" (or "$2")" }' | sed 's@,@ or @g' | sed 's@^@ - @g' | sed 's@daily:@@g' ;else \
gcloud compute images list --filter="family=ubuntu" | grep -e lts | grep minimal | awk '{ print $3}' | sed 's@^@ - @g';fi
echo
echo "Examples:"
echo " ${scriptname} -n testvm launch new testvm as masterless minion"
echo " ${scriptname} -n 'testvm1 testvm2 testvm3' launch multiple new testvms as masterless minions"
echo " ${scriptname} -n 'testvm1:c2:m1:d3 testvm2:c4:m2' launch multiple new testvms as masterless minions"
echo " with special settings for cpu, memory and disk:"
echo " - testvm1 with: c2 => 2 cpu, m1 => 1GB memory and d3 => 3GB disk"
echo " - testvm2 with: c4 => 4 cpu, m2 => 2GB memory"
echo " (defaults are c${C_DEFAULT} m${M_DEFAULT} d${D_DEFAULT})"
echo
echo " ${scriptname} -s 'salt-master1 testvm1 testvm2 testvm3' launch a saltmaster with multiple new testvms"
echo " - First vm = saltmaster"
echo " - Minimum = 2 vms"
echo " ${scriptname} -s 'salt-master1:c2:m2:d6 testvm1' same as above but with custom resource settings"
echo
echo " ${scriptname} -d testvm delaunch/delete testvm"
echo " ${scriptname} -d 'testvm1 testvm2 testvm3' delaunch/delete multiple testvms"
echo
}
# Parse options
#PARAMS=""
IMAGE=""
IMAGE_INFO=""
IMAGE_CODE=""
unset MasterlessVMs_2_CREATE MasterVMs_2_CREATE VMs_2_DELETE
while (("$#")); do
case "$1" in
-h | --help)
help
exit 0
;;
-n | --new)
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
MasterlessVMs_2_CREATE=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
-s | --new-with-saltmaster)
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
MasterVMs_2_CREATE=$2
counter=0
for i in $MasterVMs_2_CREATE; do counter=$((counter + 1)); done
if [ $counter -lt 2 ]; then
echo "Error: need minimum 2 vms - 1 saltmaster-vm and 1 minion-vm"
exit 2
fi
shift 2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
-d | --delete)
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
VMs_2_DELETE=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
-l | --list)
if echo "${PROV}" | grep -q '^multipass$'; then \
multipass list; else \
gcloud compute instances list; fi
shift
;;
-r)
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
IMAGE="$2"
echo "${PROV}" | grep -q '^multipass$' && VALID_IMAGE=$(multipass find | grep LTS | grep -c "${IMAGE}" | grep -q '^0$' && echo not found || echo found) ||\
VALID_IMAGE=$(gcloud compute images list --filter="family=ubuntu" | grep -e lts | grep minimal | awk '{ print $3}' | grep -c "${IMAGE}" | grep -q '^0$' && echo not found || echo found)
if [ "${VALID_IMAGE}" == "not found" ]; then
echo "Error: Argument '${IMAGE}' for $1 is invalid" >&2
exit 2
fi
shift 2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
--* | -*=) # unsupported flags
echo "Error: Unsupported flag $1" >&2
echo
help
exit 1
;;
*) # preserve positional arguments
#PARAMS="$PARAMS $1"
#shift
help
exit 0
;;
esac
done
IMAGE=${IMAGE:-${CODENAME_OF_LTS}}
IMAGE_INFO=" and image '${IMAGE}'"
echo "${IMAGE}" | grep -c lts | grep -q '^1$' && IMAGE=${CODENAME_OF_LTS}
echo "${PROV}" | grep -q '^multipass$' && \
IMAGE=$(multipass find | grep LTS | grep "${IMAGE}" | awk '{ print $2 }' | sed 's@,@\n@' | grep -v lts | head -n1) || \
OS_CODENAME=$(gcloud compute images list --filter="family=ubuntu" | grep -e lts | grep minimal | grep "${IMAGE}" | awk '{ print $1 }' | awk -F- '{ print $4 }')
echo "${PROV}" | grep -q '^multipass$' && \
IMAGE_CODE=$(multipass find | grep LTS | grep "${IMAGE}" | awk '{ print $1 }' | sed 's@daily:@@g') || \
IMAGE_CODE=$(echo "$IMAGE" | awk -F- '{ split($3, chars, ""); print chars[1]chars[2]"."chars[3]chars[4] }')
# set positional arguments in their proper place
#eval set -- "$PARAMS"
#echo PARAMS=$PARAMS
function create_test_VMs() {
# TBD before doing anything: check all names whether they exist already (shared project!)
TYPE=$1
VMs="$2"
if [ "xxx${3}xxx" != "xxxxxx" ]; then MASTER_IP="$3"; fi
# build needed cloudinit-file
case $TYPE in
"masterless")
# shellcheck disable=SC2154
echo "$CLOUDINIT_1_header" "$CLOUDINIT_2_masterless" "$CLOUDINIT_3_user" > tmp_cloudinit.$$
;;
"minion")
# shellcheck disable=SC2154
echo "$CLOUDINIT_1_header" "$CLOUDINIT_2_minion" "$CLOUDINIT_3_user" | sed -e "s/xxxIPxxx/$MASTER_IP/" > tmp_cloudinit.$$
;;
"master")
# shellcheck disable=SC2154
echo "$CLOUDINIT_1_header" "$CLOUDINIT_2_master" "$CLOUDINIT_3_user" > tmp_cloudinit.$$
;;
esac
# switch cloud-init config as $RELEASE variables et al sadly seem to be not yet available in this cloudinit version
sed -i "s@VERSION_ID@$IMAGE_CODE@g" tmp_cloudinit.$$
if echo "${PROV}" | grep -q '^multipass$'; then sed -i "s@VERSION_CODENAME@$IMAGE@g" tmp_cloudinit.$$; else \
sed -i "s@VERSION_CODENAME@$OS_CODENAME@g" tmp_cloudinit.$$; fi
AUTOSSH_MPORT=8000
# create VMs
for VMinput in $VMs; do
SECONDS=0
unset aCPU CPU aMEM MEM aDISK DISK
VM="${VMinput//:*/}"
if echo "$VMinput" | grep -q :; then
# shellcheck disable=SC2001
for i in $(echo "${VMinput#*:}" | sed -e "s/:/ /g"); do
case $i in
c*)
aCPU=${i/c/}
;;
m*)
aMEM=${i/m/}
;;
d*)
aDISK=${i/d/}
;;
esac
done
fi
CPU=${aCPU:-$C_DEFAULT}
MEM=${aMEM:-$M_DEFAULT}
DISK=${aDISK:-$D_DEFAULT}
if [ "${PROV}" != "multipass" ]; then
GCP_MACH_TYPE=$(gcloud compute machine-types list --filter="CPUS=${CPU} AND zone=${DEFAULT_GCP_ZONE}" 2>&1 | grep "$(printf %.2f "${MEM}" | sed 's@,@.@g')"$ | awk '{ print $1 }')
echo "$GCP_MACH_TYPE" | sed '/^$/d' | wc -c | grep -q '^0$' && echo "WARNING: Invalid compute resource values for gcp (VM '$VM'), falling back to '${FALLBACK_GCP_MACH_TYPE}'."
echo "$GCP_MACH_TYPE" | sed '/^$/d' | wc -c | grep -q '^0$' && GCP_MACH_TYPE=${FALLBACK_GCP_MACH_TYPE}
if [ "$DISK" -lt ${GCP_MINIMUM_DISK} ]; then
echo "WARNING: Disk size '$DISK' (VM '$VM') is below minimum of '${GCP_MINIMUM_DISK}' (for gcp) using '${GCP_MINIMUM_DISK}' instead"
DISK=${GCP_MINIMUM_DISK}
fi
fi
echo "launching ${VM} ($TYPE with cpu=$CPU mem=${MEM}G disk=${DISK}G${IMAGE_INFO})"
if echo "${PROV}" | grep -q '^multipass$'; then \
multipass launch --cpus "${CPU}" --disk "${DISK}"G --memory "${MEM}"G --name "${VM}" --cloud-init tmp_cloudinit.$$ "${IMAGE}"; else \
gcloud compute instances create "${VM}" --zone="${DEFAULT_GCP_ZONE}" --machine-type="$GCP_MACH_TYPE" --image-project=ubuntu-os-cloud --image-family="${IMAGE}" --boot-disk-type=pd-standard --boot-disk-size="${DISK}GB" --metadata-from-file user-data=tmp_cloudinit.$$; fi
RET=$?
if [ "$TYPE" != "minion" ]; then
if [ "${PROV}" != "multipass" ]; then
VMIP=$(gcloud compute instances list --filter="NAME=$VM" | awk '{ print $5 }' | grep -v IP)
until ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "root@${VMIP}" whoami 2>/dev/null; do echo "Waiting for vm to be ready..." && sleep 10; done
EXEC_CMD_PREFIX="ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@${VMIP}"
if [ $RET = 0 ]; then
# nfs exports
# backup
sudo cp -p /etc/exports{,.$$}
# remove pre-existing entries for $VM
sudo sed -i "/^.*# samutev .* $VM\$/d" /etc/exports
# add new exports
{
echo "${salt_base}/salt-states localhost(insecure,rw,sync,no_subtree_check,no_root_squash) # samutev $TYPE $VM"
echo "${salt_base}/salt-pillars localhost(insecure,rw,sync,no_subtree_check,no_root_squash) # samutev $TYPE $VM"
echo "${salt_base}/localstore localhost(insecure,rw,sync,no_subtree_check,no_root_squash) # samutev $TYPE $VM"
echo "${salt_base}/salt-dev-pillars localhost(insecure,rw,sync,no_subtree_check,no_root_squash) # samutev $TYPE $VM"
} >> /tmp/exports.$$
# In a masterless scenario this is executed multiple times. Adding the same export >1 time makes NFS fail -> do it only once
grep -q "${salt_base}/salt-states" /etc/exports || sudo bash -c "cat /tmp/exports.$$ >> /etc/exports"
# set active and remove backup or revert -> TBD terminate (and cleanup?) here when export mgmt fails
# TBD why does exportfs -a (instead of restart, which would be better) doesn't remove removed exports?
if sudo systemctl restart nfs-kernel-server; then sudo rm -f /etc/exports.$$; else sudo cp -p /etc/exports.$$ /etc/exports; fi
fi
else
EXEC_CMD_PREFIX="multipass exec ${VM}"
fi
case $TYPE in
"masterless")
echo "${PROV}" | grep -q '^multipass$' || autossh -M $AUTOSSH_MPORT -f -N -R 2049:localhost:2049 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "root@${VMIP}"
AUTOSSH_MPORT=$((AUTOSSH_MPORT+2))
${EXEC_CMD_PREFIX} -- sudo mkdir -p /srv/salt/salt-states
${EXEC_CMD_PREFIX} -- sudo mkdir -p /srv/salt/salt-pillars
if echo "${PROV}" | grep -q '^multipass$'; then multipass mount "${salt_base}"/salt-states "${VM}":/srv/salt/salt-states; else \
${EXEC_CMD_PREFIX} -- "while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo Waiting for cloud-init to finish... && sleep 5; done && mount -t nfs -o proto=tcp localhost:\"${salt_base}\"/salt-states /srv/salt/salt-states"; fi
if echo "${PROV}" | grep -q '^multipass$'; then multipass mount "${salt_base}"/salt-pillars "${VM}":/srv/salt/salt-pillars; else \
${EXEC_CMD_PREFIX} -- "mount -t nfs -o proto=tcp localhost:\"${salt_base}\"/salt-pillars /srv/salt/salt-pillars"; fi
${EXEC_CMD_PREFIX} -- sudo mkdir -p /srv/salt/localstore
if echo "${PROV}" | grep -q '^multipass$'; then multipass mount "${salt_base}"/localstore "${VM}":/srv/salt/localstore; else \
${EXEC_CMD_PREFIX} -- "mount -t nfs -o proto=tcp localhost:\"${salt_base}\"/localstore /srv/salt/localstore"; fi
${EXEC_CMD_PREFIX} -- sudo mkdir -p /srv/salt/salt-dev-pillars
if echo "${PROV}" | grep -q '^multipass$'; then multipass mount "${salt_base}"/salt-dev-pillars "${VM}":/srv/salt/salt-dev-pillars; else \
${EXEC_CMD_PREFIX} -- "mount -t nfs -o proto=tcp localhost:\"${salt_base}\"/salt-dev-pillars /srv/salt/salt-dev-pillars"; fi
${EXEC_CMD_PREFIX} -- sudo systemctl restart salt-minion
;;
"minion")
${EXEC_CMD_PREFIX} -- sudo systemctl restart salt-minion
;;
"master")
echo "${PROV}" | grep -q '^multipass$' || autossh -M $AUTOSSH_MPORT -f -N -R 2049:localhost:2049 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "root@${VMIP}"
${EXEC_CMD_PREFIX} -- sudo mkdir -p /srv/salt/salt-states
${EXEC_CMD_PREFIX} -- sudo mkdir -p /srv/salt/salt-pillars
if echo "${PROV}" | grep -q '^multipass$'; then multipass mount "${salt_base}"/salt-states "${VM}":/srv/salt/salt-states; else \
${EXEC_CMD_PREFIX} -- "while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo Waiting for cloud-init to finish... && sleep 5; done && mount -t nfs -o proto=tcp localhost:\"${salt_base}\"/salt-states /srv/salt/salt-states"; fi
if echo "${PROV}" | grep -q '^multipass$'; then multipass mount "${salt_base}"/salt-pillars "${VM}":/srv/salt/salt-pillars; else \
${EXEC_CMD_PREFIX} -- "mount -t nfs -o proto=tcp localhost:\"${salt_base}\"/salt-pillars /srv/salt/salt-pillars"; fi
${EXEC_CMD_PREFIX} -- sudo mkdir -p /srv/salt/localstore
if echo "${PROV}" | grep -q '^multipass$'; then multipass mount "${salt_base}"/localstore "${VM}":/srv/salt/localstore; else \
${EXEC_CMD_PREFIX} -- "mount -t nfs -o proto=tcp localhost:\"${salt_base}\"/localstore /srv/salt/localstore"; fi
${EXEC_CMD_PREFIX} -- sudo mkdir -p /srv/salt/salt-dev-pillars
if echo "${PROV}" | grep -q '^multipass$'; then multipass mount "${salt_base}"/salt-dev-pillars "${VM}":/srv/salt/salt-dev-pillars; else \
${EXEC_CMD_PREFIX} -- "mount -t nfs -o proto=tcp localhost:\"${salt_base}\"/salt-dev-pillars /srv/salt/salt-dev-pillars"; fi
${EXEC_CMD_PREFIX} -- sudo systemctl restart salt-master
;;
esac
echo
fi
if echo "${PROV}" | grep -q '^multipass$'; then multipass info "${VM}"; else \
gcloud compute instances describe "${VM}" | grep -e ^name -e natIP -e diskSize -e ubuntu-os-cloud | sed 's@^name:@a_name:@g' | sed 's@^ - https.*licenses/@OS: @g' | sed 's@^ *@@g' | sort | sed 's@^a_@@g'; fi
duration=$SECONDS
echo "launched ${VM} in $((duration / 60)) minutes and $((duration % 60)) seconds."
echo
done
rm -f tmp_cloudinit.$$
echo
}
function delete_and_purge_VMs() {
# TBD cleanup /etc/exports when using gcp (upon recreation there is a cleanup -> so low prio for now)
time_start=$(date +%s)
for VMin in ${1}; do
SECONDS=0
VM=${VMin%%:*}
echo "delaunching $VM"
echo
if echo "${PROV}" | grep -q '^multipass$'; then multipass delete "${VM}"; else \
gcloud compute instances delete -q --delete-disks=all "${VM}"; fi
if [ "${PROV}" != "multipass" ]; then
killall autossh > /dev/null 2>&1
sudo cp -p /etc/exports{,_preremove.$$}
sudo sed -i "/^.*# samutev .* $VM\$/d" /etc/exports
if sudo systemctl restart nfs-kernel-server; then sudo rm -f /etc/exports_preremove.$$; else sudo cp -p /etc/exports_preremove.$$ /etc/exports; fi
fi
duration=$SECONDS
echo "delaunched $VM in $((duration / 60)) minutes and $((duration % 60)) seconds."
echo
done
echo "${PROV}" | grep -q '^multipass$' && multipass purge
time_end=$(date +%s)
alltogether=$(date -d "0 $time_end seconds - $time_start seconds" +'%M:%S')
echo "alltogether: $alltogether min."
echo
if echo "${PROV}" | grep -q '^multipass$'; then multipass list; else \
gcloud compute instances list; fi
echo
}
# --- main
# make sure there is no trailing / - important for some NFS export operations
# below shellcheck disable is because the suggested alternative doesn't support regex for line end matching which is a requirement here
# shellcheck disable=SC2001
salt_base=$(echo "${salt_base}" | sed 's@/$@@g')
if [ ! -d "${salt_base}"/salt-dev-pillars ]; then mkdir "${salt_base}"/salt-dev-pillars; fi
# shellcheck disable=SC2154
if [ ! -f "${salt_base}"/salt-dev-pillars/top.sls ]; then echo "$DEVPILLARS_top" > "${salt_base}"/salt-dev-pillars/top.sls; fi
if [ ! -f "${salt_base}"/salt-dev-pillars/devpillars.sls ]; then echo "$DEVPILLARS" > "${salt_base}"/salt-dev-pillars/devpillars.sls; fi
if [ ! -d "${salt_base}"/localstore ]; then mkdir "${salt_base}"/localstore; fi
if [ -n "${MasterlessVMs_2_CREATE+x}" ]; then
time_start=$(date +%s)
create_test_VMs masterless "${MasterlessVMs_2_CREATE}"
time_end=$(date +%s)
alltogether=$(date -d "0 $time_end seconds - $time_start seconds" +'%M:%S')
echo "alltogether: $alltogether min."
echo
# below shellcheck is disabled because pgrep ist not an alternative if you want to display the full command line (in contrast to information extraction)
# shellcheck disable=SC2009
echo "${PROV}" | grep -q '^multipass$' || \
echo -e "HINT: ssh tunnels are not maintained across reboots yet - re-establish them after reboot by running (foreach IP in <master IP or masterless minion IPs>):\n $(ps -ef | grep '[a]utossh' | sed 's@^.*/autossh@autossh -f@g' | grep "root")"
fi
if [ -n "${MasterVMs_2_CREATE+x}" ]; then
time_start=$(date +%s)
MASTER=$(echo "$MasterVMs_2_CREATE" | head -n1 | cut -d ' ' -f1)
MINION=$(echo "$MasterVMs_2_CREATE" | head -n1 | cut -d ' ' -f2-)
create_test_VMs master "${MASTER}"
echo "${PROV}" | grep -q '^multipass$' && NEW_MASTER_IP=$(multipass info "${MASTER%%:*}" | grep IPv4 | awk -F: '{print $2}' | xargs) || \
NEW_MASTER_IP=$(gcloud compute instances list --filter="NAME=$VM" | awk '{ print $5 }' | grep -v IP)
#echo NEW_MASTER_IP=$NEW_MASTER_IP
create_test_VMs minion "${MINION}" "$NEW_MASTER_IP"
MINION_CTR=$(for VMinput in ${MINION}; do VM="${VMinput//:*/}" ; echo "${VM}"; done | wc -l)
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@"$NEW_MASTER_IP" 'while [ "$(find /etc/salt/pki/master/minions_pre/ -type f | wc -l)" != "'"${MINION_CTR}"'" ]; do echo Waiting for minions... && sleep 5; done; salt-key -A -y && echo && salt-key -L'
time_end=$(date +%s)
alltogether=$(date -d "0 $time_end seconds - $time_start seconds" +'%M:%S')
echo
echo "alltogether: $alltogether min."
echo
echo "READY to go:"
echo " ssh root@$NEW_MASTER_IP"
echo " salt '*' test.ping"
echo
# TBD improve this
# below shellcheck is disabled because pgrep ist not an alternative if you want to display the full command line (in contrast to information extraction)
# shellcheck disable=SC2009
echo "${PROV}" | grep -q '^multipass$' || \
echo -e "HINT: ssh tunnels are not maintained across reboots yet - re-establish them after reboot by running (foreach IP in <master IP or masterless minion IPs>):\n $(ps -ef | grep '[a]utossh' | sed 's@^.*/autossh@autossh -f@g' | grep "root")"
fi
if [ -n "${VMs_2_DELETE+x}" ]; then
delete_and_purge_VMs "$VMs_2_DELETE"
fi
# cleanup
rm -f tmp_cloudinit.*