update_mta.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #!/bin/bash
  2. # LinuxGSM update_mta.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: http://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of Multi Theft Auto servers.
  7. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. fn_update_mta_dl(){
  9. fn_fetch_file "http://linux.mtasa.com/dl/multitheftauto_linux_x64.tar.gz" "" "" "" "${tmpdir}" "multitheftauto_linux_x64.tar.gz" "" "norun" "noforce" "nomd5"
  10. mkdir "${tmpdir}/multitheftauto_linux_x64"
  11. fn_dl_extract "${tmpdir}" "multitheftauto_linux_x64.tar.gz" "${tmpdir}/multitheftauto_linux_x64"
  12. echo -e "copying to ${serverfiles}...\c"
  13. cp -R "${tmpdir}/multitheftauto_linux_x64/multitheftauto_linux_x64/"* "${serverfiles}"
  14. local exitcode=$?
  15. fn_clear_tmp
  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. else
  21. fn_print_fail_eol_nl
  22. fn_script_log_fatal "Copying to ${serverfiles}"
  23. core_exit.sh
  24. fi
  25. }
  26. fn_update_mta_localbuild(){
  27. # Gets local build info.
  28. fn_print_dots "Checking local build: ${remotelocation}"
  29. # Uses log file to gather info.
  30. # Gives time for log file to generate.
  31. requirerestart=1
  32. if [ ! -f "${serverfiles}/mods/deathmatch/logs/server.log" ]; then
  33. fn_print_error "Checking local build: ${remotelocation}"
  34. fn_print_error_nl "Checking local build: ${remotelocation}: no log files containing version info"
  35. fn_print_info_nl "Checking local build: ${remotelocation}: forcing server restart"
  36. fn_script_log_error "No log files containing version info"
  37. fn_script_log_info "Forcing server restart"
  38. exitbypass=1
  39. command_stop.sh
  40. exitbypass=1
  41. command_start.sh
  42. fn_firstcommand_reset
  43. totalseconds=0
  44. # Check again, allow time to generate logs.
  45. while [ ! -f "${serverfiles}/mods/deathmatch/logs/server.log" ]; do
  46. sleep 1
  47. fn_print_info "Checking local build: ${remotelocation}: waiting for log file: ${totalseconds}"
  48. if [ -v "${loopignore}" ]; then
  49. loopignore=1
  50. fn_script_log_info "Waiting for log file to generate"
  51. fi
  52. if [ "${totalseconds}" -gt "120" ]; then
  53. localbuild="0"
  54. fn_print_error "Checking local build: ${remotelocation}: waiting for log file: missing log file"
  55. fn_script_log_error "Missing log file"
  56. fn_script_log_error "Set localbuild to 0"
  57. fi
  58. totalseconds=$((totalseconds + 1))
  59. done
  60. fi
  61. if [ -z "${localbuild}" ]; then
  62. localbuild=$(grep "= Multi Theft Auto: San Andreas v" "${serverfiles}/mods/deathmatch/logs/server.log" | awk '{ print $7 }' | sed -r 's/^.{1}//' | tail -1)
  63. fi
  64. if [ -z "${localbuild}" ]; then
  65. # Gives time for var to generate.
  66. totalseconds=0
  67. for seconds in {1..120}; do
  68. fn_print_info "Checking local build: ${remotelocation}: 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 local build: ${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 local build: ${remotelocation}"
  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 [ "${firstcommandname}" != "INSTALL" ]; then
  98. fn_print_dots "Checking remote build: ${remotelocation}"
  99. # Checks if remotebuild variable has been set.
  100. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  101. fn_print_fail "Checking remote build: ${remotelocation}"
  102. fn_script_log_fatal "Checking remote build"
  103. core_exit.sh
  104. else
  105. fn_print_ok "Checking remote build: ${remotelocation}"
  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. echo -en "\n"
  135. fn_script_log_info "Update available"
  136. fn_script_log_info "Local build: ${localbuild}"
  137. fn_script_log_info "Remote build: ${remotebuild}"
  138. fn_script_log_info "${localbuild} > ${remotebuild}"
  139. unset updateonstart
  140. check_status.sh
  141. # If server stopped.
  142. if [ "${status}" == "0" ]; then
  143. exitbypass=1
  144. fn_update_mta_dl
  145. if [ "${requirerestart}" == "1" ]; then
  146. exitbypass=1
  147. command_start.sh
  148. fn_firstcommand_reset
  149. exitbypass=1
  150. command_stop.sh
  151. fn_firstcommand_reset
  152. fi
  153. # If server started.
  154. else
  155. fn_print_restart_warning
  156. exitbypass=1
  157. command_stop.sh
  158. fn_firstcommand_reset
  159. exitbypass=1
  160. fn_update_mta_dl
  161. exitbypass=1
  162. command_start.sh
  163. fn_firstcommand_reset
  164. fi
  165. unset exitbypass
  166. date +%s > "${lockdir}/lastupdate.lock"
  167. alert="update"
  168. alert.sh
  169. else
  170. fn_print_ok_nl "Checking for update: ${remotelocation}"
  171. echo -en "\n"
  172. echo -e "No update available"
  173. echo -e "* Local build: ${green}${localbuild}${default}"
  174. echo -e "* Remote build: ${green}${remotebuild}${default}"
  175. echo -en "\n"
  176. fn_script_log_info "No update available"
  177. fn_script_log_info "Local build: ${localbuild}"
  178. fn_script_log_info "Remote build: ${remotebuild}"
  179. fi
  180. }
  181. # The location where the builds are checked and downloaded.
  182. remotelocation="linux.mtasa.com"
  183. if [ "${firstcommandname}" == "INSTALL" ]; then
  184. fn_update_mta_remotebuild
  185. fn_update_mta_dl
  186. else
  187. fn_print_dots "Checking for update"
  188. fn_print_dots "Checking for update: ${remotelocation}"
  189. fn_script_log_info "Checking for update: ${remotelocation}"
  190. fn_update_mta_localbuild
  191. fn_update_mta_remotebuild
  192. fn_update_mta_compare
  193. fi