Added explanatory exit codes to the script.

These codes are used by `test.sh` to better analyse whether an action
was successful or not.
This commit is contained in:
Marcus Whybrow 2012-06-05 19:40:32 +01:00
parent 4addb6dd9a
commit c79efe0abd
2 changed files with 156 additions and 166 deletions

242
init/msm
View File

@ -66,8 +66,7 @@ as_user() {
if [ "$user" == "root" ]; then if [ "$user" == "root" ]; then
su - "$1" -s /bin/bash -c "$2" su - "$1" -s /bin/bash -c "$2"
else else
echoerr "This command must be executed as the user \"$1\" or \"root\"." error_exit INVALID_USER "This command must be executed as the user \"$1\" or \"root\"."
exit 1
fi fi
fi fi
} }
@ -82,6 +81,25 @@ echoerr() {
echo "$@" 1>&2 echo "$@" 1>&2
} }
# Exit's the script
error_exit() {
case "$1" in
INVALID_USER) code=64;;
INVALID_COMMAND) code=65;;
INVALID_ARGUMENT) code=66;;
SERVER_STOPPED) code=67;;
SERVER_RUNNING) code=68;;
NAME_NOT_FOUND) code=69;;
FILE_NOT_FOUND) code=70;;
DUPLICATE_NAME) code=71;;
LOGS_NOT_ROLLED) code=72;;
CONF_ERROR) code=73;;
esac
echo "$(basename $0) Error $1 ($code): ${2:-"Unknown Error"}" 1>&2
exit "${code:-$1}"
}
# A function used to print debug messages to stdout. Prevents messages from # A function used to print debug messages to stdout. Prevents messages from
# appearing unless in debug mode, and allows debug statements to be easily # appearing unless in debug mode, and allows debug statements to be easily
# distinguished from necessary echo statements. # distinguished from necessary echo statements.
@ -102,14 +120,12 @@ is_valid_name() {
if [[ "$1" =~ $valid ]]; then if [[ "$1" =~ $valid ]]; then
if [[ "$1" =~ $invalid ]]; then if [[ "$1" =~ $invalid ]]; then
echoerr "Invalid name \"$1\": A name may not be any of the following reserved worlds \"start\", \"stop\", \"restart\", \"server\", \"version\", \"jargroup\" or \"all\"." error_exit INVALID_ARGUMENT "Invalid name \"$1\": A name may not be any of the following reserved worlds \"start\", \"stop\", \"restart\", \"server\", \"version\", \"jargroup\" or \"all\"."
return 1
else else
return 0 return 0
fi fi
else else
echoerr "Invalid name \"$1\": A name may only contain letters, numbers, dashes and unscores." error_exit INVALID_ARGUMENT "Invalid name \"$1\": A name may only contain letters, numbers, dashes and unscores."
return 1
fi fi
} }
@ -219,8 +235,7 @@ world_activate() {
if [ -d "${world_active_path[$1]}" ]; then if [ -d "${world_active_path[$1]}" ]; then
echo "World \"${world_name[$1]}\" is already activate." echo "World \"${world_name[$1]}\" is already activate."
else else
echoerr "Error: Directory \"${world_inactive_path[$1]}\" could not be found." error_exit DIR_NOT_FOUND "Directory \"${world_inactive_path[$1]}\" could not be found."
return 1
fi fi
fi fi
} }
@ -229,8 +244,7 @@ world_activate() {
# $1: The ID of the world # $1: The ID of the world
world_deactivate() { world_deactivate() {
if server_is_running "${world_server_id[$1]}"; then if server_is_running "${world_server_id[$1]}"; then
echoerr "Error: Worlds cannot be deactivated whilst the server is running." exit_error 68 "Worlds cannot be deactivated whilst the server is running."
return 1
else else
if [ -d "${world_active_path[$1]}" ]; then if [ -d "${world_active_path[$1]}" ]; then
echo -n "Moving world \"${world_name[$1]}\" to the inactive worldstorage directory... " echo -n "Moving world \"${world_name[$1]}\" to the inactive worldstorage directory... "
@ -241,8 +255,7 @@ world_deactivate() {
if [ -d "${world_inactive_path[$1]}" ]; then if [ -d "${world_inactive_path[$1]}" ]; then
echo "World \"${world_name[$1]}\" is already deactivate." echo "World \"${world_name[$1]}\" is already deactivate."
else else
echoerr "Error: Directory \"${world_active_path[$1]}\" could not be found." exit_error DIR_NOT_FOUND "Directory \"${world_active_path[$1]}\" could not be found."
return 1
fi fi
fi fi
fi fi
@ -265,7 +278,7 @@ server_get_id() {
i="$(( $i + 1 ))" i="$(( $i + 1 ))"
done done
return 1 error_exit NAME_NOT_FOUND "Could not find id for server name \"$1\"."
} }
# Returns the ID of a server's world. # Returns the ID of a server's world.
@ -287,7 +300,7 @@ server_world_get_id() {
done done
fi fi
return 1 error_exit NAME_NOT_FOUND "Could not find id for world \"$2\" for server \"$1\"."
} }
# Returns 0 if the server $1 is running and 1 if not # Returns 0 if the server $1 is running and 1 if not
@ -497,7 +510,7 @@ server_set_active() {
server_active[$1]="false" server_active[$1]="false"
;; ;;
*) *)
return 1 error_exit INVALID_ARGUMENT "Invalid argument."
;; ;;
esac esac
} }
@ -521,21 +534,19 @@ jargroup_list() {
# $1: The name for the jargroup # $1: The name for the jargroup
jargroup_create() { jargroup_create() {
if is_valid_name "$1"; then if is_valid_name "$1"; then
if [[ ! -e "$JAR_STORAGE_PATH/$1" ]]; then if [[ ! -d "$JAR_STORAGE_PATH/$1" ]]; then
printf "Creating jar group... " printf "Creating jar group... "
local error="$(as_user_stderr "$USERNAME" "mkdir -p \"$JAR_STORAGE_PATH/$1\"")" local error="$(as_user_stderr "$USERNAME" "mkdir -p \"$JAR_STORAGE_PATH/$1\"")"
if [[ "$error" != "" ]]; then if [[ "$error" != "" ]]; then
echo "Failed." echo "Failed."
echo "Reason: $error" error_exit FILE_NOT_FOUND "$error"
return 1
fi fi
error="$(as_user "$USERNAME" "echo \"$2\" > \"$JAR_STORAGE_PATH/$1/$JARGROUP_TARGET\"")" error="$(as_user "$USERNAME" "echo \"$2\" > \"$JAR_STORAGE_PATH/$1/$JARGROUP_TARGET\"")"
if [[ "$error" != "" ]]; then if [[ "$error" != "" ]]; then
echo "Failed." echo "Failed."
echo "Reason: $error" error_exit FILE_NOT_FOUND "$error"
return 1
fi fi
echo "Done." echo "Done."
@ -543,11 +554,8 @@ jargroup_create() {
# Download the latest version now # Download the latest version now
jargroup_getlatest "$1" jargroup_getlatest "$1"
else else
echo "A jar group with that name already exists." error_exit DUPLICATE_NAME "A jar group with that name already exists."
return 1
fi fi
else
return 1
fi fi
} }
@ -558,16 +566,15 @@ jargroup_create() {
# $1: The jargroup name to download the latest version for # $1: The jargroup name to download the latest version for
jargroup_getlatest() { jargroup_getlatest() {
if is_valid_name "$1"; then if is_valid_name "$1"; then
if [[ -e "$JAR_STORAGE_PATH/$1" ]]; then if [[ -d "$JAR_STORAGE_PATH/$1" ]]; then
if [[ -e "$JAR_STORAGE_PATH/$1/$JARGROUP_TARGET" ]]; then if [[ -f "$JAR_STORAGE_PATH/$1/$JARGROUP_TARGET" ]]; then
printf "Downloading latest version... " printf "Downloading latest version... "
# Try and make # Try and make
local error="$(as_user_stderr "$USERNAME" "mkdir -p '$JAR_STORAGE_PATH/$1/$JARGROUP_DOWNLOAD_DIR'")" local error="$(as_user_stderr "$USERNAME" "mkdir -p '$JAR_STORAGE_PATH/$1/$JARGROUP_DOWNLOAD_DIR'")"
if [[ "$error" != "" ]]; then if [[ "$error" != "" ]]; then
echo "Failed." echo "Failed."
echo "Reason: $error" error_exit FILE_NOT_FOUND "$error"
return 1
fi fi
as_user "$USERNAME" "wget --quiet --trust-server-names --no-check-certificate --input-file='$JAR_STORAGE_PATH/$1/$JARGROUP_TARGET' --directory-prefix='$JAR_STORAGE_PATH/$1/$JARGROUP_DOWNLOAD_DIR'" as_user "$USERNAME" "wget --quiet --trust-server-names --no-check-certificate --input-file='$JAR_STORAGE_PATH/$1/$JARGROUP_TARGET' --directory-prefix='$JAR_STORAGE_PATH/$1/$JARGROUP_DOWNLOAD_DIR'"
@ -583,12 +590,12 @@ jargroup_getlatest() {
local most_recent_jar="$(get_latest_file "$JAR_STORAGE_PATH/$1")" local most_recent_jar="$(get_latest_file "$JAR_STORAGE_PATH/$1")"
if [[ ! -e "$most_recent_jar" ]] || ! diff "$most_recent_jar" "$JAR_STORAGE_PATH/$1/$JARGROUP_DOWNLOAD_DIR/$file_name" > /dev/null; then if [[ ! -f "$most_recent_jar" ]] || ! diff "$most_recent_jar" "$JAR_STORAGE_PATH/$1/$JARGROUP_DOWNLOAD_DIR/$file_name" > /dev/null; then
# There is not a previous version to do a comparison against, or # There is not a previous version to do a comparison against, or
# The previous version is different: # The previous version is different:
# Add it to the group # Add it to the group
[[ -e "$most_recent_jar" ]] [[ -f "$most_recent_jar" ]]
local was_previous="$?" local was_previous="$?"
as_user "$USERNAME" "mv '$JAR_STORAGE_PATH/$1/$JARGROUP_DOWNLOAD_DIR/$file_name' '$JAR_STORAGE_PATH/$1/$new_name'" as_user "$USERNAME" "mv '$JAR_STORAGE_PATH/$1/$JARGROUP_DOWNLOAD_DIR/$file_name' '$JAR_STORAGE_PATH/$1/$new_name'"
@ -613,15 +620,11 @@ jargroup_getlatest() {
# Clean up the temp download folder # Clean up the temp download folder
as_user "$USERNAME" "rm -fr '$JAR_STORAGE_PATH/$1/$JARGROUP_DOWNLOAD_DIR'" as_user "$USERNAME" "rm -fr '$JAR_STORAGE_PATH/$1/$JARGROUP_DOWNLOAD_DIR'"
else else
echo "Target URL not found, use $0 jargroup seturl <download-url>" error_exit FILE_NOT_FOUND "Target URL not found, use $0 jargroup seturl <download-url>"
return 1
fi fi
else else
echo "There is no jar group with the name \"$1\"." error_exit NAME_NOT_FOUND "There is no jar group with the name \"$1\"."
return 1
fi fi
else
return 1
fi fi
} }
@ -629,7 +632,7 @@ jargroup_getlatest() {
# $1: The name of the existing jargroup # $1: The name of the existing jargroup
jargroup_delete() { jargroup_delete() {
if is_valid_name "$1"; then if is_valid_name "$1"; then
if [[ -e "$JAR_STORAGE_PATH/$1" ]]; then if [[ -d "$JAR_STORAGE_PATH/$1" ]]; then
printf "Are you sure you want to delete this jar group [y/N]: " printf "Are you sure you want to delete this jar group [y/N]: "
read answer read answer
@ -640,11 +643,8 @@ jargroup_delete() {
echo "Jar group was NOT deleted." echo "Jar group was NOT deleted."
fi fi
else else
echo "There is no jar group with the name \"$1\"." error_exit NAME_NOT_FOUND "There is no jar group with the name \"$1\"."
return 1
fi fi
else
return 1
fi fi
} }
@ -653,28 +653,22 @@ jargroup_delete() {
# $2: The new name # $2: The new name
jargroup_rename() { jargroup_rename() {
if is_valid_name "$1"; then if is_valid_name "$1"; then
if [[ -e "$JAR_STORAGE_PATH/$1" ]]; then if [[ -d "$JAR_STORAGE_PATH/$1" ]]; then
# If the jar group name is valid, # If the jar group name is valid,
# and there is no other jar group with the name $1 # and there is no other jar group with the name $1
if is_valid_name "$2"; then if is_valid_name "$2"; then
if [[ -e "$JAR_STORAGE_PATH/$2" ]]; then if [[ -e "$JAR_STORAGE_PATH/$2" ]]; then
echo "Could not be renamed, there is already a jar group with the name \"$2\"." error_exit DUPLICATE_NAME "Could not be renamed, there is already a jar group with the name \"$2\"."
return 1
else else
# TODO: Update any symbolic links which point to a jar in this directory # TODO: Update any symbolic links which point to a jar in this directory
as_user "$USERNAME" "mv '$JAR_STORAGE_PATH/$1' '$JAR_STORAGE_PATH/$2'" as_user "$USERNAME" "mv '$JAR_STORAGE_PATH/$1' '$JAR_STORAGE_PATH/$2'"
echo "Renamed jar group \"$1\" to \"$2\"." echo "Renamed jar group \"$1\" to \"$2\"."
fi fi
else
return 1
fi fi
else else
echo "There is no jar group with the name \"$1\"." error_exit NAME_NOT_FOUND "There is no jar group with the name \"$1\"."
return 1
fi fi
else
return 1
fi fi
} }
@ -690,9 +684,8 @@ server_list() {
# $1: The server name to create # $1: The server name to create
server_create() { server_create() {
if is_valid_name "$1"; then if is_valid_name "$1"; then
if [[ -e "$SERVER_STORAGE_PATH/$1" ]]; then if [[ -d "$SERVER_STORAGE_PATH/$1" ]]; then
echo "A server with that name already exists." error_exit DUPLICATE_NAME "A server with that name already exists."
return 1
else else
printf "Creating server directory... " printf "Creating server directory... "
as_user "$USERNAME" "mkdir -p '$SERVER_STORAGE_PATH/$1'" as_user "$USERNAME" "mkdir -p '$SERVER_STORAGE_PATH/$1'"
@ -713,8 +706,6 @@ server_create() {
server_set_jar "$(server_get_id "$1")" "minecraft" server_set_jar "$(server_get_id "$1")" "minecraft"
fi fi
fi fi
else
return 1
fi fi
} }
@ -734,11 +725,8 @@ server_delete() {
echo "Server was NOT deleted." echo "Server was NOT deleted."
fi fi
else else
echo "There is no server with the name \"$1\"." error_exit NAME_NOT_FOUND "There is no server with the name \"$1\"."
return 1
fi fi
else
return 1
fi fi
} }
@ -748,8 +736,7 @@ server_delete() {
server_rename() { server_rename() {
if is_valid_name "$1"; then if is_valid_name "$1"; then
if server_is_running "$(server_get_id "$1")"; then if server_is_running "$(server_get_id "$1")"; then
echoerr "Error: Can only rename a stopped server." error_exit SERVER_RUNNING "Can only rename a stopped server."
return 1
else else
if [[ -e "$SERVER_STORAGE_PATH/$1" ]]; then if [[ -e "$SERVER_STORAGE_PATH/$1" ]]; then
# If the server name is valid and exists # If the server name is valid and exists
@ -760,22 +747,16 @@ server_rename() {
# If the server name is valid # If the server name is valid
if [[ -e "$SERVER_STORAGE_PATH/$2" ]]; then if [[ -e "$SERVER_STORAGE_PATH/$2" ]]; then
# and there is not already a server with the name $2 # and there is not already a server with the name $2
echoerr "Error: Could not be renamed, there is already a server with the name \"$2\"." error_exit DUPLICATE_NAME "Could not be renamed, there is already a server with the name \"$2\"."
return 1
else else
as_user "$USERNAME" "mv '$SERVER_STORAGE_PATH/$1' '$SERVER_STORAGE_PATH/$2'" as_user "$USERNAME" "mv '$SERVER_STORAGE_PATH/$1' '$SERVER_STORAGE_PATH/$2'"
echo "Renamed server \"$1\" to \"$2\"." echo "Renamed server \"$1\" to \"$2\"."
fi fi
else
return 1
fi fi
else else
echoerr "Error: There is no server with the name \"$1\"." error_exit NAME_NOT_FOUND "There is no server with the name \"$1\"."
return 1
fi fi
fi fi
else
return 1
fi fi
} }
@ -985,8 +966,8 @@ server_log_roll() {
as_user "${server_user_name[$1]}" "cp \"/dev/null\" \"${server_log[$1]}\"" as_user "${server_user_name[$1]}" "cp \"/dev/null\" \"${server_log[$1]}\""
as_user "${server_user_name[$1]}" "echo \"Previous logs can be found at \\\"${server_log_archive_path[$1]}\\\"\" > \"${server_log[$1]}\"" as_user "${server_user_name[$1]}" "echo \"Previous logs can be found at \\\"${server_log_archive_path[$1]}\\\"\" > \"${server_log[$1]}\""
else else
echoerr "Failed." echo "Failed."
return 1 error_exit LOGS_NOT_ROLLED "Logs were not rolled."
fi fi
fi fi
@ -1016,7 +997,7 @@ server_backup() {
# $2: The name of the jar group # $2: The name of the jar group
# $3: Optionally, a specific jar to use. # $3: Optionally, a specific jar to use.
server_set_jar() { server_set_jar() {
if [ -e "$JAR_STORAGE_PATH/$2" ]; then if [ -d "$JAR_STORAGE_PATH/$2" ]; then
if [ -z "$3" ]; then if [ -z "$3" ]; then
# If a specific jar file is not mentioned # If a specific jar file is not mentioned
@ -1029,8 +1010,7 @@ server_set_jar() {
local jar="$JAR_STORAGE_PATH/$2/$3" local jar="$JAR_STORAGE_PATH/$2/$3"
if [[ ! -e "$jar" ]]; then if [[ ! -e "$jar" ]]; then
echo "There is no jar named \"$3\" in jargroup \"$2\"." error_exit NAME_NOT_FOUND "There is no jar named \"$3\" in jargroup \"$2\"."
return 1
fi fi
fi fi
@ -1039,7 +1019,7 @@ server_set_jar() {
echo "Server \"${server_name[$1]}\" is now using \"$jar\"." echo "Server \"${server_name[$1]}\" is now using \"$jar\"."
fi fi
else else
echo "There is no jargorup named \"$2\"." error_exit NAME_NOT_FOUND "There is no jargorup named \"$2\"."
fi fi
} }
@ -1220,8 +1200,7 @@ server_world_init() {
else else
world_status[$2]="unknown" world_status[$2]="unknown"
echoerr "Error: World cannot be found in either \"${world_active_path[$2]}\" or \"${world_inactive_path[$2]}\"." error_exit NAME_NOT_FOUND "World cannot be found in either \"${world_active_path[$2]}\" or \"${world_inactive_path[$2]}\"."
return 1
fi fi
fi fi
@ -1439,8 +1418,7 @@ server_init() {
# $2: The message to print to stderr if the variable is unset # $2: The message to print to stderr if the variable is unset
assert_is_set_in_config() { assert_is_set_in_config() {
[[ ! ${!1} && ${!1-_} ]] && { [[ ! ${!1} && ${!1-_} ]] && {
echoerr "Error: $1 must be set in $CONF" error_exit CONF_ERROR "$1 must be set in $CONF"
exit 1
} }
} }
@ -1605,7 +1583,7 @@ main() {
create) create)
if [ -z "$3" ]; then if [ -z "$3" ]; then
# If a server name is not provided # If a server name is not provided
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
else else
# Create a new server # Create a new server
server_create "$3" server_create "$3"
@ -1614,7 +1592,7 @@ main() {
delete) delete)
if [ -z "$3" ]; then if [ -z "$3" ]; then
# If a server name is not provided # If a server name is not provided
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
else else
# Delete an existing server, with confirmation # Delete an existing server, with confirmation
server_delete "$3" server_delete "$3"
@ -1623,7 +1601,7 @@ main() {
rename) rename)
if [ -z "$3" ] || [ -z "$4" ]; then if [ -z "$3" ] || [ -z "$4" ]; then
# If a server name is not provided # If a server name is not provided
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
else else
# Rename an existing server # Rename an existing server
server_rename "$3" "$4" server_rename "$3" "$4"
@ -1631,7 +1609,7 @@ main() {
;; ;;
*) *)
# "server" is not a valid command # "server" is not a valid command
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
;; ;;
esac esac
;; ;;
@ -1643,14 +1621,14 @@ main() {
;; ;;
create) create)
if [ -z "$3" ] || [ -z "$4" ]; then if [ -z "$3" ] || [ -z "$4" ]; then
echo "Invlaid command." error_exit INVALID_COMMAND "Invalid command."
else else
jargroup_create "$3" "$4" jargroup_create "$3" "$4"
fi fi
;; ;;
delete) delete)
if [ -z "$3" ]; then if [ -z "$3" ]; then
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
else else
jargroup_delete "$3" jargroup_delete "$3"
fi fi
@ -1658,28 +1636,28 @@ main() {
;; ;;
rename) rename)
if [ -z "$3" ] || [ -z "$4" ]; then if [ -z "$3" ] || [ -z "$4" ]; then
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
else else
jargroup_rename "$3" "$4" jargroup_rename "$3" "$4"
fi fi
;; ;;
changetarget) changetarget)
if [ -z "$3" ] || [ -z "$4" ]; then if [ -z "$3" ] || [ -z "$4" ]; then
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
else else
jargroup_settarget "$3" "$4" jargroup_settarget "$3" "$4"
fi fi
;; ;;
getlatest) getlatest)
if [ -z "$3" ]; then if [ -z "$3" ]; then
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
else else
jargroup_getlatest "$3" jargroup_getlatest "$3"
fi fi
;; ;;
*) *)
# "jargroup" is not a valid command # "jargroup" is not a valid command
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
;; ;;
esac esac
;; ;;
@ -1791,14 +1769,14 @@ main() {
;; ;;
ram) ram)
if [ -z "$4" ]; then if [ -z "$4" ]; then
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
else else
world_id="$(server_world_get_id "$id" "$4")" world_id="$(server_world_get_id "$id" "$4")"
if [ ! -z "$world_id" ]; then if [ ! -z "$world_id" ]; then
world_toggle_ramdisk_state "$world_id" world_toggle_ramdisk_state "$world_id"
else else
echo "Server \"${server_name[$id]}\" has no world with that name." error_exit NAME_NOT_FOUND "Server \"${server_name[$id]}\" has no world with that name."
fi fi
fi fi
;; ;;
@ -1827,20 +1805,20 @@ main() {
;; ;;
on) on)
if [ -z "$4" ]; then if [ -z "$4" ]; then
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
else else
world_activate "$(server_world_get_id "$id" "$4")" world_activate "$(server_world_get_id "$id" "$4")"
fi fi
;; ;;
off) off)
if [ -z "$4" ]; then if [ -z "$4" ]; then
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
else else
world_deactivate "$(server_world_get_id "$id" "$4")" world_deactivate "$(server_world_get_id "$id" "$4")"
fi fi
;; ;;
*) *)
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
;; ;;
esac esac
;; ;;
@ -1866,7 +1844,7 @@ main() {
;; ;;
jar) jar)
if [ -z "$3" ]; then if [ -z "$3" ]; then
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
else else
server_set_jar "$id" "$3" "$4" server_set_jar "$id" "$3" "$4"
fi fi
@ -1878,7 +1856,7 @@ main() {
server_eval "$id" "whitelist on" server_eval "$id" "whitelist on"
echo "Whitelist enabled" echo "Whitelist enabled"
else else
echo "Server \"${server_name[$id]}\" is not running." error_exit SERVER_STOPPED "Server \"${server_name[$id]}\" is not running."
fi fi
;; ;;
off) off)
@ -1886,30 +1864,30 @@ main() {
server_eval "$id" "whitelist off" server_eval "$id" "whitelist off"
echo "Whitelist disabled" echo "Whitelist disabled"
else else
echo "Server \"${server_name[$id]}\" is not running." error_exit SERVER_STOPPED "Server \"${server_name[$id]}\" is not running."
fi fi
;; ;;
add) add)
if [ -z "$4" ]; then if [ -z "$4" ]; then
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
else else
if server_is_running "$id"; then if server_is_running "$id"; then
server_eval "$id" "whitelist add $4" server_eval "$id" "whitelist add $4"
echo "Added \"$4\" to the whitelist." echo "Added \"$4\" to the whitelist."
else else
echo "Server \"${server_name[$id]}\" is not running." error_exit SERVER_STOPPED "Server \"${server_name[$id]}\" is not running."
fi fi
fi fi
;; ;;
remove) remove)
if [ -z "$4" ]; then if [ -z "$4" ]; then
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
else else
if server_is_running "$id"; then if server_is_running "$id"; then
server_eval "$id" "whitelist remove $4" server_eval "$id" "whitelist remove $4"
echo "Removed \"$4\" from the whitelist." echo "Removed \"$4\" from the whitelist."
else else
echo "Server \"${server_name[$id]}\" is not running." error_exit SERVER_STOPPED "Server \"${server_name[$id]}\" is not running."
fi fi
fi fi
;; ;;
@ -1923,7 +1901,7 @@ main() {
fi fi
;; ;;
*) *)
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
;; ;;
esac esac
;; ;;
@ -1931,7 +1909,7 @@ main() {
case "$3" in case "$3" in
player) player)
if [ -z "$5" ]; then if [ -z "$5" ]; then
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
else else
case "$4" in case "$4" in
add) add)
@ -1965,7 +1943,7 @@ main() {
fi fi
;; ;;
*) *)
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
;; ;;
esac esac
fi fi
@ -2003,7 +1981,7 @@ main() {
fi fi
;; ;;
*) *)
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
;; ;;
esac esac
;; ;;
@ -2031,7 +2009,7 @@ main() {
fi fi
;; ;;
*) *)
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
;; ;;
esac esac
;; ;;
@ -2042,7 +2020,7 @@ main() {
server_eval "$id" "op $4" server_eval "$id" "op $4"
echo "The player \"$4\" is now an operator for the server." echo "The player \"$4\" is now an operator for the server."
else else
echo "Server \"${server_name[$id]}\" is not running." error_exit SERVER_STOPPED "Server \"${server_name[$id]}\" is not running."
fi fi
;; ;;
remove) remove)
@ -2050,7 +2028,7 @@ main() {
server_eval "$id" "deop $4" server_eval "$id" "deop $4"
echo "The player \"$4\" is no longer an operator for the server." echo "The player \"$4\" is no longer an operator for the server."
else else
echo "Server \"${server_name[$id]}\" is not running." error_exit SERVER_STOPPED "Server \"${server_name[$id]}\" is not running."
fi fi
;; ;;
list) list)
@ -2063,7 +2041,7 @@ main() {
fi fi
;; ;;
*) *)
echo "Invalid command" error_exit INVALID_COMMAND "Invalid command."
;; ;;
esac esac
;; ;;
@ -2077,7 +2055,7 @@ main() {
case "$3" in case "$3" in
creative|1) local mode=1;; creative|1) local mode=1;;
survival|0) local mode=0;; survival|0) local mode=0;;
*) echoerr "Invalid mode"; exit 1;; *) error_exit INVALID_COMMAND "Invalid mode";;
esac esac
local line="$(server_eval_and_get_line "$id" "gamemode $4 $mode" "${server_confirm_gamemode[$id]}" "${server_confirm_gamemode_fail_no_user[$id]}" "${server_confirm_gamemode_fail_no_change[$id]}")" local line="$(server_eval_and_get_line "$id" "gamemode $4 $mode" "${server_confirm_gamemode[$id]}" "${server_confirm_gamemode_fail_no_user[$id]}" "${server_confirm_gamemode_fail_no_change[$id]}")"
@ -2095,18 +2073,18 @@ main() {
echo "The player \"$4\" was already in mode \"$3\"." echo "The player \"$4\" was already in mode \"$3\"."
fi fi
else else
echo "Server \"${server_name[$id]}\" is not running." error_exit SERVER_STOPPED "Server \"${server_name[$id]}\" is not running."
fi fi
fi fi
;; ;;
*) *)
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
;; ;;
esac esac
;; ;;
kick) kick)
if [ -z "$3" ]; then if [ -z "$3" ]; then
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
else else
if server_is_running "$id"; then if server_is_running "$id"; then
local line="$(server_eval_and_get_line "$id" "kick $3" "${server_confirm_kick[$id]}" "${server_confirm_kick_fail[$id]}")" local line="$(server_eval_and_get_line "$id" "kick $3" "${server_confirm_kick[$id]}" "${server_confirm_kick_fail[$id]}")"
@ -2120,19 +2098,19 @@ main() {
echo "The player \"$3\" is not connected." echo "The player \"$3\" is not connected."
fi fi
else else
echo "Server \"${server_name[$id]}\" is not running." error_exit SERVER_STOPPED "Server \"${server_name[$id]}\" is not running."
fi fi
fi fi
;; ;;
say) say)
if [ -z "$3" ]; then if [ -z "$3" ]; then
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
else else
if server_is_running "$id"; then if server_is_running "$id"; then
server_eval "$id" "say ${*:3}" server_eval "$id" "say ${*:3}"
echo "Message sent to players." echo "Message sent to players."
else else
echo "Server \"${server_name[$id]}\" is not running." error_exit SERVER_STOPPED "Server \"${server_name[$id]}\" is not running."
fi fi
fi fi
;; ;;
@ -2140,7 +2118,7 @@ main() {
case "$3" in case "$3" in
set) set)
if [ -z "$4" ]; then if [ -z "$4" ]; then
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
else else
if server_is_running "$id"; then if server_is_running "$id"; then
local line="$(server_eval_and_get_line "$id" "time set $4" "${server_confirm_time_set[$id]}" "${server_confirm_time_set_fail[$id]}")" local line="$(server_eval_and_get_line "$id" "time set $4" "${server_confirm_time_set[$id]}" "${server_confirm_time_set_fail[$id]}")"
@ -2151,16 +2129,16 @@ main() {
fi fi
local regex="${LOG_REGEX} ${server_confirm_time_set_fail[$id]}" local regex="${LOG_REGEX} ${server_confirm_time_set_fail[$id]}"
if [[ "$line" =~ $regex ]]; then if [[ "$line" =~ $regex ]]; then
echo "Unable to convert \"$4\" to a time." error_exit INVALID_ARGUMENT "Unable to convert \"$4\" to a time."
fi fi
else else
echo "Server \"${server_name[$id]}\" is not running." error_exit SERVER_STOPPED "Server \"${server_name[$id]}\" is not running."
fi fi
fi fi
;; ;;
add) add)
if [ -z "$4" ]; then if [ -z "$4" ]; then
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
else else
if server_is_running "$id"; then if server_is_running "$id"; then
local line="$(server_eval_and_get_line "$id" "time add $4" "${server_confirm_time_add[$id]}" "${server_confirm_time_add_fail[$id]}")" local line="$(server_eval_and_get_line "$id" "time add $4" "${server_confirm_time_add[$id]}" "${server_confirm_time_add_fail[$id]}")"
@ -2171,15 +2149,15 @@ main() {
fi fi
local regex="${LOG_REGEX} ${server_confirm_time_add_fail[$id]}" local regex="${LOG_REGEX} ${server_confirm_time_add_fail[$id]}"
if [[ "$line" =~ $regex ]]; then if [[ "$line" =~ $regex ]]; then
echo "Unable to convert \"$4\" to a time." error_exit INVALID_ARGUMENT "Unable to convert \"$4\" to a time."
fi fi
else else
echo "Server \"${server_name[$id]}\" is not running." error_exit SERVER_STOPPED "Server \"${server_name[$id]}\" is not running."
fi fi
fi fi
;; ;;
*) *)
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
;; ;;
esac esac
;; ;;
@ -2196,7 +2174,7 @@ main() {
echo "${line:34:(-3)}" echo "${line:34:(-3)}"
fi fi
else else
echo "Server \"${server_name[$id]}\" is not running." error_exit SERVER_STOPPED "Server \"${server_name[$id]}\" is not running."
fi fi
;; ;;
save) save)
@ -2211,32 +2189,32 @@ main() {
server_save_all "$id" server_save_all "$id"
;; ;;
*) *)
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
;; ;;
esac esac
;; ;;
cmd) cmd)
if [ -z "$3" ]; then if [ -z "$3" ]; then
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
else else
if server_is_running "$id"; then if server_is_running "$id"; then
server_eval "$id" "${*:3}" server_eval "$id" "${*:3}"
echo "Command sent." echo "Command sent."
else else
echo "Server \"${server_name[$id]}\" is not running." error_exit SERVER_STOPPED "Server \"${server_name[$id]}\" is not running."
fi fi
fi fi
;; ;;
cmdlog) cmdlog)
if [ -z "$3" ]; then if [ -z "$3" ]; then
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
else else
if server_is_running "$id"; then if server_is_running "$id"; then
server_eval "$id" "${*:3}" server_eval "$id" "${*:3}"
echo "Now watching logs (press Ctrl+C to exit):" echo "Now watching logs (press Ctrl+C to exit):"
as_user "${server_user_name[$id]}" "tail --follow --lines=0 --sleep-interval=0.1 ${server_log[$id]}" as_user "${server_user_name[$id]}" "tail --follow --lines=0 --sleep-interval=0.1 ${server_log[$id]}"
else else
echo "Server \"${server_name[$id]}\" is not running." error_exit SERVER_STOPPED "Server \"${server_name[$id]}\" is not running."
fi fi
fi fi
;; ;;
@ -2244,15 +2222,15 @@ main() {
if server_is_running "$id"; then if server_is_running "$id"; then
as_user "${server_user_name[$1]}" "screen -r ${server_screen_name[$1]}" as_user "${server_user_name[$1]}" "screen -r ${server_screen_name[$1]}"
else else
echo "Server \"${server_name[$id]}\" is not running." error_exit SERVER_STOPPED "Server \"${server_name[$id]}\" is not running."
fi fi
;; ;;
*) *)
echo "Invalid command." error_exit INVALID_COMMAND "Invalid command."
;; ;;
esac esac
else else
echo "No server with that name." error_exit NAME_NOT_FOUND "No server with that name."
fi fi
;; ;;
esac esac

80
test.sh
View File

@ -5,6 +5,19 @@ DEFUALT_CONF="${MSM_DEFAULT_CONF:-${DIR}/msm.conf}"
TESTS_DIR="${MSM_TESTS_DIR:-${DIR}/tests}" TESTS_DIR="${MSM_TESTS_DIR:-${DIR}/tests}"
TMP_DIR="/tmp/msmtest" TMP_DIR="/tmp/msmtest"
# Exit codes
declare -r EX_OK=0
declare -r EX_INVALID_USER=64
declare -r EX_INVALID_COMMAND=65
declare -r EX_INVALID_ARGUMENT=66
declare -r EX_SERVER_STOPPED=67
declare -r EX_SERVER_RUNNING=68
declare -r EX_NAME_NOT_FOUND=69
declare -r EX_FILE_NOT_FOUND=70
declare -r EX_DUPLICATE_NAME=71
declare -r EX_LOGS_NOT_ROLLED=72
declare -r EX_CONF_ERROR=73
oneTimeSetUp() { oneTimeSetUp() {
# Variables used in tests # Variables used in tests
SCRIPT="${MSM_SCRIPT:-${DIR}/init/msm}" SCRIPT="${MSM_SCRIPT:-${DIR}/init/msm}"
@ -29,6 +42,8 @@ setUp() {
echo "LOG_ARCHIVE_PATH=\"${TMP_DIR}/archives/logs\"" >> "$MSM_CONF" echo "LOG_ARCHIVE_PATH=\"${TMP_DIR}/archives/logs\"" >> "$MSM_CONF"
echo "BACKUP_ARCHIVE_PATH=\"${TMP_DIR}/archives/backups\"" >> "$MSM_CONF" echo "BACKUP_ARCHIVE_PATH=\"${TMP_DIR}/archives/backups\"" >> "$MSM_CONF"
echo "DEBUG=\"true\"" >> "$MSM_CONF" echo "DEBUG=\"true\"" >> "$MSM_CONF"
source "$MSM_CONF"
} }
tearDown() { tearDown() {
@ -42,8 +57,9 @@ tearDown() {
# Utils # Utils
# ----- # -----
stdall() { quiet() {
$1 "${@:2}" 2>&1 "$@" >& /dev/null
return $?
} }
@ -68,70 +84,62 @@ stdall() {
### "msm server create" tests ### "msm server create" tests
test_reserved_server_names() { test_reserved_server_names() {
local result
local expected_regex="^Invalid\ name"
source "$MSM_CONF"
for name in "start" "stop" "restart" "server" "version" "jargroup" "all"; do for name in "start" "stop" "restart" "server" "version" "jargroup" "all"; do
result="$(stdall $SCRIPT server create $name)" quiet $SCRIPT server create $name
assertTrue "Server name \"$name\" was accepted but should be invalid." "[[ '$result' =~ $expected_regex ]]" assertEquals "Incorrect exit code when creating server name \"$name\"." $EX_INVALID_ARGUMENT $?
assertFalse "Server \"$name\" directory was created when it should not have been." "[ -d \"$SERVER_STORAGE_PATH/$name\" ]" assertFalse "Server \"$name\" directory was created when it should not have been." "[ -d \"$SERVER_STORAGE_PATH/$name\" ]"
done done
} }
test_common_invalid_server_names() { test_common_invalid_server_names() {
local result quiet $SCRIPT server create "name with spaces"
local expected_regex="^Invalid\ name" assertEquals "Incorrect exit code when creating server name \"name with spaces\"." $EX_INVALID_ARGUMENT $?
source "$MSM_CONF" assertFalse "Server \"name with spaces\" directory was created when it should not have been." "[ -d \"$SERVER_STORAGE_PATH/$name\" ]"
for name in "name with spaces"; do
result="$(stdall $SCRIPT server create $name)"
assertFalse "Server \"$name\" directory was created when it should not have been." "[ -d \"$SERVER_STORAGE_PATH/$name\" ]"
done
} }
test_valid_edge_case_server_names() { test_valid_edge_case_server_names() {
local result
local expected_regex="^Invalid\ name"
source "$MSM_CONF"
for name in "serverstart" "CapitalLetters" "0987654321" "name-with-dashes" "name_with_underscores" "Combination-of_different1Things2"; do for name in "serverstart" "CapitalLetters" "0987654321" "name-with-dashes" "name_with_underscores" "Combination-of_different1Things2"; do
result="$(stdall $SCRIPT server create $name)" quiet $SCRIPT server create $name
assertEquals "Incorrect exit code when creating server name \"$name\"." $EX_OK $?
assertTrue "Server \"$name\" directory was NOT created when it should not have been." "[ -d \"$SERVER_STORAGE_PATH/$name\" ]" assertTrue "Server \"$name\" directory was NOT created when it should not have been." "[ -d \"$SERVER_STORAGE_PATH/$name\" ]"
done done
} }
test_create_server_without_any_jargroups() { test_create_server_without_any_jargroups() {
$SCRIPT server create example > /dev/null quiet $SCRIPT server create example
assertEquals "Incorrect exit code." $EX_OK $?
source "$MSM_CONF"
assertTrue "Server was not created." "[ -d \"$SERVER_STORAGE_PATH/example\" ]" assertTrue "Server was not created." "[ -d \"$SERVER_STORAGE_PATH/example\" ]"
} }
# Assumes: test_create_server_without_any_jargroups # Assumes: test_create_server_without_any_jargroups
test_creating_server_when_that_name_already_exists() { test_creating_server_when_that_name_already_exists() {
$SCRIPT server create example > /dev/null # Create server "example"
local result="$(stdall $SCRIPT server create example)" quiet $SCRIPT server create example
# Create another server called "example", should be prevented
quiet $SCRIPT server create example
source "$MSM_CONF" assertEquals "Incorrect exit code." $EX_DUPLICATE_NAME $?
assertTrue "Failed to prevent duplicating an existing server name." "[[ \"$result\" == \"A server with that name already exists.\" ]]"
} }
# Assumes: test_creating_jargroup
test_create_server_with_jar_groups() { test_create_server_with_jar_groups() {
$SCRIPT jargroup create minecraft "https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar" > /dev/null # Create the "minecraft" jar group, which is used by default when creating
$SCRIPT server create example > /dev/null # new servers.
quiet $SCRIPT jargroup create minecraft "https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar"
# Create a new server that will use the "minecraft" jar group.
quiet $SCRIPT server create example
assertTrue "Server was not created." "[ -d \"$SERVER_STORAGE_PATH/example\" ]" assertEquals "Incorrect exit code." $EX_OK $?
assertTrue "Server direcotry was not created." "[ -d \"$SERVER_STORAGE_PATH/example\" ]"
assertTrue "Server jar was not linked." "[ -f \"$SERVER_STORAGE_PATH/example/$DEFAULT_JAR\" ]" assertTrue "Server jar was not linked." "[ -f \"$SERVER_STORAGE_PATH/example/$DEFAULT_JAR\" ]"
} }
### "msm server delete" tests ### "msm server delete" tests
test_deleting_server_that_does_not_exist() { test_deleting_server_that_does_not_exist() {
local result="$(stdall $SCRIPT server delete example)" quiet $SCRIPT server delete example
local regex="^There\ is\ no\ server\ with\ the\ name"
assertTrue "" "[[ \"$result\" =~ $regex ]]" assertEquals "Incorrect exit code." $EX_NAME_NOT_FOUND $?
} }
### "msm server rename" tests ### "msm server rename" tests
@ -230,6 +238,10 @@ test_deleting_server_that_does_not_exist() {
### "msm jargroup create" test ### "msm jargroup create" test
# test_creating_jargroup() {
#
# }
### "msm jargroup delete" test ### "msm jargroup delete" test
### "msm jargroup rename" test ### "msm jargroup rename" test