update_factorio.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 modulename="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. # Uses executable to find local build.
  29. cd "${executabledir}" || exit
  30. if [ -f "${executable}" ]; then
  31. localbuild=$(${executable} --version | grep "Version:" | awk '{print $2}')
  32. fn_print_ok "Checking for update: ${remotelocation}: checking local build"
  33. fn_script_log_pass "Checking local build"
  34. else
  35. localbuild="0"
  36. fn_print_error "Checking for update: ${remotelocation}: checking local build"
  37. fn_script_log_error "Checking local build"
  38. fi
  39. }
  40. fn_update_factorio_remotebuild(){
  41. # Gets remote build info.
  42. remotebuild=$(curl -s "https://factorio.com/get-download/${downloadbranch}/headless/${factorioarch}" | grep -o '[0-9]\.[0-9]\{1,\}\.[0-9]\{1,\}' | head -1)
  43. if [ "${installer}" != "1" ]; then
  44. fn_print_dots "Checking for update: ${remotelocation}: checking remote build"
  45. # Checks if remotebuild variable has been set.
  46. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  47. fn_print_fail "Checking for update: ${remotelocation}: checking remote build"
  48. fn_script_log_fatal "Checking remote build"
  49. core_exit.sh
  50. else
  51. fn_print_ok "Checking for update: ${remotelocation}: checking remote build"
  52. fn_script_log_pass "Checking remote build"
  53. fi
  54. else
  55. # Checks if remotebuild variable has been set.
  56. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  57. fn_print_failure "Unable to get remote build"
  58. fn_script_log_fatal "Unable to get remote build"
  59. core_exit.sh
  60. fi
  61. fi
  62. }
  63. fn_update_factorio_compare(){
  64. fn_print_dots "Checking for update: ${remotelocation}"
  65. # Removes dots so if statement can compare version numbers.
  66. localbuilddigit=$(echo -e "${localbuild}" | tr -cd '[:digit:]')
  67. remotebuilddigit=$(echo -e "${remotebuild}" | tr -cd '[:digit:]')
  68. if [ "${localbuilddigit}" -ne "${remotebuilddigit}" ]||[ "${forceupdate}" == "1" ]; then
  69. fn_print_ok_nl "Checking for update: ${remotelocation}"
  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 [ -v "${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. fn_sleep_time
  84. echo -en "\n"
  85. echo -en "applying update.\r"
  86. sleep 1
  87. echo -en "applying update..\r"
  88. sleep 1
  89. echo -en "applying update...\r"
  90. sleep 1
  91. echo -en "\n"
  92. unset updateonstart
  93. check_status.sh
  94. # If server stopped.
  95. if [ "${status}" == "0" ]; then
  96. exitbypass=1
  97. fn_update_factorio_dl
  98. exitbypass=1
  99. command_start.sh
  100. exitbypass=1
  101. command_stop.sh
  102. # If server started.
  103. else
  104. exitbypass=1
  105. command_stop.sh
  106. exitbypass=1
  107. fn_update_factorio_dl
  108. exitbypass=1
  109. command_start.sh
  110. fi
  111. alert="update"
  112. alert.sh
  113. else
  114. fn_print_ok_nl "Checking for update: ${remotelocation}"
  115. echo -e "No update available"
  116. echo -e "* Local build: ${green}${localbuild} ${factorioarch}${default}"
  117. echo -e "* Remote build: ${green}${remotebuild} ${factorioarch}${default}"
  118. if [ -v "${branch}" ]; then
  119. echo -e "* Branch: ${branch}"
  120. fi
  121. fn_script_log_info "No update available"
  122. fn_script_log_info "Local build: ${localbuild} ${factorioarch}"
  123. fn_script_log_info "Remote build: ${remotebuild} ${factorioarch}"
  124. if [ -v "${branch}" ]; then
  125. fn_script_log_info "Branch: ${branch}"
  126. fi
  127. fi
  128. }
  129. # The location where the builds are checked and downloaded.
  130. remotelocation="factorio.com"
  131. # Game server architecture.
  132. factorioarch="linux64"
  133. if [ "${branch}" == "stable" ]; then
  134. downloadbranch="stable"
  135. elif [ "${branch}" == "experimental" ]; then
  136. downloadbranch="latest"
  137. else
  138. downloadbranch="${branch}"
  139. fi
  140. if [ "${installer}" == "1" ]; then
  141. fn_update_factorio_remotebuild
  142. fn_update_factorio_dl
  143. else
  144. fn_print_dots "Checking for update: ${remotelocation}"
  145. fn_script_log_info "Checking for update: ${remotelocation}"
  146. fn_update_factorio_localbuild
  147. fn_update_factorio_remotebuild
  148. fn_update_factorio_compare
  149. fi