From 7cd3d9e15f6bac658fce03eee48ab36aed767841 Mon Sep 17 00:00:00 2001 From: Ben Peddell Date: Tue, 17 Nov 2015 00:29:06 +1000 Subject: [PATCH] Add per-command multi-instance support --- tools/arkmanager | 267 ++++++++++++++++++++++++++++------------------- 1 file changed, 159 insertions(+), 108 deletions(-) diff --git a/tools/arkmanager b/tools/arkmanager index 49db4b7..10be771 100755 --- a/tools/arkmanager +++ b/tools/arkmanager @@ -1484,6 +1484,48 @@ useConfig() { source "$1" } +showUsage() { + echo -e "Usage: arkmanager [Commands]\n" + echo "Commands can be followed by one or more @instance arguments" + echo "The special '@all' instance selects all instances" + echo "Commands may also be followed by zero or more --options" + echo + echo "Commands that take no instances:" + echo "Command Description" + echo "upgrade-tools Check for a new ARK Server Tools version and upgrades it if needed" + echo "uninstall-tools Uninstall the ARK Server Tools" + echo "useconfig Sets the default instance for the commands that follow" + echo "--help Show this help" + echo "--version Show the version info of ARK Server Tools" + echo + echo "Commands that take one or more instances:" + echo "Command Description" + echo "backup Saves a backup of your server inside the backup directory" + echo "broadcast Sends a message to all users connected to server" + echo "saveworld Saves the game world to disk" + echo "rconcmd Execute RCON command on server" + echo "checkupdate Check for a new ARK server version" + echo "install Install the ARK server files from steamcmd" + echo "installmod Installs a mod from the Steam workshop" + echo "restart Stops the server and then starts it" + echo "run Runs the server without daemonizing" + echo "start Starts the server" + echo "stop Stops the server" + echo "status Returns the status of the current ARK server instance" + echo "update Check for a new ARK server version, if needed, stops the server, updates it, and starts it again" + echo + echo "Update command takes the below options:" + echo " --force Apply update without checking the current version" + echo " --safe Wait for server to perform world save and update." + echo " --warn Warn players before updating server" + echo " --validate Validates all ARK server files" + echo " --saveworld Saves world before update" + echo " --update-mods Updates installed and requested mods" + echo " --backup Takes a backup of the save files before updating" + echo " --downloadonly Download the mod and/or server update without applying it" + echo " Requires arkStagingDir be set to a staging directory on the same filesystem as the server" +} + #--------------------- # Main program #--------------------- @@ -1493,6 +1535,8 @@ checkConfig while true; do options=( ) + allinstances=no + instances=( ) args=( ) command="$1" shift @@ -1519,6 +1563,12 @@ while true; do --*) options+=( "$1" ) ;; + @all) + allinstances=yes + ;; + @*) + instances+=( "${1#@}" ) + ;; *) if [ $nrarg -gt 0 ]; then args+=( "$1" ) @@ -1531,75 +1581,19 @@ while true; do shift done + # handle non-instance separately case "$command" in - run) - doRun - ;; - start) - if [ " ${options[*]} " =~ " --all " ]; then - doStartAll - else - doStart - fi - ;; - stop) - if [ " ${options[*]} " =~ " --all " ]; then - doStopAll - else - doStop stop "${options[@]}" - fi - ;; - restart) - if [ " ${options[*]} " =~ " --all " ]; then - doStopAll - else - doStop restart "${options[@]}" - fi - echo "`timestamp`: stop" >> "$logdir/$arkmanagerLog" - sleep 1 - if [ " ${options[*]} " =~ " --all " ]; then - doStartAll - else - doStart - fi - echo "`timestamp`: start" >> "$logdir/$arkmanagerLog" - echo "`timestamp`: restart" >> "$logdir/$arkmanagerLog" - ;; - install) - doInstall - ;; - update) - doUpdate "${options[@]}" - ;; - checkupdate) - checkForUpdate - ;; - installmod) - doInstallMod "${args[@]}" - ;; - backup) - doBackup - ;; - broadcast) - doBroadcast "${args[@]}" - ;; - saveworld) - doSaveWorld - ;; - rconcmd) - rconcmd "${args[@]}" - ;; - status) - printStatus - ;; upgrade-tools) doUpgradeTools + exit ;; uninstall-tools) doUninstallTools + exit ;; useconfig) - useConfig "${args[0]}" + defaultinstance="${args[0]}" + continue ;; --version) echo "Version: ${arkstVersion}" @@ -1610,58 +1604,115 @@ while true; do exit 1 ;; -h|--help) - echo -e "Usage: arkmanager [OPTION]\n" - echo "Option Description" - echo "backup Saves a backup of your server inside the backup directory" - echo "broadcast Sends a message to all users connected to server" - echo "saveworld Saves the game world to disk" - echo "rconcmd Execute RCON command on server" - echo "checkupdate Check for a new ARK server version" - echo "install Install the ARK server files from steamcmd" - echo "installmod Installs a mod from the Steam workshop" - echo "restart Stops the server and then starts it" - echo "restart --all Restarts all servers specified in configfile_xxxxx" - echo "run Runs the server without daemonizing" - echo "start Starts the server" - echo "start --all Starts all servers specified in configfile_xxxxx" - echo "stop Stops the server" - echo "stop --all Stops all servers specified in configfile_xxxxx" - echo "status Returns the status of the current ARK server instance" - echo "update [OPTION ...] Check for a new ARK server version, if needed, stops the server, updates it, and starts it again" - echo "upgrade-tools Check for a new ARK Server Tools version and upgrades it if needed" - echo "uninstall-tools Uninstall the ARK Server Tools" - echo "useconfig Use the configuration overrides in the specified config name or file" - echo "--help Show this help" - echo "--version Show the version info of ARK Server Tools" - echo - echo "Update command takes the below options:" - echo " --force Apply update without checking the current version" - echo " --safe Wait for server to perform world save and update." - echo " --warn Warn players before updating server" - echo " --validate Validates all ARK server files" - echo " --saveworld Saves world before update" - echo " --update-mods Updates installed and requested mods" - echo " --backup Takes a backup of the save files before updating" - echo " --downloadonly Download the mod and/or server update without applying it" - echo " Requires arkStagingDir be set to a staging directory on the same filesystem as the server" - echo - echo "stop and restart commands take the below options:" - echo " --warn Warn players before shutting down the server" - echo " --saveworld Saves world before shutdown" + showUsage exit 1 ;; - *) - echo -n "arkmanager v${arkstVersion}: " - if [ $# -eq 0 ]; then - echo "no command specified" - else - echo "unknown command '$1' specified" - fi - echo "Try 'arkmanager -h' or 'arkmanager --help' for more information." + "") + echo "arkmanager v${arkstVersion}: no command specified" + showUsage exit 1 ;; esac - status=$? + + # 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 + + # Handle all instances being requested + if [[ "$allinstances" == "yes" ]]; then + instances=( ) + for varname in "${!configfile_@}"; do + instances+=( "${varname#configfile_}" ) + done + fi + + # Run the command for each instance requested + for instance in "${instances[@]}"; do + ( + echo "Running command '${command}' for instance '${instance}'" + useConfig "$instance" + + case "$command" in + run) + doRun + ;; + start) + doStart + ;; + stop) + doStop shutdown "${options[@]}" + ;; + restart) + doStop restart "${options[@]}" + echo "`timestamp`: stop" >> "$logdir/$arkmanagerLog" + ;; + install) + doInstall + ;; + update) + doUpdate "${options[@]}" + ;; + checkupdate) + checkForUpdate + ;; + installmod) + doInstallMod "${args[@]}" + ;; + backup) + doBackup + ;; + broadcast) + doBroadcast "${args[@]}" + ;; + saveworld) + doSaveWorld + ;; + rconcmd) + rconcmd "${args[@]}" + ;; + status) + printStatus + ;; + *) + 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 + ( + useConfig "$instance" + doStart + echo "`timestamp`: start" >> "$logdir/$arkmanagerLog" + echo "`timestamp`: restart" >> "$logdir/$arkmanagerLog" + ) + done + fi + if [ $# -eq 0 ]; then break fi