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

330 lines
8.7 KiB
Bash
Raw Normal View History

2021-07-31 13:45:43 -05:00
#!/bin/bash
### 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
DEFAULT_RCON_PASSWORD=""
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:
# echo_rgb 10 80 3 "Yep"
#
# 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 18:45:24 -05:00
FORMAT="[$(echo_rgb "$(date +%Y-%m-%dT%H:%M:%S%z)" 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)
[[ ${debug} == 0 ]] && return 0
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
#
printf "Usage: %s%s\n" \
"$(basename ${0}) " \
"
-i \"this is some input\" -t \"this is some more example input\"
--input <string> | -i <string>
Example:
--input \"this is an example input\"
--test <string> | -t <string>
Example:
--test \"this is more example input\""
}
server_id=""
minecraft_jar=""
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}"
;;
-j | --jar)
shift
minecraft_jar="${1}"
;;
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
[[ -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)
minecraft_directory=~/Minecraft/Server-"${server_id}"
mkdir -p "${minecraft_directory}"
2021-07-31 18:45:24 -05:00
mkdir -p "${minecraft_directory}/backups"
2021-07-31 13:45:43 -05:00
cd "${minecraft_directory}" || (log "error" "Unable to change directory to ${minecraft_directory}" && exit 1)
2021-07-31 18:45:24 -05:00
if [ -n "${minecraft_jar}" ]; then
if [ ! -f "${minecraft_jar}" ]; then
log "error" "Could not find a minecraft server jar \"${minecraft_jar}\""
exit 1
fi
minecraft_jar_name="$(basename "${minecraft_jar}")"
log "info" "Copying minecraft server files..."
cp "${minecraft_jar}" "${minecraft_directory}" \
&& log "info" "Finished copying files"
mv "${minecraft_directory}/${minecraft_jar_name}" "${minecraft_directory}/server.jar"
log "info" "Installed a minecraft server.jar to Minecraft-Server-${server_id}"
java -jar "${minecraft_directory}"/server.jar > /dev/null 2>&1
log "info" "Successfully installed the minecraft server.jar to Minecraft-Server-${server_id}, continuing with setup"
2021-07-31 13:45:43 -05:00
elif [ ! -d "${minecraft_directory}" ]; then
log "error" "The minecraft server \"${minecraft_directory}\" did not exist, to create it please pass the \"--jar\" flag..."
exit 1
fi
server_port=$(("12000" + "${server_id}"))
query_port=$(("12100" + "${server_id}"))
rcon_port=$(("12200" + "${server_id}"))
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\""
sed -i "s/rcon.password=.+/rcon.password=${DEFAULT_RCON_PASSWORD}/g" server.properties \
&& log "info" "RCON password set"
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
log "warning" "That minecraft server is currently running"
confirmation "Would you like to kill it and then run the new one (y/N)?"
if [ "${?}" == 0 ]; then
log "info" "Ok, killing server..."
tmux kill-session -t "Minecraft-Server-${server_id}" \
&& log "info" "Successfully killed the server"
else
log "info" "Not ending current Minecraft-Server-${server_id}, exiting..."
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-07-31 18:45:24 -05:00
log "info" "Starting Minecraft-Server-${server_id}"
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}" \
java -Xms"${INITIAL_MEM}G" -Xmx"${MAX_MEM}G" -jar server.jar --nogui \
&& log "info" "Server Minecraft-Server-${server_id} started successfully!"