Introduce versioning to sequencer and seqs Refactored sequencer option handling More robust way to include sequencer.sh to seqs
298 lines
6.7 KiB
Bash
Executable File
298 lines
6.7 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
## Sequencer script is doing nothing on its own. It is included by a squence script
|
|
## which uses the sequencer.sh to provide sequencial operations with or without
|
|
## user interaction (see seqTemplate.sh)
|
|
|
|
## Version information
|
|
|
|
VERSION_REV=1
|
|
VERSION_MAJOR=0
|
|
VERSION_MINOR=0
|
|
|
|
## Start of generic script part
|
|
|
|
QUIET=0
|
|
ERNO=0
|
|
MAX_STEP=255
|
|
TEMPLATE_NAME=seqTemplateExample.sh
|
|
VERSION_STRING="${VERSION_REV}.${VERSION_MAJOR}.${VERSION_MINOR}"
|
|
|
|
function helpSequencer() {
|
|
echo "Usage: ${0##*/} [Options] [Step Number(s)]"
|
|
echo
|
|
echo " [Options]"
|
|
echo " --help, -h : Display help"
|
|
echo " --quiet, -q : Don't ask for permission to execute steps"
|
|
echo " If called without starting step number, only this help is shown"
|
|
echo " --version,-v : Display version of sequencer and revision of sequence"
|
|
echo
|
|
echo " [Step Number(s) 1-${MAX_STEP}]"
|
|
echo " Single step number : starting point of process"
|
|
echo " Multiple step numbers : execute only given steps"
|
|
echo " execute only one step with using special step 0"
|
|
echo " ( e.g. only execute step 4: $0 4 0 )"
|
|
}
|
|
|
|
# endCheckEmpty [VariableName] [DESCRIPTION]
|
|
# DESCRIPTION : Optional text for error
|
|
function endCheckEmpty() {
|
|
local errorText=$1
|
|
eval 'local ref=$'$1
|
|
|
|
if [ ! -z "$2" ] ; then
|
|
errorText=$2
|
|
fi
|
|
if [ -z $ref ] ; then
|
|
echo -e "[Error] $errorText must not be empty.\nAborting installation."
|
|
exit 666
|
|
fi
|
|
}
|
|
|
|
function existsFunction() {
|
|
local NOTFOUND=0
|
|
eval 'local ref=$'$1
|
|
declare -F $1 &>>/dev/null || NOTFOUND=1
|
|
return $NOTFOUND
|
|
}
|
|
|
|
function saveReturn() {
|
|
if [ $1 -ne 0 ] ; then
|
|
ERNO=$1
|
|
fi
|
|
}
|
|
|
|
# endReturn [-f]
|
|
# -f : force exit with $ERNO without user input
|
|
function endReturn() {
|
|
if [[ ( $ERNO -ne 0 && $QUIET -ne 0 ) || ( $ERNO -ne 0 && ! -z $1 && $1 == "-f" ) ]] ; then
|
|
echo
|
|
echo -e "[Error] Return value $ERNO detected.\nAborting installation."
|
|
exit $ERNO
|
|
fi
|
|
if [ $ERNO -ne 0 ] ; then
|
|
echo
|
|
echo "[Error] Return value $ERNO detected."
|
|
read -p "End installation: y(default)/n? " answer
|
|
case $answer in
|
|
[nN])
|
|
echo
|
|
echo Continuing installation...
|
|
;;
|
|
*)
|
|
echo
|
|
echo Installation aborted
|
|
exit $ERNO;
|
|
;;
|
|
esac
|
|
fi
|
|
}
|
|
|
|
# execute [-q] <Step Number>
|
|
# -q: don't stop and don't report step functions which cannot be found
|
|
# execute given step_<Step Number> function
|
|
function execute() {
|
|
local NOTFOUND=0
|
|
local NOREPORT=0
|
|
|
|
if [ $1 == "-q" ] ; then
|
|
NOREPORT=1
|
|
shift
|
|
fi
|
|
|
|
# check if step function exists
|
|
declare -F step_$1 &>>/dev/null || NOTFOUND=1
|
|
if [ $NOTFOUND -eq 1 ] && [ $NOREPORT -ne 1 ] ; then
|
|
echo "Step $1 not found"
|
|
exit 1;
|
|
fi
|
|
|
|
# don't execute step functions which are not available
|
|
if [ $NOTFOUND -eq 1 ] ; then
|
|
return $NOTFOUND
|
|
fi
|
|
|
|
echo -en "\n[STEP $1] "
|
|
existsFunction step_${1}_info
|
|
if [ $? -eq 0 ] ; then
|
|
step_${1}_info $1
|
|
fi
|
|
if [ $QUIET -ne 1 ] ; then
|
|
read -p "Start: y/n(default)? " answer
|
|
case $answer in
|
|
[yY])
|
|
step_$1 $1
|
|
;;
|
|
*)
|
|
echo Aborting installation at step $1
|
|
exit 1;
|
|
;;
|
|
esac
|
|
else
|
|
step_$1 $1
|
|
fi
|
|
}
|
|
|
|
# continous <Starting Step Number>
|
|
# (max $MAX_STEP)
|
|
# execute installation continously from given starting step
|
|
function continous() {
|
|
for ((i=$1; i<=${MAX_STEP}; i++)); do
|
|
execute -q $i
|
|
local res=$?
|
|
if [ $res -ne 0 ] ; then
|
|
break;
|
|
fi
|
|
done
|
|
}
|
|
|
|
# selection <Step Number List (separated by space)>
|
|
# execute given step list
|
|
# e.g.: selection -q 1 4 12
|
|
function selection() {
|
|
for i in $@ ; do
|
|
# stop on step 0
|
|
if [ $i -eq 0 ] ; then
|
|
break
|
|
else
|
|
execute $i
|
|
fi
|
|
done
|
|
}
|
|
|
|
# Creating a minimal step definition template
|
|
function createTemplate() {
|
|
if [ -f $TEMPLATE_NAME ] ; then
|
|
return 1
|
|
fi
|
|
echo "#!/bin/bash" > $TEMPLATE_NAME
|
|
echo >> $TEMPLATE_NAME
|
|
echo "# Step 1 is mandatory" >> $TEMPLATE_NAME
|
|
echo "step_1_info() { echo \"My custom step \$1\"; }" >> $TEMPLATE_NAME
|
|
echo "step_1() {" >> $TEMPLATE_NAME
|
|
echo " echo \"Doing something...\"" >> $TEMPLATE_NAME
|
|
echo "}" >> $TEMPLATE_NAME
|
|
echo >> $TEMPLATE_NAME
|
|
echo ". $0" >> $TEMPLATE_NAME
|
|
|
|
chmod +x $TEMPLATE_NAME
|
|
return 0
|
|
}
|
|
|
|
# Always display sequencer help and, if available, sequence help
|
|
function displayHelp() {
|
|
|
|
helpSequencer
|
|
|
|
# check if step definition exists by looking for step_1()
|
|
existsFunction step_1
|
|
if [ $? -ne 0 ] ; then
|
|
echo -e "\n It seems ${0##*/} was called directly."
|
|
echo -e " Please create a sequence script first.\n"
|
|
read -p " Create a template now? y/n(default)? " answer
|
|
case $answer in
|
|
[yY])
|
|
createTemplate
|
|
if [ $? -eq 0 ] ; then
|
|
echo -e "\n $TEMPLATE_NAME created."
|
|
else
|
|
echo -e "\n $TEMPLATE_NAME exists...Nothing to do!"
|
|
fi
|
|
;;
|
|
*)
|
|
echo -e "\n Nothing to do!"
|
|
;;
|
|
esac
|
|
exit 1;
|
|
else
|
|
echo " Step documentation:"
|
|
for ((i=1; i<=${MAX_STEP}; i++)); do
|
|
|
|
# Display step reference in help if step function exists
|
|
existsFunction step_${i}
|
|
if [ $? -ne 0 ] ; then
|
|
continue
|
|
fi
|
|
printf ' Step %3s : ' $i
|
|
|
|
# Display step help only if info function exists
|
|
existsFunction step_${i}_info
|
|
if [ $? -eq 0 ] ; then
|
|
step_${i}_info $i
|
|
else
|
|
echo " - step_${i}_info() missing"
|
|
fi
|
|
done
|
|
fi
|
|
}
|
|
|
|
# showVersion
|
|
function showVersion() {
|
|
echo "Sequencer ${VERSION_STRING}"
|
|
echo -n "Seq Revision "
|
|
if [ ! -z "${VERSION_SEQREV}" ] ; then
|
|
echo "${VERSION_SEQREV}"
|
|
else
|
|
echo "-"
|
|
fi
|
|
}
|
|
|
|
main() {
|
|
local START=0
|
|
|
|
# option check
|
|
case "$1" in
|
|
--help|-h) # show only help
|
|
displayHelp
|
|
exit 0;
|
|
;;
|
|
--quiet|-q) # detect if option quiet is available
|
|
QUIET=1
|
|
shift
|
|
;;
|
|
--version|-v) # version request
|
|
showVersion
|
|
exit 0;
|
|
;;
|
|
"") # Empty -> show help
|
|
displayHelp
|
|
# Assume starting at one for interactive mode
|
|
START=1
|
|
;;
|
|
esac
|
|
|
|
# compatibility check of sequence
|
|
if [ ! -z $VERSION_SEQREV ] && [ $VERSION_SEQREV -gt $VERSION_REV ] ; then
|
|
echo "[ERROR] Unsupported sequence revision"
|
|
showVersion
|
|
exit 1
|
|
fi
|
|
# TODO exclude older versions if needed
|
|
if [ -z $VERSION_SEQREV ] ; then
|
|
echo -e "[WARNING] No sequence revision found. Trying anyway...\n";
|
|
fi
|
|
|
|
# check for starting step
|
|
if [ ! -z "$1" ] ; then
|
|
START=$1
|
|
else
|
|
# End here on quiet mode and no step was given
|
|
if [ $QUIET -eq 1 ] ; then
|
|
exit 1;
|
|
fi
|
|
fi
|
|
|
|
# check if more than one step is given and select execution mode
|
|
if [ ! -z $2 ] ; then
|
|
selection $@
|
|
else
|
|
continous $START
|
|
fi
|
|
|
|
echo
|
|
echo "${0##*/} finished"
|
|
}
|
|
|
|
main "$@"
|
|
exit 0;
|