linuxgsm.sh 14 KB

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