From deb4cbd8674f171272910686bd4cd63fc9c1602a Mon Sep 17 00:00:00 2001 From: Jason Mehring Date: Fri, 24 Oct 2014 16:17:29 -0400 Subject: [PATCH] Added ability to name tempaltes within configuration file and check/fix to make sure names under 32 characters --- Makefile | 9 ++ builder_fix_filenames | 9 ++ builder_setup | 5 + functions.sh | 50 ++++++++- tests/template-flavors/test.sh | 187 ++++++++++++++++++++++++++++----- 5 files changed, 232 insertions(+), 28 deletions(-) create mode 100755 builder_fix_filenames diff --git a/Makefile b/Makefile index f5e65d4..f9f3e50 100644 --- a/Makefile +++ b/Makefile @@ -6,10 +6,19 @@ export DIST dist_ver := $(shell DIST=$(DIST) ./builder_setup) DISTRIBUTION := $(word 1,$(dist_ver)) DIST_VERSION := $(word 2,$(dist_ver)) +TEMPLATE_NAME := $(word 3,$(dist_ver)) + +ifeq (,$(TEMPLATE_NAME)) TEMPLATE_NAME := $(DISTRIBUTION)-$(DIST_VERSION)-x64 ifdef TEMPLATE_FLAVOR TEMPLATE_NAME := $(TEMPLATE_NAME)-$(TEMPLATE_FLAVOR) endif +endif + +# Make sure names are < 32 characters +fix_up := $(shell TEMPLATE_NAME=$(TEMPLATE_NAME) ./builder_fix_filenames) +TEMPLATE_NAME := $(word 1,$(fix_up)) + VERSION := $(shell cat version) TIMESTAMP := $(shell date -u +%Y%m%d%H%M) diff --git a/builder_fix_filenames b/builder_fix_filenames new file mode 100755 index 0000000..6d3848f --- /dev/null +++ b/builder_fix_filenames @@ -0,0 +1,9 @@ +#!/bin/bash + +# Check template name length and fix if not under 32 characters +# Return the result + +. ./functions.sh > /dev/null + +# Check for custom template name +templateNameDist "${TEMPLATE_NAME}" diff --git a/builder_setup b/builder_setup index ba265b9..bd2b23f 100755 --- a/builder_setup +++ b/builder_setup @@ -2,6 +2,8 @@ # Setup env variables +. ./functions.sh > /dev/null + case "$DIST" in fc*) DISTRIBUTION=fedora @@ -31,3 +33,6 @@ CACHEDIR=cache_$DIST export SCRIPTSDIR CACHEDIR echo $DISTRIBUTION $VERSION + +# Check for custom template name +#templateName > /dev/null && templateName diff --git a/functions.sh b/functions.sh index d0967e6..d86543b 100755 --- a/functions.sh +++ b/functions.sh @@ -157,13 +157,61 @@ templateFlavorPrefix() { echo "${DIST}${template_flavor:++}" } +templateNameDist() { + local dist_name="${1}" + template_name="$(templateName)" && dist_name="${template_name}" + + # XXX: Temp hack to shorten name + if [ ${#dist_name} -ge 32 ]; then + if [ ${#template_name} -lt 32 ]; then + dist_name="${template_name}" + else + dist_name="${dist_name:0:31}" + fi + fi + echo ${dist_name} +} + +templateName() { + local template_flavor=${1-$(templateFlavor)} + retval=1 # Default is 1; mean no replace happened + + # Only apply options if $1 was not passed + if [ -n "${1}" ]; then + local template_options= + else + local template_options="${TEMPLATE_OPTIONS// /+}" + fi + + local template_name="$(templateFlavorPrefix ${template_flavor})${template_flavor}${template_options:++}${template_options}" + + for element in "${TEMPLATE_LABEL[@]}"; do + if [ "${element%:*}" == "${template_name}" ]; then + template_name="${element#*:}" + retval=0 + break + fi + done + + if [ ${#template_name} -ge 32 ]; then + error "Template name is greater than 31 characters: ${template_name}" + error "Please set an alias" + error "Exiting!!!" + exit 1 + fi + + echo ${template_name} + return $retval +} + templateDir() { local template_flavor=${1-$(templateFlavor)} for element in "${TEMPLATE_FLAVOR_DIR[@]}" do # (wheezy+whonix-gateway / wheezy+whonix-gateway+gnome[+++] / wheezy+gnome ) - if [ "${element%:*}" == "$(templateFlavorPrefix ${template_flavor})${template_flavor}" ]; then + #if [ "${element%:*}" == "$(templateFlavorPrefix ${template_flavor})${template_flavor}" ]; then + if [ "${element%:*}" == "$(templateName ${template_flavor})" ]; then eval echo -e ${element#*:} return # Very short name compare (+proxy) diff --git a/tests/template-flavors/test.sh b/tests/template-flavors/test.sh index c76faff..43f8fac 100755 --- a/tests/template-flavors/test.sh +++ b/tests/template-flavors/test.sh @@ -55,6 +55,7 @@ values() { else printf " ${bold}${black}%-22s = ${value}${reset}\n" "${label}" || : fi + #printf "${bold}${black}%s=\"${value}\"${reset}\n" "${label}" || : VALUES[$label]="${value}" } } @@ -86,8 +87,11 @@ assertEnd() { # ------------------------------------------------------------------------------ SCRIPTSDIR="tests/template-flavors" DIST="wheezy" +DISTS_VM="" TEMPLATE_FLAVOR="whonix-gateway" TEMPLATE_FLAVOR_PREFIX="" +TEMPLATE_FLAVOR_DIR="" +TEMPLATE_OPTIONS="" header <