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: ## Sequencer interface:
## - step_[1 - 255]_info [STEP NUMBER] ## - step_[1 - 255]_info [STEP NUMBER]
## functions providing short header for help and user interaction ## 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] ## - step_[1 - 255] [STEP NUMBER]
## functions performing custom operations ## performing custom shell operations
## ##
step_1_info() { echo "Step $1 header"; } step_1_info() { echo "Step $1 header"; }
step_1_alias() { ALIAS="begin"; }
step_1() { step_1() {
cat .nofile cat .nofile
saveReturn $? saveReturn $?
@@ -32,15 +35,19 @@ step_10() {
} }
step_11_info() { echo "Step $1 header"; } step_11_info() { echo "Step $1 header"; }
step_11_alias() { ALIAS="elf"; }
step_11() { step_11() {
echo elf echo elf
} }
# Sequence Revision # Sequence Revision
VERSION_SEQREV=1 VERSION_SEQREV=2
# Workaround when called from different directory # Workaround when called from different directory
# Not needed when path to sequencer is absolut # 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 )" DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >>/dev/null 2>&1 && pwd )"
# Path to local sequencer.sh script # Path to local sequencer.sh script
. ${DIR}/./sequencer/sequencer.sh . ${DIR}/./sequencer/sequencer.sh

View File

@@ -126,6 +126,7 @@ step_40() {
} }
step_42_info() { echo "Fix librenms permission"; } step_42_info() { echo "Fix librenms permission"; }
step_42_alias() { ALIAS="repair"; }
step_42() { step_42() {
chown -R librenms:librenms /opt/librenms chown -R librenms:librenms /opt/librenms
setfacl -d -m g::rwx /opt/librenms/bootstrap/cache /opt/librenms/storage /opt/librenms/logs /opt/librenms/rrd 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 information
VERSION_REV=1 VERSION_REV=2
VERSION_MAJOR=0 VERSION_MAJOR=0
VERSION_MINOR=0 VERSION_MINOR=0
@@ -15,28 +15,29 @@ VERSION_MINOR=0
QUIET=0 QUIET=0
ERNO=0 ERNO=0
MAX_STEP=255 MAX_STEP=255
ALIAS=
TEMPLATE_NAME=seqTemplateExample.sh TEMPLATE_NAME=seqTemplateExample.sh
VERSION_STRING="${VERSION_REV}.${VERSION_MAJOR}.${VERSION_MINOR}" VERSION_STRING="${VERSION_REV}.${VERSION_MAJOR}.${VERSION_MINOR}"
function helpSequencer() { helpSequencer() {
echo "Usage: ${0##*/} [Options] [Step Number(s)]" echo "Usage: ${0##*/} [OPTIONS] [STEP NUMBER(s) or ALIAS]"
echo echo
echo " [Options]" echo " [OPTIONS]"
echo " --help, -h : Display help" echo " --help, -h : Display help"
echo " --quiet, -q : Don't ask for permission to execute steps" echo " --quiet, -q : Don't ask for permission to execute steps"
echo " If called without starting step number, only this help is shown" echo " If called without starting step number, only this help is shown"
echo " --version,-v : Display version of sequencer and revision of sequence" echo " --version,-v : Display version of sequencer and revision of sequence"
echo echo
echo " [Step Number(s) 1-${MAX_STEP}]" echo " [STEP NUMBER(s) 1-${MAX_STEP} or ALIAS]"
echo " Single step number : starting point of process" echo " Single STEP or ALIAS : starting point of process"
echo " Multiple step numbers : execute only given steps" echo " Multiple STEPS or ALIAS : execute only given steps"
echo " execute only one step with using special step 0" echo " execute only one step with using special step 0"
echo " ( e.g. only execute step 4: $0 4 0 )" echo " ( e.g. only execute step 4: $0 4 0 )"
} }
# endCheckEmpty [VariableName] [DESCRIPTION] # endCheckEmpty [VariableName] [DESCRIPTION]
# DESCRIPTION : Optional text for error # DESCRIPTION : Optional text for error
function endCheckEmpty() { endCheckEmpty() {
local errorText=$1 local errorText=$1
eval 'local ref=$'$1 eval 'local ref=$'$1
@@ -49,14 +50,13 @@ function endCheckEmpty() {
fi fi
} }
function existsFunction() { existsFunction() {
local NOTFOUND=0 local NOTFOUND=0
eval 'local ref=$'$1
declare -F $1 &>>/dev/null || NOTFOUND=1 declare -F $1 &>>/dev/null || NOTFOUND=1
return $NOTFOUND return $NOTFOUND
} }
function saveReturn() { saveReturn() {
if [ $1 -ne 0 ] ; then if [ $1 -ne 0 ] ; then
ERNO=$1 ERNO=$1
fi fi
@@ -64,7 +64,7 @@ function saveReturn() {
# endReturn [-f] # endReturn [-f]
# -f : force exit with $ERNO without user input # -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 if [[ ( $ERNO -ne 0 && $QUIET -ne 0 ) || ( $ERNO -ne 0 && ! -z $1 && $1 == "-f" ) ]] ; then
echo echo
echo -e "[Error] Return value $ERNO detected.\nAborting installation." echo -e "[Error] Return value $ERNO detected.\nAborting installation."
@@ -91,7 +91,7 @@ function endReturn() {
# execute [-q] <Step Number> # execute [-q] <Step Number>
# -q: don't stop and don't report step functions which cannot be found # -q: don't stop and don't report step functions which cannot be found
# execute given step_<Step Number> function # execute given step_<Step Number> function
function execute() { execute() {
local NOTFOUND=0 local NOTFOUND=0
local NOREPORT=0 local NOREPORT=0
@@ -133,11 +133,36 @@ function execute() {
fi 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> # continous <Starting Step Number>
# (max $MAX_STEP) # (max $MAX_STEP)
# execute installation continously from given starting step # execute installation continously from given starting step
function continous() { continous() {
for ((i=$1; i<=${MAX_STEP}; i++)); do local step=0
checkStep $1
step=$?
if [[ $step == 0 ]] ; then
return 1
fi
for ((i=$step; i<=${MAX_STEP}; i++)); do
execute -q $i execute -q $i
local res=$? local res=$?
if [ $res -ne 0 ] ; then if [ $res -ne 0 ] ; then
@@ -149,19 +174,22 @@ function continous() {
# selection <Step Number List (separated by space)> # selection <Step Number List (separated by space)>
# execute given step list # execute given step list
# e.g.: selection -q 1 4 12 # e.g.: selection -q 1 4 12
function selection() { selection() {
local step=0
for i in $@ ; do for i in $@ ; do
checkStep $i
step=$?
# stop on step 0 # stop on step 0
if [ $i -eq 0 ] ; then if [ $step -eq 0 ] ; then
break break
else else
execute $i execute $step
fi fi
done done
} }
# Creating a minimal step definition template # Creating a minimal step definition template
function createTemplate() { createTemplate() {
if [ -f $TEMPLATE_NAME ] ; then if [ -f $TEMPLATE_NAME ] ; then
return 1 return 1
fi fi
@@ -169,18 +197,38 @@ function createTemplate() {
echo >> $TEMPLATE_NAME echo >> $TEMPLATE_NAME
echo "# Step 1 is mandatory" >> $TEMPLATE_NAME echo "# Step 1 is mandatory" >> $TEMPLATE_NAME
echo "step_1_info() { echo \"My custom step \$1\"; }" >> $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 "step_1() {" >> $TEMPLATE_NAME
echo " echo \"Doing something...\"" >> $TEMPLATE_NAME echo " echo \"Doing something...\"" >> $TEMPLATE_NAME
echo "}" >> $TEMPLATE_NAME echo "}" >> $TEMPLATE_NAME
echo >> $TEMPLATE_NAME echo >> $TEMPLATE_NAME
echo "VERSION_SEQREV=2" >> $TEMPLATE_NAME
echo ". $0" >> $TEMPLATE_NAME echo ". $0" >> $TEMPLATE_NAME
chmod +x $TEMPLATE_NAME chmod +x $TEMPLATE_NAME
return 0 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 # Always display sequencer help and, if available, sequence help
function displayHelp() { displayHelp() {
helpSequencer helpSequencer
@@ -205,7 +253,7 @@ function displayHelp() {
esac esac
exit 1; exit 1;
else else
echo " Step documentation:" echo " Step (= alias) documentation:"
for ((i=1; i<=${MAX_STEP}; i++)); do for ((i=1; i<=${MAX_STEP}; i++)); do
# Display step reference in help if step function exists # Display step reference in help if step function exists
@@ -213,7 +261,17 @@ function displayHelp() {
if [ $? -ne 0 ] ; then if [ $? -ne 0 ] ; then
continue continue
fi fi
printf ' Step %3s : ' $i 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
# Display step help only if info function exists # Display step help only if info function exists
existsFunction step_${i}_info existsFunction step_${i}_info
@@ -227,7 +285,7 @@ function displayHelp() {
} }
# showVersion # showVersion
function showVersion() { showVersion() {
echo "Sequencer ${VERSION_STRING}" echo "Sequencer ${VERSION_STRING}"
echo -n "Seq Revision " echo -n "Seq Revision "
if [ ! -z "${VERSION_SEQREV}" ] ; then if [ ! -z "${VERSION_SEQREV}" ] ; then
@@ -282,6 +340,8 @@ main() {
fi fi
fi fi
parseAlias
# check if more than one step is given and select execution mode # check if more than one step is given and select execution mode
if [ ! -z $2 ] ; then if [ ! -z $2 ] ; then
selection $@ selection $@