update_mta.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 for update: ${remotelocation}: checking local build"
  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 for update: ${remotelocation}: checking local build"
  34. fn_print_error_nl "Checking for update: ${remotelocation}: checking local build: no log files"
  35. fn_script_log_error "No log file found"
  36. fn_print_info_nl "Checking for update: ${remotelocation}: checking local build: forcing server restart"
  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 for update: ${remotelocation}: checking local build: 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 for update: ${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. end=$((SECONDS+120))
  66. totalseconds=0
  67. while [ "${SECONDS}" -lt "${end}" ]; do
  68. fn_print_info "Checking for update: ${remotelocation}: checking local build: 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 for update: ${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 for update: ${remotelocation}: checking local build"
  88. fn_script_log_pass "Checking local build"
  89. fi
  90. }
  91. fn_update_mta_remotebuild(){
  92. # Gets remote build info.
  93. 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')
  94. 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')
  95. 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')
  96. remotebuild="${majorversion}.${minorversion}.${maintenanceversion}"
  97. if [ "${installer}" != "1" ]; then
  98. fn_print_dots "Checking for update: ${remotelocation}: checking remote build"
  99. # Checks if remotebuild variable has been set.
  100. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  101. fn_print_fail "Checking for update: ${remotelocation}: checking remote build"
  102. fn_script_log_fatal "Checking remote build"
  103. core_exit.sh
  104. else
  105. fn_print_ok "Checking for update: ${remotelocation}: checking remote build"
  106. fn_script_log_pass "Checking remote build"
  107. fi
  108. else
  109. # Checks if remotebuild variable has been set.
  110. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  111. fn_print_failure "Unable to get remote build"
  112. fn_script_log_fatal "Unable to get remote build"
  113. core_exit.sh
  114. fi
  115. fi
  116. }
  117. fn_update_mta_compare(){
  118. # Removes dots so if statement can compare version numbers.
  119. fn_print_dots "Checking for update: ${remotelocation}"
  120. localbuilddigit=$(echo -e "${localbuild}" | tr -cd '[:digit:]')
  121. remotebuilddigit=$(echo -e "${remotebuild}" | tr -cd '[:digit:]')
  122. if [ "${localbuilddigit}" -ne "${remotebuilddigit}" ]||[ "${forceupdate}" == "1" ]; then
  123. fn_print_ok_nl "Checking for update: ${remotelocation}"
  124. if [ "${forceupdate}" == "1" ]; then
  125. # forceupdate bypasses checks, useful for small build changes
  126. mtaupdatestatus="forced"
  127. else
  128. mtaupdatestatus="available"
  129. fi
  130. echo -en "\n"
  131. echo -e "Update ${mtaupdatestatus}:"
  132. echo -e "* Local build: ${red}${localbuild}${default}"
  133. echo -e "* Remote build: ${green}${remotebuild}${default}"
  134. fn_script_log_info "Update available"
  135. fn_script_log_info "Local build: ${localbuild}"
  136. fn_script_log_info "Remote build: ${remotebuild}"
  137. fn_script_log_info "${localbuild} > ${remotebuild}"
  138. fn_sleep_time
  139. echo -en "\n"
  140. echo -en "applying update.\r"
  141. sleep 1
  142. echo -en "applying update..\r"
  143. sleep 1
  144. echo -en "applying update...\r"
  145. sleep 1
  146. echo -en "\n"
  147. unset updateonstart
  148. check_status.sh
  149. # If server stopped.
  150. if [ "${status}" == "0" ]; then
  151. exitbypass=1
  152. fn_update_mta_dl
  153. exitbypass=1
  154. command_start.sh
  155. exitbypass=1
  156. command_stop.sh
  157. # If server started.
  158. else
  159. exitbypass=1
  160. command_stop.sh
  161. exitbypass=1
  162. fn_update_mta_dl
  163. exitbypass=1
  164. command_start.sh
  165. fi
  166. alert="update"
  167. alert.sh
  168. else
  169. fn_print_ok_nl "Checking for update: ${remotelocation}"
  170. echo -e "No update available"
  171. echo -e "* Local build: ${green}${localbuild}${default}"
  172. echo -e "* Remote build: ${green}${remotebuild}${default}"
  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 [ "${installer}" == "1" ]; then
  181. fn_update_mta_remotebuild
  182. fn_update_mta_dl
  183. else
  184. fn_print_dots "Checking for update: ${remotelocation}"
  185. fn_script_log_info "Checking for update: ${remotelocation}"
  186. fn_update_mta_localbuild
  187. fn_update_mta_remotebuild
  188. fn_update_mta_compare
  189. fi