update_mta.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #!/bin/bash
  2. # LinuxGSM update_mta.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: http://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of Multi Theft Auto servers.
  7. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. fn_update_mta_dl() {
  9. fn_fetch_file "http://linux.mtasa.com/dl/multitheftauto_linux_x64.tar.gz" "" "" "" "${tmpdir}" "multitheftauto_linux_x64.tar.gz" "" "norun" "noforce" "nohash"
  10. mkdir "${tmpdir}/multitheftauto_linux_x64"
  11. fn_dl_extract "${tmpdir}" "multitheftauto_linux_x64.tar.gz" "${tmpdir}/multitheftauto_linux_x64"
  12. echo -e "copying to ${serverfiles}...\c"
  13. cp -R "${tmpdir}/multitheftauto_linux_x64/multitheftauto_linux_x64/"* "${serverfiles}"
  14. local exitcode=$?
  15. fn_clear_tmp
  16. if [ "${exitcode}" == "0" ]; then
  17. fn_print_ok_eol_nl
  18. fn_script_log_pass "Copying to ${serverfiles}"
  19. chmod u+x "${serverfiles}/mta-server64"
  20. else
  21. fn_print_fail_eol_nl
  22. fn_script_log_fatal "Copying to ${serverfiles}"
  23. core_exit.sh
  24. fi
  25. }
  26. fn_update_mta_localbuild() {
  27. # Gets local build info.
  28. fn_print_dots "Checking local build: ${remotelocation}"
  29. # Uses log file to gather info.
  30. # Gives time for log file to generate.
  31. requirerestart=1
  32. if [ ! -f "${serverfiles}/mods/deathmatch/logs/server.log" ]; then
  33. fn_print_error "Checking local build: ${remotelocation}"
  34. fn_print_error_nl "Checking local build: ${remotelocation}: no log files containing version info"
  35. fn_print_info_nl "Checking local build: ${remotelocation}: forcing server restart"
  36. fn_script_log_error "No log files containing version info"
  37. fn_script_log_info "Forcing server restart"
  38. exitbypass=1
  39. command_stop.sh
  40. exitbypass=1
  41. command_start.sh
  42. fn_firstcommand_reset
  43. totalseconds=0
  44. # Check again, allow time to generate logs.
  45. while [ ! -f "${serverfiles}/mods/deathmatch/logs/server.log" ]; do
  46. sleep 1
  47. fn_print_info "Checking local build: ${remotelocation}: waiting for log file: ${totalseconds}"
  48. if [ -v "${loopignore}" ]; then
  49. loopignore=1
  50. fn_script_log_info "Waiting for log file to generate"
  51. fi
  52. if [ "${totalseconds}" -gt "120" ]; then
  53. localbuild="0"
  54. fn_print_error "Checking local build: ${remotelocation}: waiting for log file: missing log file"
  55. fn_script_log_error "Missing log file"
  56. fn_script_log_error "Set localbuild to 0"
  57. fi
  58. totalseconds=$((totalseconds + 1))
  59. done
  60. fi
  61. if [ -z "${localbuild}" ]; then
  62. localbuild=$(grep "= Multi Theft Auto: San Andreas v" "${serverfiles}/mods/deathmatch/logs/server.log" | awk '{ print $7 }' | sed -r 's/^.{1}//' | tail -1)
  63. fi
  64. if [ -z "${localbuild}" ]; then
  65. # Gives time for var to generate.
  66. totalseconds=0
  67. for seconds in {1..120}; do
  68. fn_print_info "Checking local build: ${remotelocation}: waiting for local build: ${totalseconds}"
  69. if [ -z "${loopignore}" ]; then
  70. loopignore=1
  71. fn_script_log_info "Waiting for local build to generate"
  72. fi
  73. localbuild=$(grep "= Multi Theft Auto: San Andreas v" "${serverfiles}/mods/deathmatch/logs/server.log" | awk '{ print $7 }' | sed -r 's/^.{1}//' | tail -1)
  74. if [ "${localbuild}" ]; then
  75. break
  76. fi
  77. sleep 1
  78. totalseconds=$((totalseconds + 1))
  79. done
  80. fi
  81. if [ -z "${localbuild}" ]; then
  82. localbuild="0"
  83. fn_print_error "Checking local build: ${remotelocation}: waiting for local build: missing local build info"
  84. fn_script_log_error "Missing local build info"
  85. fn_script_log_error "Set localbuild to 0"
  86. else
  87. fn_print_ok "Checking local build: ${remotelocation}"
  88. fn_script_log_pass "Checking local build"
  89. fi
  90. }
  91. fn_update_mta_remotebuild() {
  92. # Gets remote build info.
  93. remotebuild=$(curl -s "https://api.github.com/repos/multitheftauto/mtasa-blue/releases/latest" | jq -r '.tag_name')
  94. if [ "${firstcommandname}" != "INSTALL" ]; then
  95. fn_print_dots "Checking remote build: ${remotelocation}"
  96. # Checks if remotebuild variable has been set.
  97. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  98. fn_print_fail "Checking remote build: ${remotelocation}"
  99. fn_script_log_fatal "Checking remote build"
  100. core_exit.sh
  101. else
  102. fn_print_ok "Checking remote build: ${remotelocation}"
  103. fn_script_log_pass "Checking remote build"
  104. fi
  105. else
  106. # Checks if remotebuild variable has been set.
  107. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  108. fn_print_failure "Unable to get remote build"
  109. fn_script_log_fatal "Unable to get remote build"
  110. core_exit.sh
  111. fi
  112. fi
  113. }
  114. fn_update_mta_compare() {
  115. # Removes dots so if statement can compare version numbers.
  116. fn_print_dots "Checking for update: ${remotelocation}"
  117. localbuilddigit=$(echo -e "${localbuild}" | tr -cd '[:digit:]')
  118. remotebuilddigit=$(echo -e "${remotebuild}" | tr -cd '[:digit:]')
  119. if [ "${localbuilddigit}" -ne "${remotebuilddigit}" ] || [ "${forceupdate}" == "1" ]; then
  120. fn_print_ok_nl "Checking for update: ${remotelocation}"
  121. if [ "${forceupdate}" == "1" ]; then
  122. # forceupdate bypasses checks, useful for small build changes
  123. mtaupdatestatus="forced"
  124. else
  125. mtaupdatestatus="available"
  126. fi
  127. echo -en "\n"
  128. echo -e "Update ${mtaupdatestatus}:"
  129. echo -e "* Local build: ${red}${localbuild}${default}"
  130. echo -e "* Remote build: ${green}${remotebuild}${default}"
  131. echo -en "\n"
  132. fn_script_log_info "Update available"
  133. fn_script_log_info "Local build: ${localbuild}"
  134. fn_script_log_info "Remote build: ${remotebuild}"
  135. fn_script_log_info "${localbuild} > ${remotebuild}"
  136. unset updateonstart
  137. check_status.sh
  138. # If server stopped.
  139. if [ "${status}" == "0" ]; then
  140. exitbypass=1
  141. fn_update_mta_dl
  142. if [ "${requirerestart}" == "1" ]; then
  143. exitbypass=1
  144. command_start.sh
  145. fn_firstcommand_reset
  146. exitbypass=1
  147. command_stop.sh
  148. fn_firstcommand_reset
  149. fi
  150. # If server started.
  151. else
  152. fn_print_restart_warning
  153. exitbypass=1
  154. command_stop.sh
  155. fn_firstcommand_reset
  156. exitbypass=1
  157. fn_update_mta_dl
  158. exitbypass=1
  159. command_start.sh
  160. fn_firstcommand_reset
  161. fi
  162. unset exitbypass
  163. date +%s > "${lockdir}/lastupdate.lock"
  164. alert="update"
  165. alert.sh
  166. else
  167. fn_print_ok_nl "Checking for update: ${remotelocation}"
  168. echo -en "\n"
  169. echo -e "No update available"
  170. echo -e "* Local build: ${green}${localbuild}${default}"
  171. echo -e "* Remote build: ${green}${remotebuild}${default}"
  172. echo -en "\n"
  173. fn_script_log_info "No update available"
  174. fn_script_log_info "Local build: ${localbuild}"
  175. fn_script_log_info "Remote build: ${remotebuild}"
  176. fi
  177. }
  178. # The location where the builds are checked and downloaded.
  179. remotelocation="linux.mtasa.com"
  180. if [ "${firstcommandname}" == "INSTALL" ]; then
  181. fn_update_mta_remotebuild
  182. fn_update_mta_dl
  183. else
  184. fn_print_dots "Checking for update"
  185. fn_print_dots "Checking for update: ${remotelocation}"
  186. fn_script_log_info "Checking for update: ${remotelocation}"
  187. fn_update_mta_localbuild
  188. fn_update_mta_remotebuild
  189. fn_update_mta_compare
  190. fi