mirror of
https://github.com/eliasstepanik/ark-ac-server-tools.git
synced 2026-01-25 07:48:28 +00:00
Make arkmanager script sourceable
This commit is contained in:
parent
91776b27a1
commit
29ffa2f3a3
457
tools/arkmanager
457
tools/arkmanager
@ -2114,254 +2114,263 @@ showUsage() {
|
|||||||
# Main program
|
# Main program
|
||||||
#---------------------
|
#---------------------
|
||||||
|
|
||||||
# check the configuration and throw errors or warnings if needed
|
main(){
|
||||||
checkConfig
|
# check the configuration and throw errors or warnings if needed
|
||||||
|
checkConfig
|
||||||
|
|
||||||
while true; do
|
while true; do
|
||||||
options=( )
|
options=( )
|
||||||
allinstances=no
|
allinstances=no
|
||||||
instances=( )
|
instances=( )
|
||||||
args=( )
|
args=( )
|
||||||
command="$1"
|
command="$1"
|
||||||
shift
|
shift
|
||||||
nrarg=0
|
nrarg=0
|
||||||
|
|
||||||
# Handle global options
|
# Handle global options
|
||||||
case "$command" in
|
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=$#
|
|
||||||
;;
|
|
||||||
--verbose)
|
--verbose)
|
||||||
verbose=1
|
verbose=1
|
||||||
|
continue
|
||||||
;;
|
;;
|
||||||
--dots)
|
--dots)
|
||||||
progressDisplayType=dots
|
progressDisplayType=dots
|
||||||
|
continue
|
||||||
;;
|
;;
|
||||||
--spinner)
|
--spinner)
|
||||||
progressDisplayType=spinner
|
progressDisplayType=spinner
|
||||||
|
continue
|
||||||
;;
|
;;
|
||||||
--*)
|
--cronjob)
|
||||||
options+=( "$1" )
|
inCronJob=true
|
||||||
|
continue
|
||||||
;;
|
;;
|
||||||
@all)
|
esac
|
||||||
allinstances=yes
|
|
||||||
;;
|
# get the number of arguments for commands that take arguments
|
||||||
@*)
|
case "$command" in
|
||||||
instances+=( "${1#@}" )
|
installmod) nrarg=1; ;;
|
||||||
;;
|
uninstallmod) nrarg=1; ;;
|
||||||
*)
|
reinstallmod) nrarg=1; ;;
|
||||||
if [ $nrarg -gt 0 ]; then
|
broadcast) nrarg=1; ;;
|
||||||
args+=( "$1" )
|
rconcmd) nrarg=1; ;;
|
||||||
(( nrarg-- ))
|
useconfig) nrarg=1; ;;
|
||||||
else
|
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
|
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
|
fi
|
||||||
;;
|
;;
|
||||||
esac
|
list-instances)
|
||||||
shift
|
doListAllInstances "${options[@]}"
|
||||||
done
|
exit
|
||||||
|
;;
|
||||||
# handle non-instance separately
|
--version)
|
||||||
case "$command" in
|
echo "Version: ${arkstVersion}"
|
||||||
upgrade-tools)
|
echo "Channel: ${arkstChannel}"
|
||||||
doUpgradeTools
|
if [ -n "${arkstCommit}" ]; then
|
||||||
exit
|
echo "Commit: ${arkstCommit:0:7}"
|
||||||
;;
|
fi
|
||||||
uninstall-tools)
|
blobsize="$(sed "s@^arkstCommit=.*@arkstCommit=''@" "$0" | wc -c)"
|
||||||
doUninstallTools
|
echo "Blob SHA: $( (echo -ne "blob ${blobsize}\0"; sed "s@^arkstCommit=.*@arkstCommit=''@" "$0") | sha1sum | cut -d' ' -f1)"
|
||||||
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
|
|
||||||
exit 1
|
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
|
fi
|
||||||
fi
|
|
||||||
|
|
||||||
# Handle all instances being requested
|
# Handle all instances being requested
|
||||||
if [[ "$allinstances" == "yes" ]]; then
|
if [[ "$allinstances" == "yes" ]]; then
|
||||||
instances=( $(getAllInstanceNames) )
|
instances=( $(getAllInstanceNames) )
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Run the command for each instance requested
|
# 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
|
|
||||||
for instance in "${instances[@]}"; do
|
for instance in "${instances[@]}"; do
|
||||||
(
|
(
|
||||||
|
echo "Running command '${command}' for instance '${instance}'"
|
||||||
useConfig "$instance"
|
useConfig "$instance"
|
||||||
doStart "${options[@]}"
|
checkConfig
|
||||||
echo "`timestamp`: start" >> "$logdir/$arkmanagerLog"
|
|
||||||
echo "`timestamp`: restart" >> "$logdir/$arkmanagerLog"
|
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
|
done
|
||||||
fi
|
|
||||||
|
|
||||||
if [ $# -eq 0 ]; then
|
# Perform the restart portion of the restart command
|
||||||
break
|
if [[ "$command" == "restart" ]]; then
|
||||||
fi
|
sleep 1
|
||||||
done
|
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
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user