update_mta.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 commandname="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}/minecraft_server.jar"
  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. sleep 0.5
  31. # Uses log file to gather info.
  32. # Gives time for log file to generate.
  33. if [ ! -f "${serverfiles}/mods/deathmatch/logs/server.log" ]; then
  34. fn_print_error "Checking for update: ${remotelocation}: checking local build"
  35. sleep 0.5
  36. fn_print_error_nl "Checking for update: ${remotelocation}: checking local build: no log files"
  37. fn_script_log_error "No log file found"
  38. sleep 0.5
  39. fn_print_info_nl "Checking for update: ${remotelocation}: checking local build: forcing server restart"
  40. fn_script_log_info "Forcing server restart"
  41. sleep 0.5
  42. exitbypass=1
  43. command_stop.sh
  44. exitbypass=1
  45. command_start.sh
  46. totalseconds=0
  47. # Check again, allow time to generate logs.
  48. while [ ! -f "${serverfiles}/mods/deathmatch/logs/server.log" ]; do
  49. sleep 1
  50. fn_print_info "Checking for update: ${remotelocation}: checking local build: waiting for log file: ${totalseconds}"
  51. if [ -v "${loopignore}" ]; then
  52. loopignore=1
  53. fn_script_log_info "Waiting for log file to generate"
  54. fi
  55. if [ "${totalseconds}" -gt "120" ]; then
  56. localbuild="0"
  57. fn_print_error "Checking for update: ${remotelocation}: waiting for log file: missing log file"
  58. fn_script_log_error "Missing log file"
  59. fn_script_log_error "Set localbuild to 0"
  60. sleep 0.5
  61. fi
  62. totalseconds=$((totalseconds + 1))
  63. done
  64. fi
  65. if [ -z "${localbuild}" ]; then
  66. localbuild=$(grep "= Multi Theft Auto: San Andreas v" "${serverfiles}/mods/deathmatch/logs/server.log" | awk '{ print $7 }' | sed -r 's/^.{1}//' | tail -1)
  67. fi
  68. if [ -z "${localbuild}" ]; then
  69. # Gives time for var to generate.
  70. end=$((SECONDS+120))
  71. totalseconds=0
  72. while [ "${SECONDS}" -lt "${end}" ]; do
  73. fn_print_info "Checking for update: ${remotelocation}: checking local build: waiting for local build: ${totalseconds}"
  74. if [ -z "${loopignore}" ]; then
  75. loopignore=1
  76. fn_script_log_info "Waiting for local build to generate"
  77. fi
  78. localbuild=$(grep "= Multi Theft Auto: San Andreas v" "${serverfiles}/mods/deathmatch/logs/server.log" | awk '{ print $7 }' | sed -r 's/^.{1}//' | tail -1)
  79. if [ "${localbuild}" ]; then
  80. break
  81. fi
  82. sleep 1
  83. totalseconds=$((totalseconds + 1))
  84. done
  85. fi
  86. if [ -z "${localbuild}" ]; then
  87. localbuild="0"
  88. fn_print_error "Checking for update: ${remotelocation}: waiting for local build: missing local build info"
  89. fn_script_log_error "Missing local build info"
  90. fn_script_log_error "Set localbuild to 0"
  91. else
  92. fn_print_ok "Checking for update: ${remotelocation}: checking local build"
  93. fn_script_log_pass "Checking local build"
  94. fi
  95. sleep 0.5
  96. }
  97. fn_update_mta_remotebuild(){
  98. # Gets remote build info.
  99. 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')"
  100. 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')"
  101. 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')"
  102. remotebuild="${majorversion}.${minorversion}.${maintenanceversion}"
  103. if [ "${installer}" != "1" ]; then
  104. fn_print_dots "Checking for update: ${remotelocation}: checking remote build"
  105. sleep 0.5
  106. # Checks if remotebuild variable has been set.
  107. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  108. fn_print_fail "Checking for update: ${remotelocation}: checking remote build"
  109. fn_script_log_fatal "Checking remote build"
  110. core_exit.sh
  111. else
  112. fn_print_ok "Checking for update: ${remotelocation}: checking remote build"
  113. fn_script_log_pass "Checking remote build"
  114. sleep 0.5
  115. fi
  116. else
  117. # Checks if remotebuild variable has been set.
  118. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  119. fn_print_failure "Unable to get remote build"
  120. fn_script_log_fatal "Unable to get remote build"
  121. core_exit.sh
  122. fi
  123. fi
  124. }
  125. fn_update_mta_compare(){
  126. # Removes dots so if statement can compare version numbers.
  127. fn_print_dots "Checking for update: ${remotelocation}"
  128. sleep 0.5
  129. localbuilddigit=$(echo "${localbuild}" | tr -cd '[:digit:]')
  130. remotebuilddigit=$(echo "${remotebuild}" | tr -cd '[:digit:]')
  131. if [ "${localbuilddigit}" -ne "${remotebuilddigit}" ]||[ "${forceupdate}" == "1" ]; then
  132. fn_print_ok_nl "Checking for update: ${remotelocation}"
  133. if [ "${forceupdate}" == "1" ]; then
  134. # forceupdate bypasses checks, useful for small build changes
  135. mtaupdatestatus="forced"
  136. else
  137. mtaupdatestatus="available"
  138. fi
  139. sleep 0.5
  140. echo -en "\n"
  141. echo -e "Update ${mtaupdatestatus}:"
  142. echo -e "* Local build: ${red}${localbuild}${default}"
  143. echo -e "* Remote build: ${green}${remotebuild}${default}"
  144. fn_script_log_info "Update available"
  145. fn_script_log_info "Local build: ${localbuild}"
  146. fn_script_log_info "Remote build: ${remotebuild}"
  147. fn_script_log_info "${localbuild} > ${remotebuild}"
  148. sleep 0.5
  149. echo -en "\n"
  150. echo -en "applying update.\r"
  151. sleep 1
  152. echo -en "applying update..\r"
  153. sleep 1
  154. echo -en "applying update...\r"
  155. sleep 1
  156. echo -en "\n"
  157. unset updateonstart
  158. check_status.sh
  159. # If server stopped.
  160. if [ "${status}" == "0" ]; then
  161. exitbypass=1
  162. fn_update_mta_dl
  163. exitbypass=1
  164. command_start.sh
  165. exitbypass=1
  166. command_stop.sh
  167. # If server started.
  168. else
  169. exitbypass=1
  170. command_stop.sh
  171. exitbypass=1
  172. fn_update_mta_dl
  173. exitbypass=1
  174. command_start.sh
  175. fi
  176. alert="update"
  177. alert.sh
  178. else
  179. fn_print_ok_nl "Checking for update: ${remotelocation}"
  180. sleep 0.5
  181. echo -en "\n"
  182. echo -e "No update available"
  183. echo -e "* Local build: ${green}${localbuild}${default}"
  184. echo -e "* Remote build: ${green}${remotebuild}${default}"
  185. fn_script_log_info "No update available"
  186. fn_script_log_info "Local build: ${localbuild}"
  187. fn_script_log_info "Remote build: ${remotebuild}"
  188. fi
  189. }
  190. # The location where the builds are checked and downloaded.
  191. remotelocation="linux.mtasa.com"
  192. if [ "${installer}" == "1" ]; then
  193. fn_update_mta_remotebuild
  194. fn_update_mta_dl
  195. else
  196. fn_print_dots "Checking for update: ${remotelocation}"
  197. fn_script_log_info "Checking for update: ${remotelocation}"
  198. sleep 0.5
  199. fn_update_mta_localbuild
  200. fn_update_mta_remotebuild
  201. fn_update_mta_compare
  202. fi