update_factorio.sh 5.2 KB

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