4
0

update_etl.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/bin/bash
  2. # LinuxGSM update_etl.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of ET: Legacy servers.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. fn_update_dl() {
  9. # Download and extract files to serverfiles.
  10. fn_fetch_file "${remotebuildurl}" "" "" "" "${tmpdir}" "${remotebuildfilename}" "nochmodx" "norun" "force" "${remotebuildhash}"
  11. fn_dl_extract "${tmpdir}" "${remotebuildfilename}" "${serverfiles}"
  12. echo "${remotebuild}" > "${serverfiles}/build.txt"
  13. fn_clear_tmp
  14. }
  15. fn_update_localbuild() {
  16. # Gets local build info.
  17. fn_print_dots "Checking local build: ${remotelocation}"
  18. # Try to get build version from etconsole.log.
  19. if [ -f "${gamelogdir}/etconsole.log" ]; then
  20. localbuild=$(grep "Initializing legacy game" "${gamelogdir}/etconsole.log" | sed -n 's/.*\^2\(v[0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/p' | tail -1)
  21. fi
  22. # Fall back to build.txt if log parse failed or log does not exist.
  23. if [ -z "${localbuild}" ]; then
  24. localbuild=$(head -n 1 "${serverfiles}/build.txt" 2> /dev/null)
  25. fi
  26. if [ -z "${localbuild}" ]; then
  27. fn_print_error "Checking local build: ${remotelocation}: missing local build info"
  28. fn_script_log_error "Missing local build info"
  29. fn_script_log_error "Set localbuild to 0"
  30. localbuild="0"
  31. else
  32. fn_print_ok "Checking local build: ${remotelocation}"
  33. fn_script_log_pass "Checking local build"
  34. fi
  35. }
  36. fn_update_remotebuild() {
  37. # Gets remote build info.
  38. apiurl="https://api.github.com/repos/GameServerManagers/etlserver-build/releases/latest"
  39. remotebuildresponse=$(curl -s "${apiurl}")
  40. remotebuildfilename=$(echo "${remotebuildresponse}" | jq -r '.assets[] | select(.browser_download_url | contains("i386-et-260b")) | .name')
  41. remotebuildurl=$(echo "${remotebuildresponse}" | jq -r '.assets[] | select(.browser_download_url | contains("i386-et-260b")) | .browser_download_url')
  42. remotebuild=$(echo "${remotebuildresponse}" | jq -r '.tag_name')
  43. remotebuildhash=$(echo "${remotebuildresponse}" | jq -r '.body' | grep 'MD5' | grep -oE '[a-f0-9]{32}')
  44. if [ "${firstcommandname}" != "INSTALL" ]; then
  45. fn_print_dots "Checking remote build: ${remotelocation}"
  46. # Checks if remotebuild variable has been set.
  47. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  48. fn_print_fail "Checking remote build: ${remotelocation}"
  49. fn_script_log_fail "Checking remote build"
  50. core_exit.sh
  51. else
  52. fn_print_ok "Checking remote build: ${remotelocation}"
  53. fn_script_log_pass "Checking remote build"
  54. fi
  55. else
  56. # Checks if remotebuild variable has been set.
  57. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  58. fn_print_failure "Unable to get remote build"
  59. fn_script_log_fail "Unable to get remote build"
  60. core_exit.sh
  61. fi
  62. fi
  63. }
  64. fn_update_compare() {
  65. fn_print_dots "Checking for update: ${remotelocation}"
  66. # Update has been found or force update.
  67. if [ "${localbuild}" != "${remotebuild}" ] || [ "${forceupdate}" == "1" ]; then
  68. # Create update lockfile.
  69. date '+%s' > "${lockdir:?}/update.lock"
  70. fn_print_ok_nl "Checking for update: ${remotelocation}"
  71. fn_print "\n"
  72. fn_print_nl "${bold}${underline}Update${default} available"
  73. fn_print_nl "* Local build: ${red}${localbuild}${default}"
  74. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  75. if [ -n "${branch}" ]; then
  76. fn_print_nl "* Branch: ${branch}"
  77. fi
  78. if [ -f "${rootdir}/.dev-debug" ]; then
  79. fn_print_nl "Remote build info"
  80. fn_print_nl "* apiurl: ${apiurl}"
  81. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  82. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  83. fn_print_nl "* remotebuild: ${remotebuild}"
  84. fi
  85. fn_print "\n"
  86. fn_script_log_info "Update available"
  87. fn_script_log_info "Local build: ${localbuild}"
  88. fn_script_log_info "Remote build: ${remotebuild}"
  89. if [ -n "${branch}" ]; then
  90. fn_script_log_info "Branch: ${branch}"
  91. fi
  92. fn_script_log_info "${localbuild} > ${remotebuild}"
  93. if [ "${commandname}" == "UPDATE" ]; then
  94. date +%s > "${lockdir:?}/last-updated.lock"
  95. unset updateonstart
  96. check_status.sh
  97. # If server stopped.
  98. if [ "${status}" == "0" ]; then
  99. fn_update_dl
  100. if [ "${localbuild}" == "0" ]; then
  101. exitbypass=1
  102. command_start.sh
  103. fn_firstcommand_reset
  104. exitbypass=1
  105. fn_sleep_time_5
  106. command_stop.sh
  107. fn_firstcommand_reset
  108. fi
  109. # If server started.
  110. else
  111. fn_print_restart_warning
  112. exitbypass=1
  113. command_stop.sh
  114. fn_firstcommand_reset
  115. exitbypass=1
  116. fn_update_dl
  117. exitbypass=1
  118. command_start.sh
  119. fn_firstcommand_reset
  120. fi
  121. unset exitbypass
  122. alert="update"
  123. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  124. alert="check-update"
  125. fi
  126. alert.sh
  127. else
  128. fn_print_ok_nl "Checking for update: ${remotelocation}"
  129. fn_print "\n"
  130. fn_print_nl "${bold}${underline}No update${default} available"
  131. fn_print_nl "* Local build: ${green}${localbuild}${default}"
  132. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  133. if [ -n "${branch}" ]; then
  134. fn_print_nl "* Branch: ${branch}"
  135. fi
  136. fn_print "\n"
  137. fn_script_log_info "No update available"
  138. fn_script_log_info "Local build: ${localbuild}"
  139. fn_script_log_info "Remote build: ${remotebuild}"
  140. if [ -n "${branch}" ]; then
  141. fn_script_log_info "Branch: ${branch}"
  142. fi
  143. if [ -f "${rootdir}/.dev-debug" ]; then
  144. fn_print_nl "Remote build info"
  145. fn_print_nl "* apiurl: ${apiurl}"
  146. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  147. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  148. fn_print_nl "* remotebuild: ${remotebuild}"
  149. fi
  150. fi
  151. }
  152. # The location where the builds are checked and downloaded.
  153. remotelocation="github.com"
  154. if [ "${firstcommandname}" == "INSTALL" ]; then
  155. fn_update_remotebuild
  156. fn_update_dl
  157. else
  158. fn_print_dots "Checking for update"
  159. fn_print_dots "Checking for update: ${remotelocation}"
  160. fn_script_log_info "Checking for update: ${remotelocation}"
  161. fn_update_localbuild
  162. fn_update_remotebuild
  163. fn_update_compare
  164. fi