linuxgsm.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #!/bin/bash
  2. # Project: Game Server Managers - LinuxGSM
  3. # Author: Daniel Gibbs
  4. # License: MIT License, Copyright (c) 2017 Daniel Gibbs
  5. # Purpose: Linux Game Server Management Script
  6. # Contributors: https://github.com/GameServerManagers/LinuxGSM/graphs/contributors
  7. # Documentation: https://github.com/GameServerManagers/LinuxGSM/wiki
  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://github.com/GameServerManagers/LinuxGSM/wiki/LinuxGSM-Config-Files
  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="180313"
  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. ## GitHub Branch Select
  39. # Allows for the use of different function files
  40. # from a different repo and/or branch.
  41. githubuser="GameServerManagers"
  42. githubrepo="LinuxGSM"
  43. githubbranch="master"
  44. # Core Function that is required first
  45. core_functions.sh(){
  46. functionfile="${FUNCNAME}"
  47. fn_bootstrap_fetch_file_github "lgsm/functions" "core_functions.sh" "${functionsdir}" "chmodx" "run" "noforcedl" "nomd5"
  48. }
  49. # Bootstrap
  50. # Fetches the core functions required before passed off to core_dl.sh
  51. # Fetches core functions
  52. fn_bootstrap_fetch_file(){
  53. remote_fileurl="${1}"
  54. local_filedir="${2}"
  55. local_filename="${3}"
  56. chmodx="${4:-0}"
  57. run="${5:-0}"
  58. forcedl="${6:-0}"
  59. md5="${7:-0}"
  60. # download file if missing or download forced
  61. if [ ! -f "${local_filedir}/${local_filename}" ]||[ "${forcedl}" == "forcedl" ]; then
  62. if [ ! -d "${local_filedir}" ]; then
  63. mkdir -p "${local_filedir}"
  64. fi
  65. # Defines curl path
  66. curlpath=$(command -v curl 2>/dev/null)
  67. # If curl exists download file
  68. if [ "$(basename ${curlpath})" == "curl" ]; then
  69. # trap to remove part downloaded files
  70. echo -ne " fetching ${local_filename}...\c"
  71. curlcmd=$(${curlpath} -s --fail -L -o "${local_filedir}/${local_filename}" "${remote_fileurl}" 2>&1)
  72. local exitcode=$?
  73. if [ ${exitcode} -ne 0 ]; then
  74. echo -e "\e[0;31mFAIL\e[0m\n"
  75. if [ -f "${lgsmlog}" ]; then
  76. echo -e "${remote_fileurl}" | tee -a "${lgsmlog}"
  77. echo "${curlcmd}" | tee -a "${lgsmlog}"
  78. fi
  79. exit 1
  80. else
  81. echo -e "\e[0;32mOK\e[0m"
  82. fi
  83. else
  84. echo "[ 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. source "${local_filedir}/${local_filename}"
  96. fi
  97. fi
  98. }
  99. fn_bootstrap_fetch_file_github(){
  100. github_file_url_dir="${1}"
  101. github_file_url_name="${2}"
  102. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  103. remote_fileurl="${githuburl}"
  104. local_filedir="${3}"
  105. local_filename="${github_file_url_name}"
  106. chmodx="${4:-0}"
  107. run="${5:-0}"
  108. forcedl="${6:-0}"
  109. md5="${7:-0}"
  110. # Passes vars to the file download function
  111. fn_bootstrap_fetch_file "${remote_fileurl}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  112. }
  113. # Installer menu
  114. fn_print_center() {
  115. columns="$(tput cols)"
  116. line="$@"
  117. printf "%*s\n" $(( (${#line} + columns) / 2)) "${line}"
  118. }
  119. fn_print_horizontal(){
  120. char="${1:-=}"
  121. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' "${char}"
  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 "${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 "${line}" | awk -F "," '{print $3}')
  160. val=$(echo "${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 [ -x $(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 "Usage: $0 [option]"
  203. echo -e ""
  204. echo "Installer - Linux Game Server Managers - Version ${version}"
  205. echo "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| e.g $0 csgoserver. Enter name of server/game to install."
  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. let 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 "Installed ${gamename} server as ${local_filename}"
  226. echo ""
  227. if [ ! -d "${serverfiles}" ]; then
  228. echo "./${local_filename} install"
  229. else
  230. echo "Remember to check server ports"
  231. echo "./${local_filename} details"
  232. fi
  233. echo ""
  234. exit
  235. }
  236. # Prevent from running this script as root.
  237. if [ "$(whoami)" == "root" ]; then
  238. if [ ! -f "${functionsdir}/core_functions.sh" ]||[ ! -f "${functionsdir}/check_root.sh" ]||[ ! -f "${functionsdir}/core_messages.sh" ]; then
  239. echo "[ FAIL ] Do NOT run this script as root!"
  240. exit 1
  241. else
  242. core_functions.sh
  243. check_root.sh
  244. fi
  245. fi
  246. # LinuxGSM installer mode
  247. if [ "${shortname}" == "core" ]; then
  248. userinput=$1
  249. datadir="${tmpdir}/data"
  250. serverlist="${datadir}/serverlist.csv"
  251. # Download the latest serverlist. This is the complete list of all supported servers.
  252. fn_bootstrap_fetch_file_github "lgsm/data" "serverlist.csv" "${datadir}" "nochmodx" "norun" "forcedl" "nomd5"
  253. if [ ! -f "${serverlist}" ]; then
  254. echo "[ FAIL ] serverlist.csv could not be loaded."
  255. exit 1
  256. fi
  257. if [ "${userinput}" == "list" ]; then
  258. {
  259. awk -F "," '{print $2 "\t" $3}' "${serverlist}"
  260. } | column -s $'\t' -t | more
  261. exit
  262. elif [ "${userinput}" == "install" ]||[ "${userinput}" == "i" ]; then
  263. fn_install_menu result "LinuxGSM" "Select game to install" "${serverlist}"
  264. userinput="${result}"
  265. fn_server_info
  266. if [ "${result}" == "${gameservername}" ]; then
  267. fn_install_file
  268. elif [ "${result}" == "" ]; then
  269. echo "Install canceled"
  270. else
  271. echo "[ FAIL ] menu result does not match gameservername"
  272. echo "result: ${result}"
  273. echo "gameservername: ${gameservername}"
  274. fi
  275. elif [ -n "${userinput}" ]; then
  276. fn_server_info
  277. if [ "${userinput}" == "${gameservername}" ]||[ "${userinput}" == "${gamename}" ]||[ "${userinput}" == "${shortname}" ]; then
  278. fn_install_file
  279. else
  280. echo "[ FAIL ] unknown game server"
  281. fi
  282. else
  283. fn_install_getopt
  284. fi
  285. # LinuxGSM Server Mode
  286. else
  287. core_functions.sh
  288. # Load LinuxGSM configs
  289. # These are required to get all the default variables for the specific server.
  290. # Load the default config. If missing download it. If changed reload it.
  291. if [ ! -f "${configdirdefault}/config-lgsm/${gameservername}/_default.cfg" ]; then
  292. mkdir -p "${configdirdefault}/config-lgsm/${gameservername}"
  293. fn_fetch_config "lgsm/config-default/config-lgsm/${gameservername}" "_default.cfg" "${configdirdefault}/config-lgsm/${gameservername}" "_default.cfg" "nochmodx" "norun" "noforcedl" "nomd5"
  294. fi
  295. if [ ! -f "${configdirserver}/_default.cfg" ]; then
  296. mkdir -p "${configdirserver}"
  297. echo -ne " copying _default.cfg...\c"
  298. cp -R "${configdirdefault}/config-lgsm/${gameservername}/_default.cfg" "${configdirserver}/_default.cfg"
  299. exitcode=$?
  300. if [ ${exitcode} -ne 0 ]; then
  301. echo -e "\e[0;31mFAIL\e[0m\n"
  302. exit 1
  303. else
  304. echo -e "\e[0;32mOK\e[0m"
  305. fi
  306. else
  307. function_file_diff=$(diff -q "${configdirdefault}/config-lgsm/${gameservername}/_default.cfg" "${configdirserver}/_default.cfg")
  308. if [ "${function_file_diff}" != "" ]; then
  309. fn_print_warn_nl "_default.cfg has been altered. reloading config."
  310. echo -ne " copying _default.cfg...\c"
  311. cp -R "${configdirdefault}/config-lgsm/${gameservername}/_default.cfg" "${configdirserver}/_default.cfg"
  312. exitcode=$?
  313. if [ ${exitcode} -ne 0 ]; then
  314. echo -e "\e[0;31mFAIL\e[0m\n"
  315. exit 1
  316. else
  317. echo -e "\e[0;32mOK\e[0m"
  318. fi
  319. fi
  320. fi
  321. source "${configdirserver}/_default.cfg"
  322. # Load the common.cfg config. If missing download it
  323. if [ ! -f "${configdirserver}/common.cfg" ]; then
  324. fn_fetch_config "lgsm/config-default/config-lgsm" "common-template.cfg" "${configdirserver}" "common.cfg" "${chmodx}" "nochmodx" "norun" "noforcedl" "nomd5"
  325. source "${configdirserver}/common.cfg"
  326. else
  327. source "${configdirserver}/common.cfg"
  328. fi
  329. # Load the instance.cfg config. If missing download it
  330. if [ ! -f "${configdirserver}/${servicename}.cfg" ]; then
  331. fn_fetch_config "lgsm/config-default/config-lgsm" "instance-template.cfg" "${configdirserver}" "${servicename}.cfg" "nochmodx" "norun" "noforcedl" "nomd5"
  332. source "${configdirserver}/${servicename}.cfg"
  333. else
  334. source "${configdirserver}/${servicename}.cfg"
  335. fi
  336. # Load the linuxgsm.sh in to tmpdir. If missing download it
  337. if [ ! -f "${tmpdir}/linuxgsm.sh" ]; then
  338. fn_fetch_file_github "" "linuxgsm.sh" "${tmpdir}" "chmodx" "norun" "noforcedl" "nomd5"
  339. fi
  340. # Prevents running of core_exit.sh for Travis.
  341. if [ "${travistest}" != "1" ]; then
  342. getopt=$1
  343. core_getopt.sh
  344. fi
  345. fi