diff --git a/seqTemplate.sh b/seqTemplate.sh index 839f295..b26394e 100644 --- a/seqTemplate.sh +++ b/seqTemplate.sh @@ -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 diff --git a/seqs/gitea.sh b/seqs/gitea.sh index dfec676..0aff5b0 100755 --- a/seqs/gitea.sh +++ b/seqs/gitea.sh @@ -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 } @@ -85,7 +85,7 @@ step_10() { mysql -u root -e 'CREATE DATABASE '$mysqlDatabase' CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;' saveReturn $? endReturn - + echo "Existing mysql user:" mysql -u root -e 'SELECT User, Host FROM mysql.user;' echo -en "Enter mysql user name: " @@ -98,7 +98,7 @@ step_10() { mysql -u root -e 'CREATE USER '"'"$mysqlUser"'"'@'"'"'localhost'"'"' IDENTIFIED BY '"'"$mysqlPass"'"';' saveReturn $? endReturn - + mysql -u root -e 'GRANT ALL PRIVILEGES ON '$mysqlDatabase'.* TO '"'"$mysqlUser"'"'@'"'"'localhost'"'"';' saveReturn $? endReturn diff --git a/seqs/librenms.sh b/seqs/librenms.sh index ba23ce2..8653a45 100755 --- a/seqs/librenms.sh +++ b/seqs/librenms.sh @@ -60,7 +60,7 @@ step_10() { mysql -u root -e 'CREATE DATABASE '$mysqlDatabase' CHARACTER SET utf8 COLLATE utf8_unicode_ci;' saveReturn $? endReturn - + echo "Existing mysql user:" mysql -u root -e 'SELECT User, Host FROM mysql.user;' echo -en "Enter mysql user name: " @@ -73,7 +73,7 @@ step_10() { mysql -u root -e 'CREATE USER '"'"$mysqlUser"'"'@'"'"'localhost'"'"' IDENTIFIED BY '"'"$mysqlPass"'"';' saveReturn $? endReturn - + mysql -u root -e 'GRANT ALL PRIVILEGES ON '$mysqlDatabase'.* TO '"'"$mysqlUser"'"'@'"'"'localhost'"'"';' saveReturn $? endReturn @@ -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 diff --git a/sequencer/sequencer.sh b/sequencer/sequencer.sh index e3fdb84..badc067 100755 --- a/sequencer/sequencer.sh +++ b/sequencer/sequencer.sh @@ -6,7 +6,7 @@ ## Version information -VERSION_REV=1 +VERSION_REV=2 VERSION_MAJOR=0 VERSION_MINOR=0 @@ -15,31 +15,32 @@ 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 - + if [ ! -z "$2" ] ; then errorText=$2 fi @@ -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,10 +91,10 @@ function endReturn() { # execute [-q] # -q: don't stop and don't report step functions which cannot be found # execute given step_ function -function execute() { +execute() { local NOTFOUND=0 local NOREPORT=0 - + if [ $1 == "-q" ] ; then NOREPORT=1 shift @@ -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 +# 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 # (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 # 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,19 +197,39 @@ 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 # 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 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 - + 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 existsFunction step_${i}_info if [ $? -eq 0 ] ; then @@ -227,7 +285,7 @@ function displayHelp() { } # showVersion -function showVersion() { +showVersion() { echo "Sequencer ${VERSION_STRING}" echo -n "Seq Revision " if [ ! -z "${VERSION_SEQREV}" ] ; then @@ -281,7 +339,9 @@ main() { exit 1; fi fi - + + parseAlias + # check if more than one step is given and select execution mode if [ ! -z $2 ] ; then selection $@