Skip to content

Commit 7910a71

Browse files
committed
Satisfied shellcheck
1 parent d94de9e commit 7910a71

File tree

2 files changed

+53
-40
lines changed

2 files changed

+53
-40
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,6 @@ Lenovo x390 (16GB RAM)| 04:27 min
135135
Lenoveo P53 (32GB RAM)| 03:31 min
136136

137137
#### gcp
138-
TBD
138+
environment | time
139+
------------|------
140+
not relevant| 05:38

samutev.sh

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fi
5454

5555
# TBD check that gcloud init was done
5656

57-
if [ "${PROV}" != "multipass" -a "$(apt list --installed 2>&1| grep -c -e nfs-kernel-server -e autossh)" != "2" ]; then
57+
if [ "${PROV}" != "multipass" ] && [ "$(apt list --installed 2>&1| grep -c -e nfs-kernel-server -e autossh)" != "2" ]; then
5858
echo
5959
echo " please install nfs-kernel-server and autossh to use samutev with provider $PROV"
6060
echo " => apt install -y nfs-kernel-server portmap autossh"
@@ -80,9 +80,9 @@ function help() {
8080
echo
8181
echo " <release>: default is 'lts' aliased to '${CODENAME_OF_LTS}'"
8282
echo " Other available options are:"
83-
echo "${PROV}" | grep -q '^multipass$' && \
84-
multipass find | grep LTS | awk '{ print $1" (or "$2")" }' | sed 's@,@ or @g' | sed 's@^@ - @g' | sed 's@daily:@@g' || \
85-
gcloud compute images list --filter="family=ubuntu" | grep -e lts | grep minimal | awk '{ print $3}' | sed 's@^@ - @g'
83+
if echo "${PROV}" | grep -q '^multipass$'; then \
84+
multipass find | grep LTS | awk '{ print $1" (or "$2")" }' | sed 's@,@ or @g' | sed 's@^@ - @g' | sed 's@daily:@@g' ;else \
85+
gcloud compute images list --filter="family=ubuntu" | grep -e lts | grep minimal | awk '{ print $3}' | sed 's@^@ - @g';fi
8686
echo
8787
echo "Examples:"
8888
echo " ${scriptname} -n testvm launch new testvm as masterless minion"
@@ -150,9 +150,9 @@ while (("$#")); do
150150
fi
151151
;;
152152
-l | --list)
153-
echo "${PROV}" | grep -q '^multipass$' && \
154-
multipass list || \
155-
gcloud compute instances list
153+
if echo "${PROV}" | grep -q '^multipass$'; then \
154+
multipass list; else \
155+
gcloud compute instances list; fi
156156
shift
157157
;;
158158
-r)
@@ -211,20 +211,23 @@ function create_test_VMs() {
211211
# build needed cloudinit-file
212212
case $TYPE in
213213
"masterless")
214+
# shellcheck disable=SC2154
214215
echo "$CLOUDINIT_1_header" "$CLOUDINIT_2_masterless" "$CLOUDINIT_3_user" > tmp_cloudinit.$$
215216
;;
216217
"minion")
218+
# shellcheck disable=SC2154
217219
echo "$CLOUDINIT_1_header" "$CLOUDINIT_2_minion" "$CLOUDINIT_3_user" | sed -e "s/xxxIPxxx/$MASTER_IP/" > tmp_cloudinit.$$
218220
;;
219221
"master")
222+
# shellcheck disable=SC2154
220223
echo "$CLOUDINIT_1_header" "$CLOUDINIT_2_master" "$CLOUDINIT_3_user" > tmp_cloudinit.$$
221224
;;
222225
esac
223226

224227
# switch cloud-init config as $RELEASE variables et al sadly seem to be not yet available in this cloudinit version
225228
sed -i "s@VERSION_ID@$IMAGE_CODE@g" tmp_cloudinit.$$
226-
echo "${PROV}" | grep -q '^multipass$' && sed -i "s@VERSION_CODENAME@$IMAGE@g" tmp_cloudinit.$$ || \
227-
sed -i "s@VERSION_CODENAME@$OS_CODENAME@g" tmp_cloudinit.$$
229+
if echo "${PROV}" | grep -q '^multipass$'; then sed -i "s@VERSION_CODENAME@$IMAGE@g" tmp_cloudinit.$$; else \
230+
sed -i "s@VERSION_CODENAME@$OS_CODENAME@g" tmp_cloudinit.$$; fi
228231

229232
AUTOSSH_MPORT=8000
230233
# create VMs
@@ -233,6 +236,7 @@ function create_test_VMs() {
233236
unset aCPU CPU aMEM MEM aDISK DISK
234237
VM="${VMinput//:*/}"
235238
if echo "$VMinput" | grep -q :; then
239+
# shellcheck disable=SC2001
236240
for i in $(echo "${VMinput#*:}" | sed -e "s/:/ /g"); do
237241
case $i in
238242
c*)
@@ -263,9 +267,9 @@ function create_test_VMs() {
263267
fi
264268

265269
echo "launching ${VM} ($TYPE with cpu=$CPU mem=${MEM}G disk=${DISK}G${IMAGE_INFO})"
266-
echo "${PROV}" | grep -q '^multipass$' && \
267-
multipass launch --cpus "${CPU}" --disk "${DISK}"G --mem "${MEM}"G --name "${VM}" --cloud-init tmp_cloudinit.$$ "${IMAGE}" || \
268-
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.$$
270+
if echo "${PROV}" | grep -q '^multipass$'; then \
271+
multipass launch --cpus "${CPU}" --disk "${DISK}"G --mem "${MEM}"G --name "${VM}" --cloud-init tmp_cloudinit.$$ "${IMAGE}"; else \
272+
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
269273
RET=$?
270274
if [ "$TYPE" != "minion" ]; then
271275
if [ "${PROV}" != "multipass" ]; then
@@ -291,7 +295,7 @@ function create_test_VMs() {
291295
grep -q "${salt_base}/salt-states" /etc/exports || sudo bash -c "cat /tmp/exports.$$ >> /etc/exports"
292296
# set active and remove backup or revert -> TBD terminate (and cleanup?) here when export mgmt fails
293297
# TBD why does exportfs -a (instead of restart, which would be better) doesn't remove removed exports?
294-
sudo systemctl restart nfs-kernel-server && sudo rm -f /etc/exports.$$ || sudo cp -p /etc/exports.$$ /etc/exports
298+
if sudo systemctl restart nfs-kernel-server; then sudo rm -f /etc/exports.$$; else sudo cp -p /etc/exports.$$ /etc/exports; fi
295299
fi
296300
else
297301
EXEC_CMD_PREFIX="multipass exec ${VM}"
@@ -303,20 +307,20 @@ function create_test_VMs() {
303307
${EXEC_CMD_PREFIX} -- sudo mkdir -p /srv/salt/salt-states
304308
${EXEC_CMD_PREFIX} -- sudo mkdir -p /srv/salt/salt-pillars
305309

306-
echo "${PROV}" | grep -q '^multipass$' && multipass mount "${salt_base}"/salt-states "${VM}":/srv/salt/salt-states || \
307-
${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"
308-
echo "${PROV}" | grep -q '^multipass$' && multipass mount "${salt_base}"/salt-pillars "${VM}":/srv/salt/salt-pillars || \
309-
${EXEC_CMD_PREFIX} -- "mount -t nfs -o proto=tcp localhost:\"${salt_base}\"/salt-pillars /srv/salt/salt-pillars"
310+
if echo "${PROV}" | grep -q '^multipass$'; then multipass mount "${salt_base}"/salt-states "${VM}":/srv/salt/salt-states; else \
311+
${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
312+
if echo "${PROV}" | grep -q '^multipass$'; then multipass mount "${salt_base}"/salt-pillars "${VM}":/srv/salt/salt-pillars; else \
313+
${EXEC_CMD_PREFIX} -- "mount -t nfs -o proto=tcp localhost:\"${salt_base}\"/salt-pillars /srv/salt/salt-pillars"; fi
310314

311315
${EXEC_CMD_PREFIX} -- sudo mkdir -p /srv/salt/localstore
312316

313-
echo "${PROV}" | grep -q '^multipass$' && multipass mount "${salt_base}"/localstore "${VM}":/srv/salt/localstore || \
314-
${EXEC_CMD_PREFIX} -- "mount -t nfs -o proto=tcp localhost:\"${salt_base}\"/localstore /srv/salt/localstore"
317+
if echo "${PROV}" | grep -q '^multipass$'; then multipass mount "${salt_base}"/localstore "${VM}":/srv/salt/localstore; else \
318+
${EXEC_CMD_PREFIX} -- "mount -t nfs -o proto=tcp localhost:\"${salt_base}\"/localstore /srv/salt/localstore"; fi
315319

316320
${EXEC_CMD_PREFIX} -- sudo mkdir -p /srv/salt/salt-dev-pillars
317321

318-
echo "${PROV}" | grep -q '^multipass$' && multipass mount "${salt_base}"/salt-dev-pillars "${VM}":/srv/salt/salt-dev-pillars || \
319-
${EXEC_CMD_PREFIX} -- "mount -t nfs -o proto=tcp localhost:\"${salt_base}\"/salt-dev-pillars /srv/salt/salt-dev-pillars"
322+
if echo "${PROV}" | grep -q '^multipass$'; then multipass mount "${salt_base}"/salt-dev-pillars "${VM}":/srv/salt/salt-dev-pillars; else \
323+
${EXEC_CMD_PREFIX} -- "mount -t nfs -o proto=tcp localhost:\"${salt_base}\"/salt-dev-pillars /srv/salt/salt-dev-pillars"; fi
320324

321325
${EXEC_CMD_PREFIX} -- sudo systemctl restart salt-minion
322326
;;
@@ -329,31 +333,31 @@ function create_test_VMs() {
329333
${EXEC_CMD_PREFIX} -- sudo mkdir -p /srv/salt/salt-states
330334
${EXEC_CMD_PREFIX} -- sudo mkdir -p /srv/salt/salt-pillars
331335

332-
echo "${PROV}" | grep -q '^multipass$' && multipass mount "${salt_base}"/salt-states "${VM}":/srv/salt/salt-states || \
333-
${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"
334-
echo "${PROV}" | grep -q '^multipass$' && multipass mount "${salt_base}"/salt-pillars "${VM}":/srv/salt/salt-pillars || \
335-
${EXEC_CMD_PREFIX} -- "mount -t nfs -o proto=tcp localhost:\"${salt_base}\"/salt-pillars /srv/salt/salt-pillars"
336+
if echo "${PROV}" | grep -q '^multipass$'; then multipass mount "${salt_base}"/salt-states "${VM}":/srv/salt/salt-states; else \
337+
${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
338+
if echo "${PROV}" | grep -q '^multipass$'; then multipass mount "${salt_base}"/salt-pillars "${VM}":/srv/salt/salt-pillars; else \
339+
${EXEC_CMD_PREFIX} -- "mount -t nfs -o proto=tcp localhost:\"${salt_base}\"/salt-pillars /srv/salt/salt-pillars"; fi
336340

337341
${EXEC_CMD_PREFIX} -- sudo mkdir -p /srv/salt/localstore
338342

339-
echo "${PROV}" | grep -q '^multipass$' && multipass mount "${salt_base}"/localstore "${VM}":/srv/salt/localstore || \
340-
${EXEC_CMD_PREFIX} -- "mount -t nfs -o proto=tcp localhost:\"${salt_base}\"/localstore /srv/salt/localstore"
343+
if echo "${PROV}" | grep -q '^multipass$'; then multipass mount "${salt_base}"/localstore "${VM}":/srv/salt/localstore; else \
344+
${EXEC_CMD_PREFIX} -- "mount -t nfs -o proto=tcp localhost:\"${salt_base}\"/localstore /srv/salt/localstore"; fi
341345

342346
${EXEC_CMD_PREFIX} -- sudo mkdir -p /srv/salt/salt-dev-pillars
343347

344-
echo "${PROV}" | grep -q '^multipass$' && multipass mount "${salt_base}"/salt-dev-pillars "${VM}":/srv/salt/salt-dev-pillars || \
345-
${EXEC_CMD_PREFIX} -- "mount -t nfs -o proto=tcp localhost:\"${salt_base}\"/salt-dev-pillars /srv/salt/salt-dev-pillars"
348+
if echo "${PROV}" | grep -q '^multipass$'; then multipass mount "${salt_base}"/salt-dev-pillars "${VM}":/srv/salt/salt-dev-pillars; else \
349+
${EXEC_CMD_PREFIX} -- "mount -t nfs -o proto=tcp localhost:\"${salt_base}\"/salt-dev-pillars /srv/salt/salt-dev-pillars"; fi
346350

347351
${EXEC_CMD_PREFIX} -- sudo systemctl restart salt-master
348352
;;
349353
esac
350354
echo
351355
fi
352356

353-
echo "${PROV}" | grep -q '^multipass$' && multipass info "${VM}" || \
354-
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'
357+
if echo "${PROV}" | grep -q '^multipass$'; then multipass info "${VM}"; else \
358+
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
355359
duration=$SECONDS
356-
echo "launched ${VM} in $(($duration / 60)) minutes and $(($duration % 60)) seconds."
360+
echo "launched ${VM} in $((duration / 60)) minutes and $((duration % 60)) seconds."
357361
echo
358362
done
359363
rm -f tmp_cloudinit.$$
@@ -368,13 +372,13 @@ function delete_and_purge_VMs() {
368372
VM=${VMin%%:*}
369373
echo "delaunching $VM"
370374
echo
371-
echo "${PROV}" | grep -q '^multipass$' && multipass delete "${VM}" || \
372-
gcloud compute instances delete -q --delete-disks=all "${VM}"
375+
if echo "${PROV}" | grep -q '^multipass$'; then multipass delete "${VM}"; else \
376+
gcloud compute instances delete -q --delete-disks=all "${VM}"; fi
373377
if [ "${PROV}" != "multipass" ]; then
374378
killall autossh > /dev/null 2>&1
375379
sudo cp -p /etc/exports{,_preremove.$$}
376380
sudo sed -i "/^.*# samutev .* $VM\$/d" /etc/exports
377-
sudo systemctl restart nfs-kernel-server && sudo rm -f /etc/exports_preremove.$$ || sudo cp -p /etc/exports_preremove.$$ /etc/exports
381+
if sudo systemctl restart nfs-kernel-server; then sudo rm -f /etc/exports_preremove.$$; else sudo cp -p /etc/exports_preremove.$$ /etc/exports; fi
378382
fi
379383
duration=$SECONDS
380384
echo "delaunched $VM in $((duration / 60)) minutes and $((duration % 60)) seconds."
@@ -385,15 +389,18 @@ function delete_and_purge_VMs() {
385389
alltogether=$(date -d "0 $time_end seconds - $time_start seconds" +'%M:%S')
386390
echo "alltogether: $alltogether min."
387391
echo
388-
echo "${PROV}" | grep -q '^multipass$' && multipass list || \
389-
gcloud compute instances list
392+
if echo "${PROV}" | grep -q '^multipass$'; then multipass list; else \
393+
gcloud compute instances list; fi
390394
echo
391395
}
392396

393397
# --- main
394398
# make sure there is no trailing / - important for some NFS export operations
399+
# below shellcheck disable is because the suggested alternative doesn't support regex for line end matching which is a requirement here
400+
# shellcheck disable=SC2001
395401
salt_base=$(echo "${salt_base}" | sed 's@/$@@g')
396402
if [ ! -d "${salt_base}"/salt-dev-pillars ]; then mkdir "${salt_base}"/salt-dev-pillars; fi
403+
# shellcheck disable=SC2154
397404
if [ ! -f "${salt_base}"/salt-dev-pillars/top.sls ]; then echo "$DEVPILLARS_top" > "${salt_base}"/salt-dev-pillars/top.sls; fi
398405
if [ ! -f "${salt_base}"/salt-dev-pillars/devpillars.sls ]; then echo "$DEVPILLARS" > "${salt_base}"/salt-dev-pillars/devpillars.sls; fi
399406
if [ ! -d "${salt_base}"/localstore ]; then mkdir "${salt_base}"/localstore; fi
@@ -405,8 +412,10 @@ if [ -n "${MasterlessVMs_2_CREATE+x}" ]; then
405412
alltogether=$(date -d "0 $time_end seconds - $time_start seconds" +'%M:%S')
406413
echo "alltogether: $alltogether min."
407414
echo
415+
# below shellcheck is disabled because pgrep ist not an alternative if you want to display the full command line (in contrast to information extraction)
416+
# shellcheck disable=SC2009
408417
echo "${PROV}" | grep -q '^multipass$' || \
409-
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")"
418+
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")"
410419
fi
411420

412421
if [ -n "${MasterVMs_2_CREATE+x}" ]; then
@@ -433,8 +442,10 @@ if [ -n "${MasterVMs_2_CREATE+x}" ]; then
433442
echo " salt '*' test.ping"
434443
echo
435444
# TBD improve this
445+
# below shellcheck is disabled because pgrep ist not an alternative if you want to display the full command line (in contrast to information extraction)
446+
# shellcheck disable=SC2009
436447
echo "${PROV}" | grep -q '^multipass$' || \
437-
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")"
448+
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")"
438449
fi
439450

440451
if [ -n "${VMs_2_DELETE+x}" ]; then

0 commit comments

Comments
 (0)