linuxgsm.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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="170619"
  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. steamcmddir="${rootdir}/steamcmd"
  30. serverfiles="${rootdir}/serverfiles"
  31. functionsdir="${lgsmdir}/functions"
  32. libdir="${lgsmdir}/lib"
  33. tmpdir="${lgsmdir}/tmp"
  34. configdir="${lgsmdir}/config-lgsm"
  35. configdirserver="${configdir}/${gameservername}"
  36. configdirdefault="${lgsmdir}/config-default"
  37. ## GitHub Branch Select
  38. # Allows for the use of different function files
  39. # from a different repo and/or branch.
  40. githubuser="GameServerManagers"
  41. githubrepo="LinuxGSM"
  42. githubbranch="master"
  43. # Core Function that is required first
  44. core_functions.sh(){
  45. functionfile="${FUNCNAME}"
  46. fn_bootstrap_fetch_file_github "lgsm/functions" "core_functions.sh" "${functionsdir}" "chmodx" "run" "noforcedl" "nomd5"
  47. }
  48. # Bootstrap
  49. # Fetches the core functions required before passed off to core_dl.sh
  50. # Fetches core functions
  51. fn_bootstrap_fetch_file(){
  52. remote_fileurl="${1}"
  53. local_filedir="${2}"
  54. local_filename="${3}"
  55. chmodx="${4:-0}"
  56. run="${5:-0}"
  57. forcedl="${6:-0}"
  58. md5="${7:-0}"
  59. # If the file is missing, then download
  60. if [ ! -f "${local_filedir}/${local_filename}" ]; then
  61. if [ ! -d "${local_filedir}" ]; then
  62. mkdir -p "${local_filedir}"
  63. fi
  64. # Defines curl path
  65. 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)
  66. for curlpath in "${curl_paths_array}"
  67. do
  68. if [ -x "${curlpath}" ]; then
  69. break
  70. fi
  71. done
  72. # If curl exists download file
  73. if [ "$(basename ${curlpath})" == "curl" ]; then
  74. # trap to remove part downloaded files
  75. echo -ne " fetching ${local_filename}...\c"
  76. curlcmd=$(${curlpath} -s --fail -L -o "${local_filedir}/${local_filename}" "${remote_fileurl}" 2>&1)
  77. local exitcode=$?
  78. if [ ${exitcode} -ne 0 ]; then
  79. echo -e "\e[0;31mFAIL\e[0m\n"
  80. if [ -f "${lgsmlog}" ]; then
  81. echo -e "${remote_fileurl}" | tee -a "${lgsmlog}"
  82. echo "${curlcmd}" | tee -a "${lgsmlog}"
  83. fi
  84. exit 1
  85. else
  86. echo -e "\e[0;32mOK\e[0m"
  87. fi
  88. else
  89. echo "[ FAIL ] Curl is not installed"
  90. exit 1
  91. fi
  92. # make file chmodx if chmodx is set
  93. if [ "${chmodx}" == "chmodx" ]; then
  94. chmod +x "${local_filedir}/${local_filename}"
  95. fi
  96. fi
  97. if [ -f "${local_filedir}/${local_filename}" ]; then
  98. # run file if run is set
  99. if [ "${run}" == "run" ]; then
  100. source "${local_filedir}/${local_filename}"
  101. fi
  102. fi
  103. }
  104. fn_bootstrap_fetch_file_github(){
  105. github_file_url_dir="${1}"
  106. github_file_url_name="${2}"
  107. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  108. remote_remote_fileurl="${githuburl}"
  109. local_local_filedir="${3}"
  110. local_local_filename="${github_file_url_name}"
  111. chmodx="${4:-0}"
  112. run="${5:-0}"
  113. forcedldl="${6:-0}"
  114. md5="${7:-0}"
  115. # Passes vars to the file download function
  116. fn_bootstrap_fetch_file "${remote_remote_fileurl}" "${local_local_filedir}" "${local_local_filename}" "${chmodx}" "${run}" "${forcedldl}" "${md5}"
  117. }
  118. # Installer menu
  119. fn_print_center() {
  120. columns="$(tput cols)"
  121. line="$@"
  122. printf "%*s\n" $(( (${#line} + columns) / 2)) "${line}"
  123. }
  124. fn_print_horizontal(){
  125. char="${1:-=}"
  126. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' "${char}"
  127. }
  128. # Bash Menu
  129. fn_install_menu_bash() {
  130. local resultvar=$1
  131. title=$2
  132. caption=$3
  133. options=$4
  134. fn_print_horizontal
  135. fn_print_center $title
  136. fn_print_center $caption
  137. fn_print_horizontal
  138. menu_options=()
  139. while read -r line || [[ -n "${line}" ]]; do
  140. var=$(echo "${line}" | awk -F "," '{print $2 " - " $3}')
  141. menu_options+=( "${var}" )
  142. done < $options
  143. menu_options+=( "Cancel" )
  144. select option in "${menu_options[@]}"; do
  145. if [ -n "${option}" ] && [ "${option}" != "Cancel" ]; then
  146. eval "$resultvar=\"${option/%\ */}\""
  147. fi
  148. break
  149. done
  150. }
  151. # Whiptail/Dialog Menu
  152. fn_install_menu_whiptail() {
  153. local menucmd=$1
  154. local resultvar=$2
  155. title=$3
  156. caption=$4
  157. options=$5
  158. height=${6:-40}
  159. width=${7:-80}
  160. menuheight=${8:-30}
  161. IFS=","
  162. menu_options=()
  163. while read -r line; do
  164. key=$(echo "${line}" | awk -F "," '{print $3}')
  165. val=$(echo "${line}" | awk -F "," '{print $2}')
  166. menu_options+=( ${val//\"} "${key//\"}" )
  167. done < $options
  168. OPTION=$(${menucmd} --title "${title}" --menu "${caption}" ${height} ${width} ${menuheight} "${menu_options[@]}" 3>&1 1>&2 2>&3)
  169. if [ $? == 0 ]; then
  170. eval "$resultvar=\"${OPTION}\""
  171. else
  172. eval "$resultvar="
  173. fi
  174. }
  175. # Menu selector
  176. fn_install_menu() {
  177. local resultvar=$1
  178. local selection=""
  179. title=$2
  180. caption=$3
  181. options=$4
  182. # Get menu command
  183. for menucmd in whiptail dialog bash; do
  184. if [ -x $(which ${menucmd}) ]; then
  185. menucmd=$(which ${menucmd})
  186. break
  187. fi
  188. done
  189. case "$(basename ${menucmd})" in
  190. whiptail|dialog)
  191. fn_install_menu_whiptail "${menucmd}" selection "${title}" "${caption}" "${options}" 40 80 30;;
  192. *)
  193. fn_install_menu_bash selection "${title}" "${caption}" "${options}";;
  194. esac
  195. eval "$resultvar=\"${selection}\""
  196. }
  197. # Gets server info from serverlist.csv and puts in to array
  198. fn_server_info(){
  199. IFS=","
  200. server_info_array=($(grep -aw "${userinput}" "${serverlist}"))
  201. shortname="${server_info_array[0]}" # csgo
  202. gameservername="${server_info_array[1]}" # csgoserver
  203. gamename="${server_info_array[2]}" # Counter Strike: Global Offensive
  204. }
  205. fn_install_getopt(){
  206. userinput="empty"
  207. echo "Usage: $0 [option]"
  208. echo -e ""
  209. echo "Installer - Linux Game Server Managers - Version ${version}"
  210. echo "https://gameservermanagers.com"
  211. echo -e ""
  212. echo -e "Commands"
  213. echo -e "install |Select server to install."
  214. echo -e "servername |e.g $0 csgoserver. Enter the required servername will install it."
  215. echo -e "list |List all servers available for install."
  216. exit
  217. }
  218. fn_install_file(){
  219. local_filename="${gameservername}"
  220. if [ -e "${local_filename}" ]; then
  221. i=2
  222. while [ -e "${local_filename}-${i}" ] ; do
  223. let i++
  224. done
  225. local_filename="${local_filename}-${i}"
  226. fi
  227. cp -R "${selfname}" "${local_filename}"
  228. sed -i -e "s/shortname=\"core\"/shortname=\"${shortname}\"/g" "${local_filename}"
  229. sed -i -e "s/gameservername=\"core\"/gameservername=\"${gameservername}\"/g" "${local_filename}"
  230. echo "Installed ${gamename} server as ${local_filename}"
  231. echo ""
  232. if [ ! -d "${serverfiles}" ]; then
  233. echo "./${local_filename} install"
  234. else
  235. echo "Remember to check server ports"
  236. echo "./${local_filename} details"
  237. fi
  238. echo ""
  239. exit
  240. }
  241. # Prevent from running this script as root.
  242. if [ "$(whoami)" == "root" ]; then
  243. if [ ! -f "${functionsdir}/core_functions.sh" ]||[ ! -f "${functionsdir}/check_root.sh" ]||[ ! -f "${functionsdir}/core_messages.sh" ]; then
  244. echo "[ FAIL ] Do NOT run this script as root!"
  245. exit 1
  246. else
  247. core_functions.sh
  248. check_root.sh
  249. fi
  250. fi
  251. # LinuxGSM installer mode
  252. if [ "${shortname}" == "core" ]; then
  253. userinput=$1
  254. datadir="${tmpdir}/data"
  255. serverlist="${datadir}/serverlist.csv"
  256. # Download the serverlist. This is the complete list of all supported servers.
  257. if [ -f "${serverlist}" ]; then
  258. rm "${serverlist}"
  259. fi
  260. fn_bootstrap_fetch_file_github "lgsm/data" "serverlist.csv" "${datadir}" "serverlist.csv" "nochmodx" "norun" "noforcedl" "nomd5"
  261. if [ ! -f "${serverlist}" ]; then
  262. echo "[ FAIL ] serverlist.csv could not be loaded."
  263. exit 1
  264. fi
  265. if [ "${userinput}" == "list" ]; then
  266. {
  267. awk -F "," '{print $2 "\t" $3}' "${serverlist}"
  268. } | column -s $'\t' -t | more
  269. exit
  270. elif [ "${userinput}" == "install" ]||[ "${userinput}" == "i" ]; then
  271. fn_install_menu result "LinuxGSM" "Select game to install" "${serverlist}"
  272. userinput="${result}"
  273. fn_server_info
  274. if [ "${result}" == "${gameservername}" ]; then
  275. fn_install_file
  276. elif [ "${result}" == "" ]; then
  277. echo "Install canceled"
  278. else
  279. echo "[ FAIL ] menu result does not match gameservername"
  280. echo "result: ${result}"
  281. echo "gameservername: ${gameservername}"
  282. fi
  283. elif [ -n "${userinput}" ]; then
  284. fn_server_info
  285. if [ "${userinput}" == "${gameservername}" ]; then
  286. fn_install_file
  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. getopt=$1
  347. core_getopt.sh
  348. fi