linuxgsm.sh 11 KB

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