update_factorio.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/bin/bash
  2. # LinuxGSM update_factorio.sh function
  3. # Author: Daniel Gibbs
  4. # Contributor: Kristian Polso
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of Factorio servers.
  7. local commandname="UPDATE"
  8. local commandaction="Update"
  9. local function_selfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  10. fn_update_factorio_dl(){
  11. if [ "${branch}" == "stable" ]; then
  12. downloadbranch="stable"
  13. elif [ "${branch}" == "experimental" ]; then
  14. downloadbranch="latest"
  15. fi
  16. fn_fetch_file "https://factorio.com/get-download/${downloadbranch}/headless/${factorioarch}" "${tmpdir}" "factorio_headless_${factorioarch}-${availablebuild}.tar.xz"
  17. fn_dl_extract "${tmpdir}" "factorio_headless_${factorioarch}-${availablebuild}.tar.xz" "${tmpdir}"
  18. echo -e "copying to ${serverfiles}...\c"
  19. fn_script_log "Copying to ${serverfiles}"
  20. cp -R "${tmpdir}/factorio/"* "${serverfiles}"
  21. local exitcode=$?
  22. if [ "${exitcode}" == "0" ]; then
  23. fn_print_ok_eol_nl
  24. else
  25. fn_print_fail_eol_nl
  26. fi
  27. }
  28. fn_update_factorio_currentbuild(){
  29. # Gets current build info
  30. # Checks if current build info is available. If it fails, then a server restart will be forced to generate logs.
  31. if [ ! -f "${serverfiles}/factorio-current.log" ]; then
  32. fn_print_error "Checking for update: factorio.com"
  33. sleep 0.5
  34. fn_print_error_nl "Checking for update: factorio.com: No logs with server version found"
  35. fn_script_log_error "Checking for update: factorio.com: No logs with server version found"
  36. sleep 0.5
  37. fn_print_info_nl "Checking for update: factorio.com: Forcing server restart"
  38. fn_script_log_info "Checking for update: factorio.com: Forcing server restart"
  39. sleep 0.5
  40. exitbypass=1
  41. command_stop.sh
  42. exitbypass=1
  43. command_start.sh
  44. sleep 0.5
  45. # Check again and exit on failure.
  46. if [ ! -f "${serverfiles}/factorio-current.log" ]; then
  47. fn_print_fail_nl "Checking for update: factorio.com: Still No logs with server version found"
  48. fn_script_log_fatal "Checking for update: factorio.com: Still No logs with server version found"
  49. core_exit.sh
  50. fi
  51. fi
  52. # Get current build from logs
  53. currentbuild=$(grep "Loading mod base" "${serverfiles}/factorio-current.log" 2> /dev/null | awk '{print $5}' | tail -1)
  54. if [ -z "${currentbuild}" ]; then
  55. fn_print_error_nl "Checking for update: factorio.com: Current build version not found"
  56. fn_script_log_error "Checking for update: factorio.com: Current build version not found"
  57. sleep 0.5
  58. fn_print_info_nl "Checking for update: factorio.com: Forcing server restart"
  59. fn_script_log_info "Checking for update: factorio.com: Forcing server restart"
  60. exitbypass=1
  61. command_stop.sh
  62. exitbypass=1
  63. command_start.sh
  64. currentbuild=$(grep "Loading mod base" "${serverfiles}/factorio-current.log" 2> /dev/null | awk '{print $5}' | tail -1)
  65. if [ -z "${currentbuild}" ]; then
  66. fn_print_fail_nl "Checking for update: factorio.com: Current build version still not found"
  67. fn_script_log_fatal "Checking for update: factorio.com: Current build version still not found"
  68. core_exit.sh
  69. fi
  70. fi
  71. }
  72. fn_update_factorio_arch(){
  73. # Factorio is linux64 only for now
  74. factorioarch="linux64"
  75. }
  76. fn_update_factorio_availablebuild(){
  77. # Gets latest build info.
  78. if [ "${branch}" != "stable" ]; then
  79. availablebuild=$(${curlpath} -s https://factorio.com/get-download/stable/headless/linux64 | grep -o '[0-9]\.[0-9]\{2\}\.[0-9]\{2\}' | head -1)
  80. else
  81. availablebuild=$(${curlpath} -s https://factorio.com/get-download/latest/headless/linux64 | grep -o '[0-9]\.[0-9]\{2\}\.[0-9]\{2\}' | head -1)
  82. fi
  83. # Checks if availablebuild variable has been set
  84. if [ -z "${availablebuild}" ]; then
  85. fn_print_fail "Checking for update: factorio.com"
  86. sleep 0.5
  87. fn_print_fail "Checking for update: factorio.com: Not returning version info"
  88. fn_script_log_fatal "Failure! Checking for update: factorio.com: Not returning version info"
  89. core_exit.sh
  90. elif [ "${installer}" == "1" ]; then
  91. :
  92. else
  93. fn_print_ok "Checking for update: factorio.com"
  94. fn_script_log_pass "Checking for update: factorio.com"
  95. sleep 0.5
  96. fi
  97. }
  98. fn_update_factorio_compare(){
  99. # Removes dots so if can compare version numbers
  100. currentbuilddigit=$(echo "${currentbuild}" | tr -cd '[:digit:]')
  101. availablebuilddigit=$(echo "${availablebuild}" | tr -cd '[:digit:]')
  102. if [ "${currentbuilddigit}" -ne "${availablebuilddigit}" ]; then
  103. echo -e "\n"
  104. echo -e "Update available:"
  105. sleep 0.5
  106. echo -e " Current build: ${red}${currentbuild} ${factorioarch} ${branch} ${default}"
  107. echo -e " Available build: ${green}${availablebuild} ${factorioarch} ${branch}${default}"
  108. echo -e ""
  109. sleep 0.5
  110. echo -en "Applying update.\r"
  111. sleep 1
  112. echo -en "Applying update..\r"
  113. sleep 1
  114. echo -en "Applying update...\r"
  115. sleep 1
  116. echo -en "\n"
  117. fn_script_log "Update available"
  118. fn_script_log "Current build: ${currentbuild} ${factorioarch}${branch}"
  119. fn_script_log "Available build: ${availablebuild} ${factorioarch}${branch}"
  120. fn_script_log "${currentbuild} > ${availablebuild}"
  121. unset updateonstart
  122. check_status.sh
  123. if [ "${status}" == "0" ]; then
  124. fn_update_factorio_dl
  125. exitbypass=1
  126. command_start.sh
  127. exitbypass=1
  128. command_stop.sh
  129. else
  130. exitbypass=1
  131. command_stop.sh
  132. fn_update_factorio_dl
  133. exitbypass=1
  134. command_start.sh
  135. fi
  136. alert="update"
  137. alert.sh
  138. else
  139. echo -e "\n"
  140. echo -e "No update available:"
  141. echo -e " Current build: ${green}${currentbuild} ${factorioarch} ${branch}${default}"
  142. echo -e " Available build: ${green}${availablebuild} ${factorioarch} ${branch}${default}"
  143. echo -e ""
  144. fn_print_ok_nl "No update available"
  145. fn_script_log_info "Current build: ${currentbuild} ${factorioarch} ${branch}"
  146. fn_script_log_info "Available build: ${availablebuild} ${factorioarch} ${branch}"
  147. fi
  148. }
  149. fn_update_factorio_arch
  150. if [ "${installer}" == "1" ]; then
  151. fn_update_factorio_availablebuild
  152. fn_update_factorio_dl
  153. else
  154. # Checks for server update from factorio.com
  155. fn_print_dots "Checking for update: factorio.com"
  156. fn_script_log_info "Checking for update: factorio.com"
  157. sleep 0.5
  158. fn_update_factorio_currentbuild
  159. fn_update_factorio_availablebuild
  160. fn_update_factorio_compare
  161. fi