Advertisement
Guest User

Untitled

a guest
Nov 9th, 2019
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.99 KB | None | 0 0
  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.  
  10. # DO NOT EDIT THIS FILE
  11. # LinuxGSM configuration is no longer edited here
  12. # To update your LinuxGSM config go to:
  13. # lgsm/config-lgsm
  14. # https://docs.linuxgsm.com/configuration/linuxgsm-config
  15.  
  16. # Debugging
  17. if [ -f ".dev-debug" ]; then
  18. exec 5>dev-debug.log
  19. BASH_XTRACEFD="5"
  20. set -x
  21. fi
  22.  
  23. version="v19.11.0"
  24. shortname="rust"
  25. gameservername="rustserver"
  26. rootdir="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
  27. selfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  28. servicename="${selfname}"
  29. lockselfname=".${servicename}.lock"
  30. lgsmdir="${rootdir}/lgsm"
  31. logdir="${rootdir}/log"
  32. lgsmlogdir="${logdir}/lgsm"
  33. steamcmddir="${rootdir}/steamcmd"
  34. serverfiles="${rootdir}/serverfiles"
  35. functionsdir="${lgsmdir}/functions"
  36. libdir="${lgsmdir}/lib"
  37. tmpdir="${lgsmdir}/tmp"
  38. datadir="${lgsmdir}/data"
  39. serverlist="${datadir}/serverlist.csv"
  40. serverlistmenu="${datadir}/serverlistmenu.csv"
  41. configdir="${lgsmdir}/config-lgsm"
  42. configdirserver="${configdir}/${gameservername}"
  43. configdirdefault="${lgsmdir}/config-default"
  44. userinput="${1}"
  45.  
  46. ## GitHub Branch Select
  47. # Allows for the use of different function files
  48. # from a different repo and/or branch.
  49. githubuser="GameServerManagers"
  50. githubrepo="LinuxGSM"
  51. githubbranch="master"
  52.  
  53. # Core function that is required first.
  54. core_functions.sh(){
  55. functionfile="${FUNCNAME}"
  56. fn_bootstrap_fetch_file_github "lgsm/functions" "core_functions.sh" "${functionsdir}" "chmodx" "run" "noforcedl" "nomd5"
  57. }
  58.  
  59. # Bootstrap
  60. # Fetches the core functions required before passed off to core_dl.sh.
  61. fn_bootstrap_fetch_file(){
  62. remote_fileurl="${1}"
  63. local_filedir="${2}"
  64. local_filename="${3}"
  65. chmodx="${4:-0}"
  66. run="${5:-0}"
  67. forcedl="${6:-0}"
  68. md5="${7:-0}"
  69. # Download file if missing or download forced.
  70. if [ ! -f "${local_filedir}/${local_filename}" ]||[ "${forcedl}" == "forcedl" ]; then
  71. if [ ! -d "${local_filedir}" ]; then
  72. mkdir -p "${local_filedir}"
  73. fi
  74. # Defines curl path.
  75. curlpath=$(command -v curl 2>/dev/null)
  76.  
  77. # If curl exists download file.
  78. if [ "$(basename "${curlpath}")" == "curl" ]; then
  79. # Trap to remove part downloaded files.
  80. echo -en " fetching ${local_filename}...\c"
  81. curlcmd=$(${curlpath} -s --fail -L -o "${local_filedir}/${local_filename}" "${remote_fileurl}" 2>&1)
  82. local exitcode=$?
  83. if [ ${exitcode} -ne 0 ]; then
  84. echo -e "FAIL"
  85. if [ -f "${lgsmlog}" ]; then
  86. echo -e "${remote_fileurl}" | tee -a "${lgsmlog}"
  87. echo -e "${curlcmd}" | tee -a "${lgsmlog}"
  88. fi
  89. exit 1
  90. else
  91. echo -e "OK"
  92. fi
  93. else
  94. echo -e "[ FAIL ] Curl is not installed"
  95. exit 1
  96. fi
  97. # Make file chmodx if chmodx is set.
  98. if [ "${chmodx}" == "chmodx" ]; then
  99. chmod +x "${local_filedir}/${local_filename}"
  100. fi
  101. fi
  102.  
  103. if [ -f "${local_filedir}/${local_filename}" ]; then
  104. # Run file if run is set.
  105. if [ "${run}" == "run" ]; then
  106. source "${local_filedir}/${local_filename}"
  107. fi
  108. fi
  109. }
  110.  
  111. fn_bootstrap_fetch_file_github(){
  112. github_file_url_dir="${1}"
  113. github_file_url_name="${2}"
  114. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  115.  
  116. remote_fileurl="${githuburl}"
  117. local_filedir="${3}"
  118. local_filename="${github_file_url_name}"
  119. chmodx="${4:-0}"
  120. run="${5:-0}"
  121. forcedl="${6:-0}"
  122. md5="${7:-0}"
  123. # Passes vars to the file download function.
  124. fn_bootstrap_fetch_file "${remote_fileurl}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
  125. }
  126.  
  127. # Installer menu.
  128.  
  129. fn_print_center() {
  130. columns="$(tput cols)"
  131. line="$@"
  132. printf "%*s\n" $(( (${#line} + columns) / 2)) "${line}"
  133. }
  134.  
  135. fn_print_horizontal(){
  136. char="${1:-=}"
  137. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' "${char}"
  138. }
  139.  
  140. # Bash menu.
  141. fn_install_menu_bash() {
  142. local resultvar=$1
  143. title=$2
  144. caption=$3
  145. options=$4
  146. fn_print_horizontal
  147. fn_print_center "${title}"
  148. fn_print_center "${caption}"
  149. fn_print_horizontal
  150. menu_options=()
  151. while read -r line || [[ -n "${line}" ]]; do
  152. var=$(echo -e "${line}" | awk -F "," '{print $2 " - " $3}')
  153. menu_options+=( "${var}" )
  154. done < "${options}"
  155. menu_options+=( "Cancel" )
  156. select option in "${menu_options[@]}"; do
  157. if [ -n "${option}" ]&&[ "${option}" != "Cancel" ]; then
  158. eval "$resultvar=\"${option/%\ */}\""
  159. fi
  160. break
  161. done
  162. }
  163.  
  164. # Whiptail/Dialog menu.
  165. fn_install_menu_whiptail() {
  166. local menucmd=$1
  167. local resultvar=$2
  168. title=$3
  169. caption=$4
  170. options=$5
  171. height=${6:-40}
  172. width=${7:-80}
  173. menuheight=${8:-30}
  174. IFS=","
  175. menu_options=()
  176. while read -r line; do
  177. key=$(echo -e "${line}" | awk -F "," '{print $3}')
  178. val=$(echo -e "${line}" | awk -F "," '{print $2}')
  179. menu_options+=( "${val//\"}" "${key//\"}" )
  180. done < "${options}"
  181. OPTION=$(${menucmd} --title "${title}" --menu "${caption}" "${height}" "${width}" "${menuheight}" "${menu_options[@]}" 3>&1 1>&2 2>&3)
  182. if [ $? == 0 ]; then
  183. eval "$resultvar=\"${OPTION}\""
  184. else
  185. eval "$resultvar="
  186. fi
  187. }
  188.  
  189. # Menu selector.
  190. fn_install_menu() {
  191. local resultvar=$1
  192. local selection=""
  193. title=$2
  194. caption=$3
  195. options=$4
  196. # Get menu command.
  197. for menucmd in whiptail dialog bash; do
  198. if [ -x "$(command -v "${menucmd}")" ]; then
  199. menucmd=$(command -v "${menucmd}")
  200. break
  201. fi
  202. done
  203. case "$(basename "${menucmd}")" in
  204. whiptail|dialog)
  205. fn_install_menu_whiptail "${menucmd}" selection "${title}" "${caption}" "${options}" 40 80 30;;
  206. *)
  207. fn_install_menu_bash selection "${title}" "${caption}" "${options}";;
  208. esac
  209. eval "$resultvar=\"${selection}\""
  210. }
  211.  
  212. # Gets server info from serverlist.csv and puts in to array.
  213. fn_server_info(){
  214. IFS=","
  215. server_info_array=($(grep -aw "${userinput}" "${serverlist}"))
  216. shortname="${server_info_array[0]}" # csgo
  217. gameservername="${server_info_array[1]}" # csgoserver
  218. gamename="${server_info_array[2]}" # Counter Strike: Global Offensive
  219. }
  220.  
  221. fn_install_getopt(){
  222. userinput="empty"
  223. echo -e "Usage: $0 [option]"
  224. echo -e ""
  225. echo -e "Installer - Linux Game Server Managers - Version ${version}"
  226. echo -e "https://linuxgsm.com"
  227. echo -e ""
  228. echo -e "Commands"
  229. echo -e "install\t\t| Select server to install."
  230. echo -e "servername\t| Enter name of game server to install. e.g $0 csgoserver."
  231. echo -e "list\t\t| List all servers available for install."
  232. exit
  233. }
  234.  
  235. fn_install_file(){
  236. local_filename="${gameservername}"
  237. if [ -e "${local_filename}" ]; then
  238. i=2
  239. while [ -e "${local_filename}-${i}" ] ; do
  240. let i++
  241. done
  242. local_filename="${local_filename}-${i}"
  243. fi
  244. cp -R "${selfname}" "${local_filename}"
  245. sed -i -e "s/shortname=\"core\"/shortname=\"${shortname}\"/g" "${local_filename}"
  246. sed -i -e "s/gameservername=\"core\"/gameservername=\"${gameservername}\"/g" "${local_filename}"
  247. echo -e "Installed ${gamename} server as ${local_filename}"
  248. echo -e ""
  249. if [ ! -d "${serverfiles}" ]; then
  250. echo -e "./${local_filename} install"
  251. else
  252. echo -e "Remember to check server ports"
  253. echo -e "./${local_filename} details"
  254. fi
  255. echo -e ""
  256. exit
  257. }
  258.  
  259. # Prevent LinuxGSM from running as root. Except if doing a dependency install.
  260. if [ "$(whoami)" == "root" ]; then
  261. if [ "${userinput}" == "install" ]||[ "${userinput}" == "auto-install" ]||[ "${userinput}" == "i" ]||[ "${userinput}" == "ai" ]; then
  262. if [ "${shortname}" == "core" ]; then
  263. echo -e "[ FAIL ] Do NOT run this script as root!"
  264. exit 1
  265. fi
  266. elif [ ! -f "${functionsdir}/core_functions.sh" ]||[ ! -f "${functionsdir}/check_root.sh" ]||[ ! -f "${functionsdir}/core_messages.sh" ]; then
  267. echo -e "[ FAIL ] Do NOT run this script as root!"
  268. exit 1
  269. else
  270. core_functions.sh
  271. check_root.sh
  272. fi
  273. fi
  274.  
  275. # LinuxGSM installer mode.
  276. if [ "${shortname}" == "core" ]; then
  277. # Download the latest serverlist. This is the complete list of all supported servers.
  278. fn_bootstrap_fetch_file_github "lgsm/data" "serverlist.csv" "${datadir}" "nochmodx" "norun" "forcedl" "nomd5"
  279. if [ ! -f "${serverlist}" ]; then
  280. echo -e "[ FAIL ] serverlist.csv could not be loaded."
  281. exit 1
  282. fi
  283.  
  284. if [ "${userinput}" == "list" ]||[ "${userinput}" == "l" ]; then
  285. {
  286. tail -n +2 "${serverlist}" | awk -F "," '{print $2 "\t" $3}'
  287. } | column -s $'\t' -t | more
  288. exit
  289. elif [ "${userinput}" == "install" ]||[ "${userinput}" == "i" ]; then
  290. tail -n +2 "${serverlist}" | awk -F "," '{print $1 "," $2 "," $3}' > "${serverlistmenu}"
  291. fn_install_menu result "LinuxGSM" "Select game server to install." "${serverlistmenu}"
  292. userinput="${result}"
  293. fn_server_info
  294. if [ "${result}" == "${gameservername}" ]; then
  295. fn_install_file
  296. elif [ "${result}" == "" ]; then
  297. echo -e "Install canceled"
  298. else
  299. echo -e "[ FAIL ] menu result does not match gameservername"
  300. echo -e "result: ${result}"
  301. echo -e "gameservername: ${gameservername}"
  302. fi
  303. elif [ -n "${userinput}" ]; then
  304. fn_server_info
  305. if [ "${userinput}" == "${gameservername}" ]||[ "${userinput}" == "${gamename}" ]||[ "${userinput}" == "${shortname}" ]; then
  306. fn_install_file
  307. else
  308. echo -e "[ FAIL ] unknown game server"
  309. fi
  310. else
  311. fn_install_getopt
  312. fi
  313.  
  314. # LinuxGSM server mode.
  315. else
  316. core_functions.sh
  317. if [ "${shortname}" != "core-dep" ]; then
  318. # Load LinuxGSM configs.
  319. # These are required to get all the default variables for the specific server.
  320. # Load the default config. If missing download it. If changed reload it.
  321. if [ ! -f "${configdirdefault}/config-lgsm/${gameservername}/_default.cfg" ]; then
  322. mkdir -p "${configdirdefault}/config-lgsm/${gameservername}"
  323. fn_fetch_config "lgsm/config-default/config-lgsm/${gameservername}" "_default.cfg" "${configdirdefault}/config-lgsm/${gameservername}" "_default.cfg" "nochmodx" "norun" "noforcedl" "nomd5"
  324. fi
  325. if [ ! -f "${configdirserver}/_default.cfg" ]; then
  326. mkdir -p "${configdirserver}"
  327. echo -en " copying _default.cfg...\c"
  328. cp -R "${configdirdefault}/config-lgsm/${gameservername}/_default.cfg" "${configdirserver}/_default.cfg"
  329. exitcode=$?
  330. if [ ${exitcode} -ne 0 ]; then
  331. echo -e "FAIL"
  332. exit 1
  333. else
  334. echo -e "OK"
  335. fi
  336. else
  337. function_file_diff=$(diff -q "${configdirdefault}/config-lgsm/${gameservername}/_default.cfg" "${configdirserver}/_default.cfg")
  338. if [ "${function_file_diff}" != "" ]; then
  339. fn_print_warn_nl "_default.cfg has been altered. reloading config."
  340. echo -en " copying _default.cfg...\c"
  341. cp -R "${configdirdefault}/config-lgsm/${gameservername}/_default.cfg" "${configdirserver}/_default.cfg"
  342. exitcode=$?
  343. if [ ${exitcode} -ne 0 ]; then
  344. echo -e "FAIL"
  345. exit 1
  346. else
  347. echo -e "OK"
  348. fi
  349. fi
  350. fi
  351. source "${configdirserver}/_default.cfg"
  352. # Load the common.cfg config. If missing download it.
  353. if [ ! -f "${configdirserver}/common.cfg" ]; then
  354. fn_fetch_config "lgsm/config-default/config-lgsm" "common-template.cfg" "${configdirserver}" "common.cfg" "${chmodx}" "nochmodx" "norun" "noforcedl" "nomd5"
  355. source "${configdirserver}/common.cfg"
  356. else
  357. source "${configdirserver}/common.cfg"
  358. fi
  359. # Load the instance.cfg config. If missing download it.
  360. if [ ! -f "${configdirserver}/${servicename}.cfg" ]; then
  361. fn_fetch_config "lgsm/config-default/config-lgsm" "instance-template.cfg" "${configdirserver}" "${servicename}.cfg" "nochmodx" "norun" "noforcedl" "nomd5"
  362. source "${configdirserver}/${servicename}.cfg"
  363. else
  364. source "${configdirserver}/${servicename}.cfg"
  365. fi
  366.  
  367. # Load the linuxgsm.sh in to tmpdir. If missing download it.
  368. if [ ! -f "${tmpdir}/linuxgsm.sh" ]; then
  369. fn_fetch_file_github "" "linuxgsm.sh" "${tmpdir}" "chmodx" "norun" "noforcedl" "nomd5"
  370. fi
  371. fi
  372. # Enables ANSI colours from core_messages.sh. Can be disabled with ansi=off.
  373. fn_ansi_loader
  374. # Prevents running of core_exit.sh for Travis-CI.
  375. if [ "${travistest}" != "1" ]; then
  376. getopt=$1
  377. core_getopt.sh
  378. fi
  379. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement