core_dl.sh 29 KB

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