linuxgsm.sh 12 KB

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