Bash_Scripts/CentOS/Minecraft/Minecraft-Start-Server.bash

383 lines
11 KiB
Bash
Raw Normal View History

2021-07-31 13:45:43 -05:00
#!/bin/bash
2021-08-04 01:21:11 -05:00
2021-07-31 13:45:43 -05:00
### CONSTANTS ###
#gigabytes
2021-07-31 18:45:24 -05:00
MAX_MEM=8
2021-07-31 13:45:43 -05:00
#gigabytes
INITIAL_MEM=2
2021-07-31 18:45:24 -05:00
#rcon password
2021-07-31 19:19:21 -05:00
DEFAULT_RCON_PASSWORD="bqMLwxCJKRktrQoir2pg4KkTeBDQLjb4C+RYesdmeKF4sie8"
2021-07-31 18:45:24 -05:00
2021-08-03 18:24:40 -05:00
2021-07-31 18:45:24 -05:00
echo_rgb() {
# Echo a colored string to the terminal based on rgb values
#
# Positional Arguments:
#
# message <type: string> <position: 1> <required: true>
# - The message to be printed to stdout
# red <type: int> <position: 2> <required: true>
# - The red value from 0 to 255
# green <type: int> <position: 3> <required: true>
# - The green value from 0 to 255
# blue <type: int> <position: 4> <required: true>
# - The blue value from 0 to 255
#
# Usage:
2021-08-03 18:24:40 -05:00
# echo_rgb "Yep" 10 8 30
2021-07-31 18:45:24 -05:00
#
# POSIX Compliant:
# N/A
#
local red
local green
local blue
local input
input="${1}"
red="${2}"
green="${3}"
blue="${4}"
printf "\e[0;38;2;%s;%s;%sm%s\e[m\n" "${red}" "${green}" "${blue}" "${input}"
}
2021-07-31 13:45:43 -05:00
log() {
# Print a message and send it to stdout or stderr depending upon log level, also configurable with debug etc.
#
# Arguments:
# level <type: string> <position: 1> <required: true>
# - The log level, defined within a case check in this function
# message <type: string> <position: 2> <required: true>
# - The info message
# line_number <type: int> <position: 3> <required: false>
# - The line number of the calling function (${LINNO})
#
# Usage:
# log "info" "Could not find that directory"
#
# POSIX Compliant:
# Yes
#
# Set debug status depending if a global debug variable has been set to either 1 or 0
local debug
if [ ${DEBUG} ]; then
debug=${DEBUG}
else
debug=0
fi
local FORMAT
2021-07-31 22:26:22 -05:00
FORMAT="[$(echo_rgb "$(date +%Y-%m-%dT%H:%M:%S)" 180 140 255)]"
2021-07-31 13:45:43 -05:00
# Convert the level to uppercase
local level
level=$(echo "${1}" | tr '[:lower:]' '[:upper:]')
local message
message="${2}"
case "${level}" in
INFO)
# Output all info log levels to stdout
2021-07-31 18:45:24 -05:00
printf "${FORMAT}[$(echo_rgb "INFO" 0 140 255)] %s\n" "${message}" >&1
return 0
;;
WARN | WARNING)
# Output all info log levels to stdout
printf "${FORMAT}[$(echo_rgb "WARNING" 255 255 0)] %s\n" "${message}" >&1
2021-07-31 13:45:43 -05:00
return 0
;;
DEBUG)
2021-08-03 18:24:40 -05:00
[[ ${debug} == 0 ]] && return
2021-07-31 18:45:24 -05:00
printf "${FORMAT}[$(echo_rgb "DEBUG" 0 160 110)] %s\n" "${message}" >&1
2021-07-31 13:45:43 -05:00
return 0
;;
ERROR)
# Output all error log levels to stderr
2021-07-31 18:45:24 -05:00
printf "${FORMAT}[$(echo_rgb "ERROR" 255 0 0)] %s\n" "${message}" >&2
2021-07-31 13:45:43 -05:00
return 0
;;
# Further log levels can be added by extending this switch statement with more comparisons
*) # Default case, no matches
# Returns non-zero code as an improper log option was passed, this helps with using `set -e`
2021-07-31 18:45:24 -05:00
printf "${FORMAT}[ERROR] %s\n" "Invalid log level passed, received level \"${level}\" with message \"${message}\"" >&2
2021-07-31 13:45:43 -05:00
return 1
2021-07-31 18:45:24 -05:00
;;
2021-07-31 13:45:43 -05:00
esac
}
2021-07-31 18:45:24 -05:00
confirmation() {
# Receive confirmation from user as y, Y, n, or N
# returns 0 when answer is yes and 1 when answer is no
#
# Arguments:
# message <type: string> <position: 1> <required: true>
# - The confirmation prompt sent to the user, for example:
# Would you like to overwrite foobar.txt (y/N)?
#
# Usage:
# confirmation "Some prompt"
# - Sends "Some prompt" to the user and gets their input
#
# POSIX Compliant:
# Yes
#
local message
message="${1}"
local choice
while true; do
read -p "${message} " -n 1 -r choice
case "$choice" in
y | Y)
echo ""
return 0
;;
n | N)
echo ""
return 1
;;
*) echo -e "\nInput must be either y, Y, n, or N" ;;
esac
done
}
2021-07-31 13:45:43 -05:00
usage() {
# Print out usage instructions for the local script
#
# Arguments:
# None
#
# Usage:
# usage
#
# POSIX Compliant:
# Yes
#
2021-07-31 19:19:21 -05:00
printf "Usage: %s\n" \
2021-08-03 18:24:40 -05:00
"$(basename "${0}") -s <server id> | $(basename "${0}") -s <server id> -r | $(basename "${0}") -s <server id> -i <spigot revision>
-s <server id> | --server <server id>
2021-07-31 19:19:21 -05:00
Which minecraft server to start, see the ~/Minecraft directory -- each number corresponds to an ID
Example:
--server 1
2021-08-03 18:24:40 -05:00
-r | --rcon-ignore
2021-07-31 19:19:21 -05:00
Flag that takes no parameters -- when enabled this script will not overwrite the RCON password in the targeted server
2021-07-31 13:45:43 -05:00
Example:
2021-07-31 19:19:21 -05:00
--rcon-ignore
2021-08-03 18:24:40 -05:00
-i | --install
Installs a minecraft server to the given server id from the -s flag with the given revision to this arguemnt.
To see all the revisions visit https://www.spigotmc.org/wiki/buildtools/
2021-07-31 19:19:21 -05:00
2021-07-31 13:45:43 -05:00
Example:
2021-08-03 18:24:40 -05:00
--install latest"
2021-07-31 13:45:43 -05:00
}
2021-07-31 19:19:21 -05:00
2021-07-31 13:45:43 -05:00
server_id=""
2021-08-03 18:24:40 -05:00
revision=""
2021-07-31 18:45:24 -05:00
rcon_ignore=0
2021-07-31 13:45:43 -05:00
parse_args() {
# Parse input arguments
#
# Arguments:
# Consult the `usage` function
#
# Usage:
# parse_args "$@"
# - All arguments should be ingested by parse_args first for variable setting
#
# POSIX Compliant:
# Yes
#
while :; do
case ${1} in
-h | -\? | --help)
usage # Display a usage synopsis.
exit
;;
--) # End of all options.
break
;;
-s | --server)
shift
server_id="${1}"
2021-08-03 18:24:40 -05:00
[[ -z "${server_id}" ]] \
&& log "error" "No server ID provided" \
&& exit 1
[[ "${server_id}" =~ [0-9] ]] || (log "error" "Server ID must be a number, received: ${server_id}" && exit 1)
2021-07-31 13:45:43 -05:00
;;
2021-08-03 18:24:40 -05:00
-i | --install)
2021-07-31 13:45:43 -05:00
shift
2021-08-03 18:24:40 -05:00
revision="${1}"
[[ -z "${revision}" ]] \
&& log "error" "The install flag was given, but no argument was received" \
&& exit 1
2021-07-31 13:45:43 -05:00
;;
2021-07-31 18:45:24 -05:00
-r | --rcon-ignore)
rcon_ignore=1
;;
2021-07-31 13:45:43 -05:00
-?*)
printf 'Unknown option: %s\n' "$1" >&2
usage
;;
*) # Default case: No more options, so break out of the loop.
break ;;
esac
shift
done
}
parse_args "$@"
# Check and see that the correct variables were set, some may not be expanded as if they're empty they're useless
# anyhow
2021-08-03 18:24:40 -05:00
2021-07-31 13:45:43 -05:00
minecraft_directory=~/Minecraft/Server-"${server_id}"
2021-07-31 19:19:21 -05:00
create_directories() {
mkdir -p "${minecraft_directory}"
mkdir -p "${minecraft_directory}/backups"
cd "${minecraft_directory}" || (log "error" "Unable to change directory to ${minecraft_directory}" && exit 1)
}
2021-07-31 18:45:24 -05:00
2021-08-03 18:24:40 -05:00
display_minecraft_server_name() {
echo_rgb "Minecraft-Server-${server_id}" 208 158 255
}
2021-07-31 13:45:43 -05:00
2021-07-31 19:19:21 -05:00
2021-08-03 18:24:40 -05:00
if [ -n "${revision}" ]; then
[[ -d "${minecraft_directory}" ]] \
&& log "error" "The minecraft server $(display_minecraft_server_name) already exists in ${minecraft_directory}, delete the directory or do not pass the \"--install\" flag" \
&& exit 1
create_directories
cd ~/
if [ ! -d ~/Build-Tools ]; then
log "info" "Spigot Build Tools was not found, installing Spigot Build Tools now..."
mkdir -p Build-Tools && cd Build-Tools
mkdir -p Builds
log "info" "Downloading Spigot Build Tools..."
curl -o BuildTools.jar https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar \
&& log "info" "Finished downloading Spigot Build Tools"
fi
cd ~/Build-Tools
log "info" "Building Spigot revision $(echo_rgb "${revision}" 0 255 195), this may take some time..."
java -jar BuildTools.jar --rev "${revision}" > ./build.log 2>&1
build_return_code="${?}"
[[ "${build_return_code}" != "0" ]] \
&& log "error" "Could not build the given revision, received error code ${build_return_code} see build.log" \
&& exit "${build_return_code}"
log "info" "Finished building Spigot"
2021-07-31 18:45:24 -05:00
log "info" "Copying minecraft server files..."
2021-08-03 18:24:40 -05:00
cp spigot*.jar "${minecraft_directory}"/ \
2021-07-31 18:45:24 -05:00
&& log "info" "Finished copying files"
2021-08-03 18:24:40 -05:00
mv spigot*.jar Builds
mv "${minecraft_directory}/"spigot*.jar "${minecraft_directory}/server.jar"
log "info" "Installed a minecraft server.jar to $(display_minecraft_server_name) located at ${minecraft_directory}"
cd "${minecraft_directory}"
java -jar "${minecraft_directory}"/server.jar nogui > setup.log 2>&1
log "info" "Successfully setup the server.jar for $(display_minecraft_server_name)"
2021-07-31 19:19:21 -05:00
elif [ ! -f "${minecraft_directory}"/server.jar ]; then
2021-08-03 18:24:40 -05:00
log "error" "No server.jar found within ${minecraft_directory}, to create a new server there use the \"--install\" argument"
2021-07-31 13:45:43 -05:00
exit 1
fi
server_port=$(("12000" + "${server_id}"))
query_port=$(("12100" + "${server_id}"))
rcon_port=$(("12200" + "${server_id}"))
2021-08-03 18:24:40 -05:00
create_directories
2021-07-31 13:45:43 -05:00
2021-07-31 18:45:24 -05:00
## Create a backup of the config file
backup_extension="$(date +%Y-%m-%dT%H:%M:%S%z)"
log "info" "Making a backup of the server.properties file in ${minecraft_directory}/backups/server.properties.${backup_extension}"
rm -f
cp "${minecraft_directory}/server.properties" "${minecraft_directory}/backups/server.properties.${backup_extension}" \
&& log "info" "Successfully created backup"
2021-07-31 13:45:43 -05:00
## Set the correct ports
2021-07-31 18:45:24 -05:00
sed -i "s/server-port=.*/server-port=${server_port}/g" server.properties \
&& log "info" "Set the server port to ${server_port}" || exit 1
sed -i "s/query.port=.*/query.port=${query_port}/g" server.properties \
&& log "info" "Set the query port to ${query_port}" || exit 1
sed -i "s/rcon.port=.*/rcon.port=${rcon_port}/g" server.properties \
&& log "info" "Set the RCON port to ${rcon_port}" || exit 1
2021-07-31 13:45:43 -05:00
## Turn RCON On
2021-07-31 18:45:24 -05:00
# set the rcon password to the default if rcon-ignore is not set
if [[ "${rcon_ignore}" == 0 ]]; then
log "info" "Setting the RCON password..."
[[ -z "${DEFAULT_RCON_PASSWORD}" ]] \
&& log "warning" "No RCON password has been defined within $(basename "${0}"), consider generating one with \"openssl rand -base64 36\""
2021-08-02 07:06:49 -05:00
sed -i "s/rcon.password=.*/rcon.password=${DEFAULT_RCON_PASSWORD}/g" server.properties \
2021-07-31 18:45:24 -05:00
&& log "info" "RCON password set"
2021-08-02 07:06:49 -05:00
sed -i "s/enable-rcon=false/enable-rcon=true/g" server.properties \
&& log "info" "RCON Enabled"
2021-07-31 18:45:24 -05:00
fi
2021-07-31 13:45:43 -05:00
2021-07-31 18:45:24 -05:00
## Accept the eula
sed -i "s/eula=false/eula=true/g" eula.txt \
&& log "info" "Minecraft eula accepted"
## Go ahead and start 'er on up
tmux has-session -t "Minecraft-Server-${server_id}" > /dev/null 2>&1
if [ "${?}" == 0 ]; then
2021-08-03 18:24:40 -05:00
log "warning" "Minecraft server $(display_minecraft_server_name) is currently running"
2021-07-31 18:45:24 -05:00
confirmation "Would you like to kill it and then run the new one (y/N)?"
if [ "${?}" == 0 ]; then
2021-08-03 18:24:40 -05:00
log "info" "Ok, killing server $(display_minecraft_server_name)"
tmux kill-session -t "" \
&& log "info" "Successfully killed $(display_minecraft_server_name)"
2021-07-31 18:45:24 -05:00
else
2021-08-03 18:24:40 -05:00
log "info" "Not ending current $(display_minecraft_server_name), exiting..."
2021-07-31 18:45:24 -05:00
exit 0
fi
fi
2021-07-31 13:45:43 -05:00
2021-07-31 18:45:24 -05:00
log "info" \
"Startup Arguments:
-Xms${INITIAL_MEM}G
-Xmx${MAX_MEM}G
-jar"
2021-07-31 13:45:43 -05:00
2021-08-03 18:24:40 -05:00
log "info" "Starting $(display_minecraft_server_name)"
2021-07-31 13:45:43 -05:00
2021-07-31 18:45:24 -05:00
tmux new-session -d -s \
"Minecraft-Server-${server_id}" \
2021-08-03 18:24:40 -05:00
java -Xms"${INITIAL_MEM}G" -Xmx"${MAX_MEM}G" -XX:+UseG1GC -jar server.jar nogui \
2021-07-31 19:19:21 -05:00
&& log "info" \
2021-08-03 18:24:40 -05:00
"Server $(display_minecraft_server_name) started successfully:
$(echo_rgb \
"Game Port: ${server_port}
2021-07-31 19:19:21 -05:00
Query Port: ${query_port}
2021-08-03 18:24:40 -05:00
RCON Port: ${rcon_port}" 0 255 195)"