update_factorio.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/bin/bash
  2. # LinuxGSM update_factorio.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: http://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of Factorio servers.
  7. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. fn_update_factorio_dl(){
  9. fn_fetch_file "https://factorio.com/get-download/${downloadbranch}/headless/${factorioarch}" "" "" "" "${tmpdir}" "factorio_headless_${factorioarch}-${remotebuild}.tar.xz" "" "norun" "noforce" "nomd5"
  10. fn_dl_extract "${tmpdir}" "factorio_headless_${factorioarch}-${remotebuild}.tar.xz" "${tmpdir}"
  11. echo -e "copying to ${serverfiles}...\c"
  12. cp -R "${tmpdir}/factorio/"* "${serverfiles}"
  13. local exitcode=$?
  14. if [ "${exitcode}" == "0" ]; then
  15. fn_print_ok_eol_nl
  16. fn_script_log_pass "Copying to ${serverfiles}"
  17. fn_clear_tmp
  18. else
  19. fn_print_fail_eol_nl
  20. fn_script_log_fatal "Copying to ${serverfiles}"
  21. core_exit.sh
  22. fn_clear_tmp
  23. fi
  24. }
  25. fn_update_factorio_localbuild(){
  26. # Gets local build info.
  27. fn_print_dots "Checking local build: ${remotelocation}"
  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 [ "${firstcommandname}" != "INSTALL" ]; then
  44. fn_print_dots "Checking remote build: ${remotelocation}"
  45. # Checks if remotebuild variable has been set.
  46. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  47. fn_print_fail "Checking remote build: ${remotelocation}"
  48. fn_script_log_fatal "Checking remote build"
  49. core_exit.sh
  50. else
  51. fn_print_ok "Checking remote build: ${remotelocation}"
  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. fn_print_dots "Checking for update: ${remotelocation}"
  67. localbuilddigit=$(echo -e "${localbuild}" | tr -cd '[:digit:]')
  68. remotebuilddigit=$(echo -e "${remotebuild}" | tr -cd '[:digit:]')
  69. if [ "${localbuilddigit}" -ne "${remotebuilddigit}" ]||[ "${forceupdate}" == "1" ]; then
  70. fn_print_ok_nl "Checking for update: ${remotelocation}"
  71. echo -en "\n"
  72. echo -e "Update available"
  73. echo -e "* Local build: ${red}${localbuild} ${factorioarch}${default}"
  74. echo -e "* Remote build: ${green}${remotebuild} ${factorioarch}${default}"
  75. if [ -n "${branch}" ]; then
  76. echo -e "* Branch: ${branch}"
  77. fi
  78. echo -en "\n"
  79. fn_script_log_info "Update available"
  80. fn_script_log_info "Local build: ${localbuild} ${factorioarch}"
  81. fn_script_log_info "Remote build: ${remotebuild} ${factorioarch}"
  82. if [ -v "${branch}" ]; then
  83. fn_script_log_info "Branch: ${branch}"
  84. fi
  85. fn_script_log_info "${localbuild} > ${remotebuild}"
  86. unset updateonstart
  87. check_status.sh
  88. # If server stopped.
  89. if [ "${status}" == "0" ]; then
  90. exitbypass=1
  91. fn_update_factorio_dl
  92. exitbypass=1
  93. command_start.sh
  94. fn_firstcommand_reset
  95. exitbypass=1
  96. command_stop.sh
  97. fn_firstcommand_reset
  98. # If server started.
  99. else
  100. fn_print_restart_warning
  101. exitbypass=1
  102. command_stop.sh
  103. fn_firstcommand_reset
  104. exitbypass=1
  105. fn_update_factorio_dl
  106. exitbypass=1
  107. command_start.sh
  108. fn_firstcommand_reset
  109. fi
  110. unset exitbypass
  111. date +%s > "${lockdir}/lastupdate.lock"
  112. alert="update"
  113. alert.sh
  114. else
  115. fn_print_ok_nl "Checking for update: ${remotelocation}"
  116. echo -en "\n"
  117. echo -e "No update available"
  118. echo -e "* Local build: ${green}${localbuild} ${factorioarch}${default}"
  119. echo -e "* Remote build: ${green}${remotebuild} ${factorioarch}${default}"
  120. if [ -v "${branch}" ]; then
  121. echo -e "* Branch: ${branch}"
  122. fi
  123. echo -en "\n"
  124. fn_script_log_info "No update available"
  125. fn_script_log_info "Local build: ${localbuild} ${factorioarch}"
  126. fn_script_log_info "Remote build: ${remotebuild} ${factorioarch}"
  127. if [ -v "${branch}" ]; then
  128. fn_script_log_info "Branch: ${branch}"
  129. fi
  130. fi
  131. }
  132. # The location where the builds are checked and downloaded.
  133. remotelocation="factorio.com"
  134. # Game server architecture.
  135. factorioarch="linux64"
  136. if [ "${branch}" == "stable" ]; then
  137. downloadbranch="stable"
  138. elif [ "${branch}" == "experimental" ]; then
  139. downloadbranch="latest"
  140. else
  141. downloadbranch="${branch}"
  142. fi
  143. if [ "${firstcommandname}" == "INSTALL" ]; then
  144. fn_update_factorio_remotebuild
  145. fn_update_factorio_dl
  146. else
  147. fn_print_dots "Checking for update"
  148. fn_print_dots "Checking for update: ${remotelocation}"
  149. fn_script_log_info "Checking for update: ${remotelocation}"
  150. fn_update_factorio_localbuild
  151. fn_update_factorio_remotebuild
  152. fn_update_factorio_compare
  153. fi