function.sh: Allow multiple template directories to be searched
Return all and run matches instead of only one which allows flavors like salt to have multiple optional modules like mgmt-salt-dev to run installation scripts in template subdirectories.
This commit is contained in:
parent
621a21a528
commit
b61fdaf843
123
functions.sh
123
functions.sh
@ -189,8 +189,9 @@ splitPath() {
|
|||||||
setArrayAsGlobal PARTS $return_global_var
|
setArrayAsGlobal PARTS $return_global_var
|
||||||
}
|
}
|
||||||
|
|
||||||
templateDir() {
|
templateDirs() {
|
||||||
local template_flavor=${1-${TEMPLATE_FLAVOR}}
|
local template_flavor=${1-${TEMPLATE_FLAVOR}}
|
||||||
|
local match=0
|
||||||
|
|
||||||
# If TEMPLATE_FLAVOR_DIR is not already an array, make it one
|
# If TEMPLATE_FLAVOR_DIR is not already an array, make it one
|
||||||
if ! [[ "$(declare -p TEMPLATE_FLAVOR_DIR 2>/dev/null)" =~ ^declare\ -a.* ]] ; then
|
if ! [[ "$(declare -p TEMPLATE_FLAVOR_DIR 2>/dev/null)" =~ ^declare\ -a.* ]] ; then
|
||||||
@ -202,14 +203,19 @@ templateDir() {
|
|||||||
# (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
|
match=1
|
||||||
|
|
||||||
# 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
|
match=1
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
if [ "${match}" -eq 1 ]; then
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
local template_flavor_prefix="$(templateFlavorPrefix ${template_flavor})"
|
local template_flavor_prefix="$(templateFlavorPrefix ${template_flavor})"
|
||||||
if [ -n "${template_flavor}" -a "${template_flavor}" == "+" ]; then
|
if [ -n "${template_flavor}" -a "${template_flavor}" == "+" ]; then
|
||||||
local dir="${SCRIPTSDIR}/${template_flavor_prefix}"
|
local dir="${SCRIPTSDIR}/${template_flavor_prefix}"
|
||||||
@ -241,55 +247,57 @@ templateFile() {
|
|||||||
local file="$1"
|
local file="$1"
|
||||||
local suffix="$2"
|
local suffix="$2"
|
||||||
local template_flavor="$3"
|
local template_flavor="$3"
|
||||||
local template_dir="$(templateDir "${template_flavor}")"
|
local template_dirs="$(templateDirs "${template_flavor}")"
|
||||||
|
|
||||||
splitPath "${file}" path_parts
|
splitPath "${file}" path_parts
|
||||||
|
|
||||||
# No template flavor
|
for template_dir in ${template_dirs[@]}; do
|
||||||
if [ -z "${template_flavor}" ]; then
|
# No template flavor
|
||||||
if [ "${suffix}" ]; then
|
if [ -z "${template_flavor}" ]; then
|
||||||
exists "${SCRIPTSDIR}/${path_parts[base]}_${suffix}${path_parts[dotext]}" || true
|
if [ "${suffix}" ]; then
|
||||||
else
|
exists "${SCRIPTSDIR}/${path_parts[base]}_${suffix}${path_parts[dotext]}" || true
|
||||||
exists "${SCRIPTSDIR}/${path_parts[base]}${path_parts[dotext]}" || true
|
else
|
||||||
|
exists "${SCRIPTSDIR}/${path_parts[base]}${path_parts[dotext]}" || true
|
||||||
|
fi
|
||||||
|
return
|
||||||
fi
|
fi
|
||||||
return
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Locate file in directory named after flavor
|
# Locate file in directory named after flavor
|
||||||
if [ "${suffix}" ]; then
|
if [ "${suffix}" ]; then
|
||||||
# Append suffix to filename (before extension)
|
# Append suffix to filename (before extension)
|
||||||
# `minimal` is the template_flavor being used in comment example
|
# `minimal` is the template_flavor being used in comment example
|
||||||
|
|
||||||
# (TEMPLATE_FLAVOR_DIR/minimal/packages_qubes_suffix.list)
|
# (TEMPLATE_FLAVOR_DIR/minimal/packages_qubes_suffix.list)
|
||||||
exists "${template_dir}/${template_flavor}/${path_parts[base]}_${suffix}${path_parts[dotext]}" || true
|
exists "${template_dir}/${template_flavor}/${path_parts[base]}_${suffix}${path_parts[dotext]}" || true
|
||||||
|
|
||||||
# (TEMPLATE_FLAVOR_DIR/minimal/packages_qubes_suffix.list)
|
# (TEMPLATE_FLAVOR_DIR/minimal/packages_qubes_suffix.list)
|
||||||
exists "${template_dir}/${template_flavor}/${path_parts[base]}_${suffix}${path_parts[dotext]}" || true
|
exists "${template_dir}/${template_flavor}/${path_parts[base]}_${suffix}${path_parts[dotext]}" || true
|
||||||
|
|
||||||
# (TEMPLATE_FLAVOR_DIR/packages_qubes_suffix.list)
|
# (TEMPLATE_FLAVOR_DIR/packages_qubes_suffix.list)
|
||||||
exists "${template_dir}/${path_parts[base]}_${suffix}${path_parts[dotext]}" || true
|
exists "${template_dir}/${path_parts[base]}_${suffix}${path_parts[dotext]}" || true
|
||||||
|
|
||||||
# (TEMPLATE_FLAVOR_DIR/packages_qubes_minimal_suffix.list)
|
# (TEMPLATE_FLAVOR_DIR/packages_qubes_minimal_suffix.list)
|
||||||
exists "${template_dir}/${path_parts[base]}_${suffix}_${template_flavor}${path_parts[dotext]}" || true
|
exists "${template_dir}/${path_parts[base]}_${suffix}_${template_flavor}${path_parts[dotext]}" || true
|
||||||
|
|
||||||
# (SCRIPTSDIR/packages_qubes_minimal_suffix.list)
|
# (SCRIPTSDIR/packages_qubes_minimal_suffix.list)
|
||||||
exists "${SCRIPTSDIR}/${path_parts[base]}_${suffix}_${template_flavor}${path_parts[dotext]}" || true
|
exists "${SCRIPTSDIR}/${path_parts[base]}_${suffix}_${template_flavor}${path_parts[dotext]}" || true
|
||||||
else
|
else
|
||||||
# (TEMPLATE_FLAVOR_DIR/minimal/packages_qubes.list)
|
# (TEMPLATE_FLAVOR_DIR/minimal/packages_qubes.list)
|
||||||
exists "${template_dir}/${template_flavor}/${path_parts[base]}${path_parts[dotext]}" || true
|
exists "${template_dir}/${template_flavor}/${path_parts[base]}${path_parts[dotext]}" || true
|
||||||
|
|
||||||
# (TEMPLATE_FLAVOR_DIR/minimal/packages_qubes_minimal.list)
|
# (TEMPLATE_FLAVOR_DIR/minimal/packages_qubes_minimal.list)
|
||||||
exists "${template_dir}/${template_flavor}/${path_parts[base]}_${template_flavor}${path_parts[dotext]}" || true
|
exists "${template_dir}/${template_flavor}/${path_parts[base]}_${template_flavor}${path_parts[dotext]}" || true
|
||||||
|
|
||||||
# (TEMPLATE_FLAVOR_DIR/packages_qubes.list)
|
# (TEMPLATE_FLAVOR_DIR/packages_qubes.list)
|
||||||
exists "${template_dir}/${path_parts[base]}${path_parts[dotext]}" || true
|
exists "${template_dir}/${path_parts[base]}${path_parts[dotext]}" || true
|
||||||
|
|
||||||
# (TEMPLATE_FLAVOR_DIR/packages_qubes_minimal.list)
|
# (TEMPLATE_FLAVOR_DIR/packages_qubes_minimal.list)
|
||||||
exists "${template_dir}/${path_parts[base]}_${template_flavor}${path_parts[dotext]}" || true
|
exists "${template_dir}/${path_parts[base]}_${template_flavor}${path_parts[dotext]}" || true
|
||||||
|
|
||||||
# (SCRIPTSDIR/packages_qubes_minimal.list)
|
# (SCRIPTSDIR/packages_qubes_minimal.list)
|
||||||
exists "${SCRIPTSDIR}/${path_parts[base]}_${template_flavor}${path_parts[dotext]}" || true
|
exists "${SCRIPTSDIR}/${path_parts[base]}_${template_flavor}${path_parts[dotext]}" || true
|
||||||
fi
|
fi
|
||||||
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
copyTreeExec() {
|
copyTreeExec() {
|
||||||
@ -298,27 +306,30 @@ copyTreeExec() {
|
|||||||
local template_flavor="$3"
|
local template_flavor="$3"
|
||||||
local target_dir="$4"
|
local target_dir="$4"
|
||||||
|
|
||||||
local template_dir="$(templateDir ${template_flavor})"
|
local template_dirs="$(templateDirs ${template_flavor})"
|
||||||
local source_dir="$(readlink -m ${source_dir:-${template_dir}}/${dir})"
|
|
||||||
local target_dir="$(readlink -m ${target_dir:-${INSTALLDIR}})"
|
|
||||||
|
|
||||||
if ! [ -d "${source_dir}" ]; then
|
for template_dir in ${template_dirs[@]}; do
|
||||||
debug "No extra files to copy for ${dir}"
|
local source_dir="$(readlink -m ${source_dir:-${template_dir}}/${dir})"
|
||||||
return 0
|
local target_dir="$(readlink -m ${target_dir:-${INSTALLDIR}})"
|
||||||
fi
|
|
||||||
|
|
||||||
debug "Copying ${source_dir}/* ${target_dir}"
|
if ! [ -d "${source_dir}" ]; then
|
||||||
cp -rp "${source_dir}/." "${target_dir}"
|
debug "No extra files to copy for ${dir}"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
if [ -f "${source_dir}/.facl" ]; then
|
debug "Copying ${source_dir}/* ${target_dir}"
|
||||||
debug "Restoring file permissions..."
|
cp -rp "${source_dir}/." "${target_dir}"
|
||||||
pushd "${target_dir}"
|
|
||||||
{
|
if [ -f "${source_dir}/.facl" ]; then
|
||||||
setfacl --restore="${source_dir}/.facl" 2>/dev/null ||:
|
debug "Restoring file permissions..."
|
||||||
rm -f .facl
|
pushd "${target_dir}"
|
||||||
}
|
{
|
||||||
popd
|
setfacl --restore="${source_dir}/.facl" 2>/dev/null ||:
|
||||||
fi
|
rm -f .facl
|
||||||
|
}
|
||||||
|
popd
|
||||||
|
fi
|
||||||
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
callTemplateFunction() {
|
callTemplateFunction() {
|
||||||
|
Loading…
Reference in New Issue
Block a user