diff --git a/README.md b/README.md index 6391f6f8..dfeba78f 100644 --- a/README.md +++ b/README.md @@ -157,8 +157,6 @@ Available Commands: Use "bastille -v|--version" for version information. Use "bastille command -h|--help" for more information about a command. Use "bastille -c|--config config.conf command" to specify a non-default config file. -Use "bastille -p|--parallel VALUE command" to run bastille in parallel mode. - ``` ## 1.0.x diff --git a/docs/chapters/targeting.rst b/docs/chapters/targeting.rst index cab9c518..2d5bfe1b 100644 --- a/docs/chapters/targeting.rst +++ b/docs/chapters/targeting.rst @@ -43,19 +43,6 @@ This value can be changed using ``bastille config TARGET set priority VALUE``. This value will be shown using ``bastille list all``. -Parallel Mode -------------- - -Any command that supports multiple targets, also supports parallel mode. This -means that Bastille will run the command on multiple jails at a single time, -depending on the value given. - -To use parallel mode, run ``bastille -p 4 pkg ALL update``, for example, to start -updating packages in all jails, 4 processes at a time. - -Note that the ``-p`` option should follow the main ``bastille`` command, and not -the sub-command. - Examples: Containers -------------------- diff --git a/docs/chapters/usage.rst b/docs/chapters/usage.rst index b6623a8f..efa46b54 100644 --- a/docs/chapters/usage.rst +++ b/docs/chapters/usage.rst @@ -52,5 +52,4 @@ Usage Use "bastille -v|--version" for version information. Use "bastille command -h|--help" for more information about a command. - Use "bastille -c|--config config.conf command" to specify a non-default config file. - Use "bastille -p|--parallel VALUE command" to run bastille in parallel mode. \ No newline at end of file + Use "bastille -c|--config config.conf command" to specify a non-default config file. \ No newline at end of file diff --git a/usr/local/bin/bastille b/usr/local/bin/bastille index e127226a..20012645 100755 --- a/usr/local/bin/bastille +++ b/usr/local/bin/bastille @@ -119,7 +119,6 @@ Available Commands: Use "bastille -v|--version" for version information. 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 -p|--parallel VALUE command" to run bastille in parallel mode. EOF exit 1 @@ -146,8 +145,6 @@ bastille_perms_check . /usr/local/share/bastille/common.sh # Handle options -bastille_parallel_mode=0 -bastille_process_limit="${bastille_process_limit:-1}" while [ "$#" -gt 0 ]; do case "${1}" in -h|--help|help) @@ -170,15 +167,6 @@ while [ "$#" -gt 0 ]; do . /usr/local/share/bastille/common.sh shift 2 ;; - -p|--parallel) - bastille_parallel_mode=1 - bastille_process_limit="${2}" - if ! echo "${bastille_process_limit}" | grep -Eq "^[0-9]+$"; then - error_exit "Not a valid process limit: ${bastille_process_limit}" - else - shift 2 - fi - ;; -*) error_exit "Unknown Option: \"${1}\"" ;; @@ -188,9 +176,6 @@ while [ "$#" -gt 0 ]; do esac done -# Export parallel and limit -export bastille_process_limit - if [ "$#" -lt 1 ]; then usage else @@ -200,12 +185,14 @@ fi # Handle sub-commands. case "${CMD}" in - # Commands that don't allow parallel mode + # 38 total commands bootstrap| \ clone| \ cmd| \ + config| \ console| \ convert| \ + cp| \ create| \ destroy| \ edit| \ @@ -213,38 +200,30 @@ case "${CMD}" in export| \ htop| \ import| \ + jcp| \ limits| \ list| \ migrate| \ + mount| \ network| \ pkg| \ rcp| \ rdr| \ rename| \ + restart| \ service| \ setup| \ - top| \ - update| \ - upgrade| \ - verify| \ - zfs) - if [ "${bastille_parallel_mode}" -eq 1 ]; then - error_exit "Command does not support parallel mode: ${CMD}" - fi - ;; - # Commands that allow parallel mode - config| \ - cp| \ - jcp| \ - limits| \ - mount| \ - restart| \ start| \ stop| \ sysrc| \ tags| \ template| \ - umount) + top| \ + umount| \ + update| \ + upgrade| \ + verify| \ + zfs) ;; *) usage diff --git a/usr/local/etc/rc.d/bastille b/usr/local/etc/rc.d/bastille index 51abbb3b..77606bf6 100755 --- a/usr/local/etc/rc.d/bastille +++ b/usr/local/etc/rc.d/bastille @@ -29,25 +29,46 @@ rcvar=${name}_enable : ${bastille_conf:="/usr/local/etc/bastille/bastille.conf"} : ${bastille_startup_delay:=0} : ${bastille_parallel_limit:=1} +: ${bastille_jail_list:=ALL} command=/usr/local/bin/${name} start_cmd="bastille_start" stop_cmd="bastille_stop" restart_cmd="bastille_restart" -bastille_start() -{ - ${command} -p ${bastille_parallel_limit} start --boot --delay ${bastille_startup_delay} ALL +list_jails() { + local _jailsdir=$(. $bastille_conf; echo $bastille_jailsdir) + local _jail_list=$(find ${_jailsdir}/* -mindepth 1 -maxdepth 1 -type f -name jail.conf | xargs -n1 dirname | xargs -n1 basename) + for _jail in ${_jail_list}; do + _priority="$(sysrc -f ${_jailsdir}/${_jail}/settings.conf -n priority)" + echo "${_jail} ${_priority}" + done } -bastille_stop() -{ - ${command} -p ${bastille_parallel_limit} stop ALL +sort_jails() { + local _order="${1}" + if [ "${_order}" = "forward" ]; then + bastille_jail_list="$(list_jails | sort -k2 -n | awk '{print $1}')" + elif [ "${_order}" = "reverse" ]; then + bastille_jail_list="$(list_jails | sort -k2 -nr | awk '{print $1}')" + else + echo "[ERROR]: Fatal error, could not get jail list." + fi } -bastille_restart() -{ - ${command} -p ${bastille_parallel_limit} restart --boot --delay ${bastille_startup_delay} ALL +bastille_start() { + sort_jails "forward" + echo "${bastille_jail_list}" | xargs -P ${bastille_parallel_limit} -I JAIL ${command} start --boot --delay ${bastille_startup_delay} JAIL +} + +bastille_stop() { + sort_jails "reverse" + echo "${bastille_jail_list}" | xargs -P ${bastille_parallel_limit} -I JAIL ${command} stop JAIL +} + +bastille_restart() { + sort_jails "forward" + echo "${bastille_jail_list}" | xargs -P ${bastille_parallel_limit} -I JAIL ${command} restart --boot --delay ${bastille_startup_delay} JAIL } load_rc_config ${name} diff --git a/usr/local/share/bastille/cmd.sh b/usr/local/share/bastille/cmd.sh index 10f15fb7..313db834 100644 --- a/usr/local/share/bastille/cmd.sh +++ b/usr/local/share/bastille/cmd.sh @@ -84,10 +84,7 @@ bastille_root_check TARGET="${1}" shift 1 - -# Use mktemp to store exit codes -export TMP_BASTILLE_EXIT_CODE="$(mktemp)" -echo 0 > "${TMP_BASTILLE_EXIT_CODE}" +ERRORS=0 set_target "${TARGET}" @@ -110,9 +107,15 @@ for _jail in ${JAILS}; do else jexec -l -U root "${_jail}" "$@" fi - - bastille_check_exit_code "${_jail}" "$?" - + + if [ "$?" -ne 0 ]; then + ERRORS=$((ERRORS + 1)) + fi + done -bastille_return_exit_code +if [ "${ERRORS}" -ne 0 ]; then + error_exit "[ERROR]: Command failed on ${ERRORS} jails." +fi + +echo \ No newline at end of file diff --git a/usr/local/share/bastille/common.sh b/usr/local/share/bastille/common.sh index bf7692c2..38be75d1 100644 --- a/usr/local/share/bastille/common.sh +++ b/usr/local/share/bastille/common.sh @@ -93,54 +93,6 @@ 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() { - - _process_limit="${1}" - _running_jobs=$((_running_jobs + 1)) - - if [ "${_running_jobs}" -ge "${_process_limit}" ]; then - - # Wait for at least one process to finish - wait 2>/dev/null || wait - - _running_jobs=$((_running_jobs - 1)) - - fi - -} - check_target_exists() { local _TARGET="${1}" local _jaillist="$(bastille list jails)" diff --git a/usr/local/share/bastille/config.sh b/usr/local/share/bastille/config.sh index ccffb3c5..e64bc603 100644 --- a/usr/local/share/bastille/config.sh +++ b/usr/local/share/bastille/config.sh @@ -88,13 +88,12 @@ shift 2 set_target "${TARGET}" case "${ACTION}" in - get) + get|remove) if [ "$#" -ne 1 ]; then - error_notify 'Too many parameters for [get|remove] operation.' - usage + error_exit "[ERROR]: Too many parameters for [get|remove] operation." fi ;; - add|set|remove) + add|set) ;; *) error_exit "[ERROR]: Only (add|set), get and remove are supported." @@ -125,8 +124,6 @@ print_jail_conf() { } for _jail in ${JAILS}; do - - ( # Backwards compatibility for specifying only an IP with ip[4|6].addr if [ "${ACTION}" = "set" ] && [ "${PROPERTY}" = "ip4.addr" ]; then @@ -310,13 +307,8 @@ for _jail in ${JAILS}; do rm "${_tmpfile}" fi fi - - ) & - - bastille_running_jobs "${bastille_process_limit}" done -wait # Only display this message once at the end (not for every jail). -- cwells if { [ "${ACTION}" = "set" ] || [ "${ACTION}" = "remove" ]; } && [ "${BASTILLE_PROPERTY}" -eq 0 ]; then diff --git a/usr/local/share/bastille/console.sh b/usr/local/share/bastille/console.sh index 7d4111d3..a553fa63 100644 --- a/usr/local/share/bastille/console.sh +++ b/usr/local/share/bastille/console.sh @@ -140,7 +140,7 @@ for _jail in ${JAILS}; do else check_fib "${_jail}" LOGIN="$(jexec -l "${_jail}" which login)" - ${_setfib} jexec -l "${_jail}" $LOGIN -f root + ${_setfib} jexec -l "${_jail}" ${LOGIN} -f root fi done diff --git a/usr/local/share/bastille/cp.sh b/usr/local/share/bastille/cp.sh index 10d4760e..26d27956 100644 --- a/usr/local/share/bastille/cp.sh +++ b/usr/local/share/bastille/cp.sh @@ -83,26 +83,25 @@ fi TARGET="${1}" HOST_PATH="${2}" JAIL_PATH="${3}" +ERRORS=0 bastille_root_check set_target "${TARGET}" for _jail in ${JAILS}; do - ( - info "\n[${_jail}]:" host_path="${HOST_PATH}" jail_path="$(echo ${bastille_jailsdir}/${_jail}/root/${JAIL_PATH} | sed 's#//#/#g')" if ! cp "${OPTION}" "${host_path}" "${jail_path}"; then + ERRORS=$((ERRORS + 1)) error_continue "[ERROR]: CP failed: ${host_path} -> ${jail_path}" fi - - ) & - - bastille_running_jobs "${bastille_process_limit}" done -wait + +if [ "${ERRORS}" -ne 0 ]; then + error_exit "[ERROR]: Command failed on ${ERRORS} jails." +fi diff --git a/usr/local/share/bastille/jcp.sh b/usr/local/share/bastille/jcp.sh index 0e2c736a..ec522679 100644 --- a/usr/local/share/bastille/jcp.sh +++ b/usr/local/share/bastille/jcp.sh @@ -84,6 +84,7 @@ SOURCE_TARGET="${1}" SOURCE_PATH="${2}" DEST_TARGET="${3}" DEST_PATH="${4}" +ERRORS=0 bastille_root_check set_target_single "${SOURCE_TARGET}" && SOURCE_TARGET="${TARGET}" @@ -91,8 +92,6 @@ set_target "${DEST_TARGET}" && DEST_TARGET="${JAILS}" for _jail in ${DEST_TARGET}; do - ( - if [ "${_jail}" = "${SOURCE_TARGET}" ]; then continue else @@ -103,14 +102,14 @@ for _jail in ${DEST_TARGET}; do dest_path="$(echo ${bastille_jailsdir}/${_jail}/root/${DEST_PATH} | sed 's#//#/#g')" if ! cp "${OPTION}" "${source_path}" "${dest_path}"; then + ERRORS=$((ERRORS + 1)) error_continue "[ERROR]: JCP failed: ${source_path} -> ${dest_path}" fi fi - - ) & - - bastille_running_jobs "${bastille_process_limit}" done -wait \ No newline at end of file + +if [ "${ERRORS}" -ne 0 ]; then + error_exit "[ERROR]: Command failed on ${ERRORS} jails." +fi \ No newline at end of file diff --git a/usr/local/share/bastille/limits.sh b/usr/local/share/bastille/limits.sh index 472f3ef6..64058311 100644 --- a/usr/local/share/bastille/limits.sh +++ b/usr/local/share/bastille/limits.sh @@ -139,8 +139,6 @@ add_cpuset() { for _jail in ${JAILS}; do - ( - check_target_is_running "${_jail}" || if [ "${AUTO}" -eq 1 ]; then bastille start "${_jail}" else @@ -301,10 +299,5 @@ for _jail in ${JAILS}; do ;; esac - - ) & - bastille_running_jobs "${bastille_process_limit}" - -done -wait +done \ No newline at end of file diff --git a/usr/local/share/bastille/mount.sh b/usr/local/share/bastille/mount.sh index 6b0d6d40..386859f5 100644 --- a/usr/local/share/bastille/mount.sh +++ b/usr/local/share/bastille/mount.sh @@ -144,8 +144,6 @@ fi for _jail in ${JAILS}; do - ( - check_target_is_running "${_jail}" || if [ "${AUTO}" -eq 1 ]; then bastille start "${_jail}" else @@ -202,10 +200,5 @@ for _jail in ${JAILS}; do echo "${_fstab_entry}" >> "${bastille_jailsdir}/${_jail}/fstab" || error_continue "Failed to create fstab entry: ${_fstab_entry}" mount -F "${bastille_jailsdir}/${_jail}/fstab" -a || error_continue "Failed to mount volume: ${_fullpath}" echo "Added: ${_fstab_entry}" - - ) & - - bastille_running_jobs "${bastille_process_limit}" - + done -wait diff --git a/usr/local/share/bastille/pkg.sh b/usr/local/share/bastille/pkg.sh index 677b4f4a..c0a03b09 100644 --- a/usr/local/share/bastille/pkg.sh +++ b/usr/local/share/bastille/pkg.sh @@ -96,14 +96,12 @@ fi TARGET="${1}" shift -# Use mktemp to store exit codes -export TMP_BASTILLE_EXIT_CODE="$(mktemp)" -echo 0 > "${TMP_BASTILLE_EXIT_CODE}" +ERRORS=0 bastille_root_check set_target "${TARGET}" -pkg_run_command() { +for _jail in ${JAILS}; do # Validate jail state check_target_is_running "${_jail}" || if [ "${AUTO}" -eq 1 ]; then @@ -136,12 +134,14 @@ pkg_run_command() { fi fi - bastille_check_exit_code "${_jail}" "$?" -} - -for _jail in ${JAILS}; do - pkg_run_command "$@" + if [ "$?" -ne 0 ]; then + ERRORS=$((ERRORS + 1)) + fi + done -echo -bastille_return_exit_code +if [ "${ERRORS}" -ne 0 ]; then + error_exit "[ERROR]: Command failed on ${ERRORS} jails." +fi + +echo \ No newline at end of file diff --git a/usr/local/share/bastille/restart.sh b/usr/local/share/bastille/restart.sh index dc1aed1b..4703531f 100644 --- a/usr/local/share/bastille/restart.sh +++ b/usr/local/share/bastille/restart.sh @@ -112,17 +112,12 @@ set_target "${TARGET}" for _jail in ${JAILS}; do - ( - # Only restart running jails if check_target_is_running "${_jail}"; then bastille stop ${_stop_options} ${_jail} bastille start ${_start_options} ${_jail} fi - ) & - - bastille_running_jobs "${bastille_process_limit}" - done -wait \ No newline at end of file + +echo \ No newline at end of file diff --git a/usr/local/share/bastille/service.sh b/usr/local/share/bastille/service.sh index b2988066..b909ee33 100644 --- a/usr/local/share/bastille/service.sh +++ b/usr/local/share/bastille/service.sh @@ -82,9 +82,7 @@ fi TARGET="${1}" shift -# Use mktemp to store exit codes -export TMP_BASTILLE_EXIT_CODE="$(mktemp)" -echo 0 > "${TMP_BASTILLE_EXIT_CODE}" +ERRORS=0 bastille_root_check set_target "${TARGET}" @@ -104,9 +102,14 @@ for _jail in ${JAILS}; do jexec -l "${_jail}" /usr/sbin/service "$@" - bastille_check_exit_code "${_jail}" "$?" - -done -echo + if [ "$?" -ne 0 ]; then + ERRORS=$((ERRORS + 1)) + fi -bastille_return_exit_code +done + +if [ "${ERRORS}" -ne 0 ]; then + error_exit "[ERROR]: Command failed on ${ERRORS} jails." +fi + +echo \ No newline at end of file diff --git a/usr/local/share/bastille/start.sh b/usr/local/share/bastille/start.sh index 93805d99..f36e5f00 100644 --- a/usr/local/share/bastille/start.sh +++ b/usr/local/share/bastille/start.sh @@ -104,8 +104,6 @@ set_target "${TARGET}" for _jail in ${JAILS}; do - ( - # Continue if '-b|--boot' is set and 'boot=off' if [ "${BOOT}" -eq 1 ]; then BOOT_ENABLED="$(sysrc -f ${bastille_jailsdir}/${_jail}/settings.conf -n boot)" @@ -219,10 +217,5 @@ for _jail in ${JAILS}; do # Delay between jail action sleep "${DELAY_TIME}" - - ) & - bastille_running_jobs "${bastille_process_limit}" - -done -wait \ No newline at end of file +done \ No newline at end of file diff --git a/usr/local/share/bastille/stop.sh b/usr/local/share/bastille/stop.sh index 09707da9..15a2c736 100644 --- a/usr/local/share/bastille/stop.sh +++ b/usr/local/share/bastille/stop.sh @@ -87,8 +87,6 @@ set_target "${TARGET}" "reverse" for _jail in ${JAILS}; do - ( - # Validate that all jails that 'depend' on this one are stopped for _depend_jail in $(ls --color=never ${bastille_jailsdir} | sed -e 's/\n//g'); do if ! grep -hoqsw "depend=" ${bastille_jailsdir}/${_depend_jail}/settings.conf; then @@ -161,9 +159,4 @@ for _jail in ${JAILS}; do update_jail_syntax_v1 "${_jail}" - ) & - - bastille_running_jobs "${bastille_process_limit}" - done -wait diff --git a/usr/local/share/bastille/sysrc.sh b/usr/local/share/bastille/sysrc.sh index 72e1bafd..d8f2cc75 100644 --- a/usr/local/share/bastille/sysrc.sh +++ b/usr/local/share/bastille/sysrc.sh @@ -82,17 +82,13 @@ fi TARGET="${1}" shift -# Use mktemp to store exit codes -export TMP_BASTILLE_EXIT_CODE="$(mktemp)" -echo 0 > "${TMP_BASTILLE_EXIT_CODE}" +ERRORS=0 bastille_root_check set_target "${TARGET}" for _jail in ${JAILS}; do - ( - # Validate jail state check_target_is_running "${_jail}" || if [ "${AUTO}" -eq 1 ]; then bastille start "${_jail}" @@ -106,14 +102,14 @@ for _jail in ${JAILS}; do jexec -l "${_jail}" /usr/sbin/sysrc "$@" - bastille_check_exit_code "${_jail}" "$?" - - ) & - - bastille_running_jobs "${bastille_process_limit}" + if [ "$?" -ne 0 ]; then + ERRORS=$((ERRORS + 1)) + fi done -wait -echo -bastille_return_exit_code +if [ "${ERRORS}" -ne 0 ]; then + error_exit "[ERROR]: Command failed on ${ERRORS} jails." +fi + +echo \ No newline at end of file diff --git a/usr/local/share/bastille/tags.sh b/usr/local/share/bastille/tags.sh index 1f4d7cf8..7c6423d8 100644 --- a/usr/local/share/bastille/tags.sh +++ b/usr/local/share/bastille/tags.sh @@ -77,8 +77,6 @@ set_target "${TARGET}" for _jail in ${JAILS}; do - ( - bastille_jail_tags="${bastille_jailsdir}/${_jail}/tags" case ${ACTION} in add) @@ -120,9 +118,4 @@ for _jail in ${JAILS}; do ;; esac - ) & - - bastille_running_jobs "${bastille_process_limit}" - done -wait diff --git a/usr/local/share/bastille/template.sh b/usr/local/share/bastille/template.sh index f3a40cf1..bf05ebe8 100644 --- a/usr/local/share/bastille/template.sh +++ b/usr/local/share/bastille/template.sh @@ -272,8 +272,6 @@ fi for _jail in ${JAILS}; do - ( - check_target_is_running "${_jail}" || if [ "${AUTO}" -eq 1 ]; then bastille start "${_jail}" else @@ -475,10 +473,5 @@ for _jail in ${JAILS}; do done info "\nTemplate applied: ${TEMPLATE}" - - ) & - - bastille_running_jobs "${bastille_process_limit}" done -wait diff --git a/usr/local/share/bastille/umount.sh b/usr/local/share/bastille/umount.sh index 37d8d082..4aa1e421 100644 --- a/usr/local/share/bastille/umount.sh +++ b/usr/local/share/bastille/umount.sh @@ -88,8 +88,6 @@ set_target "${TARGET}" for _jail in ${JAILS}; do - ( - # Validate jail state check_target_is_running "${_jail}" || if [ "${AUTO}" -eq 1 ]; then bastille start "${_jail}" @@ -130,9 +128,6 @@ for _jail in ${JAILS}; do echo "Unmounted: ${_jailpath}" - ) & - - bastille_running_jobs "${bastille_process_limit}" - done -wait \ No newline at end of file + +echo \ No newline at end of file