4
0

update_fivem.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/bin/bash
  2. # LinuxGSM update_fivem.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of FiveM servers.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. fn_update_resources() {
  9. # Download and extract the cfx-server-data base resources.
  10. fn_fetch_file "https://github.com/citizenfx/cfx-server-data/archive/refs/heads/master.tar.gz" "" "" "" "${tmpdir}" "cfx-server-data.tar.gz" "nochmodx" "norun" "force" "nohash"
  11. fn_dl_extract "${tmpdir}" "cfx-server-data.tar.gz" "${serverfiles}" "cfx-server-data-master/resources"
  12. }
  13. fn_update_dl() {
  14. # Download and extract files to tmpdir.
  15. fn_fetch_file "${remotebuildurl}" "" "" "" "${tmpdir}" "${remotebuildfilename}" "nochmodx" "norun" "force" "nohash"
  16. rm -rf "${serverfiles:?}/alpine"
  17. fn_dl_extract "${tmpdir}" "${remotebuildfilename}" "${serverfiles}"
  18. if [ ! -d "${serverfiles}/resources" ]; then
  19. fn_update_resources
  20. fi
  21. echo "${remotebuild}" > "${localbuildfile}"
  22. fn_clear_tmp
  23. }
  24. fn_update_localbuild() {
  25. # Gets local build info.
  26. fn_print_dots "Checking local build: ${remotelocation}"
  27. if [ -f "${localbuildfile}" ]; then
  28. localbuild="$(cat "${localbuildfile}")"
  29. fi
  30. if [ -z "${localbuild}" ]; then
  31. fn_print_error "Checking local build: ${remotelocation}: missing local build info"
  32. fn_script_log_error "Missing local build info"
  33. fn_script_log_error "Set localbuild to 0"
  34. localbuild="0"
  35. else
  36. fn_print_ok "Checking local build: ${remotelocation}"
  37. fn_script_log_pass "Checking local build"
  38. fi
  39. }
  40. fn_update_remotebuild() {
  41. # Gets remote build info.
  42. apiurl="https://changelogs-live.fivem.net/api/changelog/versions/linux/server"
  43. remotebuildresponse=$(curl -s "${apiurl}")
  44. remotebuild=$(echo "${remotebuildresponse}" | jq -r '.recommended')
  45. remotebuildurl=$(echo "${remotebuildresponse}" | jq -r '.recommended_download')
  46. remotebuildfilename="fx.tar.xz"
  47. if [ "${firstcommandname}" != "INSTALL" ]; then
  48. fn_print_dots "Checking remote build: ${remotelocation}"
  49. # Checks if remotebuild variable has been set.
  50. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ] || [ -z "${remotebuildurl}" ] || [ "${remotebuildurl}" == "null" ]; then
  51. fn_print_fail "Checking remote build: ${remotelocation}"
  52. fn_script_log_fail "Checking remote build"
  53. core_exit.sh
  54. else
  55. fn_print_ok "Checking remote build: ${remotelocation}"
  56. fn_script_log_pass "Checking remote build"
  57. fi
  58. else
  59. # Checks if remotebuild variable has been set.
  60. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ] || [ -z "${remotebuildurl}" ] || [ "${remotebuildurl}" == "null" ]; then
  61. fn_print_failure "Unable to get remote build"
  62. fn_script_log_fail "Unable to get remote build"
  63. core_exit.sh
  64. fi
  65. fi
  66. }
  67. fn_update_compare() {
  68. fn_print_dots "Checking for update: ${remotelocation}"
  69. # Update has been found or force update.
  70. if [ "${localbuild}" != "${remotebuild}" ] || [ "${forceupdate}" == "1" ]; then
  71. # Create update lockfile.
  72. date '+%s' > "${lockdir:?}/update.lock"
  73. fn_print_ok_nl "Checking for update: ${remotelocation}"
  74. fn_print "\n"
  75. fn_print_nl "${bold}${underline}Update${default} available"
  76. fn_print_nl "* Local build: ${red}${localbuild}${default}"
  77. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  78. if [ -f "${rootdir}/.dev-debug" ]; then
  79. fn_print_nl "Remote build info"
  80. fn_print_nl "* apiurl: ${apiurl}"
  81. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  82. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  83. fn_print_nl "* remotebuild: ${remotebuild}"
  84. fi
  85. fn_print "\n"
  86. fn_script_log_info "Update available"
  87. fn_script_log_info "Local build: ${localbuild}"
  88. fn_script_log_info "Remote build: ${remotebuild}"
  89. fn_script_log_info "${localbuild} > ${remotebuild}"
  90. if [ "${commandname}" == "UPDATE" ]; then
  91. unset updateonstart
  92. fn_update_apply fn_update_dl
  93. alert="update"
  94. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  95. alert="check-update"
  96. fi
  97. alert.sh
  98. else
  99. fn_print_ok_nl "Checking for update: ${remotelocation}"
  100. fn_print "\n"
  101. fn_print_nl "${bold}${underline}No update${default} available"
  102. fn_print_nl "* Local build: ${green}${localbuild}${default}"
  103. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  104. fn_print "\n"
  105. fn_script_log_info "No update available"
  106. fn_script_log_info "Local build: ${localbuild}"
  107. fn_script_log_info "Remote build: ${remotebuild}"
  108. if [ -f "${rootdir}/.dev-debug" ]; then
  109. fn_print_nl "Remote build info"
  110. fn_print_nl "* apiurl: ${apiurl}"
  111. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  112. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  113. fn_print_nl "* remotebuild: ${remotebuild}"
  114. fi
  115. fi
  116. }
  117. localbuildfile="${datadir}/${selfname}-fivem-version"
  118. # The location where the builds are checked and downloaded.
  119. remotelocation="runtime.fivem.net"
  120. if [ "${firstcommandname}" == "INSTALL" ]; then
  121. fn_update_remotebuild
  122. fn_update_dl
  123. else
  124. fn_print_dots "Checking for update"
  125. fn_print_dots "Checking for update: ${remotelocation}"
  126. fn_script_log_info "Checking for update: ${remotelocation}"
  127. fn_update_localbuild
  128. fn_update_remotebuild
  129. fn_update_compare
  130. fi