update_factorio.sh 5.5 KB

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