update_mta.sh 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. fn_firstcommand_reset
  41. totalseconds=0
  42. # Check again, allow time to generate logs.
  43. while [ ! -f "${serverfiles}/mods/deathmatch/logs/server.log" ]; do
  44. sleep 1
  45. fn_print_info "Checking local build: ${remotelocation}: waiting for log file: ${totalseconds}"
  46. if [ -v "${loopignore}" ]; then
  47. loopignore=1
  48. fn_script_log_info "Waiting for log file to generate"
  49. fi
  50. if [ "${totalseconds}" -gt "120" ]; then
  51. localbuild="0"
  52. fn_print_error "Checking local build: ${remotelocation}: waiting for log file: missing log file"
  53. fn_script_log_error "Missing log file"
  54. fn_script_log_error "Set localbuild to 0"
  55. fi
  56. totalseconds=$((totalseconds + 1))
  57. done
  58. fi
  59. if [ -z "${localbuild}" ]; then
  60. localbuild=$(grep "= Multi Theft Auto: San Andreas v" "${serverfiles}/mods/deathmatch/logs/server.log" | awk '{ print $7 }' | sed -r 's/^.{1}//' | tail -1)
  61. fi
  62. if [ -z "${localbuild}" ]; then
  63. # Gives time for var to generate.
  64. totalseconds=0
  65. for seconds in {1..120}; do
  66. fn_print_info "Checking local build: ${remotelocation}: waiting for local build: ${totalseconds}"
  67. if [ -z "${loopignore}" ]; then
  68. loopignore=1
  69. fn_script_log_info "Waiting for local build to generate"
  70. fi
  71. localbuild=$(grep "= Multi Theft Auto: San Andreas v" "${serverfiles}/mods/deathmatch/logs/server.log" | awk '{ print $7 }' | sed -r 's/^.{1}//' | tail -1)
  72. if [ "${localbuild}" ]; then
  73. break
  74. fi
  75. sleep 1
  76. totalseconds=$((totalseconds + 1))
  77. done
  78. fi
  79. if [ -z "${localbuild}" ]; then
  80. localbuild="0"
  81. fn_print_error "Checking local build: ${remotelocation}: waiting for local build: missing local build info"
  82. fn_script_log_error "Missing local build info"
  83. fn_script_log_error "Set localbuild to 0"
  84. else
  85. fn_print_ok "Checking local build: ${remotelocation}"
  86. fn_script_log_pass "Checking local build"
  87. fi
  88. }
  89. fn_update_mta_remotebuild(){
  90. # Gets remote build info.
  91. 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')
  92. 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')
  93. 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')
  94. remotebuild="${majorversion}.${minorversion}.${maintenanceversion}"
  95. if [ "${firstcommandname}" != "INSTALL" ]; then
  96. fn_print_dots "Checking remote build: ${remotelocation}"
  97. # Checks if remotebuild variable has been set.
  98. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  99. fn_print_fail "Checking remote build: ${remotelocation}"
  100. fn_script_log_fatal "Checking remote build"
  101. core_exit.sh
  102. else
  103. fn_print_ok "Checking remote build: ${remotelocation}"
  104. fn_script_log_pass "Checking remote build"
  105. fi
  106. else
  107. # Checks if remotebuild variable has been set.
  108. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  109. fn_print_failure "Unable to get remote build"
  110. fn_script_log_fatal "Unable to get remote build"
  111. core_exit.sh
  112. fi
  113. fi
  114. }
  115. fn_update_mta_compare(){
  116. # Removes dots so if statement can compare version numbers.
  117. fn_print_dots "Checking for update: ${remotelocation}"
  118. localbuilddigit=$(echo -e "${localbuild}" | tr -cd '[:digit:]')
  119. remotebuilddigit=$(echo -e "${remotebuild}" | tr -cd '[:digit:]')
  120. if [ "${localbuilddigit}" -ne "${remotebuilddigit}" ]||[ "${forceupdate}" == "1" ]; then
  121. fn_print_ok_nl "Checking for update: ${remotelocation}"
  122. if [ "${forceupdate}" == "1" ]; then
  123. # forceupdate bypasses checks, useful for small build changes
  124. mtaupdatestatus="forced"
  125. else
  126. mtaupdatestatus="available"
  127. fi
  128. echo -en "\n"
  129. echo -e "Update ${mtaupdatestatus}:"
  130. echo -e "* Local build: ${red}${localbuild}${default}"
  131. echo -e "* Remote build: ${green}${remotebuild}${default}"
  132. echo -en "\n"
  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. fn_firstcommand_reset
  148. # If server started.
  149. else
  150. fn_print_restart_warning
  151. exitbypass=1
  152. command_stop.sh
  153. fn_firstcommand_reset
  154. exitbypass=1
  155. fn_update_mta_dl
  156. exitbypass=1
  157. command_start.sh
  158. fn_firstcommand_reset
  159. fi
  160. unset exitbypass
  161. date +%s > "${lockdir}/lastupdate.lock"
  162. alert="update"
  163. alert.sh
  164. else
  165. fn_print_ok_nl "Checking for update: ${remotelocation}"
  166. echo -en "\n"
  167. echo -e "No update available"
  168. echo -e "* Local build: ${green}${localbuild}${default}"
  169. echo -e "* Remote build: ${green}${remotebuild}${default}"
  170. echo -en "\n"
  171. fn_script_log_info "No update available"
  172. fn_script_log_info "Local build: ${localbuild}"
  173. fn_script_log_info "Remote build: ${remotebuild}"
  174. fi
  175. }
  176. # The location where the builds are checked and downloaded.
  177. remotelocation="linux.mtasa.com"
  178. if [ "${firstcommandname}" == "INSTALL" ]; then
  179. fn_update_mta_remotebuild
  180. fn_update_mta_dl
  181. else
  182. fn_print_dots "Checking for update"
  183. fn_print_dots "Checking for update: ${remotelocation}"
  184. fn_script_log_info "Checking for update: ${remotelocation}"
  185. fn_update_mta_localbuild
  186. fn_update_mta_remotebuild
  187. fn_update_mta_compare
  188. fi