Introducing text alias for steps
This commit is contained in:
2019-04-08 14:44:31 +01:00
parent baef4e8d56
commit e6313d2c32
4 changed files with 139 additions and 71 deletions

View File

@@ -4,11 +4,14 @@
## Sequencer interface:
## - step_[1 - 255]_info [STEP NUMBER]
## functions providing short header for help and user interaction
## - step_[1 - 255]_alias
## (optional) provide a string as alias for this step number
## - step_[1 - 255] [STEP NUMBER]
## functions performing custom operations
## performing custom shell operations
##
step_1_info() { echo "Step $1 header"; }
step_1_alias() { ALIAS="begin"; }
step_1() {
cat .nofile
saveReturn $?
@@ -32,15 +35,19 @@ step_10() {
}
step_11_info() { echo "Step $1 header"; }
step_11_alias() { ALIAS="elf"; }
step_11() {
echo elf
}
# Sequence Revision
VERSION_SEQREV=1
VERSION_SEQREV=2
# Workaround when called from different directory
# Not needed when path to sequencer is absolut
#
# It is recommended to copy or link sequencer.sh to
# /usr/local/bin
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >>/dev/null 2>&1 && pwd )"
# Path to local sequencer.sh script
. ${DIR}/./sequencer/sequencer.sh

View File

@@ -24,13 +24,13 @@ step_2() {
step_3_info() { echo "Adding user for $toolName (git:git)"; }
step_3() {
adduser \
--system \
--shell /bin/bash \
--gecos 'Git Version Control' \
--group \
--disabled-password \
--home /home/git \
git
--system \
--shell /bin/bash \
--gecos 'Git Version Control' \
--group \
--disabled-password \
--home /home/git \
git
saveReturn $?
endReturn
}

View File

@@ -126,6 +126,7 @@ step_40() {
}
step_42_info() { echo "Fix librenms permission"; }
step_42_alias() { ALIAS="repair"; }
step_42() {
chown -R librenms:librenms /opt/librenms
setfacl -d -m g::rwx /opt/librenms/bootstrap/cache /opt/librenms/storage /opt/librenms/logs /opt/librenms/rrd

View File

@@ -6,7 +6,7 @@
## Version information
VERSION_REV=1
VERSION_REV=2
VERSION_MAJOR=0
VERSION_MINOR=0
@@ -15,28 +15,29 @@ VERSION_MINOR=0
QUIET=0
ERNO=0
MAX_STEP=255
ALIAS=
TEMPLATE_NAME=seqTemplateExample.sh
VERSION_STRING="${VERSION_REV}.${VERSION_MAJOR}.${VERSION_MINOR}"
function helpSequencer() {
echo "Usage: ${0##*/} [Options] [Step Number(s)]"
helpSequencer() {
echo "Usage: ${0##*/} [OPTIONS] [STEP NUMBER(s) or ALIAS]"
echo
echo " [Options]"
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 )"
echo " [STEP NUMBER(s) 1-${MAX_STEP} or ALIAS]"
echo " Single STEP or ALIAS : starting point of process"
echo " Multiple STEPS or ALIAS : 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() {
endCheckEmpty() {
local errorText=$1
eval 'local ref=$'$1
@@ -49,14 +50,13 @@ function endCheckEmpty() {
fi
}
function existsFunction() {
existsFunction() {
local NOTFOUND=0
eval 'local ref=$'$1
declare -F $1 &>>/dev/null || NOTFOUND=1
return $NOTFOUND
}
function saveReturn() {
saveReturn() {
if [ $1 -ne 0 ] ; then
ERNO=$1
fi
@@ -64,7 +64,7 @@ function saveReturn() {
# endReturn [-f]
# -f : force exit with $ERNO without user input
function endReturn() {
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."
@@ -76,14 +76,14 @@ function endReturn() {
read -p "End installation: y(default)/n? " answer
case $answer in
[nN])
echo
echo Continuing installation...
;;
echo
echo Continuing installation...
;;
*)
echo
echo Installation aborted
exit $ERNO;
;;
echo
echo Installation aborted
exit $ERNO;
;;
esac
fi
}
@@ -91,7 +91,7 @@ function endReturn() {
# 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() {
execute() {
local NOTFOUND=0
local NOREPORT=0
@@ -121,23 +121,48 @@ function execute() {
read -p "Start: y/n(default)? " answer
case $answer in
[yY])
step_$1 $1
;;
step_$1 $1
;;
*)
echo Aborting installation at step $1
exit 1;
;;
echo Aborting installation at step $1
exit 1;
;;
esac
else
step_$1 $1
fi
}
# checkStep <Step Number or Alias>
# return 0 - for invalid step
# Check sanitiy of step number or
# Check if alias exists
checkStep() {
if (( $1 < 1 || $1 > $MAX_STEP )) ; then
eval 'local ref=$alias_'$1
if [ -z $ref ] || [ "$ref" == "$1" ] ; then
return 0
else
return $ref
fi
else
return $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
continous() {
local step=0
checkStep $1
step=$?
if [[ $step == 0 ]] ; then
return 1
fi
for ((i=$step; i<=${MAX_STEP}; i++)); do
execute -q $i
local res=$?
if [ $res -ne 0 ] ; then
@@ -149,19 +174,22 @@ function continous() {
# selection <Step Number List (separated by space)>
# execute given step list
# e.g.: selection -q 1 4 12
function selection() {
selection() {
local step=0
for i in $@ ; do
# stop on step 0
if [ $i -eq 0 ] ; then
break
else
execute $i
fi
checkStep $i
step=$?
# stop on step 0
if [ $step -eq 0 ] ; then
break
else
execute $step
fi
done
}
# Creating a minimal step definition template
function createTemplate() {
createTemplate() {
if [ -f $TEMPLATE_NAME ] ; then
return 1
fi
@@ -169,18 +197,38 @@ function createTemplate() {
echo >> $TEMPLATE_NAME
echo "# Step 1 is mandatory" >> $TEMPLATE_NAME
echo "step_1_info() { echo \"My custom step \$1\"; }" >> $TEMPLATE_NAME
echo "step_1_alias() { ALIAS=\"begin\"; }" >> $TEMPLATE_NAME
echo "step_1() {" >> $TEMPLATE_NAME
echo " echo \"Doing something...\"" >> $TEMPLATE_NAME
echo "}" >> $TEMPLATE_NAME
echo >> $TEMPLATE_NAME
echo "VERSION_SEQREV=2" >> $TEMPLATE_NAME
echo ". $0" >> $TEMPLATE_NAME
chmod +x $TEMPLATE_NAME
return 0
}
# Parse alias functions "step_[STEP NUBER]_alias" to create
# back reference variable of schema:
# alias_[ALIAS]=[STEP NUMBER]
parseAlias() {
for ((i=1; i<=${MAX_STEP}; i++)); do
# Check for alias definition
existsFunction step_${i}_alias
if [ $? -ne 0 ] ; then
continue
fi
# Function writes global ALIAS variable
step_${i}_alias
eval 'alias_'$ALIAS'='$i
done
}
# Always display sequencer help and, if available, sequence help
function displayHelp() {
displayHelp() {
helpSequencer
@@ -192,28 +240,38 @@ function displayHelp() {
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
;;
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!"
;;
echo -e "\n Nothing to do!"
;;
esac
exit 1;
else
echo " Step documentation:"
echo " Step (= alias) 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
continue
fi
printf ' Step %3s ' $i
# Display alias if exists
existsFunction step_${i}_alias
if [ $? -eq 0 ] ; then
step_${i}_alias
echo " = $ALIAS"
printf ' : '
else
echo -n " : "
fi
printf ' Step %3s : ' $i
# Display step help only if info function exists
existsFunction step_${i}_info
@@ -227,7 +285,7 @@ function displayHelp() {
}
# showVersion
function showVersion() {
showVersion() {
echo "Sequencer ${VERSION_STRING}"
echo -n "Seq Revision "
if [ ! -z "${VERSION_SEQREV}" ] ; then
@@ -282,6 +340,8 @@ main() {
fi
fi
parseAlias
# check if more than one step is given and select execution mode
if [ ! -z $2 ] ; then
selection $@