core_dl.sh 27 KB

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