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