linuxgsm.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. # Debugging
  11. if [ -f ".dev-debug" ]; then
  12. exec 5>dev-debug.log
  13. BASH_XTRACEFD="5"
  14. set -x
  15. fi
  16. version="170305"
  17. shortname="core"
  18. servername="core"
  19. rootdir="$(dirname $(readlink -f "${BASH_SOURCE[0]}"))"
  20. selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  21. servicename="${selfname}"
  22. lockselfname=".${servicename}.lock"
  23. lgsmdir="${rootdir}/lgsm"
  24. logdir="${rootdir}/log"
  25. steamcmddir="${rootdir}/steamcmd"
  26. serverfiles="${rootdir}/serverfiles"
  27. functionsdir="${lgsmdir}/functions"
  28. libdir="${lgsmdir}/lib"
  29. tmpdir="${lgsmdir}/tmp"
  30. configdir="${lgsmdir}/config-lgsm"
  31. configdirserver="${configdir}/${servername}"
  32. configdirdefault="${lgsmdir}/config-default"
  33. ## GitHub Branch Select
  34. # Allows for the use of different function files
  35. # from a different repo and/or branch.
  36. githubuser="GameServerManagers"
  37. githubrepo="LinuxGSM"
  38. githubbranch="feature/config"
  39. # Core Function that is required first
  40. core_functions.sh(){
  41. functionfile="${FUNCNAME}"
  42. fn_bootstrap_fetch_file_github "lgsm/functions" "core_functions.sh" "${functionsdir}" "chmodx" "run" "noforcedl" "nomd5"
  43. }
  44. # Bootstrap
  45. # Fetches the core functions required before passed off to core_dl.sh
  46. # Fetches core functions
  47. fn_bootstrap_fetch_file(){
  48. remote_fileurl="${1}"
  49. local_filedir="${2}"
  50. local_filename="${3}"
  51. chmodx="${4:-0}"
  52. run="${5:-0}"
  53. forcedl="${6:-0}"
  54. md5="${7:-0}"
  55. # If the file is missing, then download
  56. if [ ! -f "${local_filedir}/${local_filename}" ]; then
  57. if [ ! -d "${local_filedir}" ]; then
  58. mkdir -p "${local_filedir}"
  59. fi
  60. # Defines curl path
  61. 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)
  62. for curlpath in "${curl_paths_array}"
  63. do
  64. if [ -x "${curlpath}" ]; then
  65. break
  66. fi
  67. done
  68. # If curl exists download file
  69. if [ "$(basename ${curlpath})" == "curl" ]; then
  70. # trap to remove part downloaded files
  71. echo -ne " 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 "\e[0;31mFAIL\e[0m\n"
  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 "\e[0;32mOK\e[0m"
  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_remote_fileurl="${githuburl}"
  105. local_local_filedir="${3}"
  106. local_local_filename="${github_file_url_name}"
  107. chmodx="${4:-0}"
  108. run="${5:-0}"
  109. forcedldl="${6:-0}"
  110. md5="${7:-0}"
  111. # Passes vars to the file download function
  112. fn_bootstrap_fetch_file "${remote_remote_fileurl}" "${local_local_filedir}" "${local_local_filename}" "${chmodx}" "${run}" "${forcedldl}" "${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 $(which ${menucmd}) ]; then
  181. menucmd=$(which ${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 -a "${userinput}" "${serverlist}"))
  197. shortname="${server_info_array[0]}" # csgo
  198. servername="${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://gameservermanagers.com"
  207. echo -e ""
  208. echo -e "Commands"
  209. echo -e "install |Select server to install."
  210. echo -e "servername |e.g $0 csgoserver. Enter the required servername will install it."
  211. echo -e "list |List all servers available for install."
  212. exit
  213. }
  214. fn_install_file(){
  215. local_filename="${servername}"
  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/servername=\"core\"/servername=\"${servername}\"/g" "${local_filename}"
  226. echo "Installed ${gamename} server as ${local_filename}"
  227. echo "./${local_filename} install"
  228. exit
  229. }
  230. # Prevent from running this script as root.
  231. if [ "$(whoami)" == "root" ]; then
  232. if [ ! -f "${functionsdir}/core_functions.sh" ]||[ ! -f "${functionsdir}/check_root.sh" ]||[ ! -f "${functionsdir}/core_messages.sh" ]; then
  233. echo "[ FAIL ] Do NOT run this script as root!"
  234. exit 1
  235. else
  236. core_functions.sh
  237. check_root.sh
  238. fi
  239. fi
  240. # LinuxGSM installer mode
  241. if [ "${shortname}" == "core" ]; then
  242. userinput=$1
  243. datadir="${lgsmdir}/data"
  244. serverlist="${datadir}/serverlist.csv"
  245. serverlist_tmp="${tmpdir}/data/serverlist.csv"
  246. # Download the serverlist. This is the complete list of all supported servers.
  247. # Download to tmp dir
  248. fn_bootstrap_fetch_file_github "lgsm/data" "serverlist.csv" "${tmpdir}/data" "serverlist.csv" "nochmodx" "norun" "noforcedl" "nomd5"
  249. # if missing in lgsm dir copy it across
  250. if [ ! -f "${serverlist}" ]; then
  251. mkdir -p "${datadir}"
  252. cp -R "${serverlist_tmp}" "${serverlist}"
  253. # check if the files are different.
  254. else
  255. file_diff=$(diff -q "${serverlist_tmp}" "${serverlist}")
  256. if [ "${file_diff}" != "" ]; then
  257. cp -Rf "${serverlist_tmp}" "${serverlist}"
  258. fi
  259. fi
  260. if [ ! -f "${serverlist}" ]; then
  261. echo "[ FAIL ] serverlist.csv could not be loaded."
  262. exit 1
  263. fi
  264. if [ "${userinput}" == "list" ]; then
  265. {
  266. awk -F "," '{print $2 "\t" $3}' "${serverlist}"
  267. } | column -s $'\t' -t | more
  268. exit
  269. elif [ "${userinput}" == "install" ]; then
  270. fn_install_menu result "LinuxGSM" "Select game to install" "lgsm/data/serverlist.csv"
  271. userinput="${result}"
  272. fn_server_info
  273. if [ "${result}" == "${servername}" ]; then
  274. fn_install_file
  275. elif [ "${result}" == "" ]; then
  276. echo "Install canceled"
  277. else
  278. echo "[ FAIL ] menu result does not match servername"
  279. fi
  280. elif [ -n "${userinput}" ]; then
  281. fn_server_info
  282. if [ "${userinput}" == "${servername}" ]; then
  283. fn_install_file
  284. fi
  285. else
  286. fn_install_getopt
  287. fi
  288. # LinuxGSM Server Mode
  289. else
  290. core_functions.sh
  291. # Load LinuxGSM configs
  292. # These are required to get all the default variables for the specific server.
  293. # Load the default config. If missing download it. If changed reload it.
  294. if [ ! -f "${configdirdefault}/config-lgsm/${servername}/_default.cfg" ]; then
  295. mkdir -p "${configdirdefault}/config-lgsm/${servername}"
  296. fn_fetch_config "lgsm/config-default/config-lgsm/${servername}" "_default.cfg" "${configdirdefault}/config-lgsm/${servername}" "_default.cfg" "nochmodx" "norun" "noforcedl" "nomd5"
  297. fi
  298. if [ ! -f "${configdirserver}/_default.cfg" ]; then
  299. mkdir -p "${configdirserver}"
  300. echo -ne " copying _default.cfg...\c"
  301. cp -R "${configdirdefault}/config-lgsm/${servername}/_default.cfg" "${configdirserver}/_default.cfg"
  302. exitcode=$?
  303. if [ ${exitcode} -ne 0 ]; then
  304. echo -e "\e[0;31mFAIL\e[0m\n"
  305. exit 1
  306. else
  307. echo -e "\e[0;32mOK\e[0m"
  308. fi
  309. else
  310. function_file_diff=$(diff -q ${configdirdefault}/config-lgsm/${servername}/_default.cfg ${configdirserver}/_default.cfg)
  311. if [ "${function_file_diff}" != "" ]; then
  312. fn_print_warn_nl "_default.cfg has been altered. reloading config."
  313. echo -ne " copying _default.cfg...\c"
  314. cp -R "${configdirdefault}/config-lgsm/${servername}/_default.cfg" "${configdirserver}/_default.cfg"
  315. exitcode=$?
  316. if [ ${exitcode} -ne 0 ]; then
  317. echo -e "\e[0;31mFAIL\e[0m\n"
  318. exit 1
  319. else
  320. echo -e "\e[0;32mOK\e[0m"
  321. fi
  322. fi
  323. fi
  324. source "${configdirserver}/_default.cfg"
  325. # Load the common.cfg config. If missing download it
  326. if [ ! -f "${configdirserver}/common.cfg" ]; then
  327. fn_fetch_config "lgsm/config-default/config-lgsm" "common-template.cfg" "${configdirserver}" "common.cfg" "${chmodx}" "nochmodx" "norun" "noforcedl" "nomd5"
  328. source "${configdirserver}/common.cfg"
  329. else
  330. source "${configdirserver}/common.cfg"
  331. fi
  332. # Load the instance.cfg config. If missing download it
  333. if [ ! -f "${configdirserver}/${servicename}.cfg" ]; then
  334. fn_fetch_config "lgsm/config-default/config-lgsm" "instance-template.cfg" "${configdirserver}" "${servicename}.cfg" "nochmodx" "norun" "noforcedl" "nomd5"
  335. source "${configdirserver}/${servicename}.cfg"
  336. else
  337. source "${configdirserver}/${servicename}.cfg"
  338. fi
  339. # Load the linuxgsm.sh in to tmpdir. If missing download it
  340. if [ ! -f "${tmpdir}/linuxgsm.sh" ]; then
  341. fn_fetch_file_github "" "linuxgsm.sh" "${tmpdir}" "chmodx" "norun" "noforcedl" "nomd5"
  342. fi
  343. getopt=$1
  344. core_getopt.sh
  345. fi