linuxgsm.sh 12 KB

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