update_factorio.sh 5.2 KB

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