Replace usage of which with command

`which` is not a default package for CentOS 9 and Rocky Linux. This
means that we should either take care of installing `which` at very
beginning or use built-in alternative of `command -v`.
Command is part of GNU Bash package and will present on each system
which have bash installed, which is OSA requirements.

`command -v` behaves exactly same way - it provides path as output
and exits with code 1 when binary can't be found. So it's a drop-in
replacement.

Change-Id: I7da7814261d734b96644624870ed7cc2f1173bff
This commit is contained in:
Dmitriy Rabotyagov 2022-09-28 13:21:51 +02:00 committed by Dmitriy Rabotyagov
parent f2cc3bbfce
commit b5ea66fcda
5 changed files with 20 additions and 20 deletions

View File

@ -62,7 +62,7 @@ case ${ID,,} in
esac
# Install git so that we can clone the tests repo if git is not available
which git &>/dev/null || eval sudo "${pkg_mgr_cmd}" git
command -v git &>/dev/null || eval sudo "${pkg_mgr_cmd}" git
# Clone the tests repo for access to the common test script
if [[ ! -d "${COMMON_TESTS_PATH}" ]]; then

View File

@ -70,12 +70,12 @@ case ${DISTRO_ID} in
case ${DISTRO_VERSION_ID} in
8)
dnf -y install python38 python38-devel libselinux-python3
PYTHON_EXEC_PATH="$(which python3.8)"
PYTHON_EXEC_PATH="$(command -v python3.8)"
OSA_ANSIBLE_PYTHON_INTERPRETER="/usr/bin/python3"
;;
9|9.[0-9]*)
dnf -y install python3 python3-devel python3-libselinux
PYTHON_EXEC_PATH="$(which python3)"
PYTHON_EXEC_PATH="$(command -v python3)"
OSA_ANSIBLE_PYTHON_INTERPRETER="/usr/bin/python3"
;;
esac
@ -84,11 +84,11 @@ case ${DISTRO_ID} in
case ${DISTRO_VERSION_ID} in
8)
dnf -y install python38 python38-devel libselinux-python3
PYTHON_EXEC_PATH="$(which python3.8)"
PYTHON_EXEC_PATH="$(command -v python3.8)"
;;
9)
dnf -y install python3 python3-devel libselinux-python3
PYTHON_EXEC_PATH="$(which python3)"
PYTHON_EXEC_PATH="$(command -v python3)"
;;
esac
;;

View File

@ -92,7 +92,7 @@ if [[ -z "${SKIP_OSA_BOOTSTRAP_AIO+defined}" ]]; then
fi
# Flush all the iptables rules set by openstack-infra
if which iptables; then
if command -v iptables; then
iptables -F
iptables -X
iptables -t nat -F

View File

@ -84,12 +84,12 @@ function repo_information {
[[ "${1}" != "host" ]] && lxc_cmd="lxc-attach --name ${1} --" || lxc_cmd=""
echo "Collecting list of installed packages and enabled repositories for \"${1}\""
# Redhat package debugging
if eval sudo ${lxc_cmd} which dnf &>/dev/null; then
if eval sudo ${lxc_cmd} command -v dnf &>/dev/null; then
eval sudo ${lxc_cmd} dnf repolist -v > "${WORKING_DIR}/logs/redhat-rpm-repolist-${1}-${TS}.txt" || true
eval sudo ${lxc_cmd} dnf list installed > "${WORKING_DIR}/logs/redhat-rpm-list-installed-${1}-${TS}.txt" || true
# Ubuntu package debugging
elif eval sudo ${lxc_cmd} which apt-get &> /dev/null; then
elif eval sudo ${lxc_cmd} command -v apt-get &> /dev/null; then
eval sudo ${lxc_cmd} apt-cache policy | grep http | awk '{print $1" "$2" "$3}' | sort -u > "${WORKING_DIR}/logs/ubuntu-apt-repolist-${1}-${TS}.txt" || true
eval sudo ${lxc_cmd} apt list --installed > "${WORKING_DIR}/logs/ubuntu-apt-list-installed-${1}-${TS}.txt" || true
fi
@ -171,7 +171,7 @@ done
# Gather container etc artifacts
if which lxc-ls &> /dev/null; then
if command -v lxc-ls &> /dev/null; then
for CONTAINER_NAME in $(sudo lxc-ls -1); do
CONTAINER_PID=$(sudo lxc-info -p -n ${CONTAINER_NAME} | awk '{print $2}')
ETC_DIR="/proc/${CONTAINER_PID}/root/etc"
@ -213,7 +213,7 @@ env > "${WORKING_DIR}/logs/environment-${TS}.txt" || true
repo_information host
# Record the active interface configs
if which ethtool &> /dev/null; then
if command -v ethtool &> /dev/null; then
for interface in $(ip -o link | awk -F':' '{print $2}' | sed 's/@.*//g'); do
echo "ethtool -k ${interface}"
ethtool -k ${interface} > "${WORKING_DIR}/logs/ethtool-${interface}-${TS}-cfg.txt" || true

View File

@ -281,27 +281,27 @@ function get_repos_info {
function get_instance_info {
TS="$(date +"%H-%M-%S")"
(cat /etc/resolv.conf && \
which systemd-resolve &> /dev/null && \
command -v systemd-resolve &> /dev/null && \
systemd-resolve --statistics && \
cat /etc/systemd/resolved.conf) > \
"/openstack/log/instance-info/host_dns_info_${TS}.log" || true
if [ "$(which tracepath)" ]; then
if [ "$(command -v tracepath)" ]; then
{ tracepath "8.8.8.8" -m 5 2>/dev/null || tracepath "8.8.8.8"; } > \
"/openstack/log/instance-info/host_tracepath_info_${TS}.log" || true
fi
if [ "$(which tracepath6)" ]; then
if [ "$(command -v tracepath6)" ]; then
{ tracepath6 "2001:4860:4860::8888" -m 5 2>/dev/null || tracepath6 "2001:4860:4860::8888"; } >> \
"/openstack/log/instance-info/host_tracepath_info_${TS}.log" || true
fi
if [ "$(which lxc-ls)" ]; then
if [ "$(command -v lxc-ls)" ]; then
lxc-ls --fancy > \
"/openstack/log/instance-info/host_lxc_container_info_${TS}.log" || true
fi
if [ "$(which lxc-checkconfig)" ]; then
if [ "$(command -v lxc-checkconfig)" ]; then
lxc-checkconfig > \
"/openstack/log/instance-info/host_lxc_config_info_${TS}.log" || true
fi
if [ "$(which networkctl)" ]; then
if [ "$(command -v networkctl)" ]; then
networkctl list > \
"/openstack/log/instance-info/host_networkd_list_${TS}.log" || true
networkctl status >> \
@ -309,11 +309,11 @@ function get_instance_info {
networkctl lldp >> \
"/openstack/log/instance-info/host_networkd_lldp_${TS}.log" || true
fi
if [ "$(which iptables)" ]; then
if [ "$(command -v iptables)" ]; then
(iptables -vnL && iptables -t nat -vnL && iptables -t mangle -vnL) > \
"/openstack/log/instance-info/host_firewall_info_${TS}.log" || true
fi
if [ "$(which ansible)" ]; then
if [ "$(command -v ansible)" ]; then
ANSIBLE_HOST_KEY_CHECKING=False \
ansible -i "localhost," localhost -m setup > \
"/openstack/log/instance-info/host_system_info_${TS}.log" || true
@ -338,7 +338,7 @@ function get_instance_info {
# Storage reports
for dir_name in lxc machines; do
if [ "$(which btrfs)" ]; then
if [ "$(command -v btrfs)" ]; then
btrfs filesystem usage /var/lib/${dir_name} 2>/dev/null > \
"/openstack/log/instance-info/btrfs_${dir_name}_usage_${TS}.log" || true
btrfs filesystem show /var/lib/${dir_name} 2>/dev/null > \
@ -350,7 +350,7 @@ function get_instance_info {
fi
done
if [ "$(which zfs)" ]; then
if [ "$(command -v zfs)" ]; then
zfs list > "/openstack/log/instance-info/zfs_lxc_${TS}.log" || true
fi