update_openra.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/bin/bash
  2. # LinuxGSM update_openra.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of OpenRA servers.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. fn_update_dl() {
  9. fn_fetch_file "${remotebuildurl}" "" "" "" "${tmpdir}" "${remotebuildfilename}" "chmodx" "norun" "force" "nohash"
  10. local extractdir
  11. extractdir=$(mktemp -d "${tmpdir}/openra.XXXXXX") || exit 1
  12. if ! (cd "${extractdir}" && "${tmpdir}/${remotebuildfilename}" --appimage-extract > /dev/null) || [ ! -x "${extractdir}/squashfs-root/usr/lib/openra/OpenRA.Server" ] || [ ! -x "${extractdir}/squashfs-root/AppRun" ]; then
  13. fn_print_fail_nl "Extracting OpenRA server files"
  14. fn_script_log_fail "Extracting OpenRA server files"
  15. exitcode=1
  16. core_exit.sh
  17. fi
  18. printf '%s\n' "${remotebuild}" > "${extractdir}/squashfs-root/build.txt" || exit 1
  19. if [ -d "${serverfiles}/openra" ]; then
  20. mv "${serverfiles}/openra" "${extractdir}/previous" || exit 1
  21. fi
  22. if ! mv "${extractdir}/squashfs-root" "${serverfiles}/openra"; then
  23. if [ -d "${extractdir}/previous" ]; then
  24. mv "${extractdir}/previous" "${serverfiles}/openra"
  25. fi
  26. exitcode=1
  27. core_exit.sh
  28. fi
  29. fn_clear_tmp
  30. }
  31. fn_update_localbuild() {
  32. fn_print_dots "Checking local build: ${remotelocation}"
  33. localbuild="0"
  34. if [ -x "${serverfiles}/openra/AppRun" ] && [ -x "${serverfiles}/openra/usr/lib/openra/OpenRA.Server" ]; then
  35. localbuild=$(head -n 1 "${serverfiles}/openra/build.txt" 2> /dev/null)
  36. fi
  37. if [ -z "${localbuild}" ]; then
  38. fn_print_error "Checking local build: ${remotelocation}: missing local build info"
  39. fn_script_log_error "Missing local build info"
  40. localbuild="0"
  41. else
  42. fn_print_ok "Checking local build: ${remotelocation}"
  43. fn_script_log_pass "Checking local build"
  44. fi
  45. }
  46. fn_update_remotebuild() {
  47. if [ "$(uname -m)" != "x86_64" ]; then
  48. fn_print_fail_nl "OpenRA requires an x86_64 Linux server"
  49. fn_script_log_fail "OpenRA requires an x86_64 Linux server"
  50. exitcode=1
  51. core_exit.sh
  52. fi
  53. apiurl="https://api.github.com/repos/OpenRA/OpenRA/releases/latest"
  54. if ! remotebuildresponse=$(curl --fail --location --silent --show-error --connect-timeout 10 --max-time 60 "${apiurl}"); then
  55. fn_print_fail_nl "Checking remote build: ${remotelocation}"
  56. fn_script_log_fail "Checking remote build: ${apiurl}"
  57. exitcode=1
  58. core_exit.sh
  59. fi
  60. remotebuildfilename="OpenRA-Red-Alert-x86_64.AppImage"
  61. remotebuild=$(echo "${remotebuildresponse}" | jq -er '.tag_name | select(test("^release-[0-9]+$"))')
  62. remotebuildurl=$(echo "${remotebuildresponse}" | jq -er --arg filename "${remotebuildfilename}" '[.assets[] | select(.name == $filename)] | select(length == 1) | .[0].browser_download_url')
  63. if [ -z "${remotebuild}" ] || [[ "${remotebuildurl}" != https://github.com/OpenRA/OpenRA/releases/download/* ]]; then
  64. fn_print_fail_nl "Unable to get OpenRA release asset"
  65. fn_script_log_fail "Unable to get OpenRA release asset"
  66. exitcode=1
  67. core_exit.sh
  68. fi
  69. fn_print_ok "Checking remote build: ${remotelocation}"
  70. fn_script_log_pass "Checking remote build: ${remotebuild}"
  71. }
  72. fn_update_compare() {
  73. fn_print_dots "Checking for update: ${remotelocation}"
  74. # Update has been found or force update.
  75. if [ "${localbuild}" != "${remotebuild}" ] || [ "${forceupdate}" == "1" ]; then
  76. # Create update lockfile.
  77. date '+%s' > "${lockdir:?}/update.lock"
  78. fn_print_ok_nl "Checking for update: ${remotelocation}"
  79. fn_print "\n"
  80. fn_print_nl "${bold}${underline}Update${default} available"
  81. fn_print_nl "* Local build: ${red}${localbuild}${default}"
  82. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  83. if [ -n "${branch}" ]; then
  84. fn_print_nl "* Branch: ${branch}"
  85. fi
  86. if [ -f "${rootdir}/.dev-debug" ]; then
  87. fn_print_nl "Remote build info"
  88. fn_print_nl "* apiurl: ${apiurl}"
  89. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  90. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  91. fn_print_nl "* remotebuild: ${remotebuild}"
  92. fi
  93. fn_print "\n"
  94. fn_script_log_info "Update available"
  95. fn_script_log_info "Local build: ${localbuild}"
  96. fn_script_log_info "Remote build: ${remotebuild}"
  97. if [ -n "${branch}" ]; then
  98. fn_script_log_info "Branch: ${branch}"
  99. fi
  100. fn_script_log_info "${localbuild} > ${remotebuild}"
  101. if [ "${commandname}" == "UPDATE" ]; then
  102. unset updateonstart
  103. fn_update_apply fn_update_dl
  104. unset exitbypass
  105. alert="update"
  106. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  107. alert="check-update"
  108. fi
  109. alert.sh
  110. else
  111. fn_print_ok_nl "Checking for update: ${remotelocation}"
  112. fn_print "\n"
  113. fn_print_nl "${bold}${underline}No update${default} available"
  114. fn_print_nl "* Local build: ${green}${localbuild}${default}"
  115. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  116. if [ -n "${branch}" ]; then
  117. fn_print_nl "* Branch: ${branch}"
  118. fi
  119. fn_print "\n"
  120. fn_script_log_info "No update available"
  121. fn_script_log_info "Local build: ${localbuild}"
  122. fn_script_log_info "Remote build: ${remotebuild}"
  123. if [ -n "${branch}" ]; then
  124. fn_script_log_info "Branch: ${branch}"
  125. fi
  126. if [ -f "${rootdir}/.dev-debug" ]; then
  127. fn_print_nl "Remote build info"
  128. fn_print_nl "* apiurl: ${apiurl}"
  129. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  130. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  131. fn_print_nl "* remotebuild: ${remotebuild}"
  132. fi
  133. fi
  134. }
  135. # The location where the builds are checked and downloaded.
  136. remotelocation="github.com/OpenRA/OpenRA"
  137. if [ "${firstcommandname}" == "INSTALL" ]; then
  138. fn_update_remotebuild
  139. fn_update_dl
  140. else
  141. fn_print_dots "Checking for update"
  142. fn_print_dots "Checking for update: ${remotelocation}"
  143. fn_script_log_info "Checking for update: ${remotelocation}"
  144. fn_update_localbuild
  145. fn_update_remotebuild
  146. fn_update_compare
  147. fi