Implement parallel mode

This PR allows to run commands on multiple jails in parallel. Simply use '-p 3' to set a process limit.

'bastille -p 3 start all' will start all jails but run 3 processes at a time.

This is a very rough implementation, and could be more streamlined, but here it is to test.
This commit is contained in:
tschettervictor
2025-04-06 15:52:14 -06:00
committed by GitHub
parent 904f8557c2
commit c0574c28d0

View File

@@ -32,7 +32,7 @@
PATH=${PATH}:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin PATH=${PATH}:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
BASTILLE_VERSION="0.13.20250126" BASTILLE_VERSION=904f855
## check for config existence ## check for config existence
bastille_conf_check() { bastille_conf_check() {
@@ -119,6 +119,7 @@ Available Commands:
Use "bastille -v|--version" for version information. Use "bastille -v|--version" for version information.
Use "bastille command -h|--help" for more information about a command. Use "bastille command -h|--help" for more information about a command.
Use "bastille [-c|--config FILE] command" to specify a non-default config file. Use "bastille [-c|--config FILE] command" to specify a non-default config file.
Use "bastille [-p|--parallel VALUE] command" to run bastille in parallel mode.
EOF EOF
exit 1 exit 1
@@ -144,6 +145,7 @@ fi
. /usr/local/share/bastille/common.sh . /usr/local/share/bastille/common.sh
# Handle options # Handle options
PARALLEL_MODE=0
while [ "$#" -gt 0 ]; do while [ "$#" -gt 0 ]; do
case "${1}" in case "${1}" in
-h|--help|help) -h|--help|help)
@@ -165,17 +167,21 @@ while [ "$#" -gt 0 ]; do
else else
error_exit "Not a valid config file: ${BASTILLE_CONFIG}" error_exit "Not a valid config file: ${BASTILLE_CONFIG}"
fi fi
# Load common.sh after setting BASTILLE_CONFIG
. /usr/local/share/bastille/common.sh
shift 2 shift 2
;; ;;
-p|--parallel)
PARALLEL_MODE=1
PROCESS_LIMIT="${2}"
if ! echo "${PROCESS_LIMIT}" | grep -Eq "^[0-9]+$"; then
error_exit "Not a valid process limit: ${PROCESS_LIMIT}"
else
shift 2
fi
;;
-*) -*)
for _opt in $(echo ${1} | sed 's/-//g' | fold -w1); do error_exit "Unknown Option: \"${1}\""
case ${_opt} in
x) enable_debug ;;
a) AUTO=1 ;;
*) error_exit "Unknown Option: \"${1}\"" ;;
esac
done
shift
;; ;;
*) *)
break break
@@ -234,6 +240,26 @@ case "${CMD}" in
;; ;;
esac esac
# Extract JAILS from command for parallel mode
OPTIONS=""
while [ "$#" -gt 0 ] && [ "${PARALLEL_MODE}" -eq 1 ]; do
case "${1}" in
-*)
OPTIONS="${OPTIONS} ${1}"
shift 1
;;
*)
if ! set_target "${1}" >/dev/null 2>&1; then
OPTIONS="${OPTIONS} ${1}"
shift 1
else
XARGS_JAILS="${JAILS}"
shift 1
break
fi
esac
done
# shellcheck disable=SC2154 # shellcheck disable=SC2154
SCRIPTPATH="${bastille_sharedir}/${CMD}.sh" SCRIPTPATH="${bastille_sharedir}/${CMD}.sh"
if [ -f "${SCRIPTPATH}" ]; then if [ -f "${SCRIPTPATH}" ]; then
@@ -242,11 +268,12 @@ if [ -f "${SCRIPTPATH}" ]; then
: "${SH:=sh}" : "${SH:=sh}"
if [ -n "${PARAMS}" ]; then if [ "${PARALLEL_MODE}" -eq 1 ]; then
exec "${SH}" "${SCRIPTPATH}" "${PARAMS}" echo "${XARGS_JAILS}" | xargs -P "${PROCESS_LIMIT}" -I {} "${SH}" "${SCRIPTPATH}" ${OPTIONS} {} "$@"
else else
exec "${SH}" "${SCRIPTPATH}" "$@" exec "${SH}" "${SCRIPTPATH}" "$@"
fi fi
else else
error_exit "${SCRIPTPATH} not found." error_exit "${SCRIPTPATH} not found."
fi fi