Separating sequence controller from step definition

This commit is contained in:
2019-04-02 22:36:56 +02:00
parent ad724ddc3d
commit cf1bfc527f
4 changed files with 123 additions and 47 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/stepTemplateExample.sh

View File

@@ -1,3 +1,12 @@
# shell_sequencer
Shell script as wrapper for sequential tasks.
Shell script as wrapper for sequential tasks, such as complex server installations or recurring maintenance tasks.
Step definition is separated from the sequence control. The step definition just needs to "include" the sequence controller at the end.
## sequencer.sh
Main sequencer script to be included at the end of a step definition script.
[...]
. ./sequencer.sh

View File

@@ -1,38 +1,16 @@
#!/bin/bash
# Automatic sequence control script
## Sequencer script is doing nothing on its own. It is included by a step definition
## script which uses the sequencer to provide sequencial operations with or without
## user interaction (see template.sh)
#
## Start of customization part
## Start of generic script part
function step_1 {
echo -e "Eins"
cat .nofile
saveReturn $?
}
QUIET=0
ERNO=0
TEMPLATE_NAME=stepTemplateExample.sh
function step_2 {
echo -e "Zwei"
endReturn
echo zwo
}
function step_3 {
echo -e "Drei"
echo drei
}
function step_10 {
echo -e "Zehn"
echo zehn
}
function step_11 {
echo -e"Elf"
echo elf
}
help() {
function helpSequencer() {
echo "Usage: ${0##*/} [Options] [Step Number(s)]"
echo
echo " [Options]"
@@ -44,22 +22,8 @@ help() {
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 Documentation"
echo " 1: Step with failure and saveReturn"
echo " 2: Step with endReturn at the beginning"
echo " 3: Regular step without return check (last step when starting with 1 or 2)"
echo
echo " 10: Regular step without return check (staring point for separate sequence)"
echo " 11: Regular step without return check (last step of sequence starting with 10)"
}
#
## Start of generic script part
## (do not change)
QUIET=0
ERNO=0
# endCheckEmpty [VariableName] [DESCRIPTION]
# DESCRIPTION : Optional text for error
function endCheckEmpty() {
@@ -175,6 +139,59 @@ function selection() {
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 "function step_1 {" >> $TEMPLATE_NAME
echo " echo \"My custom step one\"" >> $TEMPLATE_NAME
echo "}" >> $TEMPLATE_NAME
echo >> $TEMPLATE_NAME
echo "help() {" >> $TEMPLATE_NAME
echo " echo \" Step Documentation\"" >> $TEMPLATE_NAME
echo " echo \" 1: My custom step\"" >> $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, definition script help
function displayHelp() {
local NOTFOUND=0
helpSequencer
# check if help function exists
declare -F help &>>/dev/null || NOTFOUND=1
if [ $NOTFOUND -eq 1 ] ; then
echo -e "\n It seems ${0##*/} was called directly."
echo -e " Please create a step definition script first.\n"
read -p " Create a step definition 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
help
fi
}
main() {
# detect if option quiet is available
if [ ! -z "$1" ] && [ $1 == "-q" ] ; then
@@ -184,7 +201,7 @@ main() {
# display help
if [ -z "$1" ] ; then
help
displayHelp
START=1
if [ $QUIET -eq 1 ] ; then
exit 1;

49
stepTemplate.sh Executable file
View File

@@ -0,0 +1,49 @@
#!/bin/bash
##
## Sequencer interface:
## - step_[1 - 255] functions performing custom operations
## - help() function describing functionality
##
function step_1 {
echo -e "Eins"
cat .nofile
saveReturn $?
}
function step_2 {
echo -e "Zwei"
endReturn
echo zwo
}
function step_3 {
echo -e "Drei"
echo drei
}
function step_10 {
echo -e "Zehn"
echo zehn
}
function step_11 {
echo -e"Elf"
echo elf
}
help() {
echo " Step Documentation"
echo " 1: Step with failure and saveReturn"
echo " 2: Step with endReturn at the beginning"
echo " 3: Regular step without return check (last step when starting with 1 or 2)"
echo
echo " 10: Regular step without return check (staring point for separate sequence)"
echo " 11: Regular step without return check (last step of sequence starting with 10)"
}
#
## Path to local sequencer.sh script
. ./sequencer.sh