linuxgsm.sh 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #!/bin/bash
  2. # Project: Game Server Managers - LinuxGSM
  3. # Author: Daniel Gibbs
  4. # License: MIT License, Copyright (c) 2017 Daniel Gibbs
  5. # Purpose: Counter-Strike: Global Offensive | 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. # Debugging
  10. if [ -f ".dev-debug" ]; then
  11. exec 5>dev-debug.log
  12. BASH_XTRACEFD="5"
  13. set -x
  14. fi
  15. version="170305"
  16. rootdir="$(dirname $(readlink -f "${BASH_SOURCE[0]}"))"
  17. selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  18. servicename="${selfname}"
  19. shortname="core"
  20. servername="core"
  21. gamename="core"
  22. lockselfname=".${servicename}.lock"
  23. lgsmdir="${rootdir}/lgsm"
  24. functionsdir="${lgsmdir}/functions"
  25. libdir="${lgsmdir}/lib"
  26. tmpdir="${lgsmdir}/tmp"
  27. filesdir="${rootdir}/serverfiles"
  28. configdir="${lgsmdir}/config-lgsm"
  29. configdirserver="${configdir}/${servername}"
  30. configdirdefault="${lgsmdir}/config-default"
  31. ## Github Branch Select
  32. # Allows for the use of different function files
  33. # from a different repo and/or branch.
  34. githubuser="GameServerManagers"
  35. githubrepo="LinuxGSM"
  36. githubbranch="feature/config"
  37. # Core Functions that are required first
  38. core_dl.sh(){
  39. functionfile="${FUNCNAME}"
  40. fn_bootstrap_fetch_file
  41. }
  42. core_functions.sh(){
  43. functionfile="${FUNCNAME}"
  44. fn_bootstrap_fetch_file
  45. }
  46. # Bootstrap
  47. # Fetches the core functions required before passed off to core_dl.sh
  48. # Fetches core functions
  49. fn_bootstrap_fetch_file(){
  50. github_file_url_dir="lgsm/functions" # github dir containing the file
  51. github_file_url_name="${functionfile}" # name of the github file
  52. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  53. fileurl="${githuburl}"
  54. filedir="${functionsdir}"
  55. filename="${github_file_url_name}"
  56. executecmd="executecmd"
  57. run="run"
  58. force="noforce"
  59. md5="nomd5"
  60. # If the file is missing, then download
  61. if [ ! -f "${filedir}/${filename}" ]; then
  62. if [ ! -d "${filedir}" ]; then
  63. mkdir -p "${filedir}"
  64. fi
  65. # Check curl exists and use available path
  66. curlpaths="$(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 ${curlpaths}
  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 ${filename}...\c"
  77. curlcmd=$(${curlpath} -s --fail -L -o "${filedir}/${filename}" "${fileurl}" 2>&1)
  78. local exitcode=$?
  79. if [ ${exitcode} -ne 0 ]; then
  80. echo -e "\e[0;31mFAIL\e[0m\n"
  81. echo -e "${fileurl}" | tee -a "${scriptlog}"
  82. echo "${curlcmd}" | tee -a "${scriptlog}"
  83. exit 1
  84. else
  85. echo -e "\e[0;32mOK\e[0m"
  86. fi
  87. else
  88. echo "[ FAIL ] Curl is not installed"
  89. exit 1
  90. fi
  91. # make file executecmd if executecmd is set
  92. if [ "${executecmd}" == "executecmd" ]; then
  93. chmod +x "${filedir}/${filename}"
  94. fi
  95. fi
  96. if [ -f "${filedir}/${filename}" ]; then
  97. # run file if run is set
  98. if [ "${run}" == "run" ]; then
  99. source "${filedir}/${filename}"
  100. fi
  101. fi
  102. }
  103. # Installer menu
  104. fn_print_center() {
  105. columns="$(tput cols)"
  106. line="$@"
  107. printf "%*s\n" $(( (${#line} + columns) / 2)) "${line}"
  108. }
  109. fn_print_horizontal(){
  110. char="${1:-=}"
  111. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' "${char}"
  112. }
  113. # Bash Menu
  114. fn_install_menu_bash() {
  115. local resultvar=$1
  116. title=$2
  117. caption=$3
  118. options=$4
  119. fn_print_horizontal
  120. fn_print_center $title
  121. fn_print_center $caption
  122. fn_print_horizontal
  123. menu_options=()
  124. while read -r line || [[ -n "${line}" ]]; do
  125. var=$(echo "${line}" | awk -F "," '{print $2 " - " $3}')
  126. menu_options+=( "${var}" )
  127. done < $options
  128. menu_options+=( "Cancel" )
  129. select option in "${menu_options[@]}"; do
  130. if [ -n "${option}" ] && [ "${option}" != "Cancel" ]; then
  131. eval "$resultvar=\"${option/%\ */}\""
  132. fi
  133. break
  134. done
  135. }
  136. # Whiptail/Dialog Menu
  137. fn_install_menu_whiptail() {
  138. local menucmd=$1
  139. local resultvar=$2
  140. title=$3
  141. caption=$4
  142. options=$5
  143. height=${6:-40}
  144. width=${7:-80}
  145. menuheight=${8:-30}
  146. IFS=","
  147. menu_options=()
  148. while read -r line; do
  149. key=$(echo "${line}" | awk -F "," '{print $3}')
  150. val=$(echo "${line}" | awk -F "," '{print $2}')
  151. menu_options+=( ${val//\"} "${key//\"}" )
  152. done < $options
  153. OPTION=$(${menucmd} --title "${title}" --menu "${caption}" ${height} ${width} ${menuheight} "${menu_options[@]}" 3>&1 1>&2 2>&3)
  154. if [ $? = 0 ]; then
  155. eval "$resultvar=\"${OPTION}\""
  156. else
  157. eval "$resultvar="
  158. fi
  159. }
  160. # Menu selector
  161. fn_install_menu() {
  162. local resultvar=$1
  163. local selection=""
  164. title=$2
  165. caption=$3
  166. options=$4
  167. # Get menu command
  168. for menucmd in whiptail dialog bash; do
  169. if [ -x $(which ${menucmd}) ]; then
  170. menucmd=$(which ${menucmd})
  171. break
  172. fi
  173. done
  174. case "$(basename ${menucmd})" in
  175. whiptail|dialog)
  176. fn_install_menu_whiptail "${menucmd}" selection "${title}" "${caption}" "${options}" 40 80 30;;
  177. *)
  178. fn_install_menu_bash selection "${title}" "${caption}" "${options}";;
  179. esac
  180. eval "$resultvar=\"${selection}\""
  181. }
  182. # Gets server info from serverlist.csv and puts in to array
  183. fn_server_info(){
  184. IFS=","
  185. server_info_array=($(grep -a "${userinput}" "${serverlist}"))
  186. shortname="${server_info_array[0]}" # csgo
  187. servername="${server_info_array[1]}" # csgoserver
  188. gamename="${server_info_array[2]}" # Counter Strike: Global Offensive
  189. }
  190. fn_install_getopt(){
  191. userinput="empty"
  192. echo "Usage: $0 [option]"
  193. echo -e ""
  194. echo "Installer - Linux Game Server Managers - Version ${version}"
  195. echo "https://gameservermanagers.com"
  196. echo -e ""
  197. echo -e "Commands"
  198. echo -e "install |Select server to install."
  199. echo -e "servername |e.g $0 csgoserver. Enter the required servername will install it."
  200. echo -e "list |List all servers available for install."
  201. exit
  202. }
  203. fn_install_file(){
  204. filename="${servername}"
  205. if [ -e "${filename}" ]; then
  206. i=2
  207. while [ -e "${filename}-${i}" ] ; do
  208. let i++
  209. done
  210. filename="${filename}-${i}"
  211. fi
  212. cp -R "${selfname}" "${filename}"
  213. sed -i -e "s/shortname=\"core\"/shortname=\"${shortname}\"/g" "${filename}"
  214. sed -i -e "s/servername=\"core\"/servername=\"${servername}\"/g" "${filename}"
  215. sed -i -e "s/gamename=\"core\"/gamename=\"${gamename}\"/g" "${filename}"
  216. echo "Installed ${gamename} server as ${filename}"
  217. echo "./${filename} install"
  218. exit
  219. }
  220. # Prevent from running this script as root.
  221. if [ "$(whoami)" = "root" ]; then
  222. if [ ! -f "${functionsdir}/core_functions.sh" ]||[ ! -f "${functionsdir}/check_root.sh" ]||[ ! -f "${functionsdir}/core_messages.sh" ]; then
  223. echo "[ FAIL ] Do NOT run this script as root!"
  224. exit 1
  225. else
  226. core_functions.sh
  227. check_root.sh
  228. fi
  229. fi
  230. # LinuxGSM installer mode
  231. if [ "${shortname}" == "core" ]; then
  232. userinput=$1
  233. datadir="${lgsmdir}/data"
  234. serverlist="${datadir}/serverlist.csv"
  235. serverlist_tmp="${tmpdir}/data/serverlist.csv"
  236. # Download the serverlist. This is the complete list of all supported servers.
  237. # Download to tmp dir
  238. fn_boostrap_fetch_config "lgsm/data" "serverlist.csv" "${tmpdir}/data" "serverlist.csv" "noexecutecmd" "norun" "noforce" "nomd5"
  239. # if missing in lgsm dir copy it accross
  240. if [ ! -f "${serverlist}" ]; then
  241. mkdir -p "${datadir}"
  242. cp -R "${serverlist_tmp}" "${serverlist}"
  243. # check if the files are different.
  244. else
  245. file_diff=$(diff -q "${serverlist_tmp}" "${serverlist}")
  246. if [ "${file_diff}" != "" ]; then
  247. cp -Rf "${serverlist_tmp}" "${serverlist}"
  248. fi
  249. fi
  250. if [ ! -f "${serverlist}" ];then
  251. echo "[ FAIL ] serverlist.csv could not be loaded."
  252. exit 1
  253. fi
  254. if [ "${userinput}" == "list" ]; then
  255. {
  256. awk -F "," '{print $2 "\t" $3}' "${serverlist}"
  257. } | column -s $'\t' -t | more
  258. exit
  259. elif [ "${userinput}" == "install" ]; then
  260. fn_install_menu result "LinuxGSM" "Select game to install" "lgsm/data/serverlist.csv"
  261. userinput="${result}"
  262. fn_server_info
  263. if [ "${result}" == "${servername}" ]; then
  264. fn_install_file
  265. else
  266. echo "[ FAIL ] menu result does not match servername"
  267. fi
  268. elif [ -n "${userinput}" ]; then
  269. fn_server_info
  270. if [ "${userinput}" == "${servername}" ]; then
  271. fn_install_file
  272. fi
  273. else
  274. fn_install_getopt
  275. fi
  276. # LinuxGSM Server Mode
  277. else
  278. core_dl.sh
  279. core_functions.sh
  280. # Load LinuxGSM configs
  281. # These are required to get all the default variables for the specific server.
  282. # Load the default config. If missing download it. If changed reload it.
  283. if [ ! -f "${configdirdefault}/config-lgsm/${servername}/_default.cfg" ];then
  284. mkdir -p "${configdirdefault}/config-lgsm/${servername}"
  285. fn_fetch_config "lgsm/config-default/config-lgsm/${servername}" "_default.cfg" "${configdirdefault}/config-lgsm/${servername}" "_default.cfg" "noexecutecmd" "norun" "noforce" "nomd5"
  286. fi
  287. if [ ! -f "${configdirserver}/_default.cfg" ];then
  288. mkdir -p "${configdirserver}"
  289. cp -R "${configdirdefault}/config-lgsm/${servername}/_default.cfg" "${configdirserver}/_default.cfg"
  290. else
  291. function_file_diff=$(diff -q ${configdirdefault}/config-lgsm/${servername}/_default.cfg ${configdirserver}/_default.cfg)
  292. if [ "${function_file_diff}" != "" ]; then
  293. echo "_default.cfg has been altered. Reloading config."
  294. cp -R "${configdirdefault}/config-lgsm/${servername}/_default.cfg" "${configdirserver}/_default.cfg"
  295. fi
  296. fi
  297. source "${configdirserver}/_default.cfg"
  298. # Load the common.cfg config. If missing download it
  299. if [ ! -f "${configdirserver}/common.cfg" ];then
  300. fn_fetch_config "lgsm/config-default/config-lgsm" "common-template.cfg" "${configdirserver}" "common.cfg" "${executecmd}" "noexecutecmd" "norun" "noforce" "nomd5"
  301. source "${configdirserver}/common.cfg"
  302. else
  303. source "${configdirserver}/common.cfg"
  304. fi
  305. # Load the instance.cfg config. If missing download it
  306. if [ ! -f "${configdirserver}/${servicename}.cfg" ];then
  307. fn_fetch_config "lgsm/config-default/config-lgsm" "instance-template.cfg" "${configdirserver}" "${servicename}.cfg" "noexecutecmd" "norun" "noforce" "nomd5"
  308. source "${configdirserver}/${servicename}.cfg"
  309. else
  310. source "${configdirserver}/${servicename}.cfg"
  311. fi
  312. fi
  313. getopt=$1
  314. core_getopt.sh