Răsfoiți Sursa

fix(update-lgsm): build updated game script in tmpdir and restore backup on failure (#5118)

* fix(update-lgsm): build updated game script in tmpdir and restore backup on failure

sed -i writes its temporary file next to the target, so editing the game
script in place needed write access to rootdir itself. Without it the
copy from linuxgsm.sh succeeded but the substitutions failed, leaving the
script with the "core" placeholders ("unknown game server"). Only the last
sed call was checked, and the backup taken just before was never restored.

The script is now assembled in tmpdir with a single sed call and copied
over the existing file, which only needs write access to the file. If
that copy fails, the backup is restored. The backup path is computed once
so the logged location matches the file actually written.

Fixes #3060

* fix(update-lgsm): restore executable bit on command_update_linuxgsm.sh
Daniel Gibbs 4 zile în urmă
părinte
comite
a3b92f79f5
1 a modificat fișierele cu 32 adăugiri și 11 ștergeri
  1. 32 11
      lgsm/modules/command_update_linuxgsm.sh

+ 32 - 11
lgsm/modules/command_update_linuxgsm.sh

@@ -90,7 +90,8 @@ if [ "${script_diff}" != "" ]; then
 	if [ ! -d "${backupdir}/script" ]; then
 		mkdir -p "${backupdir}/script"
 	fi
-	cp "${rootdir}/${selfname}" "${backupdir}/script/${selfname}-$(date +"%m_%d_%Y_%M").bak"
+	scriptbackup="${backupdir}/script/${selfname}-$(date +"%m_%d_%Y_%M").bak"
+	cp "${rootdir}/${selfname}" "${scriptbackup}"
 	exitcode=$?
 	if [ "${exitcode}" -ne 0 ]; then
 		fn_print_fail_eol_nl
@@ -98,25 +99,45 @@ if [ "${script_diff}" != "" ]; then
 		core_exit.sh
 	else
 		fn_print_ok_eol_nl
-		fn_script_log_pass "Backup script${selfname}"
-		echo -e "backup location [ ${backupdir}/script/${selfname}-$(date +"%m_%d_%Y_%M").bak ]"
-		fn_script_log_pass "Backup location ${backupdir}/script/${selfname}-$(date +"%m_%d_%Y_%M").bak"
+		fn_script_log_pass "Backup script ${selfname}"
+		echo -e "backup location [ ${scriptbackup} ]"
+		fn_script_log_pass "Backup location ${scriptbackup}"
 	fi
 
 	echo -en "copying ${selfname}"
 	fn_script_log_info "Copying ${selfname}"
-	cp "${tmpdir}/linuxgsm.sh" "${rootdir}/${selfname}"
-	sed -i "s+shortname=\"core\"+shortname=\"${shortname}\"+g" "${rootdir}/${selfname}"
-	sed -i "s+gameservername=\"core\"+gameservername=\"${gameservername}\"+g" "${rootdir}/${selfname}"
-	sed -i "s+gamename=\"core\"+gamename=\"${gamename}\"+g" "${rootdir}/${selfname}"
-	sed -i "s+githubuser=\"GameServerManagers\"+githubuser=\"${githubuser}\"+g" "${rootdir}/${selfname}"
-	sed -i "s+githubrepo=\"LinuxGSM\"+githubrepo=\"${githubrepo}\"+g" "${rootdir}/${selfname}"
-	sed -i "s+githubbranch=\"master\"+githubbranch=\"${githubbranch}\"+g" "${rootdir}/${selfname}"
+	cp "${tmpdir}/linuxgsm.sh" "${tmpdir}/${selfname}.update" \
+		&& sed -i \
+			-e "s+shortname=\"core\"+shortname=\"${shortname}\"+g" \
+			-e "s+gameservername=\"core\"+gameservername=\"${gameservername}\"+g" \
+			-e "s+gamename=\"core\"+gamename=\"${gamename}\"+g" \
+			-e "s+githubuser=\"GameServerManagers\"+githubuser=\"${githubuser}\"+g" \
+			-e "s+githubrepo=\"LinuxGSM\"+githubrepo=\"${githubrepo}\"+g" \
+			-e "s+githubbranch=\"master\"+githubbranch=\"${githubbranch}\"+g" \
+			"${tmpdir}/${selfname}.update"
+	exitcode=$?
+	if [ "${exitcode}" -ne 0 ]; then
+		fn_print_fail_eol_nl
+		fn_script_log_fail "Preparing ${selfname} in ${tmpdir}"
+		rm -f "${tmpdir:?}/${selfname}.update"
+		core_exit.sh
+	fi
 
+	cp "${tmpdir}/${selfname}.update" "${rootdir}/${selfname}"
 	exitcode=$?
+	rm -f "${tmpdir:?}/${selfname}.update"
 	if [ "${exitcode}" -ne 0 ]; then
 		fn_print_fail_eol_nl
 		fn_script_log_fail "copying ${selfname}"
+		echo -en "restoring ${selfname} from backup\c"
+		fn_script_log_info "Restoring ${selfname} from ${scriptbackup}"
+		if cp "${scriptbackup}" "${rootdir}/${selfname}"; then
+			fn_print_ok_eol_nl
+			fn_script_log_pass "Restoring ${selfname} from backup"
+		else
+			fn_print_fail_eol_nl
+			fn_script_log_fail "Restoring ${selfname} from backup ${scriptbackup}"
+		fi
 		core_exit.sh
 	else
 		fn_print_ok_eol_nl