core_dl.sh 29 KB

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