Folder Restructuring

This commit is contained in:
Price Hiller 2021-08-03 18:24:40 -05:00
parent 4632cbfd52
commit 7c71d7b836
9 changed files with 177 additions and 64 deletions

View File

@ -11,6 +11,7 @@ INITIAL_MEM=2
#rcon password
DEFAULT_RCON_PASSWORD="bqMLwxCJKRktrQoir2pg4KkTeBDQLjb4C+RYesdmeKF4sie8"
echo_rgb() {
# Echo a colored string to the terminal based on rgb values
#
@ -26,7 +27,7 @@ echo_rgb() {
# - The blue value from 0 to 255
#
# Usage:
# echo_rgb 10 80 3 "Yep"
# echo_rgb "Yep" 10 8 30
#
# POSIX Compliant:
# N/A
@ -93,7 +94,7 @@ log() {
return 0
;;
DEBUG)
[[ ${debug} == 0 ]] && return 0
[[ ${debug} == 0 ]] && return
printf "${FORMAT}[$(echo_rgb "DEBUG" 0 160 110)] %s\n" "${message}" >&1
return 0
;;
@ -165,28 +166,29 @@ usage() {
# Yes
#
printf "Usage: %s\n" \
"$(basename "${0}") -s <server id> | $(basename "${0}") -s <server id> -r | $(basename "${0}") -s <server id> -j <path/to/minecraft/jar>
--server <server id> -s <server id>
"$(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>
Which minecraft server to start, see the ~/Minecraft directory -- each number corresponds to an ID
Example:
--server 1
--r | --rcon-ignore
-r | --rcon-ignore
Flag that takes no parameters -- when enabled this script will not overwrite the RCON password in the targeted server
Example:
--rcon-ignore
--jar <path/to/mc/server/jar/file> | -j <path/to/mc/server/jar/file>
Takes in a path to a minecraft server jar file. It copies that file to create the new server
-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/
Example:
--jar ~/Minecraft-Server-Jars/minecraft_server1.17.1.jar"
--install latest"
}
server_id=""
minecraft_jar=""
revision=""
rcon_ignore=0
parse_args() {
@ -215,10 +217,17 @@ parse_args() {
-s | --server)
shift
server_id="${1}"
[[ -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)
;;
-j | --jar)
-i | --install)
shift
minecraft_jar="${1}"
revision="${1}"
[[ -z "${revision}" ]] \
&& log "error" "The install flag was given, but no argument was received" \
&& exit 1
;;
-r | --rcon-ignore)
rcon_ignore=1
@ -238,8 +247,8 @@ 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}"
@ -249,24 +258,46 @@ create_directories() {
cd "${minecraft_directory}" || (log "error" "Unable to change directory to ${minecraft_directory}" && exit 1)
}
create_directories
display_minecraft_server_name() {
echo_rgb "Minecraft-Server-${server_id}" 208 158 255
}
if [ -n "${minecraft_jar}" ]; then
if [ ! -f "${minecraft_jar}" ]; then
log "error" "Could not find a minecraft server jar \"${minecraft_jar}\""
exit 1
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
minecraft_jar_name="$(basename "${minecraft_jar}")"
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"
log "info" "Copying minecraft server files..."
cp "${minecraft_jar}" "${minecraft_directory}" \
cp spigot*.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"
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)"
elif [ ! -f "${minecraft_directory}"/server.jar ]; then
log "error" "No server.jar found within ${minecraft_directory}, to create a new server there use the \"--jar\" flag"
log "error" "No server.jar found within ${minecraft_directory}, to create a new server there use the \"--install\" argument"
exit 1
fi
@ -274,6 +305,7 @@ server_port=$(("12000" + "${server_id}"))
query_port=$(("12100" + "${server_id}"))
rcon_port=$(("12200" + "${server_id}"))
create_directories
## Create a backup of the config file
backup_extension="$(date +%Y-%m-%dT%H:%M:%S%z)"
@ -318,14 +350,14 @@ sed -i "s/eula=false/eula=true/g" eula.txt \
tmux has-session -t "Minecraft-Server-${server_id}" > /dev/null 2>&1
if [ "${?}" == 0 ]; then
log "warning" "That minecraft server is currently running"
log "warning" "Minecraft server $(display_minecraft_server_name) 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"
log "info" "Ok, killing server $(display_minecraft_server_name)"
tmux kill-session -t "" \
&& log "info" "Successfully killed $(display_minecraft_server_name)"
else
log "info" "Not ending current Minecraft-Server-${server_id}, exiting..."
log "info" "Not ending current $(display_minecraft_server_name), exiting..."
exit 0
fi
fi
@ -336,13 +368,14 @@ log "info" \
-Xmx${MAX_MEM}G
-jar"
log "info" "Starting Minecraft-Server-${server_id}"
log "info" "Starting $(display_minecraft_server_name)"
tmux new-session -d -s \
"Minecraft-Server-${server_id}" \
java -Xms"${INITIAL_MEM}G" -Xmx"${MAX_MEM}G" -jar server.jar --nogui \
java -Xms"${INITIAL_MEM}G" -Xmx"${MAX_MEM}G" -XX:+UseG1GC -jar server.jar nogui \
&& log "info" \
"Server Minecraft-Server-${server_id} started successfully:
Game Port: ${server_port}
"Server $(display_minecraft_server_name) started successfully:
$(echo_rgb \
"Game Port: ${server_port}
Query Port: ${query_port}
RCON Port: ${rcon_port}"
RCON Port: ${rcon_port}" 0 255 195)"

View File

@ -1,28 +0,0 @@
#!/bin/bash --posix
# shellcheck disable=SC2120
read_sensitive() {
# Securely read input into a variable
#
# Arguments:
# READVARIABLE <type: variable> <position: 1> <required: true>
# - A variable must be passed and the output of this function will be set to that variable
# e.g: read_sensitive MYVAR
#
# Usage:
# read_sensitive SOMEVAR
#
# POSIX Compliant:
# Yes
#
stty -echo
local INPUT
read -r INPUT
stty echo
printf "\n"
eval "${1}=${INPUT}"
}
printf "Please input a password: " && read_sensitive PASSWORD_VARIABLE
echo "Received your password as ${PASSWORD_VARIABLE}"

View File

@ -0,0 +1,6 @@
#!/bin/bash
check_root() {
[[ "$(id -u)" = 0 ]] || (echo "Root permissions required, exiting" && exit 1)
}

View File

@ -0,0 +1,102 @@
#!/bin/bash
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 "Yep" 10 8 30
#
# 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}"
}
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
FORMAT="[$(echo_rgb "$(date +%Y-%m-%dT%H:%M:%S)" 180 140 255)]"
# 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
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
return 0
;;
DEBUG)
[[ ${debug} == 0 ]] && return
printf "${FORMAT}[$(echo_rgb "DEBUG" 0 160 110)] %s\n" "${message}" >&1
return 0
;;
ERROR)
# Output all error log levels to stderr
printf "${FORMAT}[$(echo_rgb "ERROR" 255 0 0)] %s\n" "${message}" >&2
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`
printf "${FORMAT}[ERROR] %s\n" "Invalid log level passed, received level \"${level}\" with message \"${message}\"" >&2
return 1
;;
esac
}