linuxgsm.sh 10 KB

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