linuxgsm.sh 12 KB

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