update_mta.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. fn_update_dl() {
  9. # Download and extract files to tmpdir.
  10. fn_fetch_file "http://linux.mtasa.com/dl/multitheftauto_linux_x64.tar.gz" "" "" "" "${tmpdir}" "multitheftauto_linux_x64.tar.gz" "nochmodx" "norun" "force" "nohash"
  11. fn_dl_extract "${tmpdir}" "multitheftauto_linux_x64.tar.gz" "${serverfiles}" "multitheftauto_linux_x64"
  12. }
  13. fn_update_localbuild() {
  14. # Gets local build info.
  15. fn_print_dots "Checking local build: ${remotelocation}"
  16. # Uses log file to get local build.
  17. localbuild=$(grep "= Multi Theft Auto: San Andreas v" "${serverfiles}/mods/deathmatch/logs/server.log" | awk '{ print $7 }' | sed -r 's/^.{1}//' | tail -1)
  18. if [ -z "${localbuild}" ]; then
  19. fn_print_error "Checking local build: ${remotelocation}: missing local build info"
  20. fn_script_log_error "Missing local build info"
  21. fn_script_log_error "Set localbuild to 0"
  22. localbuild="0"
  23. else
  24. fn_print_ok "Checking local build: ${remotelocation}"
  25. fn_script_log_pass "Checking local build"
  26. fi
  27. }
  28. fn_update_remotebuild() {
  29. # Get remote build info.
  30. apiurl="https://api.github.com/repos/multitheftauto/mtasa-blue/releases/latest"
  31. remotebuildresponse=$(curl -s "${apiurl}")
  32. remotebuildfilename=$(echo "${remotebuildresponse}" | jq -r '.assets[]|select(.browser_download_url | contains("Linux-amd64")) | .name')
  33. remotebuildurl=$(echo "${remotebuildresponse}" | jq -r '.assets[]|select(.browser_download_url | contains("Linux-amd64")) | .browser_download_url')
  34. remotebuildversion=$(echo "${remotebuildresponse}" | jq -r '.tag_name')
  35. if [ "${firstcommandname}" != "INSTALL" ]; then
  36. fn_print_dots "Checking remote build: ${remotelocation}"
  37. # Checks if remotebuildversion variable has been set.
  38. if [ -z "${remotebuildversion}" ] || [ "${remotebuildversion}" == "null" ]; then
  39. fn_print_fail "Checking remote build: ${remotelocation}"
  40. fn_script_log_fatal "Checking remote build"
  41. core_exit.sh
  42. else
  43. fn_print_ok "Checking remote build: ${remotelocation}"
  44. fn_script_log_pass "Checking remote build"
  45. fi
  46. else
  47. # Checks if remotebuild variable has been set.
  48. if [ -z "${remotebuildversion}" ] || [ "${remotebuildversion}" == "null" ]; then
  49. fn_print_failure "Unable to get remote build"
  50. fn_script_log_fatal "Unable to get remote build"
  51. core_exit.sh
  52. fi
  53. fi
  54. }
  55. fn_update_compare() {
  56. fn_print_dots "Checking for update: ${remotelocation}"
  57. # Update has been found or force update.
  58. if [ "${localbuild}" != "${remotebuildversion}" ] || [ "${forceupdate}" == "1" ]; then
  59. # Create update lockfile.
  60. date '+%s' > "${lockdir:?}/update.lock"
  61. if [ "${forceupdate}" == "1" ]; then
  62. # forceupdate bypasses checks, useful for small build changes
  63. mtaupdatestatus="forced"
  64. else
  65. mtaupdatestatus="available"
  66. fi
  67. fn_print_ok_nl "Checking for update: ${remotelocation}"
  68. echo -en "\n"
  69. echo -e "Update available"
  70. echo -e "* Local build: ${red}${localbuild}${default}"
  71. echo -e "* Remote build: ${green}${remotebuildversion}${default}"
  72. if [ -n "${branch}" ]; then
  73. echo -e "* Branch: ${branch}"
  74. fi
  75. if [ -f "${rootdir}/.dev-debug" ]; then
  76. echo -e "Remote build info"
  77. echo -e "* apiurl: ${apiurl}"
  78. echo -e "* remotebuildfilename: ${remotebuildfilename}"
  79. echo -e "* remotebuildurl: ${remotebuildurl}"
  80. echo -e "* remotebuildversion: ${remotebuildversion}"
  81. fi
  82. echo -en "\n"
  83. fn_script_log_info "Update available"
  84. fn_script_log_info "Local build: ${localbuild}"
  85. fn_script_log_info "Remote build: ${remotebuildversion}"
  86. if [ -n "${branch}" ]; then
  87. fn_script_log_info "Branch: ${branch}"
  88. fi
  89. fn_script_log_info "${localbuild} > ${remotebuildversion}"
  90. if [ "${commandname}" == "UPDATE" ]; then
  91. unset updateonstart
  92. check_status.sh
  93. # If server stopped.
  94. if [ "${status}" == "0" ]; then
  95. fn_update_dl
  96. if [ "${localbuild}" == "0" ]; then
  97. exitbypass=1
  98. command_start.sh
  99. fn_firstcommand_reset
  100. exitbypass=1
  101. sleep 5
  102. command_stop.sh
  103. fn_firstcommand_reset
  104. fi
  105. # If server started.
  106. else
  107. fn_print_restart_warning
  108. exitbypass=1
  109. command_stop.sh
  110. fn_firstcommand_reset
  111. exitbypass=1
  112. fn_update_dl
  113. exitbypass=1
  114. command_start.sh
  115. fn_firstcommand_reset
  116. fi
  117. unset exitbypass
  118. date +%s > "${lockdir}/last-updated.lock"
  119. alert="update"
  120. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  121. alert="check-update"
  122. fi
  123. alert.sh
  124. else
  125. fn_print_ok_nl "Checking for update: ${remotelocation}"
  126. echo -en "\n"
  127. echo -e "No update available"
  128. echo -e "* Local build: ${green}${localbuild}${default}"
  129. echo -e "* Remote build: ${green}${remotebuildversion}${default}"
  130. if [ -n "${branch}" ]; then
  131. echo -e "* Branch: ${branch}"
  132. fi
  133. echo -en "\n"
  134. fn_script_log_info "No update available"
  135. fn_script_log_info "Local build: ${localbuild}"
  136. fn_script_log_info "Remote build: ${remotebuildversion}"
  137. if [ -n "${branch}" ]; then
  138. fn_script_log_info "Branch: ${branch}"
  139. fi
  140. if [ -f "${rootdir}/.dev-debug" ]; then
  141. echo -e "Remote build info"
  142. echo -e "* apiurl: ${apiurl}"
  143. echo -e "* remotebuildfilename: ${remotebuildfilename}"
  144. echo -e "* remotebuildurl: ${remotebuildurl}"
  145. echo -e "* remotebuildversion: ${remotebuildversion}"
  146. fi
  147. fi
  148. }
  149. # The location where the builds are checked and downloaded.
  150. remotelocation="linux.mtasa.com"
  151. if [ "${firstcommandname}" == "INSTALL" ]; then
  152. fn_update_remotebuild
  153. fn_update_dl
  154. else
  155. fn_print_dots "Checking for update"
  156. fn_print_dots "Checking for update: ${remotelocation}"
  157. fn_script_log_info "Checking for update: ${remotelocation}"
  158. fn_update_localbuild
  159. fn_update_remotebuild
  160. fn_update_compare
  161. fi