2018-01-07 23:18:45 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
function join_by() {
|
|
|
|
local d=$1
|
|
|
|
shift
|
|
|
|
echo -n "$1"
|
|
|
|
shift
|
|
|
|
printf "%s" "${@/#/$d}"
|
|
|
|
}
|
2020-07-18 23:31:29 +00:00
|
|
|
|
2022-03-06 20:57:00 +00:00
|
|
|
function get_major_version() {
|
|
|
|
version=$1
|
|
|
|
echo "$version" | cut -d. -f 1-2
|
|
|
|
}
|
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
function isURL() {
|
2018-01-07 23:18:45 +00:00
|
|
|
local value=$1
|
|
|
|
|
2021-04-28 21:44:39 +00:00
|
|
|
if [[ ${value:0:8} == "https://" || ${value:0:7} == "http://" || ${value:0:6} == "ftp://" ]]; then
|
2018-01-07 23:18:45 +00:00
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
function isValidFileURL() {
|
|
|
|
suffix=${1:?Missing required suffix arg}
|
|
|
|
url=${2:?Missing required url arg}
|
|
|
|
|
|
|
|
[[ "$url" == http*://*.${suffix} || "$url" == http*://*.${suffix}\?* ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
function resolveEffectiveUrl() {
|
|
|
|
url="${1:?Missing required url argument}"
|
|
|
|
if ! curl -Ls -o /dev/null -w %{url_effective} "$url"; then
|
|
|
|
log "ERROR failed to resolve effective URL from $url"
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFilenameFromUrl() {
|
|
|
|
url="${1:?Missing required url argument}"
|
|
|
|
strippedOfQuery="${url%\?*}"
|
|
|
|
basename "$strippedOfQuery"
|
|
|
|
}
|
|
|
|
|
|
|
|
function isTrue() {
|
2022-02-05 18:27:17 +00:00
|
|
|
local oldState
|
|
|
|
oldState=$(shopt -po xtrace)
|
|
|
|
shopt -u -o xtrace
|
|
|
|
|
2018-01-07 23:18:45 +00:00
|
|
|
local value=${1,,}
|
|
|
|
|
|
|
|
result=
|
|
|
|
|
|
|
|
case ${value} in
|
2020-12-12 21:22:07 +00:00
|
|
|
true | on)
|
|
|
|
result=0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
result=1
|
|
|
|
;;
|
2018-01-07 23:18:45 +00:00
|
|
|
esac
|
|
|
|
|
2022-02-05 18:27:17 +00:00
|
|
|
eval "$oldState"
|
2018-01-07 23:18:45 +00:00
|
|
|
return ${result}
|
2019-02-03 15:49:59 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
function isDebugging() {
|
2021-03-21 16:43:21 +00:00
|
|
|
if isTrue "${DEBUG:-false}"; then
|
2019-02-03 15:49:59 +00:00
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-03-21 16:43:21 +00:00
|
|
|
function handleDebugMode() {
|
|
|
|
if isDebugging; then
|
|
|
|
set -x
|
|
|
|
extraCurlArgs=(-v)
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
function debug() {
|
2019-02-03 15:49:59 +00:00
|
|
|
if isDebugging; then
|
2020-03-06 15:52:17 +00:00
|
|
|
log "DEBUG: $*"
|
2019-02-03 15:49:59 +00:00
|
|
|
fi
|
2019-11-21 19:50:06 +00:00
|
|
|
}
|
2020-03-06 15:52:17 +00:00
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
function logn() {
|
2020-03-06 15:52:17 +00:00
|
|
|
echo -n "[init] $*"
|
|
|
|
}
|
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
function log() {
|
2022-02-05 18:27:17 +00:00
|
|
|
local oldState
|
|
|
|
# The return status when listing options is zero if all optnames are enabled, non- zero otherwise.
|
|
|
|
oldState=$(shopt -po xtrace || true)
|
|
|
|
shopt -u -o xtrace
|
|
|
|
|
|
|
|
if isDebugging || isTrue "${LOG_TIMESTAMP:-false}"; then
|
|
|
|
ts=" $(date --rfc-3339=seconds)"
|
|
|
|
else
|
|
|
|
ts=
|
|
|
|
fi
|
|
|
|
echo "[init]${ts} $*"
|
|
|
|
eval "$oldState"
|
2020-03-06 15:52:17 +00:00
|
|
|
}
|
2020-04-11 15:41:20 +00:00
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
function logAutopause() {
|
2020-05-23 14:15:10 +00:00
|
|
|
echo "[Autopause loop] $*"
|
|
|
|
}
|
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
function logAutopauseAction() {
|
2020-05-23 14:15:10 +00:00
|
|
|
echo "[$(date -Iseconds)] [Autopause] $*"
|
|
|
|
}
|
|
|
|
|
2021-12-21 00:27:27 +00:00
|
|
|
function logAutostop() {
|
|
|
|
echo "[Autostop loop] $*"
|
|
|
|
}
|
|
|
|
|
|
|
|
function logAutostopAction() {
|
|
|
|
echo "[$(date -Iseconds)] [Autostop] $*"
|
|
|
|
}
|
|
|
|
|
2022-03-02 17:29:12 +00:00
|
|
|
function logRcon() {
|
|
|
|
echo "[Rcon loop] $*"
|
|
|
|
}
|
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
function normalizeMemSize() {
|
2020-04-11 15:41:20 +00:00
|
|
|
local scale=1
|
|
|
|
case ${1,,} in
|
2020-12-12 21:22:07 +00:00
|
|
|
*k)
|
|
|
|
scale=1024
|
|
|
|
;;
|
|
|
|
*m)
|
|
|
|
scale=1048576
|
|
|
|
;;
|
|
|
|
*g)
|
|
|
|
scale=1073741824
|
|
|
|
;;
|
2020-04-11 15:41:20 +00:00
|
|
|
esac
|
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
val=${1:0:-1}
|
|
|
|
echo $((val * scale))
|
2020-04-11 15:41:20 +00:00
|
|
|
}
|
2020-04-13 23:29:44 +00:00
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
function versionLessThan() {
|
2021-12-11 02:50:40 +00:00
|
|
|
mc-image-helper compare-versions "${VANILLA_VERSION}" lt "${1?}"
|
2020-06-19 16:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
requireVar() {
|
|
|
|
if [ ! -v $1 ]; then
|
|
|
|
log "ERROR: $1 is required to be set"
|
|
|
|
exit 1
|
|
|
|
fi
|
2020-07-19 20:01:19 +00:00
|
|
|
if [ -z "${!1}" ]; then
|
|
|
|
log "ERROR: $1 is required to be set"
|
|
|
|
exit 1
|
|
|
|
fi
|
2020-06-19 16:31:56 +00:00
|
|
|
}
|
2020-07-26 18:19:45 +00:00
|
|
|
|
2021-02-09 02:42:54 +00:00
|
|
|
requireEnum() {
|
|
|
|
var=${1?}
|
|
|
|
shift
|
|
|
|
|
|
|
|
for allowed in $*; do
|
|
|
|
if [[ ${!var} = $allowed ]]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2021-12-11 02:50:40 +00:00
|
|
|
log "ERROR: $var must be set to one of $*"
|
2021-02-09 02:42:54 +00:00
|
|
|
# exit 1
|
|
|
|
}
|
|
|
|
|
2020-07-26 18:19:45 +00:00
|
|
|
function writeEula() {
|
2021-01-20 03:36:21 +00:00
|
|
|
if ! echo "# Generated via Docker
|
|
|
|
# $(date)
|
2020-07-26 18:19:45 +00:00
|
|
|
eula=${EULA,,}
|
2020-12-12 21:22:07 +00:00
|
|
|
" >/data/eula.txt; then
|
|
|
|
log "ERROR: unable to write eula to /data. Please make sure attached directory is writable by uid=${UID}"
|
|
|
|
exit 2
|
|
|
|
fi
|
2020-07-26 18:19:45 +00:00
|
|
|
}
|
2021-04-28 21:29:47 +00:00
|
|
|
|
|
|
|
function removeOldMods {
|
|
|
|
if [ -d "$1" ]; then
|
2021-05-15 17:39:48 +00:00
|
|
|
find "$1" -mindepth 1 -maxdepth ${REMOVE_OLD_MODS_DEPTH:-16} -wholename "${REMOVE_OLD_MODS_INCLUDE:-*}" -not -wholename "${REMOVE_OLD_MODS_EXCLUDE:-}" -delete
|
2021-04-28 21:29:47 +00:00
|
|
|
fi
|
|
|
|
}
|
2021-10-09 20:22:42 +00:00
|
|
|
|
|
|
|
function get() {
|
2021-10-10 14:53:52 +00:00
|
|
|
local flags=()
|
2021-10-10 17:05:37 +00:00
|
|
|
if isTrue "${DEBUG_GET:-false}"; then
|
2021-10-10 14:53:52 +00:00
|
|
|
flags+=("--debug")
|
|
|
|
fi
|
|
|
|
mc-image-helper "${flags[@]}" get "$@"
|
2021-12-11 02:50:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isFamily() {
|
|
|
|
for f in "${@}"; do
|
2022-02-09 01:24:38 +00:00
|
|
|
if [[ ${FAMILY^^} == "${f^^}" ]]; then
|
2021-12-11 02:50:40 +00:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
2022-01-17 02:49:15 +00:00
|
|
|
return 1
|
|
|
|
}
|
2022-02-09 01:24:38 +00:00
|
|
|
|
2022-01-17 02:49:15 +00:00
|
|
|
function isType() {
|
|
|
|
for t in "${@}"; do
|
2022-01-29 20:53:34 +00:00
|
|
|
# shellcheck disable=SC2153
|
2022-01-17 02:49:15 +00:00
|
|
|
if [[ $TYPE == "$t" ]]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
2021-12-11 02:50:40 +00:00
|
|
|
return 1
|
2022-01-24 04:19:25 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 20:53:34 +00:00
|
|
|
function evaluateJavaCompatibilityForForge() {
|
|
|
|
javaRelease=$(mc-image-helper java-release)
|
|
|
|
if versionLessThan 1.18 && (( javaRelease > 8 )); then
|
|
|
|
log "**********************************************************************"
|
|
|
|
log "WARNING: Some mods and modpacks may require Java 8."
|
|
|
|
log " Please use itzg/minecraft-server:java8"
|
|
|
|
log "**********************************************************************"
|
|
|
|
sleep 5
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-01-24 04:19:25 +00:00
|
|
|
function extract() {
|
|
|
|
src=${1?}
|
|
|
|
destDir=${2?}
|
|
|
|
|
|
|
|
type=$(file -b --mime-type "${src}")
|
2022-01-25 22:19:15 +00:00
|
|
|
case "${type}" in
|
|
|
|
application/zip)
|
2022-03-15 02:07:06 +00:00
|
|
|
unzip -o -q -d "${destDir}" "${src}"
|
2022-01-25 22:19:15 +00:00
|
|
|
;;
|
2022-02-12 03:00:24 +00:00
|
|
|
application/x-tar|application/gzip|application/x-gzip|application/x-bzip2|application/zstd|application/x-zstd)
|
2022-01-25 22:19:15 +00:00
|
|
|
tar -C "${destDir}" -xf "${src}"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
log "ERROR: unsupported archive type: $type"
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
2022-02-09 01:17:26 +00:00
|
|
|
|
2022-02-12 03:00:24 +00:00
|
|
|
function getDistro() {
|
|
|
|
cat /etc/os-release | grep -E "^ID=" | cut -d= -f2 | sed -e 's/"//g'
|
|
|
|
}
|
|
|
|
|
2022-02-09 01:17:26 +00:00
|
|
|
function checkSum() {
|
|
|
|
local sum_file=${1?}
|
|
|
|
|
|
|
|
# Get distro
|
2022-02-12 03:00:24 +00:00
|
|
|
distro=$(getDistro)
|
2022-02-09 01:17:26 +00:00
|
|
|
|
|
|
|
if [ "${distro}" == "debian" ] && sha1sum -c "${sum_file}" --status 2> /dev/null; then
|
|
|
|
return 0
|
|
|
|
elif [ "${distro}" == "ubuntu" ] && sha1sum -c "${sum_file}" --status 2> /dev/null; then
|
|
|
|
return 0
|
|
|
|
elif [ "${distro}" == "alpine" ] && sha1sum -c "${sum_file}" -s 2> /dev/null; then
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|