core_dl.sh 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. #!/bin/bash
  2. # LinuxGSM core_dl.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: http://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. extractcmd=$(unzip -qoj -d "${extractdest}" "${local_filedir}/${local_filename}" "${extractsrc}"/*)
  242. else
  243. extractcmd=$(unzip -qo -d "${extractdest}" "${local_filedir}/${local_filename}")
  244. fi
  245. fi
  246. local exitcode=$?
  247. if [ "${exitcode}" != 0 ]; then
  248. fn_print_fail_eol_nl
  249. fn_script_log_fail "Extracting ${local_filename}"
  250. if [ -f "${lgsmlog}" ]; then
  251. echo -e "${extractcmd}" >> "${lgsmlog}"
  252. fi
  253. echo -e "${extractcmd}"
  254. core_exit.sh
  255. else
  256. fn_print_ok_eol_nl
  257. fn_script_log_pass "Extracting ${local_filename}"
  258. fi
  259. }
  260. # Trap to remove file download if canceled before completed.
  261. fn_fetch_trap() {
  262. echo -e ""
  263. echo -en "downloading ${local_filename}..."
  264. fn_print_canceled_eol_nl
  265. fn_script_log_info "Downloading ${local_filename}...CANCELED"
  266. rm -f "${local_filedir:?}/${local_filename}"
  267. echo -en "downloading ${local_filename}..."
  268. fn_print_removed_eol_nl
  269. fn_script_log_info "Downloading ${local_filename}...REMOVED"
  270. core_exit.sh
  271. }
  272. # Will check a file exists and download it. Will not exit if fails to download.
  273. fn_check_file() {
  274. remote_fileurl="${1}"
  275. remote_fileurl_backup="${2}"
  276. remote_fileurl_name="${3}"
  277. remote_fileurl_backup_name="${4}"
  278. remote_filename="${5}"
  279. # If backup fileurl exists include it.
  280. if [ -n "${remote_fileurl_backup}" ]; then
  281. # counter set to 0 to allow second try
  282. counter=0
  283. remote_fileurls_array=(remote_fileurl remote_fileurl_backup)
  284. else
  285. # counter set to 1 to not allow second try
  286. counter=1
  287. remote_fileurls_array=(remote_fileurl)
  288. fi
  289. for remote_fileurl_array in "${remote_fileurls_array[@]}"; do
  290. if [ "${remote_fileurl_array}" == "remote_fileurl" ]; then
  291. fileurl="${remote_fileurl}"
  292. fileurl_name="${remote_fileurl_name}"
  293. elif [ "${remote_fileurl_array}" == "remote_fileurl_backup" ]; then
  294. fileurl="${remote_fileurl_backup}"
  295. fileurl_name="${remote_fileurl_backup_name}"
  296. fi
  297. counter=$((counter + 1))
  298. echo -en "checking ${fileurl_name} ${remote_filename}...\c"
  299. curlcmd=$(curl --output /dev/null --silent --head --fail "${fileurl}" 2>&1)
  300. local exitcode=$?
  301. # On first try will error. On second try will fail.
  302. if [ "${exitcode}" != 0 ]; then
  303. if [ ${counter} -ge 2 ]; then
  304. fn_print_fail_eol_nl
  305. if [ -f "${lgsmlog}" ]; then
  306. fn_script_log_fail "Checking ${remote_filename}"
  307. fn_script_log_fail "${fileurl}"
  308. checkflag=1
  309. fi
  310. else
  311. fn_print_error_eol_nl
  312. if [ -f "${lgsmlog}" ]; then
  313. fn_script_log_error "Checking ${remote_filename}"
  314. fn_script_log_error "${fileurl}"
  315. checkflag=2
  316. fi
  317. fi
  318. else
  319. fn_print_ok_eol
  320. echo -en "\033[2K\\r"
  321. if [ -f "${lgsmlog}" ]; then
  322. fn_script_log_pass "Checking ${remote_filename}"
  323. checkflag=0
  324. fi
  325. break
  326. fi
  327. done
  328. if [ -f "${local_filedir}/${local_filename}" ]; then
  329. fn_dl_hash
  330. # Execute file if run is set.
  331. if [ "${run}" == "run" ]; then
  332. # shellcheck source=/dev/null
  333. source "${local_filedir}/${local_filename}"
  334. fi
  335. fi
  336. }
  337. fn_fetch_file() {
  338. remote_fileurl="${1}"
  339. remote_fileurl_backup="${2}"
  340. remote_fileurl_name="${3}"
  341. remote_fileurl_backup_name="${4}"
  342. local_filedir="${5}"
  343. local_filename="${6}"
  344. chmodx="${7:-0}"
  345. run="${8:-0}"
  346. forcedl="${9:-0}"
  347. hash="${10:-0}"
  348. # Download file if missing or download forced.
  349. if [ ! -f "${local_filedir}/${local_filename}" ] || [ "${forcedl}" == "forcedl" ]; then
  350. # If backup fileurl exists include it.
  351. if [ -n "${remote_fileurl_backup}" ]; then
  352. # counter set to 0 to allow second try
  353. counter=0
  354. remote_fileurls_array=(remote_fileurl remote_fileurl_backup)
  355. else
  356. # counter set to 1 to not allow second try
  357. counter=1
  358. remote_fileurls_array=(remote_fileurl)
  359. fi
  360. for remote_fileurl_array in "${remote_fileurls_array[@]}"; do
  361. if [ "${remote_fileurl_array}" == "remote_fileurl" ]; then
  362. fileurl="${remote_fileurl}"
  363. fileurl_name="${remote_fileurl_name}"
  364. elif [ "${remote_fileurl_array}" == "remote_fileurl_backup" ]; then
  365. fileurl="${remote_fileurl_backup}"
  366. fileurl_name="${remote_fileurl_backup_name}"
  367. fi
  368. counter=$((counter + 1))
  369. if [ ! -d "${local_filedir}" ]; then
  370. mkdir -p "${local_filedir}"
  371. fi
  372. # Trap will remove part downloaded files if canceled.
  373. trap fn_fetch_trap INT
  374. curlcmd=(curl --connect-timeout 10 --fail -L -o "${local_filedir}/${local_filename}" --retry 2)
  375. # if is large file show progress, else be silent
  376. local exitcode=""
  377. large_files=("bz2" "gz" "zip" "jar" "xz")
  378. if grep -qE "(^|\s)${local_filename##*.}(\s|$)" <<< "${large_files[@]}"; then
  379. echo -en "downloading ${local_filename}..."
  380. fn_sleep_time
  381. echo -en "\033[1K"
  382. "${curlcmd[@]}" --progress-bar "${fileurl}" 2>&1
  383. exitcode="$?"
  384. else
  385. echo -en "fetching ${fileurl_name} ${local_filename}...\c"
  386. "${curlcmd[@]}" --silent --show-error "${fileurl}" 2>&1
  387. exitcode="$?"
  388. fi
  389. # Download will fail if downloads a html file.
  390. if [ -f "${local_filedir}/${local_filename}" ]; then
  391. if head -n 1 "${local_filedir}/${local_filename}" | grep -q "DOCTYPE"; then
  392. rm "${local_filedir:?}/${local_filename:?}"
  393. local exitcode=2
  394. fi
  395. fi
  396. # On first try will error. On second try will fail.
  397. if [ "${exitcode}" != 0 ]; then
  398. if [ ${counter} -ge 2 ]; then
  399. fn_print_fail_eol_nl
  400. if [ -f "${lgsmlog}" ]; then
  401. fn_script_log_fail "Downloading ${local_filename}..."
  402. fn_script_log_fail "${fileurl}"
  403. fi
  404. core_exit.sh
  405. else
  406. fn_print_error_eol_nl
  407. if [ -f "${lgsmlog}" ]; then
  408. fn_script_log_error "Downloading ${local_filename}..."
  409. fn_script_log_error "${fileurl}"
  410. fi
  411. fi
  412. else
  413. fn_print_ok_eol_nl
  414. if [ -f "${lgsmlog}" ]; then
  415. fn_script_log_pass "Downloading ${local_filename}..."
  416. fi
  417. # Make file executable if chmodx is set.
  418. if [ "${chmodx}" == "chmodx" ]; then
  419. chmod +x "${local_filedir}/${local_filename}"
  420. fi
  421. # Remove trap.
  422. trap - INT
  423. break
  424. fi
  425. done
  426. fi
  427. if [ -f "${local_filedir}/${local_filename}" ]; then
  428. fn_dl_hash
  429. # Execute file if run is set.
  430. if [ "${run}" == "run" ]; then
  431. # shellcheck source=/dev/null
  432. source "${local_filedir}/${local_filename}"
  433. fi
  434. fi
  435. }
  436. # GitHub file download modules.
  437. # Used to simplify downloading specific files from GitHub.
  438. # github_file_url_dir: the directory of the file in the GitHub: lgsm/modules
  439. # github_file_url_name: the filename of the file to download from GitHub: core_messages.sh
  440. # github_file_url_dir: the directory of the file in the GitHub: lgsm/modules
  441. # github_file_url_name: the filename of the file to download from GitHub: core_messages.sh
  442. # githuburl: the full GitHub url
  443. # remote_fileurl: The URL of the file: http://example.com/dl/File.tar.bz2
  444. # local_filedir: location the file is to be saved: /home/server/lgsm/tmp
  445. # local_filename: name of file (this can be different from the url name): file.tar.bz2
  446. # chmodx: Optional, set to "chmodx" to make file executable using chmod +x
  447. # run: Optional, set run to execute the file after download
  448. # forcedl: Optional, force re-download of file even if exists
  449. # hash: Optional, set an hash sum and will compare it against the file.
  450. # Fetches files from the Git repo.
  451. fn_fetch_file_github() {
  452. github_file_url_dir="${1}"
  453. github_file_url_name="${2}"
  454. # If master branch will currently running LinuxGSM version to prevent "version mixing". This is ignored if a fork.
  455. if [ "${githubbranch}" == "master" ] && [ "${githubuser}" == "GameServerManagers" ] && [ "${commandname}" != "UPDATE-LGSM" ]; then
  456. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  457. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  458. else
  459. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  460. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  461. fi
  462. remote_fileurl_name="GitHub"
  463. remote_fileurl_backup_name="Bitbucket"
  464. local_filedir="${3}"
  465. local_filename="${github_file_url_name}"
  466. chmodx="${4:-0}"
  467. run="${5:-0}"
  468. forcedl="${6:-0}"
  469. hash="${7:-0}"
  470. # Passes vars to the file download module.
  471. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  472. }
  473. fn_check_file_github() {
  474. github_file_url_dir="${1}"
  475. github_file_url_name="${2}"
  476. if [ "${githubbranch}" == "master" ] && [ "${githubuser}" == "GameServerManagers" ] && [ "${commandname}" != "UPDATE-LGSM" ]; then
  477. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  478. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  479. else
  480. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  481. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  482. fi
  483. remote_fileurl_name="GitHub"
  484. remote_fileurl_backup_name="Bitbucket"
  485. fn_check_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${github_file_url_name}"
  486. }
  487. # Fetches config files from the Git repo.
  488. fn_fetch_config() {
  489. github_file_url_dir="${1}"
  490. github_file_url_name="${2}"
  491. # If master branch will currently running LinuxGSM version to prevent "version mixing". This is ignored if a fork.
  492. if [ "${githubbranch}" == "master" ] && [ "${githubuser}" == "GameServerManagers" ] && [ "${commandname}" != "UPDATE-LGSM" ]; then
  493. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  494. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  495. else
  496. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  497. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  498. fi
  499. remote_fileurl_name="GitHub"
  500. remote_fileurl_backup_name="Bitbucket"
  501. local_filedir="${3}"
  502. local_filename="${4}"
  503. chmodx="nochmodx"
  504. run="norun"
  505. forcedl="noforce"
  506. hash="nohash"
  507. # Passes vars to the file download module.
  508. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  509. }
  510. # Fetches modules from the Git repo during first download.
  511. fn_fetch_module() {
  512. github_file_url_dir="lgsm/modules"
  513. github_file_url_name="${modulefile}"
  514. # If master branch will currently running LinuxGSM version to prevent "version mixing". This is ignored if a fork.
  515. if [ "${githubbranch}" == "master" ] && [ "${githubuser}" == "GameServerManagers" ] && [ "${commandname}" != "UPDATE-LGSM" ]; then
  516. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  517. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  518. else
  519. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  520. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  521. fi
  522. remote_fileurl_name="GitHub"
  523. remote_fileurl_backup_name="Bitbucket"
  524. local_filedir="${modulesdir}"
  525. local_filename="${github_file_url_name}"
  526. chmodx="chmodx"
  527. run="run"
  528. forcedl="noforce"
  529. hash="nohash"
  530. # Passes vars to the file download module.
  531. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  532. }
  533. # Fetches modules from the Git repo during update-lgsm.
  534. fn_update_module() {
  535. github_file_url_dir="lgsm/modules"
  536. github_file_url_name="${modulefile}"
  537. # If master branch will currently running LinuxGSM version to prevent "version mixing". This is ignored if a fork.
  538. if [ "${githubbranch}" == "master" ] && [ "${githubuser}" == "GameServerManagers" ] && [ "${commandname}" != "UPDATE-LGSM" ]; then
  539. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  540. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  541. else
  542. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  543. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  544. fi
  545. remote_fileurl_name="GitHub"
  546. remote_fileurl_backup_name="Bitbucket"
  547. local_filedir="${modulesdir}"
  548. local_filename="${github_file_url_name}"
  549. chmodx="chmodx"
  550. run="norun"
  551. forcedl="noforce"
  552. hash="nohash"
  553. # Passes vars to the file download module.
  554. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  555. }
  556. # Function to download latest github release.
  557. # $1 GitHub user / organisation.
  558. # $2 Repo name.
  559. # $3 Destination for download.
  560. # $4 Search string in releases (needed if there are more files that can be downloaded from the release pages).
  561. fn_dl_latest_release_github() {
  562. local githubreleaseuser="${1}"
  563. local githubreleaserepo="${2}"
  564. local githubreleasedownloadpath="${3}"
  565. local githubreleasesearch="${4}"
  566. local githublatestreleaseurl="https://api.github.com/repos/${githubreleaseuser}/${githubreleaserepo}/releases/latest"
  567. # Get last github release.
  568. # If no search for the release filename is set, just get the first file from the latest release.
  569. if [ -z "${githubreleasesearch}" ]; then
  570. githubreleaseassets=$(curl -s "${githublatestreleaseurl}" | jq '[ .assets[] ]')
  571. else
  572. githubreleaseassets=$(curl -s "${githublatestreleaseurl}" | jq "[ .assets[]|select(.browser_download_url | contains(\"${githubreleasesearch}\")) ]")
  573. fi
  574. # Check how many releases we got from the api and exit if we have more then one.
  575. if [ "$(echo -e "${githubreleaseassets}" | jq '. | length')" -gt 1 ]; then
  576. fn_print_fatal_nl "Found more than one release to download - Please report this to the LinuxGSM issue tracker"
  577. fn_script_log_fail "Found more than one release to download - Please report this to the LinuxGSM issue tracker"
  578. else
  579. # Set variables for download via fn_fetch_file.
  580. githubreleasefilename=$(echo -e "${githubreleaseassets}" | jq -r '.[]name')
  581. githubreleasedownloadlink=$(echo -e "${githubreleaseassets}" | jq -r '.[]browser_download_url')
  582. # Error if no version is there.
  583. if [ -z "${githubreleasefilename}" ]; then
  584. fn_print_fail_nl "Cannot get version from GitHub API for ${githubreleaseuser}/${githubreleaserepo}"
  585. fn_script_log_fail "Cannot get version from GitHub API for ${githubreleaseuser}/${githubreleaserepo}"
  586. else
  587. # Fetch file from the remote location from the existing module to the ${tmpdir} for now.
  588. fn_fetch_file "${githubreleasedownloadlink}" "" "${githubreleasefilename}" "" "${githubreleasedownloadpath}" "${githubreleasefilename}"
  589. fi
  590. fi
  591. }