update_mta.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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"
  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. if [ "${exitcode}" == "0" ]; then
  15. fn_print_ok_eol_nl
  16. fn_script_log_pass "Copying to ${serverfiles}"
  17. chmod u+x "${serverfiles}/mta-server64"
  18. fn_clear_tmp
  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. fn_script_log_info "Update available"
  132. fn_script_log_info "Local build: ${localbuild}"
  133. fn_script_log_info "Remote build: ${remotebuild}"
  134. fn_script_log_info "${localbuild} > ${remotebuild}"
  135. unset updateonstart
  136. check_status.sh
  137. # If server stopped.
  138. if [ "${status}" == "0" ]; then
  139. exitbypass=1
  140. fn_update_mta_dl
  141. exitbypass=1
  142. command_start.sh
  143. exitbypass=1
  144. command_stop.sh
  145. # If server started.
  146. else
  147. fn_stop_warning
  148. exitbypass=1
  149. command_stop.sh
  150. exitbypass=1
  151. fn_update_mta_dl
  152. exitbypass=1
  153. command_start.sh
  154. fi
  155. date +%s > "${lockdir}/lastupdate.lock"
  156. alert="update"
  157. alert.sh
  158. else
  159. fn_print_ok_nl "Checking for update: ${remotelocation}"
  160. echo -en "\n"
  161. echo -e "No update available"
  162. echo -e "* Local build: ${green}${localbuild}${default}"
  163. echo -e "* Remote build: ${green}${remotebuild}${default}"
  164. fn_script_log_info "No update available"
  165. fn_script_log_info "Local build: ${localbuild}"
  166. fn_script_log_info "Remote build: ${remotebuild}"
  167. fi
  168. }
  169. fn_stop_warning(){
  170. fn_print_warn "Updating server: SteamCMD: ${selfname} will be stopped during update"
  171. fn_script_log_warn "Updating server: SteamCMD: ${selfname} will be stopped during update"
  172. totalseconds=3
  173. for seconds in {3..1}; do
  174. fn_print_warn "Updating server: SteamCMD: ${selfname} will be stopped during update: ${totalseconds}"
  175. totalseconds=$((totalseconds - 1))
  176. sleep 1
  177. if [ "${seconds}" == "0" ]; then
  178. break
  179. fi
  180. done
  181. fn_print_warn_nl "Updating server: SteamCMD: ${selfname} will be stopped during update"
  182. }
  183. # The location where the builds are checked and downloaded.
  184. remotelocation="linux.mtasa.com"
  185. if [ "${installer}" == "1" ]; then
  186. fn_update_mta_remotebuild
  187. fn_update_mta_dl
  188. else
  189. fn_print_dots "Checking for update: ${remotelocation}"
  190. fn_script_log_info "Checking for update: ${remotelocation}"
  191. fn_update_mta_localbuild
  192. fn_update_mta_remotebuild
  193. fn_update_mta_compare
  194. fi