4
0

update_fctr.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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}-${remotebuildversion}.tar.xz" "${serverfiles}" "factorio"
  12. fn_clear_tmp
  13. }
  14. fn_update_localbuild() {
  15. # Gets local build info.
  16. fn_print_dots "Checking local build: ${remotelocation}"
  17. # Uses executable to get local build.
  18. if [ -d "${executabledir}" ]; then
  19. cd "${executabledir}" || exit
  20. localbuild=$(${executable} --version | grep -m 1 "Version:" | awk '{print $2}')
  21. fi
  22. if [ -z "${localbuild}" ]; then
  23. fn_print_error "Checking local build: ${remotelocation}: missing local build info"
  24. fn_script_log_error "Missing local build info"
  25. fn_script_log_error "Set localbuild to 0"
  26. localbuild="0"
  27. else
  28. fn_print_ok "Checking local build: ${remotelocation}"
  29. fn_script_log_pass "Checking local build"
  30. fi
  31. }
  32. fn_update_remotebuild() {
  33. # Gets remote build info.
  34. apiurl="https://factorio.com/get-download/${branch}/headless/${factorioarch}"
  35. remotebuildresponse=$(curl -s "${apiurl}")
  36. remotebuildversion=$(echo "${remotebuildresponse}" | grep -o '[0-9]\.[0-9]\{1,\}\.[0-9]\{1,\}' | head -1)
  37. remotebuildurl="https://factorio.com/get-download/${branch}/headless/${factorioarch}"
  38. remotebuildfilename="factorio_headless_${factorioarch}-${remotebuildversion}.tar.xz"
  39. if [ "${firstcommandname}" != "INSTALL" ]; then
  40. fn_print_dots "Checking remote build: ${remotelocation}"
  41. # Checks if remotebuildversion variable has been set.
  42. if [ -z "${remotebuildversion}" ] || [ "${remotebuildversion}" == "null" ]; then
  43. fn_print_fail "Checking remote build: ${remotelocation}"
  44. fn_script_log_fail "Checking remote build"
  45. core_exit.sh
  46. else
  47. fn_print_ok "Checking remote build: ${remotelocation}"
  48. fn_script_log_pass "Checking remote build"
  49. fi
  50. else
  51. # Checks if remotebuild variable has been set.
  52. if [ -z "${remotebuildversion}" ] || [ "${remotebuildversion}" == "null" ]; then
  53. fn_print_failure "Unable to get remote build"
  54. fn_script_log_fail "Unable to get remote build"
  55. core_exit.sh
  56. fi
  57. fi
  58. }
  59. fn_update_compare() {
  60. fn_print_dots "Checking for update: ${remotelocation}"
  61. # Update has been found or force update.
  62. if [ "${localbuild}" != "${remotebuildversion}" ] || [ "${forceupdate}" == "1" ]; then
  63. # Create update lockfile.
  64. date '+%s' > "${lockdir:?}/update.lock"
  65. fn_print_ok_nl "Checking for update: ${remotelocation}"
  66. echo -en "\n"
  67. echo -e "Update available"
  68. echo -e "* Local build: ${red}${localbuild} ${factorioarch}${default}"
  69. echo -e "* Remote build: ${green}${remotebuildversion} ${factorioarch}${default}"
  70. if [ -n "${branch}" ]; then
  71. echo -e "* Branch: ${branch}"
  72. fi
  73. if [ -f "${rootdir}/.dev-debug" ]; then
  74. echo -e "Remote build info"
  75. echo -e "* apiurl: ${apiurl}"
  76. echo -e "* remotebuildfilename: ${remotebuildfilename}"
  77. echo -e "* remotebuildurl: ${remotebuildurl}"
  78. echo -e "* remotebuildversion: ${remotebuildversion}"
  79. fi
  80. echo -en "\n"
  81. fn_script_log_info "Update available"
  82. fn_script_log_info "Local build: ${localbuild} ${factorioarch}"
  83. fn_script_log_info "Remote build: ${remotebuildversion} ${factorioarch}"
  84. if [ -n "${branch}" ]; then
  85. fn_script_log_info "Branch: ${branch}"
  86. fi
  87. fn_script_log_info "${localbuild} > ${remotebuildversion}"
  88. if [ "${commandname}" == "UPDATE" ]; then
  89. date +%s > "${lockdir}/last-updated.lock"
  90. unset updateonstart
  91. check_status.sh
  92. # If server stopped.
  93. if [ "${status}" == "0" ]; then
  94. fn_update_dl
  95. if [ "${localbuild}" == "0" ]; then
  96. exitbypass=1
  97. command_start.sh
  98. fn_firstcommand_reset
  99. exitbypass=1
  100. fn_sleep_time_5
  101. command_stop.sh
  102. fn_firstcommand_reset
  103. fi
  104. # If server started.
  105. else
  106. fn_print_restart_warning
  107. exitbypass=1
  108. command_stop.sh
  109. fn_firstcommand_reset
  110. exitbypass=1
  111. fn_update_dl
  112. exitbypass=1
  113. command_start.sh
  114. fn_firstcommand_reset
  115. fi
  116. unset exitbypass
  117. alert="update"
  118. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  119. alert="check-update"
  120. fi
  121. alert.sh
  122. else
  123. fn_print_ok_nl "Checking for update: ${remotelocation}"
  124. echo -en "\n"
  125. echo -e "No update available"
  126. echo -e "* Local build: ${green}${localbuild} ${factorioarch}${default}"
  127. echo -e "* Remote build: ${green}${remotebuildversion} ${factorioarch}${default}"
  128. if [ -n "${branch}" ]; then
  129. echo -e "* Branch: ${branch}"
  130. fi
  131. echo -en "\n"
  132. fn_script_log_info "No update available"
  133. fn_script_log_info "Local build: ${localbuild} ${factorioarch}"
  134. fn_script_log_info "Remote build: ${remotebuildversion} ${factorioarch}"
  135. if [ -n "${branch}" ]; then
  136. fn_script_log_info "Branch: ${branch}"
  137. fi
  138. if [ -f "${rootdir}/.dev-debug" ]; then
  139. echo -e "Remote build info"
  140. echo -e "* apiurl: ${apiurl}"
  141. echo -e "* remotebuildfilename: ${remotebuildfilename}"
  142. echo -e "* remotebuildurl: ${remotebuildurl}"
  143. echo -e "* remotebuildversion: ${remotebuildversion}"
  144. fi
  145. fi
  146. }
  147. # Game server architecture.
  148. factorioarch="linux64"
  149. # The location where the builds are checked and downloaded.
  150. remotelocation="factorio.com"
  151. if [ "${firstcommandname}" == "INSTALL" ]; then
  152. fn_update_remotebuild
  153. fn_update_dl
  154. else
  155. fn_print_dots "Checking for update"
  156. fn_print_dots "Checking for update: ${remotelocation}"
  157. fn_script_log_info "Checking for update: ${remotelocation}"
  158. fn_update_localbuild
  159. fn_update_remotebuild
  160. fn_update_compare
  161. fi