core_dl.sh 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. #!/bin/bash
  2. # LinuxGSM core_dl.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Deals with all downloads for LinuxGSM.
  7. # remote_fileurl: The URL of the file: http://example.com/dl/File.tar.bz2
  8. # local_filedir: location the file is to be saved: /home/server/lgsm/tmp
  9. # local_filename: name of file (this can be different from the url name): file.tar.bz2
  10. # chmodx: Optional, set to "chmodx" to make file executable using chmod +x
  11. # run: Optional, set run to execute the file after download
  12. # forcedl: Optional, force re-download of file even if exists
  13. # hash: Optional, set an hash sum and will compare it against the file.
  14. #
  15. # Downloads can be defined in code like so:
  16. # fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  17. # fn_fetch_file "http://example.com/file.tar.bz2" "http://example.com/file2.tar.bz2" "file.tar.bz2" "file2.tar.bz2" "/some/dir" "file.tar.bz2" "chmodx" "run" "forcedl" "10cd7353aa9d758a075c600a6dd193fd"
  18. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  19. fn_dl_steamcmd() {
  20. remotelocation="SteamCMD"
  21. fn_print_start_nl "${remotelocation}"
  22. fn_script_log_info "${commandaction} ${selfname}: ${remotelocation}"
  23. if [ -n "${branch}" ]; then
  24. echo -e "Branch: ${branch}"
  25. fn_script_log_info "Branch: ${branch}"
  26. fi
  27. if [ -n "${betapassword}" ]; then
  28. echo -e "Branch password: ${betapassword}"
  29. fn_script_log_info "Branch password: ${betapassword}"
  30. fi
  31. if [ -d "${steamcmddir}" ]; then
  32. cd "${steamcmddir}" || exit
  33. fi
  34. # Unbuffer will allow the output of steamcmd not buffer allowing a smooth output.
  35. # unbuffer us part of the expect package.
  36. if [ "$(command -v unbuffer 2> /dev/null)" ]; then
  37. unbuffer="unbuffer"
  38. fi
  39. # Validate will be added as a parameter if required.
  40. if [ "${commandname}" == "VALIDATE" ] || [ "${commandname}" == "INSTALL" ]; then
  41. validate="validate"
  42. fi
  43. # Wrap steamcmdcommand as a single-element array to avoid word-splitting
  44. # on paths/commands that should not be tokenised.
  45. steamcmdcommandarray=("${steamcmdcommand}")
  46. unbuffercommand=()
  47. if [ -n "${unbuffer}" ]; then
  48. unbuffercommand=("${unbuffer}")
  49. fi
  50. validateparam=()
  51. if [ -n "${validate}" ]; then
  52. validateparam=("${validate}")
  53. fi
  54. # To do error checking for SteamCMD the output of steamcmd will be saved to a log.
  55. steamcmdlog="${lgsmlogdir}/${selfname}-steamcmd.log"
  56. # clear previous steamcmd log
  57. if [ -f "${steamcmdlog}" ]; then
  58. rm -f "${steamcmdlog:?}"
  59. fi
  60. local invalidplatformworkaround=""
  61. counter=0
  62. while [ "${counter}" -eq 0 ] || [ "${exitcode}" -ne 0 ]; do
  63. counter=$((counter + 1))
  64. # Select SteamCMD parameters
  65. # If GoldSrc (appid 90) servers. GoldSrc (appid 90) require extra commands.
  66. if [ "${appid}" == "90" ]; then
  67. # If using a specific branch.
  68. if [ -n "${branch}" ] && [ -n "${betapassword}" ]; then
  69. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +force_install_dir "${serverfiles}" +login "${steamuser}" "${steampass}" +app_set_config 90 mod "${appidmod}" +app_update "${appid}" -beta "${branch}" -betapassword "${betapassword}" "${validateparam[@]}" +quit | uniq | tee -a "${lgsmlog}" "${steamcmdlog}"
  70. elif [ -n "${branch}" ]; then
  71. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +force_install_dir "${serverfiles}" +login "${steamuser}" "${steampass}" +app_set_config 90 mod "${appidmod}" +app_update "${appid}" -beta "${branch}" "${validateparam[@]}" +quit | uniq | tee -a "${lgsmlog}" "${steamcmdlog}"
  72. else
  73. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +force_install_dir "${serverfiles}" +login "${steamuser}" "${steampass}" +app_set_config 90 mod "${appidmod}" +app_update "${appid}" "${validateparam[@]}" +quit | uniq | tee -a "${lgsmlog}" "${steamcmdlog}"
  74. fi
  75. # Force Windows Platform type.
  76. elif [ "${steamcmdforcewindows}" == "yes" ]; then
  77. # If a base app is required, install it first.
  78. if [ -n "${baseappid}" ]; then
  79. if [ -z "${supportdir}" ]; then
  80. fn_print_failure_nl "${commandaction} ${selfname}: baseappid is set but supportdir is not defined"
  81. fn_script_log_fail "${commandaction} ${selfname}: baseappid is set but supportdir is not defined"
  82. core_exit.sh
  83. fi
  84. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +@sSteamCmdForcePlatformType windows +force_install_dir "${supportdir}" +login "${steamuser}" "${steampass}" +app_update "${baseappid}" "${validateparam[@]}" +quit | uniq | tee -a "${lgsmlog}" "${steamcmdlog}"
  85. exitcode="${PIPESTATUS[0]}"
  86. if [ "${exitcode}" -ne 0 ]; then
  87. continue
  88. fi
  89. fi
  90. if [ -n "${branch}" ] && [ -n "${betapassword}" ]; then
  91. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +@sSteamCmdForcePlatformType windows +force_install_dir "${serverfiles}" +login "${steamuser}" "${steampass}" +app_update "${appid}" -beta "${branch}" -betapassword "${betapassword}" "${validateparam[@]}" +quit | uniq | tee -a "${lgsmlog}" "${steamcmdlog}"
  92. elif [ -n "${branch}" ]; then
  93. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +@sSteamCmdForcePlatformType windows +force_install_dir "${serverfiles}" +login "${steamuser}" "${steampass}" +app_update "${appid}" -beta "${branch}" "${validateparam[@]}" +quit | uniq | tee -a "${lgsmlog}" "${steamcmdlog}"
  94. else
  95. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +@sSteamCmdForcePlatformType windows +force_install_dir "${serverfiles}" +login "${steamuser}" "${steampass}" +app_update "${appid}" "${validateparam[@]}" +quit | uniq | tee -a "${lgsmlog}" "${steamcmdlog}"
  96. fi
  97. # All other servers.
  98. else
  99. # If a base app is required, install it first.
  100. if [ -n "${baseappid}" ]; then
  101. if [ -z "${supportdir}" ]; then
  102. fn_print_failure_nl "${commandaction} ${selfname}: baseappid is set but supportdir is not defined"
  103. fn_script_log_fail "${commandaction} ${selfname}: baseappid is set but supportdir is not defined"
  104. core_exit.sh
  105. fi
  106. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +force_install_dir "${supportdir}" +login "${steamuser}" "${steampass}" +app_update "${baseappid}" "${validateparam[@]}" +quit | uniq | tee -a "${lgsmlog}" "${steamcmdlog}"
  107. exitcode="${PIPESTATUS[0]}"
  108. if [ "${exitcode}" -ne 0 ]; then
  109. continue
  110. fi
  111. fi
  112. if [ -n "${branch}" ] && [ -n "${betapassword}" ]; then
  113. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +force_install_dir "${serverfiles}" +login "${steamuser}" "${steampass}" +app_update "${appid}" -beta "${branch}" -betapassword "${betapassword}" "${validateparam[@]}" +quit | uniq | tee -a "${lgsmlog}" "${steamcmdlog}"
  114. elif [ -n "${branch}" ]; then
  115. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +force_install_dir "${serverfiles}" +login "${steamuser}" "${steampass}" +app_update "${appid}" -beta "${branch}" "${validateparam[@]}" +quit | uniq | tee -a "${lgsmlog}" "${steamcmdlog}"
  116. else
  117. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +force_install_dir "${serverfiles}" +login "${steamuser}" "${steampass}" +app_update "${appid}" "${validateparam[@]}" +quit | uniq | tee -a "${lgsmlog}" "${steamcmdlog}"
  118. fi
  119. fi
  120. # Error checking for SteamCMD. Some errors will loop to try again and some will just exit.
  121. # Check also if we have more errors than retries to be sure that we do not loop to many times and error out.
  122. exitcode=$?
  123. if [ -n "$(grep -i "Error!" "${steamcmdlog}" | tail -1)" ] && [ "$(grep -ic "Error!" "${steamcmdlog}")" -ge "${counter}" ]; then
  124. # Not enough space.
  125. if [ -n "$(grep "0x202" "${steamcmdlog}" | tail -1)" ]; then
  126. fn_print_failure_nl "${commandaction} ${selfname}: ${remotelocation}: Not enough disk space to download server files"
  127. fn_script_log_fail "${commandaction} ${selfname}: ${remotelocation}: Not enough disk space to download server files"
  128. core_exit.sh
  129. # Not enough space.
  130. elif [ -n "$(grep "0x212" "${steamcmdlog}" | tail -1)" ]; then
  131. fn_print_failure_nl "${commandaction} ${selfname}: ${remotelocation}: Not enough disk space to download server files"
  132. fn_script_log_fail "${commandaction} ${selfname}: ${remotelocation}: Not enough disk space to download server files"
  133. core_exit.sh
  134. # Invalid platform (SteamCMD bug, e.g. Left 4 Dead 2): retry once after a Windows platform download.
  135. elif [ -n "$(grep -i "Invalid platform" "${steamcmdlog}" | tail -1)" ] && [ "${steamcmdforcewindows}" != "yes" ] && [ -z "${invalidplatformworkaround}" ]; then
  136. invalidplatformworkaround="1"
  137. fn_print_error2_nl "${commandaction} ${selfname}: ${remotelocation}: Invalid platform - applying SteamCMD Windows platform workaround"
  138. fn_script_log_error "${commandaction} ${selfname}: ${remotelocation}: Invalid platform - applying SteamCMD Windows platform workaround"
  139. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +@sSteamCmdForcePlatformType windows +force_install_dir "${serverfiles}" +login "${steamuser}" "${steampass}" +app_update "${appid}" +quit | uniq | tee -a "${lgsmlog}" "${steamcmdlog}"
  140. validateparam=("validate")
  141. rm -f "${steamcmdlog:?}"
  142. counter=0
  143. # Invalid platform for app/update request (after the workaround, or when forcing Windows).
  144. elif [ -n "$(grep -i "Invalid platform" "${steamcmdlog}" | tail -1)" ]; then
  145. fn_print_failure_nl "${commandaction} ${selfname}: ${remotelocation}: Invalid platform for AppID ${appid}"
  146. fn_print_nl "Check steamcmdforcewindows setting and system architecture (x86_64 required for most servers)"
  147. fn_script_log_fail "${commandaction} ${selfname}: ${remotelocation}: Invalid platform for AppID ${appid}"
  148. core_exit.sh
  149. # Need tp purchase game.
  150. elif [ -n "$(grep "No subscription" "${steamcmdlog}" | tail -1)" ]; then
  151. fn_print_failure_nl "${commandaction} ${selfname}: ${remotelocation}: Steam account does not have a license for the required game"
  152. fn_script_log_fail "${commandaction} ${selfname}: ${remotelocation}: Steam account does not have a license for the required game"
  153. core_exit.sh
  154. # Two-factor authentication failure
  155. elif [ -n "$(grep "Two-factor code mismatch" "${steamcmdlog}" | tail -1)" ]; then
  156. fn_print_failure_nl "${commandaction} ${selfname}: ${remotelocation}: Two-factor authentication failure"
  157. fn_script_log_fail "${commandaction} ${selfname}: ${remotelocation}: Two-factor authentication failure"
  158. core_exit.sh
  159. # Incorrect Branch password
  160. elif [ -n "$(grep "Password check for AppId" "${steamcmdlog}" | tail -1)" ]; then
  161. fn_print_failure_nl "${commandaction} ${selfname}: ${remotelocation}: betapassword is incorrect"
  162. fn_script_log_fail "${commandaction} ${selfname}: ${remotelocation}: betapassword is incorrect"
  163. core_exit.sh
  164. # Update did not finish.
  165. elif [ -n "$(grep "0x402" "${steamcmdlog}" | tail -1)" ] || [ -n "$(grep "0x602" "${steamcmdlog}" | tail -1)" ]; then
  166. fn_print_error2_nl "${commandaction} ${selfname}: ${remotelocation}: Update required but not completed - check network"
  167. fn_script_log_error "${commandaction} ${selfname}: ${remotelocation}: Update required but not completed - check network"
  168. # Disk write failure.
  169. elif [ -n "$(grep "0x606" "${steamcmdlog}" | tail -1)" ] || [ -n "$(grep "0x602" "${steamcmdlog}" | tail -1)" ]; then
  170. fn_print_error2_nl "${commandaction} ${selfname}: ${remotelocation}: Disk write failure"
  171. fn_script_log_error "${commandaction} ${selfname}: ${remotelocation}: Disk write failure"
  172. # Missing update files.
  173. elif [ -n "$(grep "0x626" "${steamcmdlog}" | tail -1)" ] || [ -n "$(grep "0x626" "${steamcmdlog}" | tail -1)" ]; then
  174. fn_print_error2_nl "${commandaction} ${selfname}: ${remotelocation}: Missing update files"
  175. fn_script_log_error "${commandaction} ${selfname}: ${remotelocation}: Missing update files"
  176. elif [ -n "$(grep "0x6A6" "${steamcmdlog}" | tail -1)" ]; then
  177. fn_print_error2_nl "${commandaction} ${selfname}: ${remotelocation}: Corrupt update files"
  178. fn_script_log_error "${commandaction} ${selfname}: ${remotelocation}: Corrupt update files"
  179. # Steam app metadata unavailable.
  180. elif [ -n "$(grep -i "Missing configuration" "${steamcmdlog}" | tail -1)" ]; then
  181. fn_print_error2_nl "${commandaction} ${selfname}: ${remotelocation}: Missing configuration - Steam app metadata unavailable for AppID ${appid}"
  182. fn_print_nl "Please provide content log to LinuxGSM developers https://linuxgsm.com/steamcmd-error"
  183. fn_script_log_error "${commandaction} ${selfname}: ${remotelocation}: Missing configuration - Steam app metadata unavailable for AppID ${appid}"
  184. else
  185. fn_print_error2_nl "${commandaction} ${selfname}: ${remotelocation}: Unknown error occurred"
  186. fn_print_nl "Please provide content log to LinuxGSM developers https://linuxgsm.com/steamcmd-error"
  187. fn_script_log_error "${commandaction} ${selfname}: ${remotelocation}: Unknown error occurred"
  188. fi
  189. elif [ "${exitcode}" -ne 0 ]; then
  190. fn_print_error2_nl "${commandaction} ${selfname}: ${remotelocation}: Exit code: ${exitcode}"
  191. fn_script_log_error "${commandaction} ${selfname}: ${remotelocation}: Exit code: ${exitcode}"
  192. else
  193. fn_print_success_nl "${commandaction} ${selfname}: ${remotelocation}"
  194. fn_script_log_pass "${commandaction} ${selfname}: ${remotelocation}"
  195. fi
  196. if [ "${counter}" -gt "10" ]; then
  197. fn_print_failure_nl "${commandaction} ${selfname}: ${remotelocation}: Did not complete the download, too many retrys"
  198. fn_script_log_fail "${commandaction} ${selfname}: ${remotelocation}: Did not complete the download, too many retrys"
  199. core_exit.sh
  200. fi
  201. done
  202. }
  203. # Emptys contents of the LinuxGSM tmpdir.
  204. fn_clear_tmp() {
  205. echo -en "clearing tmp directory [ ${italic}${tmpdir}${default} ]"
  206. if [ -d "${tmpdir}" ]; then
  207. rm -rf "${tmpdir:?}/"*
  208. exitcode=$?
  209. if [ "${exitcode}" -ne 0 ]; then
  210. fn_print_error_eol_nl
  211. fn_script_log_error "clearing tmp directory ${tmpdir}"
  212. else
  213. fn_print_ok_eol_nl
  214. fn_script_log_pass "clearing tmp directory ${tmpdir}"
  215. fi
  216. fi
  217. }
  218. fn_dl_hash() {
  219. # Runs Hash Check if available.
  220. if [ "${hash}" != "0" ] && [ "${hash}" != "nohash" ] && [ "${hash}" != "nomd5" ]; then
  221. # MD5
  222. if [ "${#hash}" == "32" ]; then
  223. hashbin="md5sum"
  224. hashtype="MD5"
  225. # SHA1
  226. elif [ "${#hash}" == "40" ]; then
  227. hashbin="sha1sum"
  228. hashtype="SHA1"
  229. # SHA256
  230. elif [ "${#hash}" == "64" ]; then
  231. hashbin="sha256sum"
  232. hashtype="SHA256"
  233. # SHA512
  234. elif [ "${#hash}" == "128" ]; then
  235. hashbin="sha512sum"
  236. hashtype="SHA512"
  237. else
  238. fn_script_log_error "hash length not known for hash type"
  239. fn_print_error_nl "hash length not known for hash type"
  240. core_exit.sh
  241. fi
  242. echo -en "verifying ${local_filename} with ${hashtype}"
  243. fn_sleep_time
  244. hashsumcmd=$(${hashbin} "${local_filedir}/${local_filename}" | awk '{print $1}')
  245. if [ "${hashsumcmd}" != "${hash}" ]; then
  246. fn_print_fail_eol_nl
  247. echo -e "${local_filename} returned ${hashtype} checksum: ${hashsumcmd}"
  248. echo -e "expected ${hashtype} checksum: ${hash}"
  249. fn_script_log_fail "Verifying ${local_filename} with ${hashtype}"
  250. fn_script_log_info "${local_filename} returned ${hashtype} checksum: ${hashsumcmd}"
  251. fn_script_log_info "Expected ${hashtype} checksum: ${hash}"
  252. core_exit.sh
  253. else
  254. fn_print_ok_eol_nl
  255. fn_script_log_pass "Verifying ${local_filename} with ${hashtype}"
  256. fn_script_log_info "${local_filename} returned ${hashtype} checksum: ${hashsumcmd}"
  257. fn_script_log_info "Expected ${hashtype} checksum: ${hash}"
  258. fi
  259. fi
  260. }
  261. # Extracts bzip2, gzip or zip files.
  262. # Extracts can be defined in code like so:
  263. # fn_dl_extract "${local_filedir}" "${local_filename}" "${extractdest}" "${extractsrc}"
  264. # fn_dl_extract "/home/gameserver/lgsm/tmp" "file.tar.bz2" "/home/gamserver/serverfiles"
  265. fn_dl_extract() {
  266. local_filedir="${1}"
  267. local_filename="${2}"
  268. extractdest="${3}"
  269. extractsrc="${4}"
  270. # Extracts archives.
  271. echo -en "extracting ${local_filename}..."
  272. if [ ! -d "${extractdest}" ]; then
  273. mkdir "${extractdest}"
  274. fi
  275. if [ ! -f "${local_filedir}/${local_filename}" ]; then
  276. fn_print_fail_eol_nl
  277. echo -en "file ${local_filedir}/${local_filename} not found"
  278. fn_script_log_fail "Extracting ${local_filename}"
  279. fn_script_log_fail "File ${local_filedir}/${local_filename} not found"
  280. core_exit.sh
  281. fi
  282. mime=$(file -b --mime-type "${local_filedir}/${local_filename}")
  283. if [ "${mime}" == "application/gzip" ] || [ "${mime}" == "application/x-gzip" ]; then
  284. if [ -n "${extractsrc}" ]; then
  285. extractcmd=$(tar -zxf "${local_filedir}/${local_filename}" -C "${extractdest}" --strip-components=1 "${extractsrc}")
  286. else
  287. extractcmd=$(tar -zxf "${local_filedir}/${local_filename}" -C "${extractdest}")
  288. fi
  289. elif [ "${mime}" == "application/x-bzip2" ]; then
  290. if [ -n "${extractsrc}" ]; then
  291. extractcmd=$(tar -jxf "${local_filedir}/${local_filename}" -C "${extractdest}" --strip-components=1 "${extractsrc}")
  292. else
  293. extractcmd=$(tar -jxf "${local_filedir}/${local_filename}" -C "${extractdest}")
  294. fi
  295. elif [ "${mime}" == "application/x-xz" ]; then
  296. if [ -n "${extractsrc}" ]; then
  297. extractcmd=$(tar -Jxf "${local_filedir}/${local_filename}" -C "${extractdest}" --strip-components=1 "${extractsrc}")
  298. else
  299. extractcmd=$(tar -Jxf "${local_filedir}/${local_filename}" -C "${extractdest}")
  300. fi
  301. elif [ "${mime}" == "application/zip" ]; then
  302. if [ -n "${extractsrc}" ]; then
  303. temp_extractdir="${tmpdir}/${extractsrc}"
  304. extractcmd=$(unzip -qo "${local_filedir}/${local_filename}" "${extractsrc}/*" -d "${temp_extractdir}")
  305. cp -a "${temp_extractdir}/${extractsrc}/." "${extractdest}/"
  306. rm -rf "${temp_extractdir}"
  307. else
  308. extractcmd=$(unzip -qo -d "${extractdest}" "${local_filedir}/${local_filename}")
  309. fi
  310. fi
  311. exitcode=$?
  312. if [ "${exitcode}" -ne 0 ]; then
  313. fn_print_fail_eol_nl
  314. fn_script_log_fail "Extracting ${local_filename}"
  315. if [ -f "${lgsmlog}" ]; then
  316. echo -e "${extractcmd}" >> "${lgsmlog}"
  317. fi
  318. echo -e "${extractcmd}"
  319. core_exit.sh
  320. else
  321. fn_print_ok_eol_nl
  322. fn_script_log_pass "Extracting ${local_filename}"
  323. fi
  324. }
  325. # Trap to remove file download if canceled before completed.
  326. fn_fetch_trap() {
  327. echo -e ""
  328. echo -en "downloading ${local_filename}"
  329. fn_print_canceled_eol_nl
  330. fn_script_log_info "Downloading ${local_filename}...CANCELED"
  331. rm -f "${local_filedir:?}/${local_filename}"
  332. echo -en "downloading ${local_filename}"
  333. fn_print_removed_eol_nl
  334. fn_script_log_info "Downloading ${local_filename}...REMOVED"
  335. core_exit.sh
  336. }
  337. # Will check a file exists and download it. Will not exit if fails to download.
  338. fn_check_file() {
  339. remote_fileurl="${1}"
  340. remote_fileurl_backup="${2}"
  341. remote_fileurl_name="${3}"
  342. remote_fileurl_backup_name="${4}"
  343. remote_filename="${5}"
  344. # If backup fileurl exists include it.
  345. if [ -n "${remote_fileurl_backup}" ]; then
  346. # counter set to 0 to allow second try
  347. counter=0
  348. remote_fileurls_array=(remote_fileurl remote_fileurl_backup)
  349. else
  350. # counter set to 1 to not allow second try
  351. counter=1
  352. remote_fileurls_array=(remote_fileurl)
  353. fi
  354. for remote_fileurl_array in "${remote_fileurls_array[@]}"; do
  355. if [ "${remote_fileurl_array}" == "remote_fileurl" ]; then
  356. fileurl="${remote_fileurl}"
  357. fileurl_name="${remote_fileurl_name}"
  358. elif [ "${remote_fileurl_array}" == "remote_fileurl_backup" ]; then
  359. fileurl="${remote_fileurl_backup}"
  360. fileurl_name="${remote_fileurl_backup_name}"
  361. fi
  362. counter=$((counter + 1))
  363. echo -e "checking ${fileurl_name} ${remote_filename}\c"
  364. curlcmd=$(curl --output /dev/null --silent --head --fail "${fileurl}" 2>&1)
  365. exitcode=$?
  366. # On first try will error. On second try will fail.
  367. if [ "${exitcode}" -ne 0 ]; then
  368. if [ ${counter} -ge 2 ]; then
  369. fn_print_fail_eol_nl
  370. if [ -f "${lgsmlog}" ]; then
  371. fn_script_log_fail "Checking ${remote_filename}"
  372. fn_script_log_fail "${fileurl}"
  373. checkflag=1
  374. fi
  375. else
  376. fn_print_error_eol_nl
  377. if [ -f "${lgsmlog}" ]; then
  378. fn_script_log_error "Checking ${remote_filename}"
  379. fn_script_log_error "${fileurl}"
  380. checkflag=2
  381. fi
  382. fi
  383. else
  384. fn_print_ok_eol
  385. echo -en "\033[2K\\r"
  386. if [ -f "${lgsmlog}" ]; then
  387. fn_script_log_pass "Checking ${remote_filename}"
  388. checkflag=0
  389. fi
  390. break
  391. fi
  392. done
  393. if [ -f "${local_filedir}/${local_filename}" ]; then
  394. fn_dl_hash
  395. # Execute file if run is set.
  396. if [ "${run}" == "run" ]; then
  397. # shellcheck source=/dev/null
  398. source "${local_filedir}/${local_filename}"
  399. fi
  400. fi
  401. }
  402. fn_fetch_file() {
  403. remote_fileurl="${1}"
  404. remote_fileurl_backup="${2}"
  405. remote_fileurl_name="${3}"
  406. remote_fileurl_backup_name="${4}"
  407. local_filedir="${5}"
  408. local_filename="${6}"
  409. chmodx="${7:-0}"
  410. run="${8:-0}"
  411. forcedl="${9:-0}"
  412. hash="${10:-0}"
  413. # Download file if missing or download forced.
  414. if [ ! -f "${local_filedir}/${local_filename}" ] || [ "${forcedl}" == "forcedl" ]; then
  415. # If backup fileurl exists include it.
  416. if [ -n "${remote_fileurl_backup}" ]; then
  417. # counter set to 0 to allow second try
  418. counter=0
  419. remote_fileurls_array=(remote_fileurl remote_fileurl_backup)
  420. else
  421. # counter set to 1 to not allow second try
  422. counter=1
  423. remote_fileurls_array=(remote_fileurl)
  424. fi
  425. for remote_fileurl_array in "${remote_fileurls_array[@]}"; do
  426. if [ "${remote_fileurl_array}" == "remote_fileurl" ]; then
  427. fileurl="${remote_fileurl}"
  428. fileurl_name="${remote_fileurl_name}"
  429. elif [ "${remote_fileurl_array}" == "remote_fileurl_backup" ]; then
  430. fileurl="${remote_fileurl_backup}"
  431. fileurl_name="${remote_fileurl_backup_name}"
  432. fi
  433. counter=$((counter + 1))
  434. if [ ! -d "${local_filedir}" ]; then
  435. mkdir -p "${local_filedir}"
  436. fi
  437. # Trap will remove part downloaded files if canceled.
  438. trap fn_fetch_trap INT
  439. curlcmd=(curl --connect-timeout 3 --fail -L -o "${local_filedir}/${local_filename}" --retry 2 -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.${randomint}.212 Safari/537.36")
  440. # if is large file show progress, else be silent
  441. local exitcode=""
  442. large_files=("bz2" "gz" "zip" "jar" "xz")
  443. if grep -qE "(^|\s)${local_filename##*.}(\s|$)" <<< "${large_files[@]}"; then
  444. echo -e "downloading file [ ${italic}${local_filename}${default} ]"
  445. fn_sleep_time
  446. "${curlcmd[@]}" --progress-bar "${fileurl}" 2>&1
  447. exitcode=$?
  448. echo -en "downloading file [ ${italic}${local_filename}${default} ]"
  449. else
  450. echo -en "fetching ${fileurl_name} [ ${italic}${local_filename}${default} ]\c"
  451. "${curlcmd[@]}" --silent --show-error "${fileurl}" 2>&1
  452. exitcode=$?
  453. fi
  454. # Download will fail if downloads a html file.
  455. if [ -f "${local_filedir}/${local_filename}" ]; then
  456. if head -n 1 "${local_filedir}/${local_filename}" | grep -q "DOCTYPE"; then
  457. rm "${local_filedir:?}/${local_filename:?}"
  458. local exitcode=2
  459. fi
  460. fi
  461. # On first try will error. On second try will fail.
  462. if [ "${exitcode}" -ne 0 ]; then
  463. if [ ${counter} -ge 2 ]; then
  464. fn_print_fail_eol_nl
  465. if [ -f "${lgsmlog}" ]; then
  466. fn_script_log_fail "Downloading ${local_filename}..."
  467. fn_script_log_fail "${fileurl}"
  468. fi
  469. core_exit.sh
  470. else
  471. fn_print_error_eol_nl
  472. if [ -f "${lgsmlog}" ]; then
  473. fn_script_log_error "Downloading ${local_filename}..."
  474. fn_script_log_error "${fileurl}"
  475. fi
  476. fi
  477. else
  478. fn_print_ok_eol_nl
  479. if [ -f "${lgsmlog}" ]; then
  480. fn_script_log_pass "Downloading ${local_filename}..."
  481. fi
  482. # Make file executable if chmodx is set.
  483. if [ "${chmodx}" == "chmodx" ]; then
  484. chmod +x "${local_filedir}/${local_filename}"
  485. fi
  486. # Remove trap.
  487. trap - INT
  488. break
  489. fi
  490. done
  491. fi
  492. if [ -f "${local_filedir}/${local_filename}" ]; then
  493. fn_dl_hash
  494. # Execute file if run is set.
  495. if [ "${run}" == "run" ]; then
  496. # shellcheck source=/dev/null
  497. source "${local_filedir}/${local_filename}"
  498. fi
  499. fi
  500. }
  501. # GitHub file download modules.
  502. # Used to simplify downloading specific files from GitHub.
  503. # github_file_url_dir: the directory of the file in the GitHub: lgsm/modules
  504. # github_file_url_name: the filename of the file to download from GitHub: core_messages.sh
  505. # github_file_url_dir: the directory of the file in the GitHub: lgsm/modules
  506. # github_file_url_name: the filename of the file to download from GitHub: core_messages.sh
  507. # githuburl: the full GitHub url
  508. # remote_fileurl: The URL of the file: http://example.com/dl/File.tar.bz2
  509. # local_filedir: location the file is to be saved: /home/server/lgsm/tmp
  510. # local_filename: name of file (this can be different from the url name): file.tar.bz2
  511. # chmodx: Optional, set to "chmodx" to make file executable using chmod +x
  512. # run: Optional, set run to execute the file after download
  513. # forcedl: Optional, force re-download of file even if exists
  514. # hash: Optional, set an hash sum and will compare it against the file.
  515. # Fetches files from the Git repo.
  516. fn_fetch_file_github() {
  517. github_file_url_dir="${1}"
  518. github_file_url_name="${2}"
  519. # If master branch will currently running LinuxGSM version to prevent "version mixing". This is ignored if a fork.
  520. if [ "${githubbranch}" == "master" ] && [ "${githubuser}" == "GameServerManagers" ] && [ "${commandname}" != "UPDATE-LGSM" ]; then
  521. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  522. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  523. else
  524. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  525. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  526. fi
  527. remote_fileurl_name="GitHub"
  528. remote_fileurl_backup_name="Bitbucket"
  529. local_filedir="${3}"
  530. local_filename="${github_file_url_name}"
  531. chmodx="${4:-0}"
  532. run="${5:-0}"
  533. forcedl="${6:-0}"
  534. hash="${7:-0}"
  535. # Passes vars to the file download module.
  536. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  537. }
  538. fn_check_file_github() {
  539. github_file_url_dir="${1}"
  540. github_file_url_name="${2}"
  541. if [ "${githubbranch}" == "master" ] && [ "${githubuser}" == "GameServerManagers" ] && [ "${commandname}" != "UPDATE-LGSM" ]; then
  542. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  543. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  544. else
  545. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  546. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  547. fi
  548. remote_fileurl_name="GitHub"
  549. remote_fileurl_backup_name="Bitbucket"
  550. fn_check_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${github_file_url_name}"
  551. }
  552. # Fetches config files from the Git repo.
  553. fn_fetch_config() {
  554. github_file_url_dir="${1}"
  555. github_file_url_name="${2}"
  556. # If master branch will currently running LinuxGSM version to prevent "version mixing". This is ignored if a fork.
  557. if [ "${githubbranch}" == "master" ] && [ "${githubuser}" == "GameServerManagers" ] && [ "${commandname}" != "UPDATE-LGSM" ]; then
  558. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  559. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  560. else
  561. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  562. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  563. fi
  564. remote_fileurl_name="GitHub"
  565. remote_fileurl_backup_name="Bitbucket"
  566. local_filedir="${3}"
  567. local_filename="${4}"
  568. chmodx="nochmodx"
  569. run="norun"
  570. forcedl="noforce"
  571. hash="nohash"
  572. # Passes vars to the file download module.
  573. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  574. }
  575. # Fetches modules from the Git repo during first download.
  576. fn_fetch_module() {
  577. github_file_url_dir="lgsm/modules"
  578. github_file_url_name="${modulefile}"
  579. # If master branch will currently running LinuxGSM version to prevent "version mixing". This is ignored if a fork.
  580. if [ "${githubbranch}" == "master" ] && [ "${githubuser}" == "GameServerManagers" ] && [ "${commandname}" != "UPDATE-LGSM" ]; then
  581. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  582. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  583. else
  584. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  585. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  586. fi
  587. remote_fileurl_name="GitHub"
  588. remote_fileurl_backup_name="Bitbucket"
  589. local_filedir="${modulesdir}"
  590. local_filename="${github_file_url_name}"
  591. chmodx="chmodx"
  592. run="run"
  593. forcedl="noforce"
  594. hash="nohash"
  595. # Passes vars to the file download module.
  596. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  597. }
  598. # Fetches modules from the Git repo during update-lgsm.
  599. fn_update_module() {
  600. github_file_url_dir="lgsm/modules"
  601. github_file_url_name="${modulefile}"
  602. # If master branch will currently running LinuxGSM version to prevent "version mixing". This is ignored if a fork.
  603. if [ "${githubbranch}" == "master" ] && [ "${githubuser}" == "GameServerManagers" ] && [ "${commandname}" != "UPDATE-LGSM" ]; then
  604. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  605. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  606. else
  607. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  608. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  609. fi
  610. remote_fileurl_name="GitHub"
  611. remote_fileurl_backup_name="Bitbucket"
  612. local_filedir="${modulesdir}"
  613. local_filename="${github_file_url_name}"
  614. chmodx="chmodx"
  615. run="norun"
  616. forcedl="noforce"
  617. hash="nohash"
  618. # Passes vars to the file download module.
  619. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  620. }
  621. # Function to download latest github release.
  622. # $1 GitHub user / organisation.
  623. # $2 Repo name.
  624. # $3 Destination for download.
  625. # $4 Search string in releases (needed if there are more files that can be downloaded from the release pages).
  626. fn_dl_latest_release_github() {
  627. local githubreleaseuser="${1}"
  628. local githubreleaserepo="${2}"
  629. local githubreleasedownloadpath="${3}"
  630. local githubreleasesearch="${4}"
  631. local githublatestreleaseurl="https://api.github.com/repos/${githubreleaseuser}/${githubreleaserepo}/releases/latest"
  632. # Get last github release.
  633. # If no search for the release filename is set, just get the first file from the latest release.
  634. if [ -z "${githubreleasesearch}" ]; then
  635. githubreleaseassets=$(curl -s "${githublatestreleaseurl}" | jq '[ .assets[] ]')
  636. else
  637. githubreleaseassets=$(curl -s "${githublatestreleaseurl}" | jq "[ .assets[]|select(.browser_download_url | contains(\"${githubreleasesearch}\")) ]")
  638. fi
  639. # Check how many releases we got from the api and exit if we have more then one.
  640. if [ "$(echo -e "${githubreleaseassets}" | jq '. | length')" -gt 1 ]; then
  641. fn_print_fatal_nl "Found more than one release to download - Please report this to the LinuxGSM issue tracker"
  642. fn_script_log_fail "Found more than one release to download - Please report this to the LinuxGSM issue tracker"
  643. else
  644. # Set variables for download via fn_fetch_file.
  645. githubreleasefilename=$(echo -e "${githubreleaseassets}" | jq -r '.[]name')
  646. githubreleasedownloadlink=$(echo -e "${githubreleaseassets}" | jq -r '.[]browser_download_url')
  647. # Error if no version is there.
  648. if [ -z "${githubreleasefilename}" ]; then
  649. fn_print_fail_nl "Cannot get version from GitHub API for ${githubreleaseuser}/${githubreleaserepo}"
  650. fn_script_log_fail "Cannot get version from GitHub API for ${githubreleaseuser}/${githubreleaserepo}"
  651. else
  652. # Fetch file from the remote location from the existing module to the ${tmpdir} for now.
  653. fn_fetch_file "${githubreleasedownloadlink}" "" "${githubreleasefilename}" "" "${githubreleasedownloadpath}" "${githubreleasefilename}"
  654. fi
  655. fi
  656. }