update_mta.sh 7.4 KB

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