core_dl.sh 27 KB

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