update_fctr.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #!/bin/bash
  2. # LinuxGSM update_fctr.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of Factorio servers.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. fn_update_dl() {
  9. # Download and extract files to serverfiles.
  10. fn_fetch_file "${remotebuildurl}" "" "" "" "${tmpdir}" "${remotebuildfilename}" "nochmodx" "norun" "force" "nohash"
  11. fn_dl_extract "${tmpdir}" "factorio_headless_${factorioarch}-${remotebuild}.tar.xz" "${serverfiles}" "factorio"
  12. fn_clear_tmp
  13. }
  14. fn_update_localbuild() {
  15. # Optional arg1: 'silent' to suppress user-facing output
  16. local mode="${1:-}"
  17. if [ "${mode}" != "silent" ]; then
  18. fn_print_dots "Checking local build: ${remotelocation}"
  19. fi
  20. # Uses executable to get local build.
  21. if [ -d "${executabledir}" ]; then
  22. cd "${executabledir}" || exit
  23. localbuild=$(${executable} --version | grep -m 1 "Version:" | awk '{print $2}')
  24. fi
  25. if [ -z "${localbuild}" ]; then
  26. if [ "${mode}" != "silent" ]; then
  27. fn_print_error "Checking local build: ${remotelocation}: missing local build info"
  28. fi
  29. fn_script_log_error "Missing local build info"
  30. fn_script_log_error "Set localbuild to 0"
  31. localbuild="0"
  32. else
  33. if [ "${mode}" != "silent" ]; then
  34. fn_print_ok "Checking local build: ${remotelocation}"
  35. fi
  36. fn_script_log_pass "Checking local build"
  37. fi
  38. }
  39. fn_update_remotebuild() {
  40. # Gets remote build info.
  41. apiurl="https://factorio.com/get-download/${branch}/headless/${factorioarch}"
  42. remotebuildresponse=$(curl -s "${apiurl}")
  43. remotebuild=$(echo "${remotebuildresponse}" | grep -o '[0-9]\.[0-9]\{1,\}\.[0-9]\{1,\}' | head -1)
  44. remotebuildurl="https://factorio.com/get-download/${branch}/headless/${factorioarch}"
  45. remotebuildfilename="factorio_headless_${factorioarch}-${remotebuild}.tar.xz"
  46. if [ "${firstcommandname}" != "INSTALL" ]; then
  47. fn_print_dots "Checking remote build: ${remotelocation}"
  48. # Checks if remotebuild variable has been set.
  49. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  50. fn_print_fail "Checking remote build: ${remotelocation}"
  51. fn_script_log_fail "Checking remote build"
  52. core_exit.sh
  53. else
  54. fn_print_ok "Checking remote build: ${remotelocation}"
  55. fn_script_log_pass "Checking remote build"
  56. fi
  57. else
  58. # Checks if remotebuild variable has been set.
  59. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  60. fn_print_failure "Unable to get remote build"
  61. fn_script_log_fail "Unable to get remote build"
  62. core_exit.sh
  63. fi
  64. fi
  65. }
  66. fn_update_compare() {
  67. fn_print_dots "Checking for update: ${remotelocation}"
  68. # Update has been found or force update.
  69. if [ "${localbuild}" != "${remotebuild}" ] || [ "${forceupdate}" == "1" ]; then
  70. # Create update lockfile.
  71. date '+%s' > "${lockdir:?}/update.lock"
  72. fn_print_ok_nl "Checking for update: ${remotelocation}"
  73. fn_print "\n"
  74. fn_print_nl "${bold}${underline}Update${default} available"
  75. fn_print_nl "* Local build: ${red}${localbuild}${default}"
  76. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  77. if [ -n "${branch}" ]; then
  78. fn_print_nl "* Branch: ${branch}"
  79. fi
  80. if [ -f "${rootdir}/.dev-debug" ]; then
  81. fn_print_nl "Remote build info"
  82. fn_print_nl "* apiurl: ${apiurl}"
  83. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  84. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  85. fn_print_nl "* remotebuild: ${remotebuild}"
  86. fi
  87. fn_print "\n"
  88. fn_script_log_info "Update available"
  89. fn_script_log_info "Local build: ${localbuild} ${factorioarch}"
  90. fn_script_log_info "Remote build: ${remotebuild} ${factorioarch}"
  91. if [ -n "${branch}" ]; then
  92. fn_script_log_info "Branch: ${branch}"
  93. fi
  94. fn_script_log_info "${localbuild} > ${remotebuild}"
  95. if [ "${commandname}" == "UPDATE" ]; then
  96. # Keep track of the build before updating for alert purposes.
  97. previousbuild="${localbuild}"
  98. date +%s > "${lockdir:?}/last-updated.lock"
  99. unset updateonstart
  100. check_status.sh
  101. # If server stopped.
  102. if [ "${status}" == "0" ]; then
  103. fn_update_dl
  104. if [ "${localbuild}" == "0" ]; then
  105. exitbypass=1
  106. command_start.sh
  107. fn_firstcommand_reset
  108. exitbypass=1
  109. fn_sleep_time_5
  110. command_stop.sh
  111. fn_firstcommand_reset
  112. fi
  113. # If server started.
  114. else
  115. fn_print_restart_warning
  116. exitbypass=1
  117. command_stop.sh
  118. fn_firstcommand_reset
  119. exitbypass=1
  120. fn_update_dl
  121. exitbypass=1
  122. command_start.sh
  123. fn_firstcommand_reset
  124. fi
  125. # Refresh local build value after update (silent) so alerts show new version.
  126. fn_update_localbuild silent
  127. # Verify the update applied correctly unless forced update to same version.
  128. if [ "${localbuild}" != "${remotebuild}" ]; then
  129. # If forced update and version unchanged treat as acceptable; otherwise failure.
  130. if [ "${forceupdate}" != "1" ] || [ "${previousbuild}" = "${localbuild}" ]; then
  131. fn_script_log_error "Update verification failed: expected ${remotebuild}, got ${localbuild}"
  132. fn_print_fail_nl "Update verification failed: expected ${remotebuild}, got ${localbuild}"
  133. updatefailureexpected="${remotebuild}"
  134. updatefailuregot="${localbuild}"
  135. alert="update-failed"
  136. alert.sh
  137. core_exit.sh
  138. fi
  139. else
  140. fn_script_log_pass "Update verification success: ${previousbuild} -> ${localbuild}"
  141. fi
  142. unset exitbypass
  143. alert="update"
  144. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  145. alert="check-update"
  146. fi
  147. alert.sh
  148. else
  149. fn_print_ok_nl "Checking for update: ${remotelocation}"
  150. fn_print "\n"
  151. fn_print_nl "${bold}${underline}No update${default} available"
  152. fn_print_nl "* Local build: ${green}${localbuild}${default}"
  153. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  154. if [ -n "${branch}" ]; then
  155. fn_print_nl "* Branch: ${branch}"
  156. fi
  157. fn_print "\n"
  158. fn_script_log_info "No update available"
  159. fn_script_log_info "Local build: ${localbuild} ${factorioarch}"
  160. fn_script_log_info "Remote build: ${remotebuild} ${factorioarch}"
  161. if [ -n "${branch}" ]; then
  162. fn_script_log_info "Branch: ${branch}"
  163. fi
  164. if [ -f "${rootdir}/.dev-debug" ]; then
  165. fn_print_nl "Remote build info"
  166. fn_print_nl "* apiurl: ${apiurl}"
  167. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  168. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  169. fn_print_nl "* remotebuild: ${remotebuild}"
  170. fi
  171. fi
  172. }
  173. # Game server architecture.
  174. factorioarch="linux64"
  175. # The location where the builds are checked and downloaded.
  176. remotelocation="factorio.com"
  177. if [ "${firstcommandname}" == "INSTALL" ]; then
  178. fn_update_remotebuild
  179. fn_update_dl
  180. else
  181. fn_print_dots "Checking for update"
  182. fn_print_dots "Checking for update: ${remotelocation}"
  183. fn_script_log_info "Checking for update: ${remotelocation}"
  184. fn_update_localbuild
  185. fn_update_remotebuild
  186. fn_update_compare
  187. fi