core_dl.sh 32 KB

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