From b2e2954852c158083d89a6a1a4cc9db6d59a6adc Mon Sep 17 00:00:00 2001 From: Michael Prokop Date: Thu, 24 Apr 2025 11:14:40 +0200 Subject: [PATCH] MT#62436 Fix shellcheck issues + parse IP information programmatically As reported when sending new deployment-iso reviews, triggered by newer docker image / shellcheck: | not ok 1 source/templates/scripts/includes/deployment.sh:1543:10: warning: Quote to prevent word splitting/globbing, or split robustly with mapfile or read -a. [SC2206] | not ok 2 source/templates/scripts/includes/deployment.sh:1903:22: warning: Prefer mapfile or read -a to split command output (or quote to avoid splitting). [SC2207] | not ok 3 source/templates/scripts/includes/deployment.sh:2275:20: warning: Prefer mapfile or read -a to split command output (or quote to avoid splitting). [SC2207] | not ok 4 source/templates/scripts/includes/deployment.sh:2486:12: note: Not following: ./etc/profile.d/puppet-agent.sh was not specified as input (see shellcheck -x). [SC1091] Let's take this as a chance to properly parse ip(8) output via its JSON output, instead of awk/sed magic. Change-Id: I723959626fb514ab9e57202b0e5f415b411f5a01 --- grml_build/package_config/SIPWISE | 1 + templates/scripts/includes/deployment.sh | 20 +++++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/grml_build/package_config/SIPWISE b/grml_build/package_config/SIPWISE index 60d866c..cfed7bb 100644 --- a/grml_build/package_config/SIPWISE +++ b/grml_build/package_config/SIPWISE @@ -34,6 +34,7 @@ iputils-ping isc-dhcp-client isomd5sum iw +jq ldnsutils libnss-myhostname linux-image-amd64 diff --git a/templates/scripts/includes/deployment.sh b/templates/scripts/includes/deployment.sh index 5f3ba77..3b9ce7e 100755 --- a/templates/scripts/includes/deployment.sh +++ b/templates/scripts/includes/deployment.sh @@ -1540,7 +1540,7 @@ set_deploy_status "checkBootParam" declare -a PARAMS=() CMD_LINE=$(cat /proc/cmdline) -PARAMS+=(${CMD_LINE}) +IFS=" " read -r -a PARAMS <<< "${CMD_LINE}" PARAMS+=("$@") for param in "${PARAMS[@]}" ; do @@ -1892,17 +1892,18 @@ if [[ -n "${IP_LINE}" ]]; then fi fi -# Get current IP +# Get current IP information, programmatically with jq(1) +ensure_packages_installed 'jq' ## try ipv4 -INSTALL_DEV=$(ip -4 r | awk '/default/ {print $5; exit}') +INSTALL_DEV=$(ip -4 -j route show | jq -r '.[] | select(.dst == "default") | .dev') if [[ -z "${INSTALL_DEV}" ]]; then ## try ipv6 - INSTALL_DEV=$(ip -6 r | awk '/default/ {print $3; exit}') - INSTALL_IP=$(ip -6 addr show "${INSTALL_DEV}" | sed -rn 's/^[ ]+inet6 ([a-fA-F0-9:]+)\/.*$/\1/p') + INSTALL_DEV=$(ip -6 -j route show | jq -r '.[] | select(.dst == "default") | .gateway') + INSTALL_IP=$(ip -6 -j addr show dev "${INSTALL_DEV}" | jq -r '.[0].addr_info[0].local') else - external_ip_data=( $( ip -4 addr show "${INSTALL_DEV}" | sed -rn 's/^[ ]+inet ([0-9]+(\.[0-9]+){3})\/([0-9]+).*$/\1 \3/p' ) ) - INSTALL_IP="${external_ip_data[0]}" - current_netmask="$( cdr2mask "${external_ip_data[1]}" )" + INSTALL_IP=$(ip -4 -j addr show dev "${INSTALL_DEV}" | jq -r '.[0].addr_info[0].local') + install_ip_netmask=$(ip -4 -j addr show "${INSTALL_DEV}" | jq -r '.[0].addr_info[0].prefixlen') + current_netmask="$( cdr2mask "${install_ip_netmask}" )" EXTERNAL_NETMASK="${EXTERNAL_NETMASK:-${current_netmask}}" unset external_ip_data current_netmask GW="$(ip route show dev "${INSTALL_DEV}" | awk '/^default via/ {print $3; exit}')" @@ -2272,7 +2273,7 @@ grml-chroot "${TARGET}" apt-get --purge -y autoremove # it's necessary to use chroot instead of grml-chroot in variable=$() calls # as grml-chroot also prints "Writing /etc/debian_chroot ..." line # which breaks output -removed_packages=( $(chroot "${TARGET}" dpkg --list | awk '/^rc/ {print $2}') ) +mapfile -t removed_packages < <(chroot "${TARGET}" dpkg --list | awk '/^rc/ {print $2}') if [ ${#removed_packages[@]} -ne 0 ]; then grml-chroot "${TARGET}" dpkg --purge "${removed_packages[@]}" fi @@ -2483,6 +2484,7 @@ EOF if [ -f "${TARGET}/etc/profile.d/puppet-agent.sh" ] ; then echo "Exporting Puppet 4 new PATH (otherwise /opt/puppetlabs/bin/puppet is not found)" + # shellcheck disable=SC1091 source "${TARGET}/etc/profile.d/puppet-agent.sh" fi