From c5a3e2e37683a9ed5e99633a6ae21768a6e59fe3 Mon Sep 17 00:00:00 2001 From: Daniel Ramp <12480131+DanielRamp@users.noreply.github.com> Date: Sun, 18 Jul 2021 01:44:18 +0200 Subject: [PATCH] Cache spiget version resolution (#978) --- start-spiget | 70 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 17 deletions(-) diff --git a/start-spiget b/start-spiget index 6773d800..fe957331 100644 --- a/start-spiget +++ b/start-spiget @@ -6,6 +6,7 @@ IFS=$'\n\t' handleDebugMode : ${SPIGET_RESOURCES:=} +: ${SPIGET_DOWNLOAD_TOLERANCE:=5} # in minutes containsJars() { file=${1?} @@ -28,26 +29,61 @@ getResourceFromSpiget() { mkdir -p /data/plugins - if [ -f /data/plugins/.${resource} ]; then - log "Resource '${resource}' already downloaded" - else - tmpfile="/tmp/${resource}.zip" - url="https://api.spiget.org/v2/resources/${resource}/download" - if ! curl -o "${tmpfile}" -fsSL -H "User-Agent: itzg/minecraft-server" "${extraCurlArgs[@]}" "${url}"; then - log "ERROR failed to download resource '${resource}' from ${url}" - exit 2 - fi + versionfile="/data/plugins/.${resource}-version.json" + versionfileNew="/tmp/.${resource}-version.json" - if containsJars "${tmpfile}"; then - log "Extracting contents of resource ${resource} into plugins" - unzip -o -q -d /data/plugins "${tmpfile}" - touch "/data/plugins/.${resource}" - rm "${tmpfile}" + if [ -f "$versionfile" ]; then + if [[ -n $(find "$versionfile" -mmin +${SPIGET_DOWNLOAD_TOLERANCE}) ]]; then + urlVersion="https://api.spiget.org/v2/resources/${resource}/versions/latest" + if ! curl -o "${versionfileNew}" -fsSL -H "User-Agent: itzg/minecraft-server" "${extraCurlArgs[@]}" "${urlVersion}"; then + log "ERROR failed to download resource version meta data '${resource}' from ${urlVersion}" + exit 2 + fi + + installedVersion=$(jq '.name' $versionfile) + newVersion=$(jq '.name' $versionfileNew) + + if [ "$installedVersion" = "$newVersion" ]; then + log "resource '${resource}' not downloaded because installed version '${installedVersion}' already up to date ('${newVersion}')" + mv "${versionfileNew}" "${versionfile}" + else + if downloadResourceFromSpiget "${resource}"; then + mv "${versionfileNew}" "${versionfile}" + fi + fi else - log "Moving resource ${resource} into plugins" - mv "${tmpfile}" "/data/plugins/${resource}.jar" - touch "/data/plugins/.${resource}" + log "resource '${resource}' not checked because version meta file newer than '${SPIGET_DOWNLOAD_TOLERANCE}' minutes" fi + else + if downloadResourceFromSpiget "${resource}"; then + urlVersion="https://api.spiget.org/v2/resources/${resource}/versions/latest" + if ! curl -o "${versionfileNew}" -fsSL -H "User-Agent: itzg/minecraft-server" "${extraCurlArgs[@]}" "${urlVersion}"; then + log "ERROR failed to download resource version meta data '${resource}' from ${urlVersion}" + exit 2 + fi + mv "${versionfileNew}" "${versionfile}" + fi + fi + +} + +downloadResourceFromSpiget() { + resource=${1?} + + tmpfile="/tmp/${resource}.zip" + url="https://api.spiget.org/v2/resources/${resource}/download" + if ! curl -o "${tmpfile}" -fsSL -H "User-Agent: itzg/minecraft-server" "${extraCurlArgs[@]}" "${url}"; then + log "ERROR failed to download resource '${resource}' from ${url}" + exit 2 + fi + + if containsJars "${tmpfile}"; then + log "Extracting contents of resource ${resource} into plugins" + unzip -o -q -d /data/plugins "${tmpfile}" + rm "${tmpfile}" + else + log "Moving resource ${resource} into plugins" + mv "${tmpfile}" "/data/plugins/${resource}.jar" fi }