update_mta.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/bin/bash
  2. # LinuxGSM update_mta.sh function
  3. # Author: Daniel Gibbs
  4. # Website: https://gameservermanagers.com
  5. # Description: Handles updating of Multi Theft Auto servers.
  6. local commandname="UPDATE"
  7. local commandaction="Update"
  8. local function_selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  9. fn_update_mta_dl(){
  10. fn_fetch_file "http://linux.mtasa.com/dl/${numversion}/multitheftauto_linux_x64-${fullversion}.tar.gz" "${tmpdir}" "multitheftauto_linux_x64-${fullversion}.tar.gz"
  11. fn_dl_extract "${tmpdir}" "multitheftauto_linux_x64-${fullversion}.tar.gz" "${tmpdir}"
  12. echo -e "copying to ${serverfiles}...\c"
  13. fn_script_log "Copying to ${serverfiles}"
  14. cp -R "${tmpdir}/multitheftauto_linux_x64-${fullversion}/"* "${serverfiles}"
  15. local exitcode=$?
  16. if [ "${exitcode}" == "0" ]; then
  17. fn_print_ok_eol_nl
  18. else
  19. fn_print_fail_eol_nl
  20. fi
  21. }
  22. fn_update_mta_currentbuild(){
  23. # Gets current build info
  24. # Checks if current build info is available. If it fails, then a server restart will be forced to generate logs.
  25. if [ ! -f "${consolelogdir}/${servicename}-console.log" ]; then
  26. fn_print_error "Checking for update: linux.mtasa.com"
  27. sleep 1
  28. fn_print_error_nl "Checking for update: linux.mtasa.com: No logs with server version found"
  29. fn_script_log_error "Checking for update: linux.mtasa.com: No logs with server version found"
  30. sleep 1
  31. fn_print_info_nl "Checking for update: linux.mtasa.com: Forcing server restart"
  32. fn_script_log_info "Checking for update: linux.mtasa.com: Forcing server restart"
  33. sleep 1
  34. exitbypass=1
  35. command_stop.sh
  36. exitbypass=1
  37. command_start.sh
  38. sleep 1
  39. # Check again and exit on failure.
  40. if [ ! -f "${consolelogdir}/${servicename}-console.log" ]; then
  41. fn_print_fail_nl "Checking for update: linux.mtasa.com: Still No logs with server version found"
  42. fn_script_log_fatal "Checking for update: linux.mtasa.com: Still No logs with server version found"
  43. core_exit.sh
  44. fi
  45. fi
  46. # Get current build from logs
  47. currentbuild=$(awk -F"= Multi Theft Auto: San Andreas v" '{print $2}' "${consolelogdir}"/"${servicename}"-console.log | awk '{print $1}')
  48. if [ -z "${currentbuild}" ]; then
  49. fn_print_error_nl "Checking for update: linux.mtasa.com: Current build version not found"
  50. fn_script_log_error "Checking for update: linux.mtasa.com: Current build version not found"
  51. sleep 1
  52. fn_print_info_nl "Checking for update: linux.mtasa.com: Forcing server restart"
  53. fn_script_log_info "Checking for update: linux.mtasa.com: Forcing server restart"
  54. exitbypass=1
  55. command_stop.sh
  56. exitbypass=1
  57. command_start.sh
  58. currentbuild=$(awk -F"= Multi Theft Auto: San Andreas v" '{print $2}' "${consolelogdir}"/"${servicename}"-console.log | awk '{print $1}')
  59. if [ -z "${currentbuild}" ]; then
  60. fn_print_fail_nl "Checking for update: linux.mtasa.com: Current build version still not found"
  61. fn_script_log_fatal "Checking for update: linux.mtasa.com: Current build version still not found"
  62. core_exit.sh
  63. fi
  64. fi
  65. }
  66. fn_mta_get_availablebuild(){
  67. fn_fetch_file "https://raw.githubusercontent.com/multitheftauto/mtasa-blue/master/Server/version.h" "${tmpdir}" "version.h" # we need to find latest stable version here
  68. local majorversion="$(cat ${tmpdir}/version.h | grep "#define MTASA_VERSION_MAJOR" | awk '{ print $3 }' | sed 's/\r//g')"
  69. local minorversion="$(cat ${tmpdir}/version.h | grep "#define MTASA_VERSION_MINOR" | awk '{ print $3 }' | sed 's/\r//g')"
  70. local maintenanceversion="$(cat ${tmpdir}/version.h | grep "#define MTASA_VERSION_MAINTENANCE" | awk '{ print $3 }' | sed 's/\r//g')"
  71. numversion="${majorversion}${minorversion}${maintenanceversion}"
  72. fullversion="${majorversion}.${minorversion}.${maintenanceversion}"
  73. rm -f "${tmpdir}/version.h"
  74. }
  75. fn_update_mta_compare(){
  76. # Removes dots so if can compare version numbers
  77. currentbuilddigit=$(echo "${currentbuild}"|tr -cd '[:digit:]')
  78. if [ "${currentbuilddigit}" -ne "${numversion}" ]||[ "${forceupdate}" == "1" ]; then
  79. if [ "${forceupdate}" == "1" ]; then
  80. # forceupdate bypasses checks, useful for small build changes
  81. mta_update_string="forced"
  82. else
  83. mta_update_string="available"
  84. fi
  85. echo -e "\n"
  86. echo -e "Update ${mta_update_string}:"
  87. sleep 1
  88. echo -e " Current build: ${red}${currentbuild} ${default}"
  89. echo -e " Available build: ${green}${fullversion} ${default}"
  90. echo -e ""
  91. sleep 1
  92. echo ""
  93. echo -en "Applying update.\r"
  94. sleep 1
  95. echo -en "Applying update..\r"
  96. sleep 1
  97. echo -en "Applying update...\r"
  98. sleep 1
  99. echo -en "\n"
  100. fn_script_log "Update ${mta_update_string}"
  101. fn_script_log "Current build: ${currentbuild}"
  102. fn_script_log "Available build: ${fullversion}"
  103. fn_script_log "${currentbuild} > ${fullversion}"
  104. unset updateonstart
  105. check_status.sh
  106. if [ "${status}" == "0" ]; then
  107. fn_update_mta_dl
  108. exitbypass=1
  109. command_start.sh
  110. exitbypass=1
  111. command_stop.sh
  112. else
  113. exitbypass=1
  114. command_stop.sh
  115. fn_update_mta_dl
  116. exitbypass=1
  117. command_start.sh
  118. fi
  119. alert="update"
  120. alert.sh
  121. else
  122. echo -e "\n"
  123. echo -e "No update available:"
  124. echo -e " Current version: ${green}${currentbuild}${default}"
  125. echo -e " Available version: ${green}${fullversion}${default}"
  126. echo -e ""
  127. fn_print_ok_nl "No update available"
  128. fn_script_log_info "Current build: ${currentbuild}"
  129. fn_script_log_info "Available build: ${fullversion}"
  130. fi
  131. }
  132. if [ "${installer}" == "1" ]; then
  133. fn_mta_get_availablebuild
  134. fn_update_mta_dl
  135. else
  136. # Checks for server update from linux.mtasa.com using the github repo.
  137. fn_print_dots "Checking for update: linux.mtasa.com"
  138. fn_script_log_info "Checking for update: linux.mtasa.com"
  139. sleep 1
  140. fn_update_mta_currentbuild
  141. fn_mta_get_availablebuild
  142. fn_update_mta_compare
  143. fi