lgsminstall.sh 11 KB

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