linuxgsm.sh 12 KB

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