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. # Gets 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. remotebuild="${remotebuildresponse}"
  39. if [ "${firstcommandname}" != "INSTALL" ]; then
  40. fn_print_dots "Checking remote build: ${remotelocation}"
  41. # Checks if remotebuild variable has been set.
  42. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "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 "${remotebuild}" ] || [ "${remotebuild}" == "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}" != "${remotebuild}" ] || [ "${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. fn_print "\n"
  73. fn_print_nl "${bold}${underline}Update${default} available"
  74. fn_print_nl "* Local build: ${red}${localbuild}${default}"
  75. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  76. if [ -n "${branch}" ]; then
  77. fn_print_nl "* Branch: ${branch}"
  78. fi
  79. if [ -f "${rootdir}/.dev-debug" ]; then
  80. fn_print_nl "Remote build info"
  81. fn_print_nl "* apiurl: ${apiurl}"
  82. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  83. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  84. fn_print_nl "* remotebuild: ${remotebuild}"
  85. fi
  86. fn_print "\n"
  87. fn_script_log_info "Update available"
  88. fn_script_log_info "Local build: ${localbuild}"
  89. fn_script_log_info "Remote build: ${remotebuild}"
  90. if [ -n "${branch}" ]; then
  91. fn_script_log_info "Branch: ${branch}"
  92. fi
  93. fn_script_log_info "${localbuild} > ${remotebuild}"
  94. if [ "${commandname}" == "UPDATE" ]; then
  95. date +%s > "${lockdir:?}/last-updated.lock"
  96. unset updateonstart
  97. check_status.sh
  98. # If server stopped.
  99. if [ "${status}" == "0" ]; then
  100. fn_update_dl
  101. if [ "${localbuild}" == "0" ]; then
  102. exitbypass=1
  103. command_start.sh
  104. fn_firstcommand_reset
  105. exitbypass=1
  106. fn_sleep_time_5
  107. command_stop.sh
  108. fn_firstcommand_reset
  109. fi
  110. # If server started.
  111. else
  112. fn_print_restart_warning
  113. exitbypass=1
  114. command_stop.sh
  115. fn_firstcommand_reset
  116. exitbypass=1
  117. fn_update_dl
  118. exitbypass=1
  119. command_start.sh
  120. fn_firstcommand_reset
  121. fi
  122. unset exitbypass
  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. fn_print "\n"
  131. fn_print_nl "${bold}${underline}No update${default} available"
  132. fn_print_nl "* Local build: ${green}${localbuild}${default}"
  133. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  134. if [ -n "${branch}" ]; then
  135. fn_print_nl "* Branch: ${branch}"
  136. fi
  137. fn_print "\n"
  138. fn_script_log_info "No update available"
  139. fn_script_log_info "Local build: ${localbuild}"
  140. fn_script_log_info "Remote build: ${remotebuild}"
  141. if [ -n "${branch}" ]; then
  142. fn_script_log_info "Branch: ${branch}"
  143. fi
  144. if [ -f "${rootdir}/.dev-debug" ]; then
  145. fn_print_nl "Remote build info"
  146. fn_print_nl "* apiurl: ${apiurl}"
  147. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  148. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  149. fn_print_nl "* remotebuild: ${remotebuild}"
  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