update_mta.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/bin/bash
  2. # LinuxGSM update_mta.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://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. fn_clear_tmp
  13. }
  14. fn_update_localbuild() {
  15. # Gets local build info.
  16. fn_print_dots "Checking local build: ${remotelocation}"
  17. # Uses executable to get local build.
  18. if [ -d "${executabledir}" ]; then
  19. cd "${executabledir}" || exit
  20. localbuild=$(${executable} -v 2> /dev/null)
  21. fi
  22. if [ -z "${localbuild}" ]; then
  23. fn_print_error "Checking local build: ${remotelocation}: missing local build info"
  24. fn_script_log_error "Missing local build info"
  25. fn_script_log_error "Set localbuild to 0"
  26. localbuild="0"
  27. else
  28. fn_print_ok "Checking local build: ${remotelocation}"
  29. fn_script_log_pass "Checking local build"
  30. fi
  31. }
  32. fn_update_remotebuild() {
  33. # Get remote build info.
  34. apiurl="https://linux.multitheftauto.com/revision/latest.txt"
  35. remotebuildresponse=$(curl -s "${apiurl}")
  36. remotebuildfilename="multitheftauto_linux_x64.tar.gz"
  37. remotebuildurl="http://linux.mtasa.com/dl/multitheftauto_linux_x64.tar.gz"
  38. remotebuildversion=$(echo "${remotebuildresponse}")
  39. if [ "${firstcommandname}" != "INSTALL" ]; then
  40. fn_print_dots "Checking remote build: ${remotelocation}"
  41. # Checks if remotebuildversion variable has been set.
  42. if [ -z "${remotebuildversion}" ] || [ "${remotebuildversion}" == "null" ]; then
  43. fn_print_fail "Checking remote build: ${remotelocation}"
  44. fn_script_log_fail "Checking remote build"
  45. core_exit.sh
  46. else
  47. fn_print_ok "Checking remote build: ${remotelocation}"
  48. fn_script_log_pass "Checking remote build"
  49. fi
  50. else
  51. # Checks if remotebuild variable has been set.
  52. if [ -z "${remotebuildversion}" ] || [ "${remotebuildversion}" == "null" ]; then
  53. fn_print_failure "Unable to get remote build"
  54. fn_script_log_fail "Unable to get remote build"
  55. core_exit.sh
  56. fi
  57. fi
  58. }
  59. fn_update_compare() {
  60. fn_print_dots "Checking for update: ${remotelocation}"
  61. # Update has been found or force update.
  62. if [ "${localbuild}" != "${remotebuildversion}" ] || [ "${forceupdate}" == "1" ]; then
  63. # Create update lockfile.
  64. date '+%s' > "${lockdir:?}/update.lock"
  65. if [ "${forceupdate}" == "1" ]; then
  66. # forceupdate bypasses checks, useful for small build changes
  67. mtaupdatestatus="forced"
  68. else
  69. mtaupdatestatus="available"
  70. fi
  71. fn_print_ok_nl "Checking for update: ${remotelocation}"
  72. echo -en "\n"
  73. echo -e "Update available"
  74. echo -e "* Local build: ${red}${localbuild}${default}"
  75. echo -e "* Remote build: ${green}${remotebuildversion}${default}"
  76. if [ -n "${branch}" ]; then
  77. echo -e "* Branch: ${branch}"
  78. fi
  79. if [ -f "${rootdir}/.dev-debug" ]; then
  80. echo -e "Remote build info"
  81. echo -e "* apiurl: ${apiurl}"
  82. echo -e "* remotebuildfilename: ${remotebuildfilename}"
  83. echo -e "* remotebuildurl: ${remotebuildurl}"
  84. echo -e "* remotebuildversion: ${remotebuildversion}"
  85. fi
  86. echo -en "\n"
  87. fn_script_log_info "Update available"
  88. fn_script_log_info "Local build: ${localbuild}"
  89. fn_script_log_info "Remote build: ${remotebuildversion}"
  90. if [ -n "${branch}" ]; then
  91. fn_script_log_info "Branch: ${branch}"
  92. fi
  93. fn_script_log_info "${localbuild} > ${remotebuildversion}"
  94. if [ "${commandname}" == "UPDATE" ]; then
  95. unset updateonstart
  96. check_status.sh
  97. # If server stopped.
  98. if [ "${status}" == "0" ]; then
  99. fn_update_dl
  100. if [ "${localbuild}" == "0" ]; then
  101. exitbypass=1
  102. command_start.sh
  103. fn_firstcommand_reset
  104. exitbypass=1
  105. fn_sleep_time_5
  106. command_stop.sh
  107. fn_firstcommand_reset
  108. fi
  109. # If server started.
  110. else
  111. fn_print_restart_warning
  112. exitbypass=1
  113. command_stop.sh
  114. fn_firstcommand_reset
  115. exitbypass=1
  116. fn_update_dl
  117. exitbypass=1
  118. command_start.sh
  119. fn_firstcommand_reset
  120. fi
  121. unset exitbypass
  122. date +%s > "${lockdir}/last-updated.lock"
  123. alert="update"
  124. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  125. alert="check-update"
  126. fi
  127. alert.sh
  128. else
  129. fn_print_ok_nl "Checking for update: ${remotelocation}"
  130. echo -en "\n"
  131. echo -e "No update available"
  132. echo -e "* Local build: ${green}${localbuild}${default}"
  133. echo -e "* Remote build: ${green}${remotebuildversion}${default}"
  134. if [ -n "${branch}" ]; then
  135. echo -e "* Branch: ${branch}"
  136. fi
  137. echo -en "\n"
  138. fn_script_log_info "No update available"
  139. fn_script_log_info "Local build: ${localbuild}"
  140. fn_script_log_info "Remote build: ${remotebuildversion}"
  141. if [ -n "${branch}" ]; then
  142. fn_script_log_info "Branch: ${branch}"
  143. fi
  144. if [ -f "${rootdir}/.dev-debug" ]; then
  145. echo -e "Remote build info"
  146. echo -e "* apiurl: ${apiurl}"
  147. echo -e "* remotebuildfilename: ${remotebuildfilename}"
  148. echo -e "* remotebuildurl: ${remotebuildurl}"
  149. echo -e "* remotebuildversion: ${remotebuildversion}"
  150. fi
  151. fi
  152. }
  153. # The location where the builds are checked and downloaded.
  154. remotelocation="linux.mtasa.com"
  155. if [ "${firstcommandname}" == "INSTALL" ]; then
  156. fn_update_remotebuild
  157. fn_update_dl
  158. else
  159. fn_print_dots "Checking for update"
  160. fn_print_dots "Checking for update: ${remotelocation}"
  161. fn_script_log_info "Checking for update: ${remotelocation}"
  162. fn_update_localbuild
  163. fn_update_remotebuild
  164. fn_update_compare
  165. fi