Various improvements to functions lib

- Set xtrace verbose mode (-x or +x)
- Added systemd-nspawn alternate to chroot
- Fix message display of multiple paramaters
- templateDir was missing quotes around element which could end up splittng a directory that contained spaces
- added functionality to find template flavors within $SCRIPTSDIR and not have to be located within a sub-directory
- Added a few more comments
This commit is contained in:
Jason Mehring 2015-02-10 10:03:27 -05:00
parent 0f6cdceb61
commit b342a11c65

View File

@ -11,6 +11,20 @@ DEBUG=${DEBUG:-0}
################################################################################ ################################################################################
# Global functions # Global functions
################################################################################ ################################################################################
# ------------------------------------------------------------------------------
# Set xtrace verbose mode (-x or)
# ------------------------------------------------------------------------------
XTRACE=
function setVerboseMode() {
# Cache xtrace current status so it can be restored on exit
[[ ${-/x} != $- ]] && XTRACE=0 || XTRACE=1
if [ "${VERBOSE}" -ge 2 -o "${DEBUG}" -ge 2 ]; then
set -x
else
set +x
fi
}
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Define colors # Define colors
@ -69,12 +83,32 @@ if [ "${VERBOSE}" -ge 2 -o "${DEBUG}" == "1" ]; then
chroot() { chroot() {
local retval local retval
true ${blue} true ${blue}
/usr/sbin/chroot "$@" && { retval=$?; true; } || { retval=$?; true; } if [ "${SYSTEMD_NSPAWN_ENABLE}" == "1" ]; then
systemd-nspawn $systemd_bind -D "${INSTALLDIR}" -M "${DIST}" "$@" && { retval=$?; true; } || { retval=$?; true; }
else
/usr/sbin/chroot "${INSTALLDIR}" "$@" && { retval=$?; true; } || { retval=$?; true; }
fi
true ${reset} true ${reset}
return $retval return $retval
} }
fi fi
# ------------------------------------------------------------------------------
# Return xtrace's current mode
# 0 is enables (-x); 1 is disables (+x)
# ------------------------------------------------------------------------------
getXtrace() {
[[ ${-/x} != $- ]] && echo 0 || echo 1
}
# ------------------------------------------------------------------------------
# Return xtrace to desired state
# 0 is enables (-x); 1 is disables (+x)
# ------------------------------------------------------------------------------
setXtrace() {
[[ "${1}" -eq 0 ]] && set -x || set +x
}
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Display messages in color # Display messages in color
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@ -82,24 +116,30 @@ fi
output() { output() {
if [ "${VERBOSE}" -ge 1 ]; then if [ "${VERBOSE}" -ge 1 ]; then
# Don't echo if -x is set since it will already be displayed via true # Don't echo if -x is set since it will already be displayed via true
[[ ${-/x} != $- ]] || echo -e "${1}" [[ ${-/x} != $- ]] || echo -e ""$@""
fi fi
} }
outputc() {
color=${1}
shift
output "${!color}"$@"${reset}" || :
}
info() { info() {
output "${bold}${blue}INFO: ${1}${reset}" || : output "${bold}${blue}INFO: "$@"${reset}" || :
} }
debug() { debug() {
output "${bold}${green}DEBUG: ${1}${reset}" || : output "${bold}${green}DEBUG: "$@"${reset}" || :
} }
warn() { warn() {
output "${stout}${yellow}WARNING: ${1}${reset}" || : output "${stout}${yellow}WARNING: "$@"${reset}" || :
} }
error() { error() {
output "${bold}${red}ERROR: ${1}${reset}" || : output "${bold}${red}ERROR: "$@"${reset}" || :
} }
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@ -166,17 +206,19 @@ templateDir() {
do do
# (wheezy+whonix-gateway / wheezy+whonix-gateway+gnome[+++] / wheezy+gnome ) # (wheezy+whonix-gateway / wheezy+whonix-gateway+gnome[+++] / wheezy+gnome )
if [ "${element%:*}" == "$(templateName ${template_flavor})" ]; then if [ "${element%:*}" == "$(templateName ${template_flavor})" ]; then
eval echo -e ${element#*:} eval echo -e "${element#*:}"
return return
# Very short name compare (+proxy) # Very short name compare (+proxy)
elif [ "${element:0:1}" == "+" -a "${element%:*}" == "+${template_flavor}" ]; then elif [ "${element:0:1}" == "+" -a "${element%:*}" == "+${template_flavor}" ]; then
eval echo -e ${element#*:} eval echo -e "${element#*:}"
return return
fi fi
done done
if [ -n "${template_flavor}" ]; then local template_flavor_prefix="$(templateFlavorPrefix ${template_flavor})"
local template_flavor_prefix="$(templateFlavorPrefix ${template_flavor})" if [ -n "${template_flavor}" -a "${template_flavor}" == "+" ]; then
local dir="${SCRIPTSDIR}/${template_flavor_prefix}"
elif [ -n "${template_flavor}" ]; then
local dir="${SCRIPTSDIR}/${template_flavor_prefix}${template_flavor}" local dir="${SCRIPTSDIR}/${template_flavor_prefix}${template_flavor}"
else else
local dir="${SCRIPTSDIR}" local dir="${SCRIPTSDIR}"
@ -223,6 +265,7 @@ buildStepExec() {
# Cache $script # Cache $script
GLOBAL_CACHE[$script]=1 GLOBAL_CACHE[$script]=1
# Execute $script # Execute $script
"${script}" "${script}"
fi fi
@ -266,11 +309,16 @@ callTemplateFunction() {
local calling_arg="$2" local calling_arg="$2"
local functionExec="$3" local functionExec="$3"
local template_flavor="${TEMPLATE_FLAVOR}" local template_flavor="${TEMPLATE_FLAVOR}"
${functionExec} "${calling_script}" \ ${functionExec} "${calling_script}" \
"${calling_arg}" \ "${calling_arg}" \
"${template_flavor}" "${template_flavor}"
# Find a $DIST sub-directory
${functionExec} "${calling_script}" \
"${calling_arg}" \
"+"
for option in ${TEMPLATE_OPTIONS[@]} for option in ${TEMPLATE_OPTIONS[@]}
do do
# Long name (wheezy+whonix-gateway+proxy) # Long name (wheezy+whonix-gateway+proxy)
@ -292,6 +340,17 @@ callTemplateFunction() {
} }
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Will return all files that match pattern of suffix
# Example:
# filename = packages.list
# suffix = ${DIST} (wheezy)
#
# Will look for a file name packages_wheezy.list in:
# the $SCRIPTSDIR; beside original
# the $SCRIPTSDIR/$DIST (wheezy) directory
# any included template module directories ($SCRIPTSDIR/gnome)
#
# All matches are returned and each will be able to be used
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
getFileLocations() { getFileLocations() {
local return_global_var=$1 local return_global_var=$1
@ -311,6 +370,18 @@ getFileLocations() {
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Executes any additional optional configuration steps if the configuration # Executes any additional optional configuration steps if the configuration
# scripts exist # scripts exist
#
# Will find all scripts with
# Example:
# filename = 04_install_qubes.sh
# suffix = post
#
# Will look for a file name 04_install_qubes_post in:
# the $SCRIPTSDIR; beside original
# the $SCRIPTSDIR/$DIST (wheezy) directory
# any included template module directories ($SCRIPTSDIR/gnome)
#
# All matches are executed
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
buildStep() { buildStep() {
local filename="$1" local filename="$1"