core_dl.sh 28 KB

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