lgsminstall.sh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. lgsmdir="${rootdir}/lgsm"
  19. tmpdir="${lgsmdir}/tmp"
  20. servicename="${selfname}"
  21. shortname="core"
  22. shortname="core"
  23. shortname="core"
  24. configdir="${lgsmdir}/config"
  25. gameconfigdir="${configdir}/${servername}"
  26. ## Github Branch Select
  27. # Allows for the use of different function files
  28. # from a different repo and/or branch.
  29. githubuser="GameServerManagers"
  30. githubrepo="LinuxGSM"
  31. githubbranch="feature/config"
  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. # LinuxGSM installer
  39. if [ "${shortname}" == "core" ]; then
  40. userinput=$1
  41. if [ -z "${userinput}" ]; then
  42. userinput="empty"
  43. fi
  44. serverslist=$(grep "${userinput}" <(curl -s "https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/serverlist.csv")|awk -F "," '{print $2}')
  45. echo "USERINPUT: $userinput"
  46. echo "SERVERLIST: $serverslist"
  47. if [ "${userinput}" == "${serverslist}" ]; then
  48. echo "installing"
  49. shortname=$(grep ${userinput} <(curl -s "https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/serverlist.csv")|awk -F "," '{print $1}')
  50. servername=$(grep ${userinput} <(curl -s "https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/serverlist.csv")|awk -F "," '{print $2}')
  51. gamename=$(grep ${userinput} <(curl -s "https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/serverlist.csv")|awk -F "," '{print $3}')
  52. if [ -e "${servername}" ]; then
  53. i=2
  54. while [ -e "$servername-$i" ] ; do
  55. let i++
  56. done
  57. servername="${servername}-$i"
  58. fi
  59. cp "${selfname}" "${servername}"
  60. sed -i -e "s/shortname=\"core\"/shortname=\"${shortname}\"/g" "${servername}"
  61. sed -i -e "s/servername=\"core\"/servername=\"${servername}\"/g" "${servername}"
  62. sed -i -e "s/gamename=\"core\"/gamename=\"${gamename}\"/g" "${gamename}"
  63. exit
  64. elif [ "${userinput}" == "list" ]; then
  65. {
  66. awk -F "," '{print $2 "\t" $3}' <(curl -s "https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/serverlist.csv")
  67. } | column -s $'\t' -t
  68. exit
  69. else
  70. echo "Usage: ./${selfname} list"
  71. echo "For a complete list of available servers"
  72. echo ""
  73. echo "Usage: ./${selfname} [servername]"
  74. echo "To install a server"
  75. exit
  76. fi
  77. fi
  78. # Bootstrap
  79. # Fetches bootstrap files (configs and core functions)
  80. fn_boostrap_fetch_file(){
  81. fileurl="${1}"
  82. filedir="${2}"
  83. filename="${3}"
  84. executecmd="${4:-0}"
  85. run="${5:-0}"
  86. force="${6:-0}"
  87. # If the file is missing, then download
  88. if [ ! -f "${filedir}/${filename}" ]; then
  89. if [ ! -d "${filedir}" ]; then
  90. mkdir -p "${filedir}"
  91. fi
  92. # Check curl exists and use available path
  93. curlpaths="$(command -v curl 2>/dev/null) $(which curl >/dev/null 2>&1) /usr/bin/curl /bin/curl /usr/sbin/curl /sbin/curl)"
  94. for curlcmd in ${curlpaths}
  95. do
  96. if [ -x "${curlcmd}" ]; then
  97. break
  98. fi
  99. done
  100. # If curl exists download file
  101. if [ "$(basename ${curlcmd})" == "curl" ]; then
  102. # trap to remove part downloaded files
  103. echo -ne " fetching ${filename}...\c"
  104. curlcmd=$(${curlcmd} -s --fail -L -o "${filedir}/${filename}" "${fileurl}" 2>&1)
  105. local exitcode=$?
  106. if [ ${exitcode} -ne 0 ]; then
  107. echo -e "\e[0;31mFAIL\e[0m\n"
  108. echo -e "${fileurl}" | tee -a "${scriptlog}"
  109. echo "${curlcmd}" | tee -a "${scriptlog}"
  110. exit 1
  111. else
  112. echo -e "\e[0;32mOK\e[0m"
  113. fi
  114. else
  115. echo -e "\e[0;31mFAIL\e[0m\n"
  116. echo "Curl is not installed!"
  117. echo -e ""
  118. exit 1
  119. fi
  120. # make file executecmd if executecmd is set
  121. if [ "${executecmd}" == "executecmd" ]; then
  122. chmod +x "${filedir}/${filename}"
  123. fi
  124. fi
  125. if [ -f "${filedir}/${filename}" ]; then
  126. # run file if run is set
  127. if [ "${run}" == "run" ]; then
  128. source "${filedir}/${filename}"
  129. fi
  130. fi
  131. }
  132. fn_boostrap_fetch_function(){
  133. github_file_url_dir="lgsm/functions" # github dir containing the file
  134. github_file_url_name="${functionfile}" # name of the github file
  135. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  136. fileurl="${githuburl}"
  137. filedir="${functionsdir}"
  138. filename="${github_file_url_name}"
  139. executecmd="executecmd"
  140. run="run"
  141. force="noforce"
  142. md5="nomd5"
  143. fn_boostrap_fetch_file "${fileurl}" "${filedir}" "${filename}" "${executecmd}" "${run}" "${force}" "${md5}"
  144. }
  145. fn_boostrap_fetch_config(){
  146. github_file_url_dir="${1}" # github dir containing the file
  147. github_file_url_name="${2}" # name of the github file
  148. githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  149. fileurl="${githuburl}"
  150. filedir="${3}"
  151. filename="${4}"
  152. executecmd="noexecutecmd"
  153. run="norun"
  154. force="noforce"
  155. md5="nomd5"
  156. fn_boostrap_fetch_file "${fileurl}" "${filedir}" "${filename}" "${executecmd}" "${run}" "${force}" "${md5}"
  157. }
  158. # Load the default config. If missing download it. If changed reload it.
  159. if [ ! -f "${tmpdir}/config/${servername}/_default.cfg" ];then
  160. fn_boostrap_fetch_config "lgsm/config/${servername}" "_default.cfg" "${tmpdir}/config/${servername}" "_default.cfg" "noexecutecmd" "norun" "noforce" "nomd5"
  161. fi
  162. if [ ! -f "${gameconfigdir}/_default.cfg" ];then
  163. echo "hello"
  164. cp "${tmpdir}/config/${servername}/_default.cfg" "${gameconfigdir}/_default.cfg"
  165. else
  166. function_file_diff=$(diff -q ${tmpdir}/config/${servername}/_default.cfg ${gameconfigdir}/_default.cfg)
  167. if [ "${function_file_diff}" != "" ]; then
  168. echo "config different onverwriting"
  169. cp "${tmpdir}/config/${servername}/_default.cfg" "${gameconfigdir}/_default.cfg"
  170. fi
  171. source lgsm/config/${servername}/_default.cfg
  172. fi
  173. if [ ! -f "${gameconfigdir}/common.cfg" ];then
  174. fn_boostrap_fetch_config "lgsm/config" "common-template.cfg" "${lgsmdir}/config/${servername}" "common.cfg" "${executecmd}" "noexecutecmd" "norun" "noforce" "nomd5"
  175. source lgsm/config/${servername}/common.cfg
  176. else
  177. source lgsm/config/${servername}/common.cfg
  178. fi
  179. if [ ! -f "${gameconfigdir}/${servicename}.cfg" ];then
  180. fn_boostrap_fetch_config "lgsm/config" "instance-template.cfg" "${lgsmdir}/config/${servername}" "${servicename}.cfg" "noexecutecmd" "norun" "noforce" "nomd5"
  181. source lgsm/config/${servername}/${servicename}.cfg
  182. else
  183. source lgsm/config/${servername}/${servicename}.cfg
  184. fi
  185. ########################
  186. ######## Script ########
  187. ###### Do not edit #####
  188. ########################
  189. core_dl.sh(){
  190. # Functions are defined in core_functions.sh.
  191. functionfile="${FUNCNAME}"
  192. fn_boostrap_fetch_function
  193. }
  194. core_functions.sh(){
  195. # Functions are defined in core_functions.sh.
  196. functionfile="${FUNCNAME}"
  197. fn_boostrap_fetch_function
  198. }
  199. # Prevent from running this script as root.
  200. if [ "$(whoami)" = "root" ]; then
  201. if [ ! -f "${functionsdir}/core_functions.sh" ]||[ ! -f "${functionsdir}/check_root.sh" ]||[ ! -f "${functionsdir}/core_messages.sh" ]||[ ! -f "${functionsdir}/exit 1" ]; then
  202. echo "[ FAIL ] Do NOT run this script as root!"
  203. exit 1
  204. else
  205. core_functions.sh
  206. check_root.sh
  207. fi
  208. fi
  209. core_dl.sh
  210. core_functions.sh
  211. getopt=$1
  212. core_getopt.sh