Merge pull request #1159 from BastilleBSD/exit-code

This commit is contained in:
tschettervictor
2025-06-22 14:40:36 -06:00
committed by GitHub
5 changed files with 68 additions and 45 deletions

View File

@@ -84,8 +84,10 @@ bastille_root_check
TARGET="${1}"
shift 1
COUNT=0
RETURN=0
# Use mktemp to store exit codes
export TMP_BASTILLE_EXIT_CODE="$(mktemp)"
echo 0 > "${TMP_BASTILLE_EXIT_CODE}"
set_target "${TARGET}"
@@ -104,8 +106,6 @@ for _jail in ${JAILS}; do
info "\n[${_jail}]:"
COUNT=$(($COUNT+1))
# Allow executing commands on linux jails
if grep -qw "linsysfs" "${bastille_jailsdir}/${_jail}/fstab"; then
jexec -l -u root "${_jail}" "$@"
@@ -113,17 +113,7 @@ for _jail in ${JAILS}; do
jexec -l -U root "${_jail}" "$@"
fi
ERROR_CODE=$?
if [ "${ERROR_CODE}" -ne 0 ]; then
warn "[${_jail}]: ${ERROR_CODE}"
fi
if [ "$COUNT" -eq 1 ]; then
RETURN=${ERROR_CODE}
else
RETURN=$(($RETURN+$ERROR_CODE))
fi
bastille_check_exit_code "${_jail}" "$?"
) &
@@ -132,8 +122,4 @@ for _jail in ${JAILS}; do
done
wait
# Check when a command is executed in all running jails. (bastille cmd ALL ...)
if [ "${COUNT}" -gt 1 ] && [ "${RETURN}" -gt 0 ]; then
RETURN=1
return "${RETURN}"
fi
bastille_return_exit_code

View File

@@ -93,6 +93,37 @@ warn() {
echo -e "${COLOR_YELLOW}$*${COLOR_RESET}"
}
# This function checks and adds any error code
# that is not "0" to the tmp file
bastille_check_exit_code() {
local jail="${1}"
local exit_code="${2}"
# Set exit code variable
if [ -z "${TMP_BASTILLE_EXIT_CODE}" ]; then
error_exit "[ERROR]: Exit code status not set."
else
local old_exit_code="$(cat ${TMP_BASTILLE_EXIT_CODE})"
fi
if [ "${exit_code}" -ne 0 ]; then
local new_exit_code="$(( ${old_exit_code} + ${exit_code} ))"
echo "${new_exit_code}" > "${TMP_BASTILLE_EXIT_CODE}"
error_notify "[ERROR CODE]: ${exit_code}"
fi
}
# This needs to be the last function called
# if used on any command
bastille_return_exit_code() {
local exit_code="$(cat ${TMP_BASTILLE_EXIT_CODE})"
rm -f ${TMP_BASTILLE_EXIT_CODE}
return "${exit_code}"
}
# Parallel mode, don't exceed process limit
bastille_running_jobs() {

View File

@@ -75,7 +75,7 @@ while [ "$#" -gt 0 ]; do
-*)
for _opt in $(echo ${1} | sed 's/-//g' | fold -w1); do
case ${_opt} in
a) AUTO=1 ;;
a) AUTO=1 ;;
H) USE_HOST_PKG=1 ;;
y) AUTO_YES=1 ;;
x) enable_debug ;;
@@ -96,6 +96,9 @@ fi
TARGET="${1}"
shift
# Use mktemp to store exit codes
export TMP_BASTILLE_EXIT_CODE="$(mktemp)"
echo 0 > "${TMP_BASTILLE_EXIT_CODE}"
bastille_root_check
set_target "${TARGET}"
@@ -116,35 +119,25 @@ pkg_run_command() {
bastille_jail_path="${bastille_jailsdir}/${_jail}/root"
if [ -f "/usr/sbin/mport" ]; then
if ! jexec -l -U root "${_jail}" /usr/sbin/mport "$@"; then
errors=1
fi
jexec -l -U root "${_jail}" /usr/sbin/mport "$@"
elif [ -f "${bastille_jail_path}/usr/bin/apt" ]; then
if ! jexec -l "${_jail}" /usr/bin/apt "$@"; then
errors=1
fi
jexec -l "${_jail}" /usr/bin/apt "$@"
elif [ "${USE_HOST_PKG}" -eq 1 ]; then
if [ "${AUTO_YES}" -eq 1 ]; then
_jail_cmd="env ASSUME_ALWAYS_YES=yes /usr/sbin/pkg -j ${_jail} $@"
env ASSUME_ALWAYS_YES=yes /usr/sbin/pkg -j ${_jail} "$@"
else
_jail_cmd="/usr/sbin/pkg -j ${_jail} $@"
fi
if ! ${_jail_cmd}; then
errors=1
/usr/sbin/pkg -j ${_jail} "$@"
fi
else
if [ "${AUTO_YES}" -eq 1 ]; then
_jail_cmd="jexec -l -U root ${_jail} env ASSUME_ALWAYS_YES=yes /usr/sbin/pkg $@"
jexec -l -U root ${_jail} env ASSUME_ALWAYS_YES=yes /usr/sbin/pkg "$@"
else
_jail_cmd="jexec -l -U root ${_jail} /usr/sbin/pkg $@"
fi
if ! ${_jail_cmd}; then
errors=1
jexec -l -U root ${_jail} /usr/sbin/pkg "$@"
fi
fi
}
errors=0
bastille_check_exit_code "${_jail}" "$?"
}
for _jail in ${JAILS}; do
@@ -170,9 +163,6 @@ for _jail in ${JAILS}; do
done
wait
echo
if [ $errors -ne 0 ]; then
error_exit "[ERROR]: Failed to apply on some jails, please check logs"
else
echo
fi
bastille_return_exit_code

View File

@@ -82,6 +82,9 @@ fi
TARGET="${1}"
shift
# Use mktemp to store exit codes
export TMP_BASTILLE_EXIT_CODE="$(mktemp)"
echo 0 > "${TMP_BASTILLE_EXIT_CODE}"
bastille_root_check
set_target "${TARGET}"
@@ -102,6 +105,8 @@ for _jail in ${JAILS}; do
info "\n[${_jail}]:"
jexec -l "${_jail}" /usr/sbin/service "$@"
bastille_check_exit_code "${_jail}" "$?"
) &
@@ -109,3 +114,6 @@ for _jail in ${JAILS}; do
done
wait
echo
bastille_return_exit_code

View File

@@ -82,6 +82,9 @@ fi
TARGET="${1}"
shift
# Use mktemp to store exit codes
export TMP_BASTILLE_EXIT_CODE="$(mktemp)"
echo 0 > "${TMP_BASTILLE_EXIT_CODE}"
bastille_root_check
set_target "${TARGET}"
@@ -103,9 +106,14 @@ for _jail in ${JAILS}; do
jexec -l "${_jail}" /usr/sbin/sysrc "$@"
bastille_check_exit_code "${_jail}" "$?"
) &
bastille_running_jobs "${bastille_process_limit}"
done
wait
wait
echo
bastille_return_exit_code