Browse Source

fix(update): report failed updates instead of treating them as success (#5060)

* fix(update): report failed updates instead of treating them as success

When the server is running, the update modules set exitbypass before
downloading so the server is started again afterwards. A download failure
calls core_exit.sh, which only clears exitbypass and returns, so the
update carried on as if it had worked: it wrote last-updated.lock, sent
the "update" alert and exited 0. updateonstart had the same problem from
command_start.sh, where a failed remote build check left remotebuild
empty, the compare saw "" != localbuild, and an update was reported with
a blank "Remote build:" line (#4703).

The download step now runs in a subshell via a shared fn_update_apply in
command_update.sh, used by every update module and by force-update. A
core_exit.sh inside the download ends only the subshell. The server is
stopped and started again as before, but last-updated.lock is written only
after a successful download. On failure the update-failed alert is sent,
update.lock is removed and the command exits with the download's exit code.

updateonstart now runs the update in a subshell without exitbypass, so a
failed update is logged with its own exit code and the server still
starts. This replaces the exitbypass save and restore in
command_update.sh from #5056.

force-update uses the same fn_update_apply, so a failed forced download is
reported the same way.

* fix(update): mark variables used by sourced modules for shellcheck
Daniel Gibbs 2 days ago
parent
commit
543bb3f317

+ 7 - 3
lgsm/modules/command_start.sh

@@ -271,10 +271,14 @@ core_logs.sh
 
 # Will check for updates if updateonstart is yes.
 if [ "${updateonstart}" == "yes" ] || [ "${updateonstart}" == "1" ] || [ "${updateonstart}" == "on" ]; then
-	exitbypass=1
 	unset updateonstart
-	command_update.sh
-	fn_firstcommand_reset
+	if ! (
+		unset exitbypass
+		command_update.sh
+	); then
+		fn_print_warn_nl "Update failed, starting ${servername} anyway"
+		fn_script_log_warn "Update failed, starting ${servername} anyway"
+	fi
 fi
 
 fn_print_dots "${servername}"

+ 36 - 2
lgsm/modules/command_update.sh

@@ -9,7 +9,42 @@ commandname="UPDATE"
 commandaction="Updating"
 moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
 fn_firstcommand_set
-updateexitbypass="${exitbypass}"
+
+fn_update_apply() {
+	check_status.sh
+	updatestatus="${status}"
+	if [ "${updatestatus}" != "0" ]; then
+		fn_print_restart_warning
+		exitbypass=1
+		command_stop.sh
+		fn_firstcommand_reset
+	fi
+	(
+		unset exitbypass
+		"$@"
+		exit 0
+	)
+	updateexitcode=$?
+	if [ "${updateexitcode}" -eq 0 ]; then
+		date +%s > "${lockdir:?}/last-updated.lock"
+	fi
+	if [ "${updatestatus}" != "0" ]; then
+		# shellcheck disable=SC2034 # Used by command_start.sh.
+		exitbypass=1
+		command_start.sh
+		fn_firstcommand_reset
+	fi
+	unset exitbypass
+	if [ "${updateexitcode}" -ne 0 ]; then
+		rm -f "${lockdir:?}/update.lock"
+		# shellcheck disable=SC2034 # Used by alert.sh.
+		alert="update-failed"
+		alert.sh
+		# shellcheck disable=SC2034 # Used by core_exit.sh.
+		exitcode="${updateexitcode}"
+		core_exit.sh
+	fi
+}
 
 fn_print_dots ""
 check.sh
@@ -60,5 +95,4 @@ fi
 # remove update lockfile.
 rm -f "${lockdir:?}/update.lock"
 
-exitbypass="${updateexitbypass}"
 core_exit.sh

+ 1 - 18
lgsm/modules/core_steamcmd.sh

@@ -239,25 +239,8 @@ fn_update_steamcmd_compare() {
 		fn_script_log_info "${localbuild} > ${remotebuild}"
 
 		if [ "${commandname}" == "UPDATE" ]; then
-			date +%s > "${lockdir:?}/last-updated.lock"
 			unset updateonstart
-			check_status.sh
-			# If server stopped.
-			if [ "${status}" == "0" ]; then
-				fn_dl_steamcmd
-			# If server started.
-			else
-				fn_print_restart_warning
-				exitbypass=1
-				command_stop.sh
-				fn_firstcommand_reset
-				exitbypass=1
-				fn_dl_steamcmd
-				exitbypass=1
-				command_start.sh
-				fn_firstcommand_reset
-			fi
-			unset exitbypass
+			fn_update_apply fn_dl_steamcmd
 			alert="update"
 		elif [ "${commandname}" == "CHECK-UPDATE" ]; then
 			alert="check-update"

+ 5 - 21
lgsm/modules/update_bb.sh

@@ -96,31 +96,15 @@ fn_update_compare() {
 		fn_script_log_info "${localbuild} > ${remotebuild}"
 
 		if [ "${commandname}" == "UPDATE" ]; then
-			date +%s > "${lockdir:?}/last-updated.lock"
 			unset updateonstart
-			check_status.sh
-			# If server stopped.
-			if [ "${status}" == "0" ]; then
-				fn_update_dl
-				if [ "${localbuild}" == "0" ]; then
-					exitbypass=1
-					command_start.sh
-					fn_firstcommand_reset
-					exitbypass=1
-					fn_sleep_time_5
-					command_stop.sh
-					fn_firstcommand_reset
-				fi
-			# If server started.
-			else
-				fn_print_restart_warning
+			fn_update_apply fn_update_dl
+			if [ "${updatestatus}" == "0" ] && [ "${localbuild}" == "0" ]; then
 				exitbypass=1
-				command_stop.sh
+				command_start.sh
 				fn_firstcommand_reset
 				exitbypass=1
-				fn_update_dl
-				exitbypass=1
-				command_start.sh
+				fn_sleep_time_5
+				command_stop.sh
 				fn_firstcommand_reset
 			fi
 			unset exitbypass

+ 5 - 21
lgsm/modules/update_etl.sh

@@ -98,31 +98,15 @@ fn_update_compare() {
 		fn_script_log_info "${localbuild} > ${remotebuild}"
 
 		if [ "${commandname}" == "UPDATE" ]; then
-			date +%s > "${lockdir:?}/last-updated.lock"
 			unset updateonstart
-			check_status.sh
-			# If server stopped.
-			if [ "${status}" == "0" ]; then
-				fn_update_dl
-				if [ "${localbuild}" == "0" ]; then
-					exitbypass=1
-					command_start.sh
-					fn_firstcommand_reset
-					exitbypass=1
-					fn_sleep_time_5
-					command_stop.sh
-					fn_firstcommand_reset
-				fi
-			# If server started.
-			else
-				fn_print_restart_warning
+			fn_update_apply fn_update_dl
+			if [ "${updatestatus}" == "0" ] && [ "${localbuild}" == "0" ]; then
 				exitbypass=1
-				command_stop.sh
+				command_start.sh
 				fn_firstcommand_reset
 				exitbypass=1
-				fn_update_dl
-				exitbypass=1
-				command_start.sh
+				fn_sleep_time_5
+				command_stop.sh
 				fn_firstcommand_reset
 			fi
 			unset exitbypass

+ 5 - 21
lgsm/modules/update_fctr.sh

@@ -102,31 +102,15 @@ fn_update_compare() {
 		if [ "${commandname}" == "UPDATE" ]; then
 			# Keep track of the build before updating for alert purposes.
 			previousbuild="${localbuild}"
-			date +%s > "${lockdir:?}/last-updated.lock"
 			unset updateonstart
-			check_status.sh
-			# If server stopped.
-			if [ "${status}" == "0" ]; then
-				fn_update_dl
-				if [ "${localbuild}" == "0" ]; then
-					exitbypass=1
-					command_start.sh
-					fn_firstcommand_reset
-					exitbypass=1
-					fn_sleep_time_5
-					command_stop.sh
-					fn_firstcommand_reset
-				fi
-			# If server started.
-			else
-				fn_print_restart_warning
+			fn_update_apply fn_update_dl
+			if [ "${updatestatus}" == "0" ] && [ "${localbuild}" == "0" ]; then
 				exitbypass=1
-				command_stop.sh
+				command_start.sh
 				fn_firstcommand_reset
 				exitbypass=1
-				fn_update_dl
-				exitbypass=1
-				command_start.sh
+				fn_sleep_time_5
+				command_stop.sh
 				fn_firstcommand_reset
 			fi
 			# Refresh local build value after update (silent) so alerts show new version.

+ 1 - 16
lgsm/modules/update_hyt.sh

@@ -252,23 +252,8 @@ fn_update_compare() {
 		fn_script_log_info "Patchline: ${hytalepatchline}"
 
 		if [ "${commandname}" == "UPDATE" ]; then
-			date +%s > "${lockdir:?}/last-updated.lock"
 			unset updateonstart
-			check_status.sh
-			if [ "${status}" == "0" ]; then
-				fn_update_dl
-			else
-				fn_print_restart_warning
-				exitbypass=1
-				command_stop.sh
-				fn_firstcommand_reset
-				exitbypass=1
-				fn_update_dl
-				exitbypass=1
-				command_start.sh
-				fn_firstcommand_reset
-			fi
-			unset exitbypass
+			fn_update_apply fn_update_dl
 			alert="update"
 		elif [ "${commandname}" == "CHECK-UPDATE" ]; then
 			alert="check-update"

+ 5 - 21
lgsm/modules/update_jk2.sh

@@ -90,31 +90,15 @@ fn_update_compare() {
 		fn_script_log_info "${localbuild} > ${remotebuild}"
 
 		if [ "${commandname}" == "UPDATE" ]; then
-			date +%s > "${lockdir:?}/last-updated.lock"
 			unset updateonstart
-			check_status.sh
-			# If server stopped.
-			if [ "${status}" == "0" ]; then
-				fn_update_dl
-				if [ "${localbuild}" == "0" ]; then
-					exitbypass=1
-					command_start.sh
-					fn_firstcommand_reset
-					exitbypass=1
-					fn_sleep_time_5
-					command_stop.sh
-					fn_firstcommand_reset
-				fi
-			# If server started.
-			else
-				fn_print_restart_warning
+			fn_update_apply fn_update_dl
+			if [ "${updatestatus}" == "0" ] && [ "${localbuild}" == "0" ]; then
 				exitbypass=1
-				command_stop.sh
+				command_start.sh
 				fn_firstcommand_reset
 				exitbypass=1
-				fn_update_dl
-				exitbypass=1
-				command_start.sh
+				fn_sleep_time_5
+				command_stop.sh
 				fn_firstcommand_reset
 			fi
 			unset exitbypass

+ 1 - 17
lgsm/modules/update_jk3.sh

@@ -111,23 +111,7 @@ fn_update_compare() {
 
 		if [ "${commandname}" == "UPDATE" ]; then
 			unset updateonstart
-			check_status.sh
-			# If server stopped.
-			if [ "${status}" == "0" ]; then
-				fn_update_dl
-			# If server started.
-			else
-				fn_print_restart_warning
-				exitbypass=1
-				command_stop.sh
-				fn_firstcommand_reset
-				fn_update_dl
-				exitbypass=1
-				command_start.sh
-				fn_firstcommand_reset
-			fi
-			unset exitbypass
-			date +%s > "${lockdir:?}/last-updated.lock"
+			fn_update_apply fn_update_dl
 			alert="update"
 		elif [ "${commandname}" == "CHECK-UPDATE" ]; then
 			alert="check-update"

+ 5 - 21
lgsm/modules/update_mc.sh

@@ -105,31 +105,15 @@ fn_update_compare() {
 		fn_script_log_info "${localbuild} > ${remotebuild}"
 
 		if [ "${commandname}" == "UPDATE" ]; then
-			date +%s > "${lockdir:?}/last-updated.lock"
 			unset updateonstart
-			check_status.sh
-			# If server stopped.
-			if [ "${status}" == "0" ]; then
-				fn_update_dl
-				if [ "${localbuild}" == "0" ]; then
-					exitbypass=1
-					command_start.sh
-					fn_firstcommand_reset
-					exitbypass=1
-					fn_sleep_time_5
-					command_stop.sh
-					fn_firstcommand_reset
-				fi
-			# If server started.
-			else
-				fn_print_restart_warning
+			fn_update_apply fn_update_dl
+			if [ "${updatestatus}" == "0" ] && [ "${localbuild}" == "0" ]; then
 				exitbypass=1
-				command_stop.sh
+				command_start.sh
 				fn_firstcommand_reset
 				exitbypass=1
-				fn_update_dl
-				exitbypass=1
-				command_start.sh
+				fn_sleep_time_5
+				command_stop.sh
 				fn_firstcommand_reset
 			fi
 			unset exitbypass

+ 5 - 21
lgsm/modules/update_mcb.sh

@@ -116,31 +116,15 @@ fn_update_compare() {
 		fn_script_log_info "${localbuild} > ${remotebuild}"
 
 		if [ "${commandname}" == "UPDATE" ]; then
-			date +%s > "${lockdir:?}/last-updated.lock"
 			unset updateonstart
-			check_status.sh
-			# If server stopped.
-			if [ "${status}" == "0" ]; then
-				fn_update_dl
-				if [ "${localbuild}" == "0" ]; then
-					exitbypass=1
-					command_start.sh
-					fn_firstcommand_reset
-					exitbypass=1
-					fn_sleep_time_5
-					command_stop.sh
-					fn_firstcommand_reset
-				fi
-			# If server started.
-			else
-				fn_print_restart_warning
+			fn_update_apply fn_update_dl
+			if [ "${updatestatus}" == "0" ] && [ "${localbuild}" == "0" ]; then
 				exitbypass=1
-				command_stop.sh
+				command_start.sh
 				fn_firstcommand_reset
 				exitbypass=1
-				fn_update_dl
-				exitbypass=1
-				command_start.sh
+				fn_sleep_time_5
+				command_stop.sh
 				fn_firstcommand_reset
 			fi
 			unset exitbypass

+ 5 - 21
lgsm/modules/update_mta.sh

@@ -98,31 +98,15 @@ fn_update_compare() {
 		fn_script_log_info "${localbuild} > ${remotebuild}"
 
 		if [ "${commandname}" == "UPDATE" ]; then
-			date +%s > "${lockdir:?}/last-updated.lock"
 			unset updateonstart
-			check_status.sh
-			# If server stopped.
-			if [ "${status}" == "0" ]; then
-				fn_update_dl
-				if [ "${localbuild}" == "0" ]; then
-					exitbypass=1
-					command_start.sh
-					fn_firstcommand_reset
-					exitbypass=1
-					fn_sleep_time_5
-					command_stop.sh
-					fn_firstcommand_reset
-				fi
-			# If server started.
-			else
-				fn_print_restart_warning
+			fn_update_apply fn_update_dl
+			if [ "${updatestatus}" == "0" ] && [ "${localbuild}" == "0" ]; then
 				exitbypass=1
-				command_stop.sh
+				command_start.sh
 				fn_firstcommand_reset
 				exitbypass=1
-				fn_update_dl
-				exitbypass=1
-				command_start.sh
+				fn_sleep_time_5
+				command_stop.sh
 				fn_firstcommand_reset
 			fi
 			unset exitbypass

+ 5 - 21
lgsm/modules/update_pmc.sh

@@ -116,31 +116,15 @@ fn_update_compare() {
 		fn_script_log_info "${localbuild} > ${remotebuild}"
 
 		if [ "${commandname}" == "UPDATE" ]; then
-			date +%s > "${lockdir:?}/last-updated.lock"
 			unset updateonstart
-			check_status.sh
-			# If server stopped.
-			if [ "${status}" == "0" ]; then
-				fn_update_dl
-				if [ "${localbuild}" == "0" ]; then
-					exitbypass=1
-					command_start.sh
-					fn_firstcommand_reset
-					exitbypass=1
-					fn_sleep_time_5
-					command_stop.sh
-					fn_firstcommand_reset
-				fi
-			# If server started.
-			else
-				fn_print_restart_warning
+			fn_update_apply fn_update_dl
+			if [ "${updatestatus}" == "0" ] && [ "${localbuild}" == "0" ]; then
 				exitbypass=1
-				command_stop.sh
+				command_start.sh
 				fn_firstcommand_reset
 				exitbypass=1
-				fn_update_dl
-				exitbypass=1
-				command_start.sh
+				fn_sleep_time_5
+				command_stop.sh
 				fn_firstcommand_reset
 			fi
 			unset exitbypass

+ 2 - 15
lgsm/modules/update_steamcmd.sh

@@ -18,21 +18,8 @@ fn_print_dots "${remotelocation}"
 
 if [ "${forceupdate}" == "1" ]; then
 	# forceupdate bypasses update checks.
-	if [ "${status}" != "0" ] && [ -v "${status}" ]; then
-		fn_print_restart_warning
-		exitbypass=1
-		command_stop.sh
-		fn_firstcommand_reset
-		date '+%s' > "${lockdir:?}/update.lock"
-		fn_dl_steamcmd
-		date +%s > "${lockdir}/last-updated.lock"
-		exitbypass=1
-		command_start.sh
-		fn_firstcommand_reset
-	else
-		fn_dl_steamcmd
-		date +%s > "${lockdir}/last-updated.lock"
-	fi
+	date '+%s' > "${lockdir:?}/update.lock"
+	fn_update_apply fn_dl_steamcmd
 else
 	fn_update_steamcmd_localbuild
 	fn_update_steamcmd_remotebuild

+ 5 - 21
lgsm/modules/update_ts3.sh

@@ -97,31 +97,15 @@ fn_update_compare() {
 		fn_script_log_info "${localbuild} > ${remotebuild}"
 
 		if [ "${commandname}" == "UPDATE" ]; then
-			date +%s > "${lockdir:?}/last-updated.lock"
 			unset updateonstart
-			check_status.sh
-			# If server stopped.
-			if [ "${status}" == "0" ]; then
-				fn_update_dl
-				if [ "${localbuild}" == "0" ]; then
-					exitbypass=1
-					command_start.sh
-					fn_firstcommand_reset
-					exitbypass=1
-					fn_sleep_time_5
-					command_stop.sh
-					fn_firstcommand_reset
-				fi
-			# If server started.
-			else
-				fn_print_restart_warning
+			fn_update_apply fn_update_dl
+			if [ "${updatestatus}" == "0" ] && [ "${localbuild}" == "0" ]; then
 				exitbypass=1
-				command_stop.sh
+				command_start.sh
 				fn_firstcommand_reset
 				exitbypass=1
-				fn_update_dl
-				exitbypass=1
-				command_start.sh
+				fn_sleep_time_5
+				command_stop.sh
 				fn_firstcommand_reset
 			fi
 			unset exitbypass

+ 5 - 21
lgsm/modules/update_ut99.sh

@@ -91,31 +91,15 @@ fn_update_compare() {
 		fn_script_log_info "${localbuild} > ${remotebuild}"
 
 		if [ "${commandname}" == "UPDATE" ]; then
-			date +%s > "${lockdir:?}/last-updated.lock"
 			unset updateonstart
-			check_status.sh
-			# If server stopped.
-			if [ "${status}" == "0" ]; then
-				fn_update_dl
-				if [ "${localbuild}" == "0" ]; then
-					exitbypass=1
-					command_start.sh
-					fn_firstcommand_reset
-					exitbypass=1
-					fn_sleep_time_5
-					command_stop.sh
-					fn_firstcommand_reset
-				fi
-			# If server started.
-			else
-				fn_print_restart_warning
+			fn_update_apply fn_update_dl
+			if [ "${updatestatus}" == "0" ] && [ "${localbuild}" == "0" ]; then
 				exitbypass=1
-				command_stop.sh
+				command_start.sh
 				fn_firstcommand_reset
 				exitbypass=1
-				fn_update_dl
-				exitbypass=1
-				command_start.sh
+				fn_sleep_time_5
+				command_stop.sh
 				fn_firstcommand_reset
 			fi
 			unset exitbypass

+ 5 - 21
lgsm/modules/update_vints.sh

@@ -102,31 +102,15 @@ fn_update_compare() {
 		fn_script_log_info "${localbuild} > ${remotebuild}"
 
 		if [ "${commandname}" == "UPDATE" ]; then
-			date +%s > "${lockdir:?}/last-updated.lock"
 			unset updateonstart
-			check_status.sh
-			# If server stopped.
-			if [ "${status}" == "0" ]; then
-				fn_update_dl
-				if [ "${localbuild}" == "0" ]; then
-					exitbypass=1
-					command_start.sh
-					fn_firstcommand_reset
-					exitbypass=1
-					fn_sleep_time_5
-					command_stop.sh
-					fn_firstcommand_reset
-				fi
-			# If server started.
-			else
-				fn_print_restart_warning
+			fn_update_apply fn_update_dl
+			if [ "${updatestatus}" == "0" ] && [ "${localbuild}" == "0" ]; then
 				exitbypass=1
-				command_stop.sh
+				command_start.sh
 				fn_firstcommand_reset
 				exitbypass=1
-				fn_update_dl
-				exitbypass=1
-				command_start.sh
+				fn_sleep_time_5
+				command_stop.sh
 				fn_firstcommand_reset
 			fi
 			unset exitbypass

+ 5 - 21
lgsm/modules/update_xnt.sh

@@ -111,31 +111,15 @@ fn_update_compare() {
 		fn_script_log_info "${localbuild} > ${remotebuild}"
 
 		if [ "${commandname}" == "UPDATE" ]; then
-			date +%s > "${lockdir:?}/last-updated.lock"
 			unset updateonstart
-			check_status.sh
-			# If server stopped.
-			if [ "${status}" == "0" ]; then
-				fn_update_dl
-				if [ "${localbuild}" == "0" ]; then
-					exitbypass=1
-					command_start.sh
-					fn_firstcommand_reset
-					exitbypass=1
-					fn_sleep_time_5
-					command_stop.sh
-					fn_firstcommand_reset
-				fi
-			# If server started.
-			else
-				fn_print_restart_warning
+			fn_update_apply fn_update_dl
+			if [ "${updatestatus}" == "0" ] && [ "${localbuild}" == "0" ]; then
 				exitbypass=1
-				command_stop.sh
+				command_start.sh
 				fn_firstcommand_reset
 				exitbypass=1
-				fn_update_dl
-				exitbypass=1
-				command_start.sh
+				fn_sleep_time_5
+				command_stop.sh
 				fn_firstcommand_reset
 			fi
 			unset exitbypass