4
0

update_mta.sh 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  7. fn_update_mta_dl(){
  8. fn_fetch_file "http://linux.mtasa.com/dl/multitheftauto_linux_x64.tar.gz" "" "" "" "${tmpdir}" "multitheftauto_linux_x64.tar.gz" "" "norun" "noforce" "nomd5"
  9. mkdir "${tmpdir}/multitheftauto_linux_x64"
  10. fn_dl_extract "${tmpdir}" "multitheftauto_linux_x64.tar.gz" "${tmpdir}/multitheftauto_linux_x64"
  11. echo -e "copying to ${serverfiles}...\c"
  12. cp -R "${tmpdir}/multitheftauto_linux_x64/multitheftauto_linux_x64/"* "${serverfiles}"
  13. local exitcode=$?
  14. fn_clear_tmp
  15. if [ "${exitcode}" == "0" ]; then
  16. fn_print_ok_eol_nl
  17. fn_script_log_pass "Copying to ${serverfiles}"
  18. chmod u+x "${serverfiles}/mta-server64"
  19. else
  20. fn_print_fail_eol_nl
  21. fn_script_log_fatal "Copying to ${serverfiles}"
  22. core_exit.sh
  23. fi
  24. }
  25. fn_update_mta_localbuild(){
  26. # Gets local build info.
  27. fn_print_dots "Checking local build: ${remotelocation}"
  28. # Uses log file to gather info.
  29. # Gives time for log file to generate.
  30. if [ ! -f "${serverfiles}/mods/deathmatch/logs/server.log" ]; then
  31. fn_print_error "Checking local build: ${remotelocation}"
  32. fn_print_error_nl "Checking local build: ${remotelocation}: no log files containing version info"
  33. fn_print_info_nl "Checking local build: ${remotelocation}: forcing server restart"
  34. fn_script_log_error "No log files containing version info"
  35. fn_script_log_info "Forcing server restart"
  36. exitbypass=1
  37. command_stop.sh
  38. exitbypass=1
  39. command_start.sh
  40. totalseconds=0
  41. # Check again, allow time to generate logs.
  42. while [ ! -f "${serverfiles}/mods/deathmatch/logs/server.log" ]; do
  43. sleep 1
  44. fn_print_info "Checking local build: ${remotelocation}: waiting for log file: ${totalseconds}"
  45. if [ -v "${loopignore}" ]; then
  46. loopignore=1
  47. fn_script_log_info "Waiting for log file to generate"
  48. fi
  49. if [ "${totalseconds}" -gt "120" ]; then
  50. localbuild="0"
  51. fn_print_error "Checking local build: ${remotelocation}: waiting for log file: missing log file"
  52. fn_script_log_error "Missing log file"
  53. fn_script_log_error "Set localbuild to 0"
  54. fi
  55. totalseconds=$((totalseconds + 1))
  56. done
  57. fi
  58. if [ -z "${localbuild}" ]; then
  59. localbuild=$(grep "= Multi Theft Auto: San Andreas v" "${serverfiles}/mods/deathmatch/logs/server.log" | awk '{ print $7 }' | sed -r 's/^.{1}//' | tail -1)
  60. fi
  61. if [ -z "${localbuild}" ]; then
  62. # Gives time for var to generate.
  63. totalseconds=0
  64. for seconds in {1..120}; do
  65. fn_print_info "Checking local build: ${remotelocation}: waiting for local build: ${totalseconds}"
  66. if [ -z "${loopignore}" ]; then
  67. loopignore=1
  68. fn_script_log_info "Waiting for local build to generate"
  69. fi
  70. localbuild=$(grep "= Multi Theft Auto: San Andreas v" "${serverfiles}/mods/deathmatch/logs/server.log" | awk '{ print $7 }' | sed -r 's/^.{1}//' | tail -1)
  71. if [ "${localbuild}" ]; then
  72. break
  73. fi
  74. sleep 1
  75. totalseconds=$((totalseconds + 1))
  76. done
  77. fi
  78. if [ -z "${localbuild}" ]; then
  79. localbuild="0"
  80. fn_print_error "Checking local build: ${remotelocation}: waiting for local build: missing local build info"
  81. fn_script_log_error "Missing local build info"
  82. fn_script_log_error "Set localbuild to 0"
  83. else
  84. fn_print_ok "Checking local build: ${remotelocation}"
  85. fn_script_log_pass "Checking local build"
  86. fi
  87. }
  88. fn_update_mta_remotebuild(){
  89. # Gets remote build info.
  90. 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')
  91. 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')
  92. 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')
  93. remotebuild="${majorversion}.${minorversion}.${maintenanceversion}"
  94. if [ "${installer}" != "1" ]; 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. exitbypass=1
  143. command_start.sh
  144. exitbypass=1
  145. command_stop.sh
  146. # If server started.
  147. else
  148. fn_stop_warning
  149. exitbypass=1
  150. command_stop.sh
  151. exitbypass=1
  152. fn_update_mta_dl
  153. exitbypass=1
  154. command_start.sh
  155. fi
  156. date +%s > "${lockdir}/lastupdate.lock"
  157. alert="update"
  158. alert.sh
  159. else
  160. fn_print_ok_nl "Checking for update: ${remotelocation}"
  161. echo -en "\n"
  162. echo -e "No update available"
  163. echo -e "* Local build: ${green}${localbuild}${default}"
  164. echo -e "* Remote build: ${green}${remotebuild}${default}"
  165. echo -en "\n"
  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"
  192. fn_print_dots "Checking for update: ${remotelocation}"
  193. fn_script_log_info "Checking for update: ${remotelocation}"
  194. fn_update_mta_localbuild
  195. fn_update_mta_remotebuild
  196. fn_update_mta_compare
  197. fi
  198. if [ "${commandname}" != "INSTALL" ]; then
  199. core_exit.sh
  200. fi