Commit 52572781 authored by Nikos Skalkotos's avatar Nikos Skalkotos
Browse files

Add check_yes_no() to test boolean img properties

Use check_yes_no() to test if a boolean property is set.

Although the documentation states that to set a boolean image property
you need to assign the "yes" value to it, this function will accept
"yes", "true", 1, "on" and "set" in a case-insensitive way and reject
"no", "false", 0, "off" and "unset".

An empty or not-set variable is treated as false.

An unknown value will raise a warning but will be accepted. This is done
to protect the users because prior to this commit, in some cases we only
tested if an image property had a non-empty value.

This resolves #80
parent 27b7f907
......@@ -839,11 +839,40 @@ task_cleanup() {
cleanup
}
check_yes_no() {
local name value
name="${1}"
if [ -z "${!name+dummy}" ]; then
# variable is not defined at all
return 1
fi
# lowercase value
value="${!name,,}"
# Remove the SNF_IMAGE_PROPERTY_ prefix from the name when displaying it.
name="${name#SNF_IMAGE_PROPERTY_}"
if [[ "$value" =~ ^(yes|true|on|1|set)$ ]]; then
return 0
elif [[ "$value" =~ ^(no|false|off|0|unset)$ ]]; then
return 1
elif [ -z "$value" ]; then
warn "Variable \`$name' defined but empty. Will treat this as a \`NO'"
return 1
else
warn "Invalid value for variable: \`$name' (=$value). Will treat this as a \`YES'"
return 0
fi
}
check_if_excluded() {
local name exclude
name="$(tr [a-z] [A-Z] <<< ${PROGNAME:2})"
exclude="SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${name}"
if [ -n "${!exclude}" ]; then
if check_yes_no "$exclude"; then
warn "Task ${PROGNAME:2} was excluded and will not run."
exit 0
fi
......@@ -852,7 +881,7 @@ check_if_excluded() {
}
check_if_mounted_excluded() {
if [ -n "$SNF_IMAGE_PROPERTY_EXCLUDE_MOUNTED_TASKS" ]; then
if check_yes_no SNF_IMAGE_PROPERTY_EXCLUDE_MOUNTED_TASKS; then
warn "Task ${PROGNAME:2} was excluded and will not run."
exit 0
fi
......
......@@ -98,7 +98,7 @@ if [ -z "$RUN_PARTS" ]; then
log_error "run-parts program is missing from the system"
fi
if [ -z "$SNF_IMAGE_PROPERTY_EXCLUDE_ALL_TASKS" ]; then
if ! check_yes_no SNF_IMAGE_PROPERTY_EXCLUDE_ALL_TASKS; then
if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "" -o "$SNF_IMAGE_PROPERTY_ROOT_PARTITION" = "" ]; then
warn "Needed image properties are missing. Autodetecting them..."
......@@ -149,7 +149,7 @@ if [ -z "$SNF_IMAGE_PROPERTY_EXCLUDE_ALL_TASKS" ]; then
# Reset the handler to its original value
trap report_error ERR
else
warn "EXCLUDE_ALL_TASKS: All configuration tasks were prevented from running."
warn "EXCLUDE_ALL_TASKS is set. All configuration tasks were prevented from running."
fi
......
......@@ -57,7 +57,7 @@ if [ -z "$SNF_IMAGE_RESIZE_PART" ]; then
exit 0
fi
if [ "x$SNF_IMAGE_PROPERTY_DO_SYNC" = "xyes" ]; then
if check_yes_no SNF_IMAGE_PROPERTY_DO_SYNC; then
unset EATMYDATA
fi
......
......@@ -50,7 +50,7 @@ mkdir -p "$target/Windows/Setup/Scripts"
unattend=$(get_unattend "$target")
if [ -n "$unattend" -a -z "$SNF_IMAGE_PROPERTY_IGNORE_UNATTEND" ]; then
if [ -n "$unattend" -a ! check_yes_no SNF_IMAGE_PROPERTY_IGNORE_UNATTEND ]; then
warn "Using the Unattend.xml file found in the image"
else
rm -f "$unattend"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment