Make arkmanager script sourceable

This commit is contained in:
Ben Peddell 2016-06-11 01:58:35 +10:00
parent 91776b27a1
commit 29ffa2f3a3

View File

@ -2114,254 +2114,263 @@ showUsage() {
# Main program
#---------------------
# check the configuration and throw errors or warnings if needed
checkConfig
main(){
# check the configuration and throw errors or warnings if needed
checkConfig
while true; do
options=( )
allinstances=no
instances=( )
args=( )
command="$1"
shift
nrarg=0
while true; do
options=( )
allinstances=no
instances=( )
args=( )
command="$1"
shift
nrarg=0
# Handle global options
case "$command" in
--verbose)
verbose=1
continue
;;
--dots)
progressDisplayType=dots
continue
;;
--spinner)
progressDisplayType=spinner
continue
;;
--cronjob)
inCronJob=true
continue
;;
esac
# get the number of arguments for commands that take arguments
case "$command" in
installmod) nrarg=1; ;;
uninstallmod) nrarg=1; ;;
reinstallmod) nrarg=1; ;;
broadcast) nrarg=1; ;;
rconcmd) nrarg=1; ;;
useconfig) nrarg=1; ;;
install-cronjob) nrarg=1; ;;
remove-cronjob) nrarg=1; ;;
remove-mods) nrarg=1; ;;
esac
# Enumerate the options and arguments
while [ $# -ne 0 ]; do
case "$1" in
--)
shift
break
;;
--args)
nrarg=$#
;;
# Handle global options
case "$command" in
--verbose)
verbose=1
continue
;;
--dots)
progressDisplayType=dots
continue
;;
--spinner)
progressDisplayType=spinner
continue
;;
--*)
options+=( "$1" )
--cronjob)
inCronJob=true
continue
;;
@all)
allinstances=yes
;;
@*)
instances+=( "${1#@}" )
;;
*)
if [ $nrarg -gt 0 ]; then
args+=( "$1" )
(( nrarg-- ))
else
esac
# get the number of arguments for commands that take arguments
case "$command" in
installmod) nrarg=1; ;;
uninstallmod) nrarg=1; ;;
reinstallmod) nrarg=1; ;;
broadcast) nrarg=1; ;;
rconcmd) nrarg=1; ;;
useconfig) nrarg=1; ;;
install-cronjob) nrarg=1; ;;
remove-cronjob) nrarg=1; ;;
remove-mods) nrarg=1; ;;
esac
# Enumerate the options and arguments
while [ $# -ne 0 ]; do
case "$1" in
--)
shift
break
;;
--args)
nrarg=$#
;;
--verbose)
verbose=1
;;
--dots)
progressDisplayType=dots
;;
--spinner)
progressDisplayType=spinner
;;
--*)
options+=( "$1" )
;;
@all)
allinstances=yes
;;
@*)
instances+=( "${1#@}" )
;;
*)
if [ $nrarg -gt 0 ]; then
args+=( "$1" )
(( nrarg-- ))
else
break
fi
;;
esac
shift
done
# handle non-instance separately
case "$command" in
upgrade-tools)
doUpgradeTools
exit
;;
uninstall-tools)
doUninstallTools
exit
;;
useconfig)
defaultinstance="${args[0]}"
continue
;;
remove-mods)
doRemoveMods "${args[0]}"
if [ $# -eq 0 ]; then
exit 0
else
continue
fi
;;
esac
shift
done
# handle non-instance separately
case "$command" in
upgrade-tools)
doUpgradeTools
exit
;;
uninstall-tools)
doUninstallTools
exit
;;
useconfig)
defaultinstance="${args[0]}"
continue
;;
remove-mods)
doRemoveMods "${args[0]}"
if [ $# -eq 0 ]; then
exit 0
else
continue
fi
;;
list-instances)
doListAllInstances "${options[@]}"
exit
;;
--version)
echo "Version: ${arkstVersion}"
echo "Channel: ${arkstChannel}"
if [ -n "${arkstCommit}" ]; then
echo "Commit: ${arkstCommit:0:7}"
fi
blobsize="$(sed "s@^arkstCommit=.*@arkstCommit=''@" "$0" | wc -c)"
echo "Blob SHA: $( (echo -ne "blob ${blobsize}\0"; sed "s@^arkstCommit=.*@arkstCommit=''@" "$0") | sha1sum | cut -d' ' -f1)"
exit 1
;;
-h|--help)
showUsage
exit 1
;;
"")
echo "arkmanager v${arkstVersion}: no command specified"
showUsage
exit 1
;;
esac
# Handle no instances being specified
if [[ "${#instances[@]}" == 0 && "$allinstances" == "no" ]]; then
if [ -n "$defaultinstance" ]; then
instances=( "$defaultinstance" )
else
echo "No instances supplied for command ${command} ${options[*]} ${args[*]}"
read -p "Do you wish to run this command for all instances?" -n 1 -r
echo
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
allinstances=yes
else
list-instances)
doListAllInstances "${options[@]}"
exit
;;
--version)
echo "Version: ${arkstVersion}"
echo "Channel: ${arkstChannel}"
if [ -n "${arkstCommit}" ]; then
echo "Commit: ${arkstCommit:0:7}"
fi
blobsize="$(sed "s@^arkstCommit=.*@arkstCommit=''@" "$0" | wc -c)"
echo "Blob SHA: $( (echo -ne "blob ${blobsize}\0"; sed "s@^arkstCommit=.*@arkstCommit=''@" "$0") | sha1sum | cut -d' ' -f1)"
exit 1
;;
-h|--help)
showUsage
exit 1
;;
"")
echo "arkmanager v${arkstVersion}: no command specified"
showUsage
exit 1
;;
esac
# Handle no instances being specified
if [[ "${#instances[@]}" == 0 && "$allinstances" == "no" ]]; then
if [ -n "$defaultinstance" ]; then
instances=( "$defaultinstance" )
else
echo "No instances supplied for command ${command} ${options[*]} ${args[*]}"
read -p "Do you wish to run this command for all instances?" -n 1 -r
echo
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
allinstances=yes
else
exit 1
fi
fi
fi
fi
# Handle all instances being requested
if [[ "$allinstances" == "yes" ]]; then
instances=( $(getAllInstanceNames) )
fi
# Handle all instances being requested
if [[ "$allinstances" == "yes" ]]; then
instances=( $(getAllInstanceNames) )
fi
# Run the command for each instance requested
for instance in "${instances[@]}"; do
(
echo "Running command '${command}' for instance '${instance}'"
useConfig "$instance"
checkConfig
case "$command" in
run)
doRun
;;
start)
doStart "${options[@]}"
;;
stop)
doStop shutdown "${options[@]}"
;;
restart)
doStop restart "${options[@]}"
echo "`timestamp`: stop" >> "$logdir/$arkmanagerLog"
;;
cancelshutdown)
doCancelShutdown "${options[@]}"
;;
install)
doInstall
;;
update)
doUpdate "${options[@]}"
;;
checkupdate)
checkForUpdate
;;
installmod)
doInstallMod "${args[@]}"
;;
uninstallmod)
doUninstallMod "${args[@]}"
;;
reinstallmod)
doUninstallMod "${args[@]}"
doInstallMod "${args[@]}"
;;
backup)
doBackup
;;
broadcast)
doBroadcast "${args[@]}"
;;
saveworld)
doSaveWorld
;;
rconcmd)
rconcmd "${args[@]}"
;;
status)
printStatus
;;
install-cronjob)
doInstallCronJob "${args[@]}" "${options[@]}"
;;
remove-cronjob)
doRemoveCronJob "${args[@]}"
;;
*)
echo -n "arkmanager v${arkstVersion}: unknown command '$command' specified"
showUsage
exit 255
;;
esac
)
laststatus=$?
if [ $laststatus -eq 255 ]; then
exit 1
elif [ $laststatus -ne 0 ]; then
status=$laststatus
fi
done
# Perform the restart portion of the restart command
if [[ "$command" == "restart" ]]; then
sleep 1
# Run the command for each instance requested
for instance in "${instances[@]}"; do
(
echo "Running command '${command}' for instance '${instance}'"
useConfig "$instance"
doStart "${options[@]}"
echo "`timestamp`: start" >> "$logdir/$arkmanagerLog"
echo "`timestamp`: restart" >> "$logdir/$arkmanagerLog"
checkConfig
case "$command" in
run)
doRun
;;
start)
doStart "${options[@]}"
;;
stop)
doStop shutdown "${options[@]}"
;;
restart)
doStop restart "${options[@]}"
echo "`timestamp`: stop" >> "$logdir/$arkmanagerLog"
;;
cancelshutdown)
doCancelShutdown "${options[@]}"
;;
install)
doInstall
;;
update)
doUpdate "${options[@]}"
;;
checkupdate)
checkForUpdate
;;
installmod)
doInstallMod "${args[@]}"
;;
uninstallmod)
doUninstallMod "${args[@]}"
;;
reinstallmod)
doUninstallMod "${args[@]}"
doInstallMod "${args[@]}"
;;
backup)
doBackup
;;
broadcast)
doBroadcast "${args[@]}"
;;
saveworld)
doSaveWorld
;;
rconcmd)
rconcmd "${args[@]}"
;;
status)
printStatus
;;
install-cronjob)
doInstallCronJob "${args[@]}" "${options[@]}"
;;
remove-cronjob)
doRemoveCronJob "${args[@]}"
;;
*)
echo -n "arkmanager v${arkstVersion}: unknown command '$command' specified"
showUsage
exit 255
;;
esac
)
laststatus=$?
if [ $laststatus -eq 255 ]; then
exit 1
elif [ $laststatus -ne 0 ]; then
status=$laststatus
fi
done
fi
if [ $# -eq 0 ]; then
break
fi
done
# Perform the restart portion of the restart command
if [[ "$command" == "restart" ]]; then
sleep 1
for instance in "${instances[@]}"; do
(
useConfig "$instance"
doStart "${options[@]}"
echo "`timestamp`: start" >> "$logdir/$arkmanagerLog"
echo "`timestamp`: restart" >> "$logdir/$arkmanagerLog"
)
done
fi
if [ $# -eq 0 ]; then
break
fi
done
exit $status
}
# Only execute main function if script is not being sourced
# by another script
if [[ "$(caller)" =~ ^0 ]]; then
main "$@"
fi
exit $status