Improved command option names, now featuring "jargroup" and "server".

This commit is contained in:
Marcus Whybrow 2012-05-21 17:59:04 +01:00
parent 9ffdd27b88
commit 1ceea5bd59

239
msm
View File

@ -24,8 +24,8 @@ WHITELIST="white-list.txt"
BANNED_IPS="banned-ips.txt"
BANNED_PLAYERS="banned-players.txt"
JAR_GROUP_TARGET="target.txt"
JAR_DOWNLOAD_DIR="downloads"
JARGROUP_TARGET="target.txt"
JARGROUP_DOWNLOAD_DIR="downloads"
### Utility Functions
@ -43,13 +43,24 @@ as_user_stderr() {
as_user "$1" 2>&1 1>/dev/null
}
echoerr() {
echo "$@" 1>&2
}
is_valid_name() {
local regex="^[a-zA-Z0-9\_\-]+$"
local valid="^[a-zA-Z0-9\_\-]+$"
local invalid="^server|jargroup|start|stop|restart$"
if [[ $1 =~ $regex ]]; then
return 0
if [[ "$1" =~ $valid ]]; then
if [[ "$1" =~ $invalid ]]; then
echoerr "Invalid name \"$1\": A name may not be any of the following reserved worlds \"start\", \"stop\", \"restart\", \"server\" or \"jargroup\"."
exit 1
else
exit 0
fi
else
return 1
echoerr "Invalid name \"$1\": A name may only contain letters, numbers, dashes and unscores."
exit 1
fi
}
@ -57,9 +68,9 @@ get_latest_file() {
local best_time=0
local best_file=""
for file in $1/*; do
for file in "$1/*"; do
# Remove the path, leaving just the file name
local name=$(basename $file)
local name=$(basename "$file")
# Get the time in seconds since 1970 from file name
local seconds=$(date -d "${name:0:10}" "+%s" 2> /dev/null)
@ -78,83 +89,86 @@ get_latest_file() {
### Manager Functions
manager_create_server() {
if is_valid_name $1; then
if [[ -e $SERVER_STORAGE_PATH/$1 ]]; then
if is_valid_name "$1"; then
if [[ -e "$SERVER_STORAGE_PATH/$1" ]]; then
echo "A server with that name already exists."
exit 1
else
printf "Creating server directory... "
as_user "mkdir -p $SERVER_STORAGE_PATH/$1"
as_user "touch $SERVER_STORAGE_PATH/$1/$WHITELIST"
as_user "touch $SERVER_STORAGE_PATH/$1/$BANNED_IPS"
as_user "touch $SERVER_STORAGE_PATH/$1/$BANNED_PLAYERS"
as_user "mkdir -p '$SERVER_STORAGE_PATH/$1'"
as_user "touch '$SERVER_STORAGE_PATH/$1/$WHITELIST'"
as_user "touch '$SERVER_STORAGE_PATH/$1/$BANNED_IPS'"
as_user "touch '$SERVER_STORAGE_PATH/$1/$BANNED_PLAYERS'"
echo "Done."
fi
else
echo "Invalid name: A server name may only contain letters, numbers, a dash or an underscore."
exit 1
fi
}
manager_delete_server() {
if is_valid_name $1 && [[ -e $SERVER_STORAGE_PATH/$1 ]]; then
if is_valid_name "$1" && [[ -e "$SERVER_STORAGE_PATH/$1" ]]; then
printf "Are you sure you want to delete this server and its worlds (note: backups are preserved) [y/N]: "
read answer
if [[ $answer =~ ^y|Y|yes$ ]]; then
if [[ "$answer" =~ ^y|Y|yes$ ]]; then
# TODO: stop the server if running first
as_user "rm -r $SERVER_STORAGE_PATH/$1"
as_user "rm -r '$SERVER_STORAGE_PATH/$1'"
echo "Server deleted."
else
echo "Server was NOT deleted."
fi
else
echo "There is no server with the name \"$1\"."
if [[ ! -e "$SERVER_STORAGE_PATH/$1" ]]; then
echo "There is no server with the name \"$1\"."
fi
exit 1
fi
}
manager_rename_server() {
if is_valid_name $1 && [[ -e $SERVER_STORAGE_PATH/$1 ]]; then
if is_valid_name "$1" && [[ -e "$SERVER_STORAGE_PATH/$1" ]]; then
# If the server name is valid,
# and there is no other server with the name $1
# TODO: Check that the server is not running first, exit if it is
if is_valid_name $2; then
if [[ -e $SERVER_STORAGE_PATH/$2 ]]; then
if is_valid_name "$2"; then
if [[ -e "$SERVER_STORAGE_PATH/$2" ]]; then
echo "Could not be renamed, there is already a server with the name \"$2\"."
exit 1
else
as_user "mv $SERVER_STORAGE_PATH/$1 $SERVER_STORAGE_PATH/$2"
as_user "mv '$SERVER_STORAGE_PATH/$1' '$SERVER_STORAGE_PATH/$2'"
echo "Renamed server \"$1\" to \"$2\"."
fi
else
echo "Invalid name: The new server name may only contain letters, numbers, a dash or an underscore."
fi
else
echo "There is no server with the name \"$1\"."
if [[ -e "$SERVER_STORAGE_PATH/$1" ]]; then
echo "There is no server with the name \"$1\"."
fi
exit 1
fi
}
manager_jars_group_create() {
if is_valid_name $1; then
if [[ -e $JAR_STORAGE_PATH/$1 ]]; then
jargroup_create() {
if is_valid_name "$1"; then
if [[ -e "$JAR_STORAGE_PATH/$1" ]]; then
echo "A jar group with that name already exists."
exit 2
else
printf "Creating jar group... "
local error=$(as_user_stderr "mkdir -p $JAR_STORAGE_PATH/$1")
if [[ $error != "" ]]; then
local error=$(as_user_stderr "mkdir -p '$JAR_STORAGE_PATH/$1'")
if [[ "$error" != "" ]]; then
echo "Failed."
echo "Reason: $error"
exit 1
fi
error=$(as_user "echo \"$2\" > $JAR_STORAGE_PATH/$1/$JAR_GROUP_TARGET")
if [[ $error != "" ]]; then
error=$(as_user "echo \"$2\" > '$JAR_STORAGE_PATH/$1/$JARGROUP_TARGET'")
if [[ "$error" != "" ]]; then
echo "Failed."
echo "Reason: $error"
exit 1
@ -163,49 +177,48 @@ manager_jars_group_create() {
echo "Done."
# Download the latest version now
manager_jars_group_getlatest $1
jargroup_getlatest "$1"
fi
else
echo "Invalid name: A jar group name may only contain letters, numbers, a dash or an underscore."
exit 1
fi
}
manager_jars_group_getlatest() {
if is_valid_name "$1" && [[ -e $JAR_STORAGE_PATH/$1 ]]; then
if [[ -e $JAR_STORAGE_PATH/$1/$JAR_GROUP_TARGET ]]; then
jargroup_getlatest() {
if is_valid_name "$1" && [[ -e "$JAR_STORAGE_PATH/$1" ]]; then
if [[ -e "$JAR_STORAGE_PATH/$1/$JARGROUP_TARGET" ]]; then
printf "Downloading latest version... "
# Try and make
local error=$(as_user_stderr "mkdir -p $JAR_STORAGE_PATH/$1/$JAR_DOWNLOAD_DIR")
local error=$(as_user_stderr "mkdir -p '$JAR_STORAGE_PATH/$1/$JARGROUP_DOWNLOAD_DIR'")
if [[ $error != "" ]]; then
echo "Failed."
echo "Reason: $error"
exit 1
fi
as_user "wget --quiet --input-file=$JAR_STORAGE_PATH/$1/$JAR_GROUP_TARGET --directory-prefix=$JAR_STORAGE_PATH/$1/$JAR_DOWNLOAD_DIR --no-check-certificate"
as_user "wget --quiet --input-file='$JAR_STORAGE_PATH/$1/$JARGROUP_TARGET' --directory-prefix='$JAR_STORAGE_PATH/$1/$JARGROUP_DOWNLOAD_DIR' --no-check-certificate"
echo "Done."
local num_files=$(as_user "ls -1 $JAR_STORAGE_PATH/$1/$JAR_DOWNLOAD_DIR | wc -l")
local num_files=$(as_user "ls -1 '$JAR_STORAGE_PATH/$1/$JARGROUP_DOWNLOAD_DIR' | wc -l")
if [[ $num_files == 1 ]]; then
# There was 1 file downloaded
local file_name="$(ls -1 $JAR_STORAGE_PATH/$1/$JAR_DOWNLOAD_DIR)"
local file_name=$(ls -1 "$JAR_STORAGE_PATH/$1/$JARGROUP_DOWNLOAD_DIR")
local new_name="$(date +%F)-$file_name"
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/$JAR_DOWNLOAD_DIR/$file_name; then
if [[ ! -e "$most_recent_jar" ]] || ! diff "$most_recent_jar" "$JAR_STORAGE_PATH/$1/$JARGROUP_DOWNLOAD_DIR/$file_name"; then
# There is not a previous version to do a comparison against, or
# The previous version is different:
# Add it to the group
[[ -e $most_recent_jar ]]
[[ -e "$most_recent_jar" ]]
local was_previous=$?
as_user "mv $JAR_STORAGE_PATH/$1/$JAR_DOWNLOAD_DIR/$file_name $JAR_STORAGE_PATH/$1/$new_name"
as_user "mv '$JAR_STORAGE_PATH/$1/$JARGROUP_DOWNLOAD_DIR/$file_name' '$JAR_STORAGE_PATH/$1/$new_name'"
if [[ $was_previous == 0 ]]; then
echo "Downloaded version was different to previous latest. Saved as \"$1/$new_name\"."
@ -225,28 +238,32 @@ manager_jars_group_getlatest() {
fi
# Clean up the temp download folder
as_user "rm -r $JAR_STORAGE_PATH/$1/$JAR_DOWNLOAD_DIR"
as_user "rm -r '$JAR_STORAGE_PATH/$1/$JARGROUP_DOWNLOAD_DIR'"
else
echo "Target URL not found."
fi
else
echo "There is no jar group with the name \"$1\"."
if [[ -e "$JAR_STORAGE_PATH/$1" ]]; then
echo "There is no jar group with the name \"$1\"."
fi
fi
}
manager_jars_group_delete() {
if is_valid_name "$1" && [[ -e $JAR_STORAGE_PATH/$1 ]]; then
jargroup_delete() {
if is_valid_name "$1" && [[ -e "$JAR_STORAGE_PATH/$1" ]]; then
printf "Are you sure you want to delete this jar group [y/N]: "
read answer
if [[ $answer =~ ^y|Y|yes$ ]]; then
if [[ "$answer" =~ ^y|Y|yes$ ]]; then
as_user "rm -r $JAR_STORAGE_PATH/$1"
echo "Jar group deleted."
else
echo "Jar group was NOT deleted."
fi
else
echo "There is no jar group with the name \"$1\"."
if [[ -e "$JAR_STORAGE_PATH/$1" ]]; then
echo "There is no jar group with the name \"$1\"."
fi
fi
}
@ -263,55 +280,62 @@ case "$1" in
restart)
# Required restart option, for debian init.d scripts
;;
create)
if [ -z $2 ]; then
# If a server name is not provided
echo "Invalid command."
else
# Create a new server
manager_create_server $2
fi
;;
delete)
if [ -z $2 ]; then
# If a server name is not provided
echo "Invalid command."
else
# Delete an existing server, with confirmation
manager_delete_server $2
fi
;;
rename)
if [ -z $2 ] || [ -z $3 ]; then
# If a server name is not provided
echo "Invalid command."
else
# Rename an existing server
manager_rename_server $2 $3
fi
;;
jars)
# Jar commands
server)
case "$2" in
group)
case "$3" in
create)
manager_jars_group_create $4 $5
;;
delete)
manager_jars_group_delete $4
;;
rename)
;;
change)
;;
getlatest)
manager_jars_group_getlatest $4
;;
esac
list)
;;
"")
# Just the "jars" command
create)
if [ -z "$3" ]; then
# If a server name is not provided
echo "Invalid command."
else
# Create a new server
manager_create_server "$3"
fi
;;
delete)
if [ -z "$3" ]; then
# If a server name is not provided
echo "Invalid command."
else
# Delete an existing server, with confirmation
manager_delete_server "$3"
fi
;;
rename)
if [ -z "$3" ] || [ -z "$4" ]; then
# If a server name is not provided
echo "Invalid command."
else
# Rename an existing server
manager_rename_server "$3" "$4"
fi
;;
*)
echo "Invalid command."
;;
esac
;;
jargroup)
case "$2" in
list)
;;
create)
jargroup_create "$4" "$5"
;;
delete)
jargroup_delete "$4"
;;
rename)
;;
changetarget)
jargroup_settarget "$4" "$5"
;;
getlatest)
jargroup_getlatest "$4"
;;
*)
echo "Invalid command."
;;
esac
;;
@ -319,9 +343,10 @@ case "$1" in
echo -e "Usage: $0 command:"
echo -e
echo -e "--Setup Commands------------------------------------------------"
echo -e " create <name> \t\t\t\tCreates a new Minecraft server"
echo -e " delete <name> \t\t\t\tDeletes an existing Minecraft server"
echo -e " rename <name> <new-name> \t\t\tRenames an existing Minecraft server"
echo -e " server list \t\t\t\t\tList servers"
echo -e " server create <name> \t\t\t\tCreates a new Minecraft server"
echo -e " server delete <name> \t\t\t\tDeletes an existing Minecraft server"
echo -e " server rename <name> <new-name> \t\tRenames an existing Minecraft server"
echo -e
echo -e "--Server Mangement Commands-------------------------------------"
echo -e " <server> start \t\t\t\tStarts a server"
@ -357,12 +382,12 @@ case "$1" in
echo -e " <server> cmdlog <command> \t\t\tSame as 'cmd' but shows log output afterwards (Ctrl+C to exit)"
echo -e
echo -e "--Jar Commands--------------------------------------------------"
echo -e " jar list\t\t\t\t\tList the stored jar files."
echo -e " jar group create <name> <download-url> \tCreate a new jar directory, with the \"always latest\" download URL"
echo -e " jar group delete <name> \t\t\tDelete a jar directory"
echo -e " jar group rename <name> <new-name> \t\tRename a jar directory"
echo -e " jar group change <name> <download-url> \tChange the remove download link for a jar directory"
echo -e " jar group getlatest <name> \t\t\tDownload the latest jar file"
echo -e " jargroup list \t\t\t\tList the stored jar files."
echo -e " jargroup create <name> <download-url> \tCreate a new jar group, with a URL for new downloads"
echo -e " jargroup delete <name> \t\t\tDelete a jar group"
echo -e " jargroup rename <name> <new-name> \t\tRename a jar group"
echo -e " jargroup changeurl <name> <download-url> \tChange the download URL for a jar group"
echo -e " jargroup getlatest <name> \t\t\tDownload the latest jar file for a jar group"
echo -e
echo -e "--Global Commands-----------------------------------------------"
echo -e " start \t\t\t\t\tStarts all active servers"
@ -375,7 +400,7 @@ case "$1" in
*)
# TODO: Check first if $1 is an existing server name
if is_valid_name "$1" && [[ -e $SERVER_STORAGE_PATH/$1 ]]; then
if is_valid_name "$1" && [[ -e "$SERVER_STORAGE_PATH/$1" ]]; then
case "$2" in
start)
;;