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.8.1"
  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. # Prevent LinuxGSM from running as root. Except if doing a dependency install.
  241. if [ "$(whoami)" == "root" ]; then
  242. if [ "${userinput}" == "install" ]||[ "${userinput}" == "auto-install" ]||[ "${userinput}" == "i" ]||[ "${userinput}" == "ai" ]; then
  243. if [ "${shortname}" == "core" ]; then
  244. echo "[ FAIL ] Do NOT run this script as root!"
  245. exit 1
  246. fi
  247. elif [ ! -f "${functionsdir}/core_functions.sh" ]||[ ! -f "${functionsdir}/check_root.sh" ]||[ ! -f "${functionsdir}/core_messages.sh" ]; then
  248. echo "[ FAIL ] Do NOT run this script as root!"
  249. exit 1
  250. else
  251. core_functions.sh
  252. check_root.sh
  253. fi
  254. fi
  255. # Download the latest serverlist. This is the complete list of all supported servers.
  256. fn_bootstrap_fetch_file_github "lgsm/data" "serverlist.csv" "${datadir}" "nochmodx" "norun" "forcedl" "nomd5"
  257. if [ ! -f "${serverlist}" ]; then
  258. echo "[ FAIL ] serverlist.csv could not be loaded."
  259. exit 1
  260. fi
  261. # LinuxGSM installer mode.
  262. if [ "${shortname}" == "core" ]; then
  263. if [ "${userinput}" == "list" ]||[ "${userinput}" == "l" ]; then
  264. {
  265. tail -n +2 "${serverlist}" | awk -F "," '{print $2 "\t" $3}'
  266. } | column -s $'\t' -t | more
  267. exit
  268. elif [ "${userinput}" == "install" ]||[ "${userinput}" == "i" ]; then
  269. tail -n +2 "${serverlist}" | awk -F "," '{print $1 "," $2 "," $3}' > "${serverlistmenu}"
  270. fn_install_menu result "LinuxGSM" "Select game server to install." "${serverlistmenu}"
  271. userinput="${result}"
  272. fn_server_info
  273. if [ "${result}" == "${gameservername}" ]; then
  274. fn_install_file
  275. elif [ "${result}" == "" ]; then
  276. echo "Install canceled"
  277. else
  278. echo "[ FAIL ] menu result does not match gameservername"
  279. echo "result: ${result}"
  280. echo "gameservername: ${gameservername}"
  281. fi
  282. elif [ -n "${userinput}" ]; then
  283. fn_server_info
  284. if [ "${userinput}" == "${gameservername}" ]||[ "${userinput}" == "${gamename}" ]||[ "${userinput}" == "${shortname}" ]; then
  285. fn_install_file
  286. else
  287. echo "[ FAIL ] unknown game server"
  288. fi
  289. else
  290. fn_install_getopt
  291. fi
  292. # LinuxGSM server mode.
  293. else
  294. core_functions.sh
  295. if [ "${shortname}" != "core-dep" ]; then
  296. # Load LinuxGSM configs.
  297. # These are required to get all the default variables for the specific server.
  298. # Load the default config. If missing download it. If changed reload it.
  299. if [ ! -f "${configdirdefault}/config-lgsm/${gameservername}/_default.cfg" ]; then
  300. mkdir -p "${configdirdefault}/config-lgsm/${gameservername}"
  301. fn_fetch_config "lgsm/config-default/config-lgsm/${gameservername}" "_default.cfg" "${configdirdefault}/config-lgsm/${gameservername}" "_default.cfg" "nochmodx" "norun" "noforcedl" "nomd5"
  302. fi
  303. if [ ! -f "${configdirserver}/_default.cfg" ]; then
  304. mkdir -p "${configdirserver}"
  305. echo -en " copying _default.cfg...\c"
  306. cp -R "${configdirdefault}/config-lgsm/${gameservername}/_default.cfg" "${configdirserver}/_default.cfg"
  307. exitcode=$?
  308. if [ ${exitcode} -ne 0 ]; then
  309. echo -e "FAIL"
  310. exit 1
  311. else
  312. echo -e "OK"
  313. fi
  314. else
  315. function_file_diff=$(diff -q "${configdirdefault}/config-lgsm/${gameservername}/_default.cfg" "${configdirserver}/_default.cfg")
  316. if [ "${function_file_diff}" != "" ]; then
  317. fn_print_warn_nl "_default.cfg has been altered. reloading config."
  318. echo -en " copying _default.cfg...\c"
  319. cp -R "${configdirdefault}/config-lgsm/${gameservername}/_default.cfg" "${configdirserver}/_default.cfg"
  320. exitcode=$?
  321. if [ ${exitcode} -ne 0 ]; then
  322. echo -e "FAIL"
  323. exit 1
  324. else
  325. echo -e "OK"
  326. fi
  327. fi
  328. fi
  329. source "${configdirserver}/_default.cfg"
  330. # Load the common.cfg config. If missing download it.
  331. if [ ! -f "${configdirserver}/common.cfg" ]; then
  332. fn_fetch_config "lgsm/config-default/config-lgsm" "common-template.cfg" "${configdirserver}" "common.cfg" "${chmodx}" "nochmodx" "norun" "noforcedl" "nomd5"
  333. source "${configdirserver}/common.cfg"
  334. else
  335. source "${configdirserver}/common.cfg"
  336. fi
  337. # Load the instance.cfg config. If missing download it.
  338. if [ ! -f "${configdirserver}/${servicename}.cfg" ]; then
  339. fn_fetch_config "lgsm/config-default/config-lgsm" "instance-template.cfg" "${configdirserver}" "${servicename}.cfg" "nochmodx" "norun" "noforcedl" "nomd5"
  340. source "${configdirserver}/${servicename}.cfg"
  341. else
  342. source "${configdirserver}/${servicename}.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