lgsminstall.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. }
  203. fn_install_getopt(){
  204. userinput="empty"
  205. echo "Usage: $0 [option]"
  206. echo -e ""
  207. echo "Installer - Linux Game Server Managers - Version ${version}"
  208. echo "https://gameservermanagers.com"
  209. echo -e ""
  210. echo -e "Commands"
  211. echo -e "install |Select server to install."
  212. echo -e "servername |e.g $0 csgoserver. Enter the required servername will install it."
  213. echo -e "list |List all servers available for install."
  214. exit
  215. }
  216. fn_install_file(){
  217. filename="${servername}"
  218. if [ -e "${filename}" ]; then
  219. i=2
  220. while [ -e "${filename}-${i}" ] ; do
  221. let i++
  222. done
  223. filename="${filename}-${i}"
  224. fi
  225. cp -R "${selfname}" "${filename}"
  226. sed -i -e "s/shortname=\"core\"/shortname=\"${shortname}\"/g" "${filename}"
  227. sed -i -e "s/servername=\"core\"/servername=\"${servername}\"/g" "${filename}"
  228. sed -i -e "s/gamename=\"core\"/gamename=\"${gamename}\"/g" "${filename}"
  229. echo "Installed ${gamename} server as ${filename}"
  230. echo "./${filename} install"
  231. exit
  232. }
  233. # Prevent from running this script as root.
  234. if [ "$(whoami)" = "root" ]; then
  235. if [ ! -f "${functionsdir}/core_functions.sh" ]||[ ! -f "${functionsdir}/check_root.sh" ]||[ ! -f "${functionsdir}/core_messages.sh" ]; then
  236. echo "[ FAIL ] Do NOT run this script as root!"
  237. exit 1
  238. else
  239. core_functions.sh
  240. check_root.sh
  241. fi
  242. fi
  243. # LinuxGSM installer mode
  244. if [ "${shortname}" == "core" ]; then
  245. userinput=$1
  246. datadir="${lgsmdir}/data"
  247. serverlist="${datadir}/serverlist.csv"
  248. serverlist_tmp="${tmpdir}/data/serverlist.csv"
  249. # Download the serverlist. This is the complete list of all supported servers.
  250. # Download to tmp dir
  251. fn_boostrap_fetch_config "lgsm/data" "serverlist.csv" "${tmpdir}/data" "serverlist.csv" "noexecutecmd" "norun" "noforce" "nomd5"
  252. # if missing in lgsm dir copy it accross
  253. if [ ! -f "${serverlist}" ]; then
  254. mkdir -p "${datadir}"
  255. cp -R "${serverlist_tmp}" "${serverlist}"
  256. # check if the files are different.
  257. else
  258. file_diff=$(diff -q "${serverlist_tmp}" "${serverlist}")
  259. if [ "${file_diff}" != "" ]; then
  260. cp -Rf "${serverlist_tmp}" "${serverlist}"
  261. fi
  262. fi
  263. if [ ! -f "${serverlist}" ];then
  264. echo "[ FAIL ] serverlist.csv could not be loaded."
  265. exit 1
  266. fi
  267. if [ "${userinput}" == "list" ]; then
  268. {
  269. awk -F "," '{print $2 "\t" $3}' "${serverlist}"
  270. } | column -s $'\t' -t | more
  271. exit
  272. elif [ "${userinput}" == "install" ]; then
  273. fn_install_menu result "LinuxGSM" "Select game to install" "lgsm/data/serverlist.csv"
  274. userinput="${result}"
  275. fn_server_info
  276. if [ "${result}" == "${servername}" ]; then
  277. fn_install_file
  278. else
  279. echo "[ FAIL ] menu result does not match servername"
  280. fi
  281. elif [ -n "${userinput}" ]; then
  282. fn_server_info
  283. if [ "${userinput}" == "${servername}" ]; then
  284. fn_install_file
  285. fi
  286. else
  287. fn_install_getopt
  288. fi
  289. # LinuxGSM Server Mode
  290. else
  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 "${lgsmdir}/default-configs/lgsm-config/${servername}/_default.cfg" ];then
  295. mkdir -p "${lgsmdir}/default-configs/lgsm-config/${servername}"
  296. fn_boostrap_fetch_config "lgsm/config/${servername}" "_default.cfg" "${lgsmdir}/default-configs/lgsm-config/${servername}" "_default.cfg" "noexecutecmd" "norun" "noforce" "nomd5"
  297. fi
  298. if [ ! -f "${gameconfigdir}/_default.cfg" ];then
  299. cp -R "${lgsmdir}/default-configs/lgsm-config/${servername}/_default.cfg" "${gameconfigdir}/_default.cfg"
  300. else
  301. function_file_diff=$(diff -q ${lgsmdir}/default-configs/lgsm-config/${servername}/_default.cfg ${gameconfigdir}/_default.cfg)
  302. if [ "${function_file_diff}" != "" ]; then
  303. echo "config different onverwriting"
  304. cp -R "${lgsmdir}/default-configs/lgsm-config/${servername}/_default.cfg" "${gameconfigdir}/_default.cfg"
  305. fi
  306. source lgsm/config/${servername}/_default.cfg
  307. fi
  308. # Load the common.cfg config. If missing download it
  309. if [ ! -f "${gameconfigdir}/common.cfg" ];then
  310. fn_boostrap_fetch_config "lgsm/config" "common-template.cfg" "${lgsmdir}/config/${servername}" "common.cfg" "${executecmd}" "noexecutecmd" "norun" "noforce" "nomd5"
  311. source lgsm/config/${servername}/common.cfg
  312. else
  313. source lgsm/config/${servername}/common.cfg
  314. fi
  315. # Load the instance.cfg config. If missing download it
  316. if [ ! -f "${gameconfigdir}/${servicename}.cfg" ];then
  317. fn_boostrap_fetch_config "lgsm/config" "instance-template.cfg" "${lgsmdir}/config/${servername}" "${servicename}.cfg" "noexecutecmd" "norun" "noforce" "nomd5"
  318. source lgsm/config/${servername}/${servicename}.cfg
  319. else
  320. source lgsm/config/${servername}/${servicename}.cfg
  321. fi
  322. fi
  323. ########################
  324. ######## Script ########
  325. ###### Do not edit #####
  326. ########################
  327. core_dl.sh(){
  328. # Functions are defined in core_functions.sh.
  329. functionfile="${FUNCNAME}"
  330. fn_boostrap_fetch_function
  331. }
  332. core_functions.sh(){
  333. # Functions are defined in core_functions.sh.
  334. functionfile="${FUNCNAME}"
  335. fn_boostrap_fetch_function
  336. }
  337. core_dl.sh
  338. core_functions.sh
  339. getopt=$1
  340. core_getopt.sh