update_factorio.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/bin/bash
  2. # LinuxGSM update_factorio.sh function
  3. # Author: Daniel Gibbs
  4. # Website: https://linuxgsm.com
  5. # Description: Handles updating of Factorio servers.
  6. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  7. fn_update_factorio_dl(){
  8. fn_fetch_file "https://factorio.com/get-download/${downloadbranch}/headless/${factorioarch}" "" "" "" "${tmpdir}" "factorio_headless_${factorioarch}-${remotebuild}.tar.xz" "" "norun" "noforce" "nomd5"
  9. fn_dl_extract "${tmpdir}" "factorio_headless_${factorioarch}-${remotebuild}.tar.xz" "${tmpdir}"
  10. echo -e "copying to ${serverfiles}...\c"
  11. cp -R "${tmpdir}/factorio/"* "${serverfiles}"
  12. local exitcode=$?
  13. if [ "${exitcode}" == "0" ]; then
  14. fn_print_ok_eol_nl
  15. fn_script_log_pass "Copying to ${serverfiles}"
  16. fn_clear_tmp
  17. else
  18. fn_print_fail_eol_nl
  19. fn_script_log_fatal "Copying to ${serverfiles}"
  20. core_exit.sh
  21. fi
  22. }
  23. fn_update_factorio_localbuild(){
  24. # Gets local build info.
  25. fn_print_dots "Checking local build: ${remotelocation}"
  26. # Uses executable to find local build.
  27. cd "${executabledir}" || exit
  28. if [ -f "${executable}" ]; then
  29. localbuild=$(${executable} --version | grep "Version:" | awk '{print $2}')
  30. fn_print_ok "Checking for update: ${remotelocation}: checking local build"
  31. fn_script_log_pass "Checking local build"
  32. else
  33. localbuild="0"
  34. fn_print_error "Checking for update: ${remotelocation}: checking local build"
  35. fn_script_log_error "Checking local build"
  36. fi
  37. }
  38. fn_update_factorio_remotebuild(){
  39. # Gets remote build info.
  40. remotebuild=$(curl -s "https://factorio.com/get-download/${downloadbranch}/headless/${factorioarch}" | grep -o '[0-9]\.[0-9]\{1,\}\.[0-9]\{1,\}' | head -1)
  41. if [ "${installer}" != "1" ]; then
  42. fn_print_dots "Checking remote build: ${remotelocation}"
  43. # Checks if remotebuild variable has been set.
  44. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  45. fn_print_fail "Checking remote build: ${remotelocation}"
  46. fn_script_log_fatal "Checking remote build"
  47. core_exit.sh
  48. else
  49. fn_print_ok "Checking remote build: ${remotelocation}"
  50. fn_script_log_pass "Checking remote build"
  51. fi
  52. else
  53. # Checks if remotebuild variable has been set.
  54. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  55. fn_print_failure "Unable to get remote build"
  56. fn_script_log_fatal "Unable to get remote build"
  57. core_exit.sh
  58. fi
  59. fi
  60. }
  61. fn_update_factorio_compare(){
  62. fn_print_dots "Checking for update: ${remotelocation}"
  63. # Removes dots so if statement can compare version numbers.
  64. fn_print_dots "Checking for update: ${remotelocation}"
  65. localbuilddigit=$(echo -e "${localbuild}" | tr -cd '[:digit:]')
  66. remotebuilddigit=$(echo -e "${remotebuild}" | tr -cd '[:digit:]')
  67. if [ "${localbuilddigit}" -ne "${remotebuilddigit}" ]||[ "${forceupdate}" == "1" ]; then
  68. fn_print_ok_nl "Checking for update: ${remotelocation}"
  69. echo -en "\n"
  70. echo -e "Update available"
  71. echo -e "* Local build: ${red}${localbuild} ${factorioarch}${default}"
  72. echo -e "* Remote build: ${green}${remotebuild} ${factorioarch}${default}"
  73. if [ -n "${branch}" ]; then
  74. echo -e "* Branch: ${branch}"
  75. fi
  76. fn_script_log_info "Update available"
  77. fn_script_log_info "Local build: ${localbuild} ${factorioarch}"
  78. fn_script_log_info "Remote build: ${remotebuild} ${factorioarch}"
  79. if [ -v "${branch}" ]; then
  80. fn_script_log_info "Branch: ${branch}"
  81. fi
  82. fn_script_log_info "${localbuild} > ${remotebuild}"
  83. unset updateonstart
  84. check_status.sh
  85. # If server stopped.
  86. if [ "${status}" == "0" ]; then
  87. exitbypass=1
  88. fn_update_factorio_dl
  89. exitbypass=1
  90. command_start.sh
  91. exitbypass=1
  92. command_stop.sh
  93. # If server started.
  94. else
  95. fn_stop_warning
  96. exitbypass=1
  97. command_stop.sh
  98. exitbypass=1
  99. fn_update_factorio_dl
  100. exitbypass=1
  101. command_start.sh
  102. fi
  103. date +%s > "${lockdir}/lastupdate.lock"
  104. alert="update"
  105. alert.sh
  106. else
  107. fn_print_ok_nl "Checking for update: ${remotelocation}"
  108. echo -en "\n"
  109. echo -e "No update available"
  110. echo -e "* Local build: ${green}${localbuild} ${factorioarch}${default}"
  111. echo -e "* Remote build: ${green}${remotebuild} ${factorioarch}${default}"
  112. if [ -v "${branch}" ]; then
  113. echo -e "* Branch: ${branch}"
  114. fi
  115. fn_script_log_info "No update available"
  116. fn_script_log_info "Local build: ${localbuild} ${factorioarch}"
  117. fn_script_log_info "Remote build: ${remotebuild} ${factorioarch}"
  118. if [ -v "${branch}" ]; then
  119. fn_script_log_info "Branch: ${branch}"
  120. fi
  121. fi
  122. }
  123. fn_stop_warning(){
  124. fn_print_warn "Updating server: SteamCMD: ${selfname} will be stopped during update"
  125. fn_script_log_warn "Updating server: SteamCMD: ${selfname} will be stopped during update"
  126. totalseconds=3
  127. for seconds in {3..1}; do
  128. fn_print_warn "Updating server: SteamCMD: ${selfname} will be stopped during update: ${totalseconds}"
  129. totalseconds=$((totalseconds - 1))
  130. sleep 1
  131. if [ "${seconds}" == "0" ]; then
  132. break
  133. fi
  134. done
  135. fn_print_warn_nl "Updating server: SteamCMD: ${selfname} will be stopped during update"
  136. }
  137. # The location where the builds are checked and downloaded.
  138. remotelocation="factorio.com"
  139. # Game server architecture.
  140. factorioarch="linux64"
  141. if [ "${branch}" == "stable" ]; then
  142. downloadbranch="stable"
  143. elif [ "${branch}" == "experimental" ]; then
  144. downloadbranch="latest"
  145. else
  146. downloadbranch="${branch}"
  147. fi
  148. if [ "${installer}" == "1" ]; then
  149. fn_update_factorio_remotebuild
  150. fn_update_factorio_dl
  151. else
  152. fn_print_dots "Checking for update: ${remotelocation}"
  153. fn_script_log_info "Checking for update: ${remotelocation}"
  154. fn_update_factorio_localbuild
  155. fn_update_factorio_remotebuild
  156. fn_update_factorio_compare
  157. fi