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

@@ -24,13 +24,13 @@ step_2() {
step_3_info() { echo "Adding user for $toolName (git:git)"; } step_3_info() { echo "Adding user for $toolName (git:git)"; }
step_3() { step_3() {
adduser \ adduser \
--system \ --system \
--shell /bin/bash \ --shell /bin/bash \
--gecos 'Git Version Control' \ --gecos 'Git Version Control' \
--group \ --group \
--disabled-password \ --disabled-password \
--home /home/git \ --home /home/git \
git git
saveReturn $? saveReturn $?
endReturn endReturn
} }
@@ -85,7 +85,7 @@ step_10() {
mysql -u root -e 'CREATE DATABASE '$mysqlDatabase' CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;' mysql -u root -e 'CREATE DATABASE '$mysqlDatabase' CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;'
saveReturn $? saveReturn $?
endReturn endReturn
echo "Existing mysql user:" echo "Existing mysql user:"
mysql -u root -e 'SELECT User, Host FROM mysql.user;' mysql -u root -e 'SELECT User, Host FROM mysql.user;'
echo -en "Enter mysql user name: " echo -en "Enter mysql user name: "
@@ -98,7 +98,7 @@ step_10() {
mysql -u root -e 'CREATE USER '"'"$mysqlUser"'"'@'"'"'localhost'"'"' IDENTIFIED BY '"'"$mysqlPass"'"';' mysql -u root -e 'CREATE USER '"'"$mysqlUser"'"'@'"'"'localhost'"'"' IDENTIFIED BY '"'"$mysqlPass"'"';'
saveReturn $? saveReturn $?
endReturn endReturn
mysql -u root -e 'GRANT ALL PRIVILEGES ON '$mysqlDatabase'.* TO '"'"$mysqlUser"'"'@'"'"'localhost'"'"';' mysql -u root -e 'GRANT ALL PRIVILEGES ON '$mysqlDatabase'.* TO '"'"$mysqlUser"'"'@'"'"'localhost'"'"';'
saveReturn $? saveReturn $?
endReturn endReturn

View File

@@ -60,7 +60,7 @@ step_10() {
mysql -u root -e 'CREATE DATABASE '$mysqlDatabase' CHARACTER SET utf8 COLLATE utf8_unicode_ci;' mysql -u root -e 'CREATE DATABASE '$mysqlDatabase' CHARACTER SET utf8 COLLATE utf8_unicode_ci;'
saveReturn $? saveReturn $?
endReturn endReturn
echo "Existing mysql user:" echo "Existing mysql user:"
mysql -u root -e 'SELECT User, Host FROM mysql.user;' mysql -u root -e 'SELECT User, Host FROM mysql.user;'
echo -en "Enter mysql user name: " echo -en "Enter mysql user name: "
@@ -73,7 +73,7 @@ step_10() {
mysql -u root -e 'CREATE USER '"'"$mysqlUser"'"'@'"'"'localhost'"'"' IDENTIFIED BY '"'"$mysqlPass"'"';' mysql -u root -e 'CREATE USER '"'"$mysqlUser"'"'@'"'"'localhost'"'"' IDENTIFIED BY '"'"$mysqlPass"'"';'
saveReturn $? saveReturn $?
endReturn endReturn
mysql -u root -e 'GRANT ALL PRIVILEGES ON '$mysqlDatabase'.* TO '"'"$mysqlUser"'"'@'"'"'localhost'"'"';' mysql -u root -e 'GRANT ALL PRIVILEGES ON '$mysqlDatabase'.* TO '"'"$mysqlUser"'"'@'"'"'localhost'"'"';'
saveReturn $? saveReturn $?
endReturn endReturn
@@ -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,31 +15,32 @@ 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
if [ ! -z "$2" ] ; then if [ ! -z "$2" ] ; then
errorText=$2 errorText=$2
fi fi
@@ -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."
@@ -76,14 +76,14 @@ function endReturn() {
read -p "End installation: y(default)/n? " answer read -p "End installation: y(default)/n? " answer
case $answer in case $answer in
[nN]) [nN])
echo echo
echo Continuing installation... echo Continuing installation...
;; ;;
*) *)
echo echo
echo Installation aborted echo Installation aborted
exit $ERNO; exit $ERNO;
;; ;;
esac esac
fi fi
} }
@@ -91,10 +91,10 @@ 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
if [ $1 == "-q" ] ; then if [ $1 == "-q" ] ; then
NOREPORT=1 NOREPORT=1
shift shift
@@ -121,23 +121,48 @@ function execute() {
read -p "Start: y/n(default)? " answer read -p "Start: y/n(default)? " answer
case $answer in case $answer in
[yY]) [yY])
step_$1 $1 step_$1 $1
;; ;;
*) *)
echo Aborting installation at step $1 echo Aborting installation at step $1
exit 1; exit 1;
;; ;;
esac esac
else else
step_$1 $1 step_$1 $1
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
# stop on step 0 checkStep $i
if [ $i -eq 0 ] ; then step=$?
break # stop on step 0
else if [ $step -eq 0 ] ; then
execute $i break
fi else
execute $step
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,19 +197,39 @@ 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
# check if step definition exists by looking for step_1() # check if step definition exists by looking for step_1()
@@ -192,29 +240,39 @@ function displayHelp() {
read -p " Create a template now? y/n(default)? " answer read -p " Create a template now? y/n(default)? " answer
case $answer in case $answer in
[yY]) [yY])
createTemplate createTemplate
if [ $? -eq 0 ] ; then if [ $? -eq 0 ] ; then
echo -e "\n $TEMPLATE_NAME created." echo -e "\n $TEMPLATE_NAME created."
else else
echo -e "\n $TEMPLATE_NAME exists...Nothing to do!" echo -e "\n $TEMPLATE_NAME exists...Nothing to do!"
fi fi
;; ;;
*) *)
echo -e "\n Nothing to do!" echo -e "\n Nothing to do!"
;; ;;
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
existsFunction step_${i} existsFunction step_${i}
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
if [ $? -eq 0 ] ; then if [ $? -eq 0 ] ; then
@@ -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
@@ -281,7 +339,9 @@ main() {
exit 1; exit 1;
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 $@