linuxgsm.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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://gameservermanagers.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. curl_paths_array=($(command -v curl 2>/dev/null) $(which curl >/dev/null 2>&1) /usr/bin/curl /bin/curl /usr/sbin/curl /sbin/curl)
  67. for curlpath in "${curl_paths_array}"
  68. do
  69. if [ -x "${curlpath}" ]; then
  70. break
  71. fi
  72. done
  73. # If curl exists download file
  74. if [ "$(basename ${curlpath})" == "curl" ]; then
  75. # trap to remove part downloaded files
  76. echo -ne " fetching ${local_filename}...\c"
  77. curlcmd=$(${curlpath} -s --fail -L -o "${local_filedir}/${local_filename}" "${remote_fileurl}" 2>&1)
  78. local exitcode=$?
  79. if [ ${exitcode} -ne 0 ]; then
  80. echo -e "\e[0;31mFAIL\e[0m\n"
  81. if [ -f "${lgsmlog}" ]; then
  82. echo -e "${remote_fileurl}" | tee -a "${lgsmlog}"
  83. echo "${curlcmd}" | tee -a "${lgsmlog}"
  84. fi
  85. exit 1
  86. else
  87. echo -e "\e[0;32mOK\e[0m"
  88. fi
  89. else
  90. echo "[ FAIL ] Curl is not installed"
  91. exit 1
  92. fi
  93. # make file chmodx if chmodx is set
  94. if [ "${chmodx}" == "chmodx" ]; then
  95. chmod +x "${local_filedir}/${local_filename}"
  96. fi
  97. fi
  98. if [ -f "${local_filedir}/${local_filename}" ]; then
  99. # run file if run is set
  100. if [ "${run}" == "run" ]; then
  101. source "${local_filedir}/${local_filename}"
  102. fi
  103. fi
  104. }
  105. fn_bootstrap_fetch_file_github(){
  106. github_file_url_dir="${1}"
  107. github_file_url_name="${2}"
  108. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  109. remote_fileurl="${githuburl}"
  110. local_filedir="${3}"
  111. local_filename="${github_file_url_name}"
  112. chmodx="${4:-0}"
  113. run="${5:-0}"
  114. forcedl="${6:-0}"
  115. md5="${7:-0}"
  116. # Passes vars to the file download function
  117. fn_bootstrap_fetch_file "${remote_fileurl}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  118. }
  119. # Installer menu
  120. fn_print_center() {
  121. columns="$(tput cols)"
  122. line="$@"
  123. printf "%*s\n" $(( (${#line} + columns) / 2)) "${line}"
  124. }
  125. fn_print_horizontal(){
  126. char="${1:-=}"
  127. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' "${char}"
  128. }
  129. # Bash Menu
  130. fn_install_menu_bash() {
  131. local resultvar=$1
  132. title=$2
  133. caption=$3
  134. options=$4
  135. fn_print_horizontal
  136. fn_print_center $title
  137. fn_print_center $caption
  138. fn_print_horizontal
  139. menu_options=()
  140. while read -r line || [[ -n "${line}" ]]; do
  141. var=$(echo "${line}" | awk -F "," '{print $2 " - " $3}')
  142. menu_options+=( "${var}" )
  143. done < $options
  144. menu_options+=( "Cancel" )
  145. select option in "${menu_options[@]}"; do
  146. if [ -n "${option}" ] && [ "${option}" != "Cancel" ]; then
  147. eval "$resultvar=\"${option/%\ */}\""
  148. fi
  149. break
  150. done
  151. }
  152. # Whiptail/Dialog Menu
  153. fn_install_menu_whiptail() {
  154. local menucmd=$1
  155. local resultvar=$2
  156. title=$3
  157. caption=$4
  158. options=$5
  159. height=${6:-40}
  160. width=${7:-80}
  161. menuheight=${8:-30}
  162. IFS=","
  163. menu_options=()
  164. while read -r line; do
  165. key=$(echo "${line}" | awk -F "," '{print $3}')
  166. val=$(echo "${line}" | awk -F "," '{print $2}')
  167. menu_options+=( ${val//\"} "${key//\"}" )
  168. done < $options
  169. OPTION=$(${menucmd} --title "${title}" --menu "${caption}" ${height} ${width} ${menuheight} "${menu_options[@]}" 3>&1 1>&2 2>&3)
  170. if [ $? == 0 ]; then
  171. eval "$resultvar=\"${OPTION}\""
  172. else
  173. eval "$resultvar="
  174. fi
  175. }
  176. # Menu selector
  177. fn_install_menu() {
  178. local resultvar=$1
  179. local selection=""
  180. title=$2
  181. caption=$3
  182. options=$4
  183. # Get menu command
  184. for menucmd in whiptail dialog bash; do
  185. if [ -x $(which ${menucmd}) ]; then
  186. menucmd=$(which ${menucmd})
  187. break
  188. fi
  189. done
  190. case "$(basename ${menucmd})" in
  191. whiptail|dialog)
  192. fn_install_menu_whiptail "${menucmd}" selection "${title}" "${caption}" "${options}" 40 80 30;;
  193. *)
  194. fn_install_menu_bash selection "${title}" "${caption}" "${options}";;
  195. esac
  196. eval "$resultvar=\"${selection}\""
  197. }
  198. # Gets server info from serverlist.csv and puts in to array
  199. fn_server_info(){
  200. IFS=","
  201. server_info_array=($(grep -aw "${userinput}" "${serverlist}"))
  202. shortname="${server_info_array[0]}" # csgo
  203. gameservername="${server_info_array[1]}" # csgoserver
  204. gamename="${server_info_array[2]}" # Counter Strike: Global Offensive
  205. }
  206. fn_install_getopt(){
  207. userinput="empty"
  208. echo "Usage: $0 [option]"
  209. echo -e ""
  210. echo "Installer - Linux Game Server Managers - Version ${version}"
  211. echo "https://gameservermanagers.com"
  212. echo -e ""
  213. echo -e "Commands"
  214. echo -e "install\t\t| Select server to install."
  215. echo -e "servername\t| e.g $0 csgoserver. Enter name of server/game to install."
  216. echo -e "list\t\t| List all servers available for install."
  217. exit
  218. }
  219. fn_install_file(){
  220. local_filename="${gameservername}"
  221. if [ -e "${local_filename}" ]; then
  222. i=2
  223. while [ -e "${local_filename}-${i}" ] ; do
  224. let i++
  225. done
  226. local_filename="${local_filename}-${i}"
  227. fi
  228. cp -R "${selfname}" "${local_filename}"
  229. sed -i -e "s/shortname=\"core\"/shortname=\"${shortname}\"/g" "${local_filename}"
  230. sed -i -e "s/gameservername=\"core\"/gameservername=\"${gameservername}\"/g" "${local_filename}"
  231. echo "Installed ${gamename} server as ${local_filename}"
  232. echo ""
  233. if [ ! -d "${serverfiles}" ]; then
  234. echo "./${local_filename} install"
  235. else
  236. echo "Remember to check server ports"
  237. echo "./${local_filename} details"
  238. fi
  239. echo ""
  240. exit
  241. }
  242. # Prevent from running this script as root.
  243. if [ "$(whoami)" == "root" ]; then
  244. if [ ! -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. userinput=$1
  255. datadir="${tmpdir}/data"
  256. serverlist="${datadir}/serverlist.csv"
  257. # Download the latest serverlist. This is the complete list of all supported servers.
  258. fn_bootstrap_fetch_file_github "lgsm/data" "serverlist.csv" "${datadir}" "nochmodx" "norun" "forcedl" "nomd5"
  259. if [ ! -f "${serverlist}" ]; then
  260. echo "[ FAIL ] serverlist.csv could not be loaded."
  261. exit 1
  262. fi
  263. if [ "${userinput}" == "list" ]; then
  264. {
  265. awk -F "," '{print $2 "\t" $3}' "${serverlist}"
  266. } | column -s $'\t' -t | more
  267. exit
  268. elif [ "${userinput}" == "install" ]||[ "${userinput}" == "i" ]; then
  269. fn_install_menu result "LinuxGSM" "Select game to install" "${serverlist}"
  270. userinput="${result}"
  271. fn_server_info
  272. if [ "${result}" == "${gameservername}" ]; then
  273. fn_install_file
  274. elif [ "${result}" == "" ]; then
  275. echo "Install canceled"
  276. else
  277. echo "[ FAIL ] menu result does not match gameservername"
  278. echo "result: ${result}"
  279. echo "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 "[ 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. # 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 -ne " 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 "\e[0;31mFAIL\e[0m\n"
  308. exit 1
  309. else
  310. echo -e "\e[0;32mOK\e[0m"
  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 -ne " 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 "\e[0;31mFAIL\e[0m\n"
  321. exit 1
  322. else
  323. echo -e "\e[0;32mOK\e[0m"
  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. # Prevents running of core_exit.sh for Travis.
  347. if [ "${travistest}" != "1" ]; then
  348. getopt=$1
  349. core_getopt.sh
  350. fi
  351. fi