update_mta.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. {
  68. 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
  69. local majorversion="$(cat ${tmpdir}/version.h | grep "#define MTASA_VERSION_MAJOR" | awk '{ print $3 }' | sed 's/\r//g')"
  70. local minorversion="$(cat ${tmpdir}/version.h | grep "#define MTASA_VERSION_MINOR" | awk '{ print $3 }' | sed 's/\r//g')"
  71. local maintenanceversion="$(cat ${tmpdir}/version.h | grep "#define MTASA_VERSION_MAINTENANCE" | awk '{ print $3 }' | sed 's/\r//g')"
  72. numversion="${majorversion}${minorversion}${maintenanceversion}"
  73. fullversion="${majorversion}.${minorversion}.${maintenanceversion}"
  74. rm -f "${tmpdir}/version.h"
  75. }
  76. fn_update_mta_compare(){
  77. # Removes dots so if can compare version numbers
  78. currentbuilddigit=$(echo "${currentbuild}"|tr -cd '[:digit:]')
  79. if [ "${currentbuilddigit}" -ne "${numversion}" ]||[ "${forceupdate}" == "1" ]; then
  80. if [ "${forceupdate}" == "1" ]; then
  81. # forceupdate bypasses checks, useful for small build changes
  82. mta_update_string="forced"
  83. else
  84. mta_update_string="available"
  85. fi
  86. echo -e "\n"
  87. echo -e "Update ${mta_update_string}:"
  88. sleep 1
  89. echo -e " Current build: ${red}${currentbuild} ${default}"
  90. echo -e " Available build: ${green}${fullversion} ${default}"
  91. echo -e ""
  92. sleep 1
  93. echo ""
  94. echo -en "Applying update.\r"
  95. sleep 1
  96. echo -en "Applying update..\r"
  97. sleep 1
  98. echo -en "Applying update...\r"
  99. sleep 1
  100. echo -en "\n"
  101. fn_script_log "Update ${mta_update_string}"
  102. fn_script_log "Current build: ${currentbuild}"
  103. fn_script_log "Available build: ${fullversion}"
  104. fn_script_log "${currentbuild} > ${fullversion}"
  105. unset updateonstart
  106. check_status.sh
  107. if [ "${status}" == "0" ]; then
  108. fn_update_mta_dl
  109. exitbypass=1
  110. command_start.sh
  111. exitbypass=1
  112. command_stop.sh
  113. else
  114. exitbypass=1
  115. command_stop.sh
  116. fn_update_mta_dl
  117. exitbypass=1
  118. command_start.sh
  119. fi
  120. alert="update"
  121. alert.sh
  122. else
  123. echo -e "\n"
  124. echo -e "No update available:"
  125. echo -e " Current version: ${green}${currentbuild}${default}"
  126. echo -e " Available version: ${green}${fullversion}${default}"
  127. echo -e ""
  128. fn_print_ok_nl "No update available"
  129. fn_script_log_info "Current build: ${currentbuild}"
  130. fn_script_log_info "Available build: ${fullversion}"
  131. fi
  132. }
  133. if [ "${installer}" == "1" ]; then
  134. fn_mta_get_availablebuild
  135. fn_update_mta_dl
  136. else
  137. # Checks for server update from linux.mtasa.com using the github repo.
  138. fn_print_dots "Checking for update: linux.mtasa.com"
  139. fn_script_log_info "Checking for update: linux.mtasa.com"
  140. sleep 1
  141. fn_update_mta_currentbuild
  142. fn_mta_get_availablebuild
  143. fn_update_mta_compare
  144. fi