Remove use of 'which'.
Instead, either use the bash built-in of type to ensure it exists. Since which is an external dep, things can fail oddly in a constrained environment. Also add a dib-lint test for this. Change-Id: I645029f5b5bfe1198c89ce10fd3246be8636e8af Signed-off-by: Jesse Keating <omgjlk@us.ibm.com>
This commit is contained in:
		
							
								
								
									
										17
									
								
								bin/dib-lint
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								bin/dib-lint
									
									
									
									
									
								
							| @@ -209,6 +209,23 @@ for i in $(find $ELEMENTS_DIR -type f \ | ||||
|         fi | ||||
|     fi | ||||
|  | ||||
|     # check that which calls are not used. It is not built in and is missing | ||||
|     # from some constrained environments | ||||
|     if ! excluded which; then | ||||
|         while read LINE | ||||
|         do | ||||
|             if [[ $LINE =~ "which " ]]; then | ||||
|                 #  Don't match: | ||||
|                 #  - explicitly ignored | ||||
|                 #  - commented | ||||
|                 if [[ $LINE =~ (dib-lint: which|^#) ]]; then | ||||
|                     continue | ||||
|                 fi | ||||
|                 error "$i : potential use of which\n -- $LINE" | ||||
|             fi | ||||
|         done < $i | ||||
|     fi | ||||
|  | ||||
| done | ||||
|  | ||||
| echo "Checking indents..." | ||||
|   | ||||
| @@ -60,9 +60,9 @@ function install_grub2 { | ||||
|  | ||||
|     # XXX: grub-probe on the nbd0/loop0 device returns nothing - workaround, manually | ||||
|     # specify modules. https://bugs.launchpad.net/ubuntu/+source/grub2/+bug/1073731 | ||||
|     GRUBNAME=$(which grub-install) || echo "trying grub2-install" | ||||
|     GRUBNAME=$(type -p grub-install) || echo "trying grub2-install" | ||||
|     if [ -z "$GRUBNAME" ]; then | ||||
|         GRUBNAME=$(which grub2-install) | ||||
|         GRUBNAME=$(type -p grub2-install) | ||||
|     fi | ||||
|  | ||||
|     # If no GRUB2 is found, fallback to extlinux | ||||
| @@ -136,7 +136,7 @@ function install_grub2 { | ||||
|     echo 'GRUB_GFXPAYLOAD_LINUX=text' >>/etc/default/grub | ||||
|     echo 'GRUB_CMDLINE_LINUX_DEFAULT="console=tty0 console=ttyS0,115200 no_timer_check"' >>/etc/default/grub | ||||
|     echo 'GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"' >>/etc/default/grub | ||||
|     if which grub2-mkconfig >/dev/null; then | ||||
|     if type grub2-mkconfig >/dev/null; then | ||||
|         GRUB_MKCONFIG="grub2-mkconfig -o $GRUB_CFG" | ||||
|     else | ||||
|         GRUB_MKCONFIG="grub-mkconfig -o $GRUB_CFG" | ||||
|   | ||||
| @@ -46,7 +46,7 @@ mv "$TMP_MOUNT_PATH/init" "$MODULE_PATH/80deploy-ramdisk/init.sh" | ||||
| # been released for all of our supported platforms we can remove this.  Until then | ||||
| # this makes --include work correctly and will be a noop if we're running a fixed | ||||
| # Dracut version. | ||||
| sed -i 's|cp --reflink=auto --sparse=auto -fa -t "$s" "$i"$|cp --reflink=auto --sparse=auto -fa -t "${initdir}/${tgt}" "$i"|g' $(which dracut) | ||||
| sed -i 's|cp --reflink=auto --sparse=auto -fa -t "$s" "$i"$|cp --reflink=auto --sparse=auto -fa -t "${initdir}/${tgt}" "$i"|g' $(type -p dracut) | ||||
|  | ||||
| # Notes on the options passed to Dracut: | ||||
| # -N: Do not build a host-specific ramdisk.  We want to be able to run this ramdisk | ||||
|   | ||||
| @@ -20,7 +20,7 @@ function pxe_mac() { | ||||
|       # boot interface with no problems. | ||||
|       _mac="${BASH_REMATCH[1]//-/:}" | ||||
|       _mac="${_mac#*:}" | ||||
|   elif [[ -d /sys/firmware/efi ]] && which efibootmgr &>/dev/null; then | ||||
|   elif [[ -d /sys/firmware/efi ]] && type efibootmgr &>/dev/null; then | ||||
|       # Likewise, if we booted via the network while running in UEFI mode, and | ||||
|       # efibootmgr is installed, we can determine the MAC address of the nic we | ||||
|       # booted from.  It would be good to have code that can also do this using | ||||
|   | ||||
| @@ -32,7 +32,7 @@ for _FILE in $(ls ${TARGET_DIR}/binary-deps.d/) ; do | ||||
| done | ||||
|  | ||||
| for _BIN in $BINARY_DEPS ; do | ||||
|     _LOCATION=$(which "$_BIN" || echo "") | ||||
|     _LOCATION=$(type -p "$_BIN" || echo "") | ||||
|     if [ -z "$_LOCATION" ]; then | ||||
|         echo "$_BIN is not found in PATH. Please ensure your elements install it" | ||||
|         exit 1 | ||||
|   | ||||
| @@ -33,7 +33,7 @@ FIRMWARE_DIR=$MODULE_ROOT/lib/firmware | ||||
| LIB_UDEV=$LIB_UDEV_ROOT/lib/udev | ||||
| INIT="$_LIB/scripts/init" | ||||
| FUNCTIONS_D="$_LIB/scripts/d" | ||||
| BUSYBOX=${BUSYBOX:-$(which busybox)} | ||||
| BUSYBOX=${BUSYBOX:-$(type -p busybox)} | ||||
| # NOTE(bnemec): IMAGE_ELEMENT is normally set in disk-image-create, but we're | ||||
| # not using that to build the image here. | ||||
| IMAGE_ELEMENT= | ||||
|   | ||||
| @@ -6,7 +6,7 @@ fi | ||||
| set -eu | ||||
| set -o pipefail | ||||
|  | ||||
| SETFILES=$(which setfiles || true) | ||||
| SETFILES=$(type -p setfiles || true) | ||||
| if [ -e /etc/selinux/targeted/contexts/files/file_contexts -a -x "${SETFILES}" ]; then | ||||
|     # get all mounpoints in the system | ||||
|     IFS='|' read -ra SPLIT_MOUNTS <<< "$DIB_MOUNTPOINTS" | ||||
|   | ||||
| @@ -208,7 +208,7 @@ fi | ||||
| for X in ${!IMAGE_TYPES[@]}; do | ||||
|     case "${IMAGE_TYPES[$X]}" in | ||||
|         qcow2) | ||||
|             if [ -z "$(which qemu-img)" ]; then | ||||
|             if ! type qemu-img > /dev/null 2>&1; then | ||||
|                 echo "qcow2 output format specified but qemu-img executable not found." | ||||
|                 exit 1 | ||||
|             fi | ||||
| @@ -218,19 +218,19 @@ for X in ${!IMAGE_TYPES[@]}; do | ||||
|             IMAGE_TYPES+=('tar') | ||||
|             ;; | ||||
|         vhd) | ||||
|             if [ -z "$(which vhd-util)" ]; then | ||||
|             if ! type vhd-util > /dev/null 2>&1; then | ||||
|                 echo "vhd output format specified but no vhd-util executable found." | ||||
|                 exit 1 | ||||
|             fi | ||||
|             ;; | ||||
|         squashfs) | ||||
|             if [ -z "$(which mksquashfs)" ]; then | ||||
|             if ! type mksquashfs > /dev/null 2>&1; then | ||||
|                 echo "squashfs output format specified but no mksquashfs executable found." | ||||
|                 exit 1 | ||||
|             fi | ||||
|             ;; | ||||
|         docker) | ||||
|             if [ -z "$(which docker)" ]; then | ||||
|             if ! type docker > /dev/null 2>&1; then | ||||
|                 echo "docker output format specified but no docker executable found." | ||||
|                 exit 1 | ||||
|             fi | ||||
| @@ -244,7 +244,7 @@ done | ||||
|  | ||||
| # NOTE: fstrim is on most all recent systems. It is provided by the util-linux | ||||
| # package. | ||||
| if [[ -z "$(which fstrim)" ]]; then | ||||
| if ! type fstrim > /dev/null 2>&1; then | ||||
|     echo "fstrim utility is not found. This is provided by util-linux package" | ||||
|     echo "Please check your PATH variable is set correctly" | ||||
|     exit 1 | ||||
|   | ||||
| @@ -163,7 +163,7 @@ function populate_lib () { | ||||
|     if busybox_list | grep -v "^ip$" | grep "^$i\$" >/dev/null; then | ||||
|       continue | ||||
|     fi | ||||
|     path=`which $i 2>/dev/null` || path=$i | ||||
|     path=`type -p $i 2>/dev/null` || path=$i | ||||
|     if ! [ -x "$path" ]; then | ||||
|       echo "$i is not found in PATH" 2>&1 | ||||
|       exit 1 | ||||
|   | ||||
| @@ -61,7 +61,7 @@ function build_test_image() { | ||||
|  | ||||
| test_formats="tar tgz squashfs raw qcow2 docker aci" | ||||
| for binary in qemu-img docker mksquashfs; do | ||||
|     if [ -z "$(which $binary)" ]; then | ||||
|     if [ -z "$(type $binary)" ]; then | ||||
|         echo "Warning: No $binary binary found, cowardly refusing to run tests." | ||||
|         exit 1 | ||||
|     fi | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Jesse Keating
					Jesse Keating