update: add debug, code cleanup

This commit is contained in:
tschettervictor
2025-01-14 13:02:02 -07:00
committed by GitHub
parent b22d68db13
commit 2f7120a176

View File

@@ -34,39 +34,62 @@
. /usr/local/etc/bastille/bastille.conf . /usr/local/etc/bastille/bastille.conf
usage() { usage() {
error_exit "Usage: bastille update [release|container|template] | [force]" error_notify "Usage: bastille update [option(s)] TARGET"
} cat << EOF
Options:
# Handle special-case commands first. -a | --auto Auto mode. Start/stop jail(s) if required.
case "$1" in -f | --force Force update a release.
help|-h|--help) -x | --debug Enable debug mode.
usage
;; EOF
esac exit 1
}
if [ $# -gt 2 ] || [ $# -lt 1 ]; then if [ $# -gt 2 ] || [ $# -lt 1 ]; then
usage usage
fi fi
bastille_root_check # Handle options.
OPTION=""
TARGET="${1}" AUTO=0
OPTION="${2}" while [ "$#" -gt 0 ]; do
case "${1}" in
# Handle options -h|--help|help)
case "${OPTION}" in usage
;;
-a|--auto)
AUTO=1
shift
;;
-f|--force) -f|--force)
OPTION="-F" OPTION="-F"
shift
;;
-x|--debug)
enable_debug
shift
;;
-*)
for _opt in $(echo ${1} | sed 's/-//g' | fold -w1); do
case ${_opt} in
a) AUTO=1 ;;
f) OPTION="-F" ;;
x) enable_debug ;;
*) error_exit "Unknown Option: \"${1}\"" ;;
esac
done
shift
;; ;;
*) *)
OPTION= break
;; ;;
esac esac
done
# Check for unsupported actions TARGET="${1}"
if [ "${TARGET}" = "ALL" ]; then
error_exit "Batch upgrade is unsupported." bastille_root_check
fi
if [ -f "/bin/midnightbsd-version" ]; then if [ -f "/bin/midnightbsd-version" ]; then
echo -e "${COLOR_RED}Not yet supported on MidnightBSD.${COLOR_RESET}" echo -e "${COLOR_RED}Not yet supported on MidnightBSD.${COLOR_RESET}"
@@ -86,22 +109,25 @@ arch_check() {
jail_check() { jail_check() {
# Check if the jail is thick and is running # Check if the jail is thick and is running
if [ ! "$(/usr/sbin/jls name | awk "/^${TARGET}$/")" ]; then set_target_single "${TARGET}"
error_exit "[${TARGET}]: Not started. See 'bastille start ${TARGET}'." check_target_is_running "${TARGET}" || if [ "${AUTO}" -eq 1 ]; then
bastille start "${TARGET}"
else else
if grep -qw "${bastille_jailsdir}/${TARGET}/root/.bastille" "${bastille_jailsdir}/${TARGET}/fstab"; then error_notify "Jail is not running."
error_exit "${TARGET} is not a thick container." error_continue "Use [-a|--auto] to auto-start the jail."
fi fi
if grep -qw "${bastille_jailsdir}/${TARGET}/root/.bastille" "${bastille_jailsdir}/${TARGET}/fstab"; then
error_notify "${TARGET} is not a thick container."
error_exit "See 'bastille update RELEASE' to update thin jails."
fi fi
} }
jail_update() { jail_update() {
local _freebsd_update_conf="${bastille_jailsdir}/${TARGET}/root/etc/freebsd-update.conf" local _freebsd_update_conf="${bastille_jailsdir}/${TARGET}/root/etc/freebsd-update.conf"
local _jail_dir="${bastille_jailsdir}/${TARGET}/root" local _jail_dir="${bastille_jailsdir}/${TARGET}/root"
local _workdir="${bastille_releasesdir}/${TARGET}/root/var/db/freebsd-update" local _workdir="${bastille_jailsdir}/${TARGET}/root/var/db/freebsd-update"
# Update a thick container # Update a thick container
if [ -d "${bastille_jailsdir}/${TARGET}" ]; then if [ -d "${bastille_jailsdir}/${TARGET}" ]; then
jail_check
CURRENT_VERSION=$(/usr/sbin/jexec -l "${TARGET}" freebsd-version 2>/dev/null) CURRENT_VERSION=$(/usr/sbin/jexec -l "${TARGET}" freebsd-version 2>/dev/null)
if [ -z "${CURRENT_VERSION}" ]; then if [ -z "${CURRENT_VERSION}" ]; then
error_exit "Can't determine '${TARGET}' version." error_exit "Can't determine '${TARGET}' version."
@@ -111,32 +137,28 @@ jail_update() {
-f "${_freebsd_update_conf}" \ -f "${_freebsd_update_conf}" \
fetch install --currently-running "${CURRENT_VERSION}" fetch install --currently-running "${CURRENT_VERSION}"
fi fi
else
error_exit "${TARGET} not found. See 'bastille bootstrap'."
fi fi
} }
release_update() { release_update() {
local _freebsd_update_conf="${bastille_releasesdir}/${TARGET}/etc/freebsd-update.conf" local _freebsd_update_conf="${bastille_releasesdir}/${TARGET}/etc/freebsd-update.conf"
local _release_dir="${bastille_releasesdir}/${TARGET}" local _release_dir="${bastille_releasesdir}/${TARGET}"
local _workdir="${bastille_releasesdir}/${TARGET}/var/db/freebsd-update"
# Update a release base(affects child containers) # Update a release base(affects child containers)
if [ -d "${bastille_releasesdir}/${TARGET}" ]; then if [ -d "${_release_dir}" ]; then
TARGET_TRIM="${TARGET}" TARGET_TRIM="${TARGET}"
if [ -n "${ARCH_I386}" ]; then if [ -n "${ARCH_I386}" ]; then
TARGET_TRIM=$(echo "${TARGET}" | sed 's/-i386//') TARGET_TRIM=$(echo "${TARGET}" | sed 's/-i386//')
fi fi
env PAGER="/bin/cat" freebsd-update ${OPTION} --not-running-from-cron -b "${bastille_releasesdir}/${TARGET}" \ env PAGER="/bin/cat" freebsd-update ${OPTION} --not-running-from-cron -b "${bastille_releasesdir}/${TARGET}" \
-d "${_workdir}" \ -d "${bastille_releasesdir}/${TARGET}/var/db/freebsd-update" \
-f "${_freebsd_update_conf}" \ -f "${_freebsd_update_conf}" \
fetch --currently-running "${TARGET_TRIM}" fetch --currently-running "${TARGET_TRIM}"
env PAGER="/bin/cat" freebsd-update ${OPTION} --not-running-from-cron -b "${bastille_releasesdir}/${TARGET}" \ env PAGER="/bin/cat" freebsd-update ${OPTION} --not-running-from-cron -b "${bastille_releasesdir}/${TARGET}" \
-d "${_workdir}" \ -d "${bastille_releasesdir}/${TARGET}/var/db/freebsd-update" \
-f "${_freebsd_update_conf}" \ -f "${_freebsd_update_conf}" \
install --currently-running "${TARGET_TRIM}" install --currently-running "${TARGET_TRIM}"
else else
error_exit "${TARGET} not found. See 'bastille bootstrap'." error_exit "${TARGET} not found. See 'bastille bootstrap RELEASE'."
fi fi
} }
@@ -157,10 +179,10 @@ template_update() {
templates_update() { templates_update() {
# Update all templates # Update all templates
_updated_templates=0 _updated_templates=0
if [ -d "${bastille_templatesdir}" ]; then if [ -d ${bastille_templatesdir} ]; then
# shellcheck disable=SC2045 # shellcheck disable=SC2045
for _template_path in $(ls -d "${bastille_templatesdir}"/*/*); do for _template_path in $(ls -d ${bastille_templatesdir}/*/*); do
if [ -d "$_template_path"/.git ]; then if [ -d $_template_path/.git ]; then
BASTILLE_TEMPLATE=$(echo "$_template_path" | awk -F / '{ print $(NF-1) "/" $NF }') BASTILLE_TEMPLATE=$(echo "$_template_path" | awk -F / '{ print $(NF-1) "/" $NF }')
template_update template_update
@@ -186,5 +208,6 @@ elif echo "${TARGET}" | grep -q "[0-9]\{2\}.[0-9]-RELEASE"; then
arch_check arch_check
release_update release_update
else else
jail_check
jail_update jail_update
fi fi