core_dl.sh 28 KB

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