4
0

update_mta.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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_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. if [ "${localbuild}" != "${remotebuildversion}" ] || [ "${forceupdate}" == "1" ]; then
  58. if [ "${forceupdate}" == "1" ]; then
  59. # forceupdate bypasses checks, useful for small build changes
  60. mtaupdatestatus="forced"
  61. else
  62. mtaupdatestatus="available"
  63. fi
  64. fn_print_ok_nl "Checking for update: ${remotelocation}"
  65. echo -en "\n"
  66. echo -e "Update available"
  67. echo -e "* Local build: ${red}${localbuild}${default}"
  68. echo -e "* Remote build: ${green}${remotebuildversion}${default}"
  69. if [ -n "${branch}" ]; then
  70. echo -e "* Branch: ${branch}"
  71. fi
  72. if [ -f "${rootdir}/.dev-debug" ]; then
  73. echo -e "Remote build info"
  74. echo -e "* apiurl: ${apiurl}"
  75. echo -e "* remotebuildfilename: ${remotebuildfilename}"
  76. echo -e "* remotebuildurl: ${remotebuildurl}"
  77. echo -e "* remotebuildversion: ${remotebuildversion}"
  78. fi
  79. echo -en "\n"
  80. fn_script_log_info "Update available"
  81. fn_script_log_info "Local build: ${localbuild}"
  82. fn_script_log_info "Remote build: ${remotebuildversion}"
  83. if [ -n "${branch}" ]; then
  84. fn_script_log_info "Branch: ${branch}"
  85. fi
  86. fn_script_log_info "${localbuild} > ${remotebuildversion}"
  87. if [ "${commandname}" == "UPDATE" ]; then
  88. unset updateonstart
  89. check_status.sh
  90. # If server stopped.
  91. if [ "${status}" == "0" ]; then
  92. fn_update_dl
  93. if [ "${localbuild}" == "0" ]; then
  94. exitbypass=1
  95. command_start.sh
  96. fn_firstcommand_reset
  97. exitbypass=1
  98. sleep 5
  99. command_stop.sh
  100. fn_firstcommand_reset
  101. fi
  102. # If server started.
  103. else
  104. fn_print_restart_warning
  105. exitbypass=1
  106. command_stop.sh
  107. fn_firstcommand_reset
  108. exitbypass=1
  109. fn_update_dl
  110. exitbypass=1
  111. command_start.sh
  112. fn_firstcommand_reset
  113. fi
  114. unset exitbypass
  115. date +%s > "${lockdir}/lastupdate.lock"
  116. alert="update"
  117. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  118. alert="check-update"
  119. fi
  120. alert.sh
  121. else
  122. fn_print_ok_nl "Checking for update: ${remotelocation}"
  123. echo -en "\n"
  124. echo -e "No update available"
  125. echo -e "* Local build: ${green}${localbuild}${default}"
  126. echo -e "* Remote build: ${green}${remotebuildversion}${default}"
  127. if [ -n "${branch}" ]; then
  128. echo -e "* Branch: ${branch}"
  129. fi
  130. echo -en "\n"
  131. fn_script_log_info "No update available"
  132. fn_script_log_info "Local build: ${localbuild}"
  133. fn_script_log_info "Remote build: ${remotebuildversion}"
  134. if [ -n "${branch}" ]; then
  135. fn_script_log_info "Branch: ${branch}"
  136. fi
  137. if [ -f "${rootdir}/.dev-debug" ]; then
  138. echo -e "Remote build info"
  139. echo -e "* apiurl: ${apiurl}"
  140. echo -e "* remotebuildfilename: ${remotebuildfilename}"
  141. echo -e "* remotebuildurl: ${remotebuildurl}"
  142. echo -e "* remotebuildversion: ${remotebuildversion}"
  143. fi
  144. fi
  145. }
  146. # The location where the builds are checked and downloaded.
  147. remotelocation="linux.mtasa.com"
  148. if [ "${firstcommandname}" == "INSTALL" ]; then
  149. fn_update_remotebuild
  150. fn_update_dl
  151. else
  152. fn_print_dots "Checking for update"
  153. fn_print_dots "Checking for update: ${remotelocation}"
  154. fn_script_log_info "Checking for update: ${remotelocation}"
  155. fn_update_localbuild
  156. fn_update_remotebuild
  157. fn_update_compare
  158. fi