update_factorio.sh 5.6 KB

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