linuxgsm.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #!/bin/bash
  2. # Project: Game Server Managers - LinuxGSM
  3. # Author: Daniel Gibbs
  4. # License: MIT License, Copyright (c) 2020 Daniel Gibbs
  5. # Purpose: Linux Game Server Management Script
  6. # Contributors: https://linuxgsm.com/contrib
  7. # Documentation: https://docs.linuxgsm.com
  8. # Website: https://linuxgsm.com
  9. # DO NOT EDIT THIS FILE
  10. # LinuxGSM configuration is no longer edited here
  11. # To update your LinuxGSM config go to:
  12. # lgsm/config-lgsm
  13. # https://docs.linuxgsm.com/configuration/linuxgsm-config
  14. # Debugging
  15. if [ -f ".dev-debug" ]; then
  16. exec 5>dev-debug.log
  17. BASH_XTRACEFD="5"
  18. set -x
  19. fi
  20. version="v20.4.0"
  21. shortname="core"
  22. gameservername="core"
  23. commandname="CORE"
  24. rootdir=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
  25. selfname=$(basename "$(readlink -f "${BASH_SOURCE[0]}")")
  26. sessionname=$(echo "${selfname}" | cut -f1 -d".")
  27. lgsmdir="${rootdir}/lgsm"
  28. logdir="${rootdir}/log"
  29. lgsmlogdir="${logdir}/lgsm"
  30. steamcmddir="${HOME}/.steam/steamcmd"
  31. serverfiles="${rootdir}/serverfiles"
  32. functionsdir="${lgsmdir}/functions"
  33. tmpdir="${lgsmdir}/tmp"
  34. datadir="${lgsmdir}/data"
  35. lockdir="${lgsmdir}/lock"
  36. serverlist="${datadir}/serverlist.csv"
  37. serverlistmenu="${datadir}/serverlistmenu.csv"
  38. configdir="${lgsmdir}/config-lgsm"
  39. configdirserver="${configdir}/${gameservername}"
  40. configdirdefault="${lgsmdir}/config-default"
  41. userinput="${1}"
  42. ## GitHub Branch Select
  43. # Allows for the use of different function files
  44. # from a different repo and/or branch.
  45. githubuser="GameServerManagers"
  46. githubrepo="LinuxGSM"
  47. githubbranch="master"
  48. # Core function that is required first.
  49. core_functions.sh(){
  50. functionfile="${FUNCNAME[0]}"
  51. fn_bootstrap_fetch_file_github "lgsm/functions" "core_functions.sh" "${functionsdir}" "chmodx" "run" "noforcedl" "nomd5"
  52. }
  53. # Bootstrap
  54. # Fetches the core functions required before passed off to core_dl.sh.
  55. fn_bootstrap_fetch_file(){
  56. remote_fileurl="${1}"
  57. remote_fileurl_backup="${2}"
  58. remote_fileurl_name="${3}"
  59. remote_fileurl_backup_name="${4}"
  60. local_filedir="${5}"
  61. local_filename="${6}"
  62. chmodx="${7:-0}"
  63. run="${8:-0}"
  64. forcedl="${9:-0}"
  65. md5="${10:-0}"
  66. # Download file if missing or download forced.
  67. if [ ! -f "${local_filedir}/${local_filename}" ]||[ "${forcedl}" == "forcedl" ]; then
  68. # If backup fileurl exists include it.
  69. if [ -n "${remote_fileurl_backup}" ]; then
  70. # counter set to 0 to allow second try
  71. counter=0
  72. remote_fileurls_array=( remote_fileurl remote_fileurl_backup )
  73. else
  74. # counter set to 1 to not allow second try
  75. counter=1
  76. remote_fileurls_array=( remote_fileurl )
  77. fi
  78. for remote_fileurl_array in "${remote_fileurls_array[@]}"
  79. do
  80. if [ "${remote_fileurl_array}" == "remote_fileurl" ]; then
  81. fileurl="${remote_fileurl}"
  82. fileurl_name="${remote_fileurl_name}"
  83. elif [ "${remote_fileurl_array}" == "remote_fileurl_backup" ]; then
  84. fileurl="${remote_fileurl_backup}"
  85. fileurl_name="${remote_fileurl_backup_name}"
  86. fi
  87. counter=$((counter+1))
  88. if [ ! -d "${local_filedir}" ]; then
  89. mkdir -p "${local_filedir}"
  90. fi
  91. # Trap will remove part downloaded files if canceled.
  92. trap fn_fetch_trap INT
  93. # Larger files show a progress bar.
  94. echo -en "fetching ${fileurl_name} ${local_filename}...\c"
  95. curlcmd=$(curl -s --fail -L -o "${local_filedir}/${local_filename}" "${fileurl}" 2>&1)
  96. local exitcode=$?
  97. # Download will fail if downloads a html file.
  98. if [ -f "${local_filedir}/${local_filename}" ]; then
  99. if [ -n "$(head "${local_filedir}/${local_filename}" | grep "DOCTYPE" )" ]; then
  100. rm "${local_filedir:?}/${local_filename:?}"
  101. local exitcode=2
  102. fi
  103. fi
  104. # On first try will error. On second try will fail.
  105. if [ "${exitcode}" != 0 ]; then
  106. if [ ${counter} -ge 2 ]; then
  107. echo -e "FAIL"
  108. if [ -f "${lgsmlog}" ]; then
  109. fn_script_log_fatal "Downloading ${local_filename}"
  110. fn_script_log_fatal "${fileurl}"
  111. fi
  112. core_exit.sh
  113. else
  114. echo -e "ERROR"
  115. if [ -f "${lgsmlog}" ]; then
  116. fn_script_log_error "Downloading ${local_filename}"
  117. fn_script_log_error "${fileurl}"
  118. fi
  119. fi
  120. else
  121. echo -en "OK"
  122. sleep 0.3
  123. echo -en "\033[2K\\r"
  124. if [ -f "${lgsmlog}" ]; then
  125. fn_script_log_pass "Downloading ${local_filename}"
  126. fi
  127. # Make file executable if chmodx is set.
  128. if [ "${chmodx}" == "chmodx" ]; then
  129. chmod +x "${local_filedir}/${local_filename}"
  130. fi
  131. # Remove trap.
  132. trap - INT
  133. break
  134. fi
  135. done
  136. fi
  137. if [ -f "${local_filedir}/${local_filename}" ]; then
  138. # Execute file if run is set.
  139. if [ "${run}" == "run" ]; then
  140. # shellcheck source=/dev/null
  141. source "${local_filedir}/${local_filename}"
  142. fi
  143. fi
  144. }
  145. fn_bootstrap_fetch_file_github(){
  146. github_file_url_dir="${1}"
  147. github_file_url_name="${2}"
  148. if [ "${githubbranch}" == "master" ]&&[ "${commandname}" != "UPDATE-LGSM" ]; then
  149. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  150. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  151. else
  152. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  153. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  154. fi
  155. remote_fileurl_name="GitHub"
  156. remote_fileurl_backup_name="Bitbucket"
  157. local_filedir="${3}"
  158. local_filename="${github_file_url_name}"
  159. chmodx="${4:-0}"
  160. run="${5:-0}"
  161. forcedl="${6:-0}"
  162. md5="${7:-0}"
  163. # Passes vars to the file download function.
  164. fn_bootstrap_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  165. }
  166. # Installer menu.
  167. fn_print_center() {
  168. columns=$(tput cols)
  169. line="$*"
  170. printf "%*s\n" $(( (${#line} + columns) / 2)) "${line}"
  171. }
  172. fn_print_horizontal(){
  173. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' "="
  174. }
  175. # Bash menu.
  176. fn_install_menu_bash() {
  177. local resultvar=$1
  178. title=$2
  179. caption=$3
  180. options=$4
  181. fn_print_horizontal
  182. fn_print_center "${title}"
  183. fn_print_center "${caption}"
  184. fn_print_horizontal
  185. menu_options=()
  186. while read -r line || [[ -n "${line}" ]]; do
  187. var=$(echo -e "${line}" | awk -F "," '{print $2 " - " $3}')
  188. menu_options+=( "${var}" )
  189. done < "${options}"
  190. menu_options+=( "Cancel" )
  191. select option in "${menu_options[@]}"; do
  192. if [ "${option}" ]&&[ "${option}" != "Cancel" ]; then
  193. eval "$resultvar=\"${option/%\ */}\""
  194. fi
  195. break
  196. done
  197. }
  198. # Whiptail/Dialog menu.
  199. fn_install_menu_whiptail() {
  200. local menucmd=$1
  201. local resultvar=$2
  202. title=$3
  203. caption=$4
  204. options=$5
  205. height=${6:-40}
  206. width=${7:-80}
  207. menuheight=${8:-30}
  208. IFS=","
  209. menu_options=()
  210. while read -r line; do
  211. key=$(echo -e "${line}" | awk -F "," '{print $3}')
  212. val=$(echo -e "${line}" | awk -F "," '{print $2}')
  213. menu_options+=( "${val//\"}" "${key//\"}" )
  214. done < "${options}"
  215. OPTION=$(${menucmd} --title "${title}" --menu "${caption}" "${height}" "${width}" "${menuheight}" "${menu_options[@]}" 3>&1 1>&2 2>&3)
  216. if [ $? == 0 ]; then
  217. eval "$resultvar=\"${OPTION}\""
  218. else
  219. eval "$resultvar="
  220. fi
  221. }
  222. # Menu selector.
  223. fn_install_menu() {
  224. local resultvar=$1
  225. local selection=""
  226. title=$2
  227. caption=$3
  228. options=$4
  229. # Get menu command.
  230. for menucmd in whiptail dialog bash; do
  231. if [ "$(command -v "${menucmd}")" ]; then
  232. menucmd=$(command -v "${menucmd}")
  233. break
  234. fi
  235. done
  236. case "$(basename "${menucmd}")" in
  237. whiptail|dialog)
  238. fn_install_menu_whiptail "${menucmd}" selection "${title}" "${caption}" "${options}" 40 80 30;;
  239. *)
  240. fn_install_menu_bash selection "${title}" "${caption}" "${options}";;
  241. esac
  242. eval "$resultvar=\"${selection}\""
  243. }
  244. # Gets server info from serverlist.csv and puts in to array.
  245. fn_server_info(){
  246. IFS=","
  247. server_info_array=($(grep -aw "${userinput}" "${serverlist}"))
  248. shortname="${server_info_array[0]}" # csgo
  249. gameservername="${server_info_array[1]}" # csgoserver
  250. gamename="${server_info_array[2]}" # Counter Strike: Global Offensive
  251. }
  252. fn_install_getopt(){
  253. userinput="empty"
  254. echo -e "Usage: $0 [option]"
  255. echo -e ""
  256. echo -e "Installer - Linux Game Server Managers - Version ${version}"
  257. echo -e "https://linuxgsm.com"
  258. echo -e ""
  259. echo -e "Commands"
  260. echo -e "install\t\t| Select server to install."
  261. echo -e "servername\t| Enter name of game server to install. e.g $0 csgoserver."
  262. echo -e "list\t\t| List all servers available for install."
  263. exit
  264. }
  265. fn_install_file(){
  266. local_filename="${gameservername}"
  267. if [ -e "${local_filename}" ]; then
  268. i=2
  269. while [ -e "${local_filename}-${i}" ] ; do
  270. (( i++ ))
  271. done
  272. local_filename="${local_filename}-${i}"
  273. fi
  274. cp -R "${selfname}" "${local_filename}"
  275. sed -i -e "s/shortname=\"core\"/shortname=\"${shortname}\"/g" "${local_filename}"
  276. sed -i -e "s/gameservername=\"core\"/gameservername=\"${gameservername}\"/g" "${local_filename}"
  277. echo -e "Installed ${gamename} server as ${local_filename}"
  278. echo -e ""
  279. if [ ! -d "${serverfiles}" ]; then
  280. echo -e "./${local_filename} install"
  281. else
  282. echo -e "Remember to check server ports"
  283. echo -e "./${local_filename} details"
  284. fi
  285. echo -e ""
  286. exit
  287. }
  288. # Prevent LinuxGSM from running as root. Except if doing a dependency install.
  289. if [ "$(whoami)" == "root" ]; then
  290. if [ "${userinput}" == "install" ]||[ "${userinput}" == "auto-install" ]||[ "${userinput}" == "i" ]||[ "${userinput}" == "ai" ]; then
  291. if [ "${shortname}" == "core" ]; then
  292. echo -e "[ FAIL ] Do NOT run this script as root!"
  293. exit 1
  294. fi
  295. elif [ ! -f "${functionsdir}/core_functions.sh" ]||[ ! -f "${functionsdir}/check_root.sh" ]||[ ! -f "${functionsdir}/core_messages.sh" ]; then
  296. echo -e "[ FAIL ] Do NOT run this script as root!"
  297. exit 1
  298. else
  299. core_functions.sh
  300. check_root.sh
  301. fi
  302. fi
  303. # LinuxGSM installer mode.
  304. if [ "${shortname}" == "core" ]; then
  305. # Download the latest serverlist. This is the complete list of all supported servers.
  306. fn_bootstrap_fetch_file_github "lgsm/data" "serverlist.csv" "${datadir}" "nochmodx" "norun" "forcedl" "nomd5"
  307. if [ ! -f "${serverlist}" ]; then
  308. echo -e "[ FAIL ] serverlist.csv could not be loaded."
  309. exit 1
  310. fi
  311. if [ "${userinput}" == "list" ]||[ "${userinput}" == "l" ]; then
  312. {
  313. tail -n +2 "${serverlist}" | awk -F "," '{print $2 "\t" $3}'
  314. } | column -s $'\t' -t | more
  315. exit
  316. elif [ "${userinput}" == "install" ]||[ "${userinput}" == "i" ]; then
  317. tail -n +2 "${serverlist}" | awk -F "," '{print $1 "," $2 "," $3}' > "${serverlistmenu}"
  318. fn_install_menu result "LinuxGSM" "Select game server to install." "${serverlistmenu}"
  319. userinput="${result}"
  320. fn_server_info
  321. if [ "${result}" == "${gameservername}" ]; then
  322. fn_install_file
  323. elif [ "${result}" == "" ]; then
  324. echo -e "Install canceled"
  325. else
  326. echo -e "[ FAIL ] menu result does not match gameservername"
  327. echo -e "result: ${result}"
  328. echo -e "gameservername: ${gameservername}"
  329. fi
  330. elif [ "${userinput}" ]; then
  331. fn_server_info
  332. if [ "${userinput}" == "${gameservername}" ]||[ "${userinput}" == "${gamename}" ]||[ "${userinput}" == "${shortname}" ]; then
  333. fn_install_file
  334. else
  335. echo -e "[ FAIL ] unknown game server"
  336. fi
  337. else
  338. fn_install_getopt
  339. fi
  340. # LinuxGSM server mode.
  341. else
  342. core_functions.sh
  343. if [ "${shortname}" != "core-dep" ]; then
  344. # Load LinuxGSM configs.
  345. # These are required to get all the default variables for the specific server.
  346. # Load the default config. If missing download it. If changed reload it.
  347. if [ ! -f "${configdirdefault}/config-lgsm/${gameservername}/_default.cfg" ]; then
  348. mkdir -p "${configdirdefault}/config-lgsm/${gameservername}"
  349. fn_fetch_config "lgsm/config-default/config-lgsm/${gameservername}" "_default.cfg" "${configdirdefault}/config-lgsm/${gameservername}" "_default.cfg" "nochmodx" "norun" "noforcedl" "nomd5"
  350. fi
  351. if [ ! -f "${configdirserver}/_default.cfg" ]; then
  352. mkdir -p "${configdirserver}"
  353. echo -en "copying _default.cfg...\c"
  354. cp -R "${configdirdefault}/config-lgsm/${gameservername}/_default.cfg" "${configdirserver}/_default.cfg"
  355. if [ $? != 0 ]; then
  356. echo -e "FAIL"
  357. exit 1
  358. else
  359. echo -e "OK"
  360. fi
  361. else
  362. function_file_diff=$(diff -q "${configdirdefault}/config-lgsm/${gameservername}/_default.cfg" "${configdirserver}/_default.cfg")
  363. if [ "${function_file_diff}" != "" ]; then
  364. fn_print_warn_nl "_default.cfg has altered. reloading config."
  365. echo -en "copying _default.cfg...\c"
  366. cp -R "${configdirdefault}/config-lgsm/${gameservername}/_default.cfg" "${configdirserver}/_default.cfg"
  367. if [ $? != 0 ]; then
  368. echo -e "FAIL"
  369. exit 1
  370. else
  371. echo -e "OK"
  372. fi
  373. fi
  374. fi
  375. # shellcheck source=/dev/null
  376. source "${configdirserver}/_default.cfg"
  377. # Load the common.cfg config. If missing download it.
  378. if [ ! -f "${configdirserver}/common.cfg" ]; then
  379. fn_fetch_config "lgsm/config-default/config-lgsm" "common-template.cfg" "${configdirserver}" "common.cfg" "${chmodx}" "nochmodx" "norun" "noforcedl" "nomd5"
  380. # shellcheck source=/dev/null
  381. source "${configdirserver}/common.cfg"
  382. else
  383. # shellcheck source=/dev/null
  384. source "${configdirserver}/common.cfg"
  385. fi
  386. # Load the instance.cfg config. If missing download it.
  387. if [ ! -f "${configdirserver}/${selfname}.cfg" ]; then
  388. fn_fetch_config "lgsm/config-default/config-lgsm" "instance-template.cfg" "${configdirserver}" "${selfname}.cfg" "nochmodx" "norun" "noforcedl" "nomd5"
  389. # shellcheck source=/dev/null
  390. source "${configdirserver}/${selfname}.cfg"
  391. else
  392. # shellcheck source=/dev/null
  393. source "${configdirserver}/${selfname}.cfg"
  394. fi
  395. # Load the linuxgsm.sh in to tmpdir. If missing download it.
  396. if [ ! -f "${tmpdir}/linuxgsm.sh" ]; then
  397. fn_fetch_file_github "" "linuxgsm.sh" "${tmpdir}" "chmodx" "norun" "noforcedl" "nomd5"
  398. fi
  399. fi
  400. # Enables ANSI colours from core_messages.sh. Can be disabled with ansi=off.
  401. fn_ansi_loader
  402. # Prevents running of core_exit.sh for Travis-CI.
  403. if [ "${travistest}" != "1" ]; then
  404. getopt=$1
  405. core_getopt.sh
  406. fi
  407. fi