From f613e84c8d16b6000e18880d1657d91bfa96d52e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Franti=C5=A1ek=20Dvo=C5=99=C3=A1k?= <valtri@civ.zcu.cz>
Date: Tue, 5 Nov 2024 01:20:18 +0100
Subject: [PATCH] Shell linting

---
 cesnet-mcc/deploy.sh                          |   6 +-
 .../files/usr/local/bin/k8s-pods-cleaner.sh   |   2 +-
 .../files/usr/local/bin/xfs-quotas.sh         | 106 ++++++++++--------
 production1/deploy.sh                         |   6 +-
 production2/deploy.sh                         |   6 +-
 staging1/deploy.sh                            |   6 +-
 staging2/deploy.sh                            |   6 +-
 testing/deploy.sh                             |   4 +-
 8 files changed, 75 insertions(+), 67 deletions(-)

diff --git a/cesnet-mcc/deploy.sh b/cesnet-mcc/deploy.sh
index d051d3d..905af96 100755
--- a/cesnet-mcc/deploy.sh
+++ b/cesnet-mcc/deploy.sh
@@ -9,7 +9,7 @@ cd -
 cp -pv terraform/inventory.yaml inventory/1-cesnet.yaml
 
 # dynamic DNS
-ip="$(head -n 1 < terraform/fip.txt)"
+ip="$(head -n 1 <terraform/fip.txt)"
 shellstate=$(shopt -po xtrace)
 set +o xtrace
 # https://nsupdate.fedcloud.eu
@@ -24,11 +24,11 @@ echo "Terraform finished. Continue? (CTRL-C to quit)"
 read -r _
 
 # wait for ping and ssh
-for ip in $(cat terraform/hosts.txt); do
+while read -r ip; do
 	while ! ping -c 1 "$ip"; do sleep 5; done
 	ssh-keygen -R "$ip"
 	while ! ssh egi@"$ip" -o ConnectTimeout=10 -o PreferredAuthentications=publickey -o StrictHostKeyChecking=no :; do sleep 10; done
-done
+done <terraform/hosts.txt
 
 # check ssh access
 ansible -m command -a 'uname -a' allnodes
diff --git a/common/playbooks/files/usr/local/bin/k8s-pods-cleaner.sh b/common/playbooks/files/usr/local/bin/k8s-pods-cleaner.sh
index 6285933..007fc6e 100755
--- a/common/playbooks/files/usr/local/bin/k8s-pods-cleaner.sh
+++ b/common/playbooks/files/usr/local/bin/k8s-pods-cleaner.sh
@@ -17,7 +17,7 @@ NS="$1"
 PERFORM="$2"
 for pod in $(kubectl get pod -n "$NS" | awk 'NR>1 && $3=="Terminating" {print $1}'); do
 	if [ -n "$PERFORM" ]; then
-		kubectl get pod -n "$NS" "$pod" -oyaml > "/tmp/$NS-$pod.yaml"
+		kubectl get pod -n "$NS" "$pod" -oyaml >"/tmp/$NS-$pod.yaml"
 		kubectl delete pod -n "$NS" "$pod" --force
 	else
 		kubectl get pod -n "$NS" "$pod"
diff --git a/common/playbooks/files/usr/local/bin/xfs-quotas.sh b/common/playbooks/files/usr/local/bin/xfs-quotas.sh
index e0dd517..339f07f 100755
--- a/common/playbooks/files/usr/local/bin/xfs-quotas.sh
+++ b/common/playbooks/files/usr/local/bin/xfs-quotas.sh
@@ -29,7 +29,7 @@ DISK_MOUNTPOINT=/exports
 TARGET_PATH=/exports
 
 usage() {
-	cat << EOF
+	cat <<EOF
 $PROG [OPTIONS]
 OPTIONS are:
   -h,--help
@@ -42,7 +42,6 @@ EOF
 	return 0
 }
 
-
 TEMP=$(getopt -o 'i:e:s:q:hn' --long 'include:,exclude:,soft-quota:,hard-quota:,help,dry-run' -n 'xfs-quotas.sh' -- "$@") || exit $?
 eval set -- "$TEMP"
 unset TEMP
@@ -53,45 +52,45 @@ hard="$HARD"
 truncate -s 0 /tmp/quota.list
 while true; do
 	case "$1" in
-		-h|--help)
-			usage
-			shift
-			exit 0
-			;;
-		-s|--soft-quota)
-			soft="$2"
-			shift 2
-			continue
-			;;
-		-q|--hard-quota)
-			hard="$2"
-			shift 2
-			continue
-			;;
-		-e|--exclude)
-			grep -v "$2" /tmp/quota.list > /tmp/quota.list2
-			mv /tmp/quota.list2 /tmp/quota.list
-			shift 2
-			continue
-			;;
-		-i|--include)
-			find $TARGET_PATH -mindepth 1 -maxdepth 1 -type d | grep "$2" >> /tmp/quota.list
-			shift 2
-			continue
-			;;
-		-n|--dry-run)
-			dry_run=1
-			shift
-			continue
-			;;
-		--)
-			shift
-			break
-			;;
-		*)
-			usage
-			exit 1
-			;;
+	-h | --help)
+		usage
+		shift
+		exit 0
+		;;
+	-s | --soft-quota)
+		soft="$2"
+		shift 2
+		continue
+		;;
+	-q | --hard-quota)
+		hard="$2"
+		shift 2
+		continue
+		;;
+	-e | --exclude)
+		grep -v "$2" /tmp/quota.list >/tmp/quota.list2
+		mv /tmp/quota.list2 /tmp/quota.list
+		shift 2
+		continue
+		;;
+	-i | --include)
+		find $TARGET_PATH -mindepth 1 -maxdepth 1 -type d | grep "$2" >>/tmp/quota.list
+		shift 2
+		continue
+		;;
+	-n | --dry-run)
+		dry_run=1
+		shift
+		continue
+		;;
+	--)
+		shift
+		break
+		;;
+	*)
+		usage
+		exit 1
+		;;
 	esac
 done
 
@@ -99,26 +98,35 @@ test -f /etc/projects || touch /etc/projects
 test -f /etc/projid || touch /etc/projid
 
 # all specified directories
-sort /tmp/quota.list | uniq > /tmp/quota.list2
+sort /tmp/quota.list | uniq >/tmp/quota.list2
 mv /tmp/quota.list2 /tmp/quota.list
 
 # directories with quota
-cut -d: -f1 /etc/projid 2>/dev/null | sort | uniq > /tmp/quota.xfs.list
+cut -d: -f1 /etc/projid 2>/dev/null | sort | uniq >/tmp/quota.xfs.list
 
 lastid=0
-lastid="$( (maxid=0; IFS=:; while read -r dir id; do if test "$id" -gt "$maxid"; then maxid="$id"; fi; done; echo "$maxid") < /etc/projid )"
-cat <<EOF > /tmp/quota-cmd.sh
+lastid="$( (
+	maxid=0
+	IFS=:
+	while read -r dir id; do
+		if test "$id" -gt "$maxid"; then
+			maxid="$id"
+		fi
+	done
+	echo "$maxid"
+) </etc/projid)"
+cat <<EOF >/tmp/quota-cmd.sh
 #! /bin/sh -e
 cp -p /etc/projects /etc/projects.new
 cp -p /etc/projid /etc/projid.new
 
 EOF
 diff /tmp/quota.list /tmp/quota.xfs.list | grep '^< ' | while read -r _ dir; do
-	lastid=$((lastid+1))
+	lastid=$((lastid + 1))
 	id="$lastid"
-	cat <<EOF >> /tmp/quota-cmd.sh
-echo "$id:$dir" >> /etc/projects.new
-echo "$dir:$id" >> /etc/projid.new
+	cat <<EOF >>/tmp/quota-cmd.sh
+echo "$id:$dir" >>/etc/projects.new
+echo "$dir:$id" >>/etc/projid.new
 xfs_quota -x -D /etc/projects.new -P /etc/projid.new -c "project -s $dir" $DISK_MOUNTPOINT >/dev/null
 xfs_quota -x  -D /etc/projects.new -P /etc/projid.new -c "limit -p bsoft=$soft bhard=$hard $dir" $DISK_MOUNTPOINT
 
diff --git a/production1/deploy.sh b/production1/deploy.sh
index b1d9f7e..51af556 100755
--- a/production1/deploy.sh
+++ b/production1/deploy.sh
@@ -9,7 +9,7 @@ cd -
 cp -pv terraform/inventory.yaml inventory/1-psnc.yaml
 
 # dynamic DNS
-ip="$(head -n 1 < terraform/fip.txt)"
+ip="$(head -n 1 <terraform/fip.txt)"
 # shellstate=$(shopt -po xtrace)
 # set +o xtrace
 # # https://nsupdate.fedcloud.eu
@@ -24,11 +24,11 @@ echo "Terraform finished. Check terraform/docker-volume.sh. Continue? (CTRL-C to
 read -r _
 
 # wait for ping and ssh
-for ip in $(cat terraform/fip.txt); do
+while read -r ip; do
 	while ! ping -c 1 "$ip"; do sleep 5; done
 	ssh-keygen -R "$ip"
 	while ! ssh egi@"$ip" -o ConnectTimeout=10 -o PreferredAuthentications=publickey -o StrictHostKeyChecking=no :; do sleep 10; done
-done
+done <terraform/fip.txt
 
 # check ssh access
 ansible -m command -a 'uname -a' allnodes
diff --git a/production2/deploy.sh b/production2/deploy.sh
index 0ec26d4..24d38db 100755
--- a/production2/deploy.sh
+++ b/production2/deploy.sh
@@ -9,7 +9,7 @@ cd -
 cp -pv terraform/inventory.yaml inventory/1-safespring.yaml
 
 # dynamic DNS
-ip="$(head -n 1 < terraform/fip.txt)"
+ip="$(head -n 1 <terraform/fip.txt)"
 # shellstate=$(shopt -po xtrace)
 # set +o xtrace
 # # https://nsupdate.fedcloud.eu
@@ -24,11 +24,11 @@ echo "Terraform finished. Check terraform/docker-volume.sh. Continue? (CTRL-C to
 read -r _
 
 # wait for ping and ssh
-for ip in $(cat terraform/hosts.txt); do
+while read -r ip; do
 	while ! ping -c 1 "$ip"; do sleep 5; done
 	ssh-keygen -R "$ip"
 	while ! ssh egi@"$ip" -o ConnectTimeout=10 -o PreferredAuthentications=publickey -o StrictHostKeyChecking=no :; do sleep 10; done
-done
+done <terraform/hosts.txt
 
 # check ssh access
 ansible -m command -a 'uname -a' allnodes
diff --git a/staging1/deploy.sh b/staging1/deploy.sh
index d87a2ea..239854c 100755
--- a/staging1/deploy.sh
+++ b/staging1/deploy.sh
@@ -9,7 +9,7 @@ cd -
 cp -pv terraform/inventory.yaml inventory/1-psnc.yaml
 
 # dynamic DNS
-ip="$(head -n 1 < terraform/fip.txt)"
+ip="$(head -n 1 <terraform/fip.txt)"
 # shellstate=$(shopt -po xtrace)
 # set +o xtrace
 # # https://nsupdate.fedcloud.eu
@@ -24,11 +24,11 @@ echo "Terraform finished. Check terraform/docker-volume.sh. Continue? (CTRL-C to
 read -r _
 
 # wait for ping and ssh
-for ip in $(cat terraform/fip.txt); do
+while read -r ip; do
 	while ! ping -c 1 "$ip"; do sleep 5; done
 	ssh-keygen -R "$ip"
 	while ! ssh egi@"$ip" -o ConnectTimeout=10 -o PreferredAuthentications=publickey -o StrictHostKeyChecking=no :; do sleep 10; done
-done
+done <terraform/fip.txt
 
 # check ssh access
 ansible -m command -a 'uname -a' allnodes
diff --git a/staging2/deploy.sh b/staging2/deploy.sh
index 64c1ce2..26dea73 100755
--- a/staging2/deploy.sh
+++ b/staging2/deploy.sh
@@ -9,7 +9,7 @@ cd -
 cp -pv terraform/inventory.yaml inventory/1-safespring.yaml
 
 # dynamic DNS
-ip="$(head -n 1 < terraform/fip.txt)"
+ip="$(head -n 1 <terraform/fip.txt)"
 # shellstate=$(shopt -po xtrace)
 # set +o xtrace
 # # https://nsupdate.fedcloud.eu
@@ -24,11 +24,11 @@ echo "Terraform finished. Check terraform/docker-volume.sh. Continue? (CTRL-C to
 read -r _
 
 # wait for ping and ssh
-for ip in $(cat terraform/hosts.txt); do
+while read -r ip; do
 	while ! ping -c 1 "$ip"; do sleep 5; done
 	ssh-keygen -R "$ip"
 	while ! ssh egi@"$ip" -o ConnectTimeout=10 -o PreferredAuthentications=publickey -o StrictHostKeyChecking=no :; do sleep 10; done
-done
+done <terraform/hosts.txt
 
 # check ssh access
 ansible -m command -a 'uname -a' allnodes
diff --git a/testing/deploy.sh b/testing/deploy.sh
index c9e7a1e..771a701 100755
--- a/testing/deploy.sh
+++ b/testing/deploy.sh
@@ -9,7 +9,7 @@ cd -
 cp -pv terraform/inventory.yaml inventory/1-cesnet.yaml
 
 # dynamic DNS
-ip="$(head -n 1 < terraform/fip.txt)"
+ip="$(head -n 1 <terraform/fip.txt)"
 shellstate=$(shopt -po xtrace)
 set +o xtrace
 # https://nsupdate.fedcloud.eu
@@ -24,7 +24,7 @@ echo "Terraform finished. Check terraform/docker-volume.sh. Continue? (CTRL-C to
 read -r _
 
 # wait for ping and ssh
-for ip in $(cat terraform/hosts.txt) $(cat terraform/fip.txt); do
+cat terraform/hosts.txt terraform/fip.txt | while read -r ip; do
 	while ! ping -c 1 "$ip"; do sleep 5; done
 	ssh-keygen -R "$ip"
 	while ! ssh egi@"$ip" -o ConnectTimeout=10 -o PreferredAuthentications=publickey -o StrictHostKeyChecking=no :; do sleep 10; done
-- 
GitLab