check_deps.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #!/bin/bash
  2. # LinuxGSM check_deps.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: http://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Checks and installs missing dependencies.
  7. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. fn_install_mono_repo(){
  9. if [ "${autodepinstall}" == "0" ]; then
  10. fn_print_information_nl "Automatically adding Mono repository."
  11. fn_script_log_info "Automatically adding Mono repository."
  12. echo -en ".\r"
  13. sleep 1
  14. echo -en "..\r"
  15. sleep 1
  16. echo -en "...\r"
  17. sleep 1
  18. echo -en " \r"
  19. if [ "${distroid}" == "ubuntu" ]; then
  20. if [ "${distroversion}" == "20.04" ]; then
  21. cmd="sudo apt install gnupg ca-certificates;sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF;echo 'deb https://download.mono-project.com/repo/ubuntu stable-focal main' | sudo tee /etc/apt/sources.list.d/mono-official-stable.list;sudo apt update"
  22. elif [ "${distroversion}" == "18.04" ]; then
  23. cmd="sudo apt install gnupg ca-certificates;sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF;echo 'deb https://download.mono-project.com/repo/ubuntu stable-bionic main' | sudo tee /etc/apt/sources.list.d/mono-official-stable.list;sudo apt update"
  24. elif [ "${distroversion}" == "16.04" ]; then
  25. cmd="sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF;sudo apt install apt-transport-https ca-certificates;echo 'deb https://download.mono-project.com/repo/ubuntu stable-xenial main' | sudo tee /etc/apt/sources.list.d/mono-official-stable.list;sudo apt update"
  26. else
  27. monoautoinstall="1"
  28. fi
  29. elif [ "${distroid}" == "debian" ]; then
  30. if [ "${distroversion}" == "10" ]; then
  31. cmd="sudo apt install apt-transport-https dirmngr gnupg ca-certificates;sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF;echo 'deb https://download.mono-project.com/repo/debian stable-buster main' | sudo tee /etc/apt/sources.list.d/mono-official-stable.list;sudo apt update"
  32. elif [ "${distroversion}" == "9" ]; then
  33. cmd="sudo apt install apt-transport-https dirmngr gnupg ca-certificates;sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF;echo 'deb https://download.mono-project.com/repo/debian stable-stretch main' | sudo tee /etc/apt/sources.list.d/mono-official-stable.list;sudo apt update"
  34. else
  35. monoautoinstall="1"
  36. fi
  37. elif [ "${distroid}" == "centos" ]||[ "${distroid}" == "almalinux" ]||[ "${distroid}" == "rocky" ]; then
  38. if [ "${distroversion}" == "8" ]; then
  39. cmd="sudo rpmkeys --import 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF';su -c 'curl https://download.mono-project.com/repo/centos8-stable.repo | tee /etc/yum.repos.d/mono-centos8-stable.repo'"
  40. elif [ "${distroversion}" == "7" ]; then
  41. cmd="sudo rpmkeys --import 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF';su -c 'curl https://download.mono-project.com/repo/centos7-stable.repo | tee /etc/yum.repos.d/mono-centos7-stable.repo'"
  42. else
  43. monoautoinstall="1"
  44. fi
  45. elif [ "${distroid}" == "fedora" ]; then
  46. if [ "${distroversion}" -ge "29" ]; then
  47. cmd="sudo rpm --import 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF';su -c 'curl https://download.mono-project.com/repo/centos8-stable.repo | tee /etc/yum.repos.d/mono-centos8-stable.repo';dnf update"
  48. else
  49. cmd="sudo rpm --import 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF';su -c 'curl https://download.mono-project.com/repo/centos7-stable.repo | tee /etc/yum.repos.d/mono-centos7-stable.repo';dnf update"
  50. fi
  51. else
  52. monoautoinstall="1"
  53. fi
  54. # Run the mono repo install.
  55. eval "${cmd}"
  56. # Did Mono repo install correctly?
  57. if [ "${monoautoinstall}" != "1" ]; then
  58. if [ $? != 0 ]; then
  59. fn_print_failure_nl "Unable to install Mono repository."
  60. fn_script_log_fatal "Unable to install Mono repository."
  61. else
  62. fn_print_complete_nl "Installing Mono repository completed."
  63. fn_script_log_pass "Installing Mono repository completed."
  64. fi
  65. fi
  66. # Mono can not be auto installed with this distro.
  67. if [ "${monoautoinstall}" == "1" ]; then
  68. fn_print_warning_nl "Mono auto install not available for ${distroname}."
  69. echo -e "Follow instructions on Mono website to install."
  70. echo -e "https://www.mono-project.com/download/stable/#download-lin"
  71. fn_script_log_warn "Unable to install Mono repository. Mono auto install not available for ${distroname}."
  72. fi
  73. else
  74. fn_print_information_nl "Installing Mono repository."
  75. fn_print_warning_nl "$(whoami) does not have sudo access."
  76. echo -e "Follow instructions on Mono website to install."
  77. echo -e "https://www.mono-project.com/download/stable/#download-lin"
  78. fn_script_log_warn "Unable to install Mono repository. $(whoami) does not have sudo access."
  79. fi
  80. }
  81. fn_deps_email(){
  82. # Adds postfix to required dependencies if email alert is enabled.
  83. if [ "${emailalert}" == "on" ]; then
  84. if [ -f /usr/bin/mailx ]; then
  85. if [ -d /etc/exim4 ]; then
  86. array_deps_required+=( exim4 )
  87. elif [ -d /etc/sendmail ]; then
  88. array_deps_required+=( sendmail )
  89. elif [ "$(command -v dpkg-query 2>/dev/null)" ]; then
  90. array_deps_required+=( mailutils postfix )
  91. elif [ "$(command -v rpm 2>/dev/null)" ]; then
  92. array_deps_required+=( mailx postfix )
  93. fi
  94. else
  95. if [ "$(command -v dpkg-query 2>/dev/null)" ]; then
  96. array_deps_required+=( mailutils postfix )
  97. elif [ "$(command -v rpm 2>/dev/null)" ]; then
  98. array_deps_required+=( mailx postfix )
  99. fi
  100. fi
  101. fi
  102. }
  103. fn_install_missing_deps(){
  104. # If any dependencies are not installed.
  105. if [ "${#array_deps_missing[*]}" != "0" ]; then
  106. if [ "${commandname}" == "INSTALL" ]; then
  107. fn_print_warning_nl "Missing dependencies: ${red}${array_deps_missing[*]}${default}"
  108. fn_script_log_warn "Missing dependencies: ${array_deps_missing[*]}"
  109. else
  110. fn_print_dots "Missing dependencies"
  111. fn_print_warn "Missing dependencies: ${red}${array_deps_missing[*]}${default}"
  112. fn_script_log_warn "Missing dependencies: ${array_deps_missing[*]}"
  113. fi
  114. fn_sleep_time
  115. # Attempt automatic dependency installation
  116. if [ "${autoinstall}" == "1" ]; then
  117. sudo -n true > /dev/null 2>&1
  118. else
  119. sudo -v > /dev/null 2>&1
  120. fi
  121. autodepinstall="$?"
  122. if [ "${monostatus}" == "1" ]; then
  123. fn_install_mono_repo
  124. fi
  125. if [ "${commandname}" == "INSTALL" ]; then
  126. if [ "${autodepinstall}" == "0" ]; then
  127. fn_print_information_nl "$(whoami) has sudo access."
  128. fn_script_log_info "$(whoami) has sudo access."
  129. else
  130. fn_print_warning_nl "$(whoami) does not have sudo access. Manually install dependencies."
  131. fn_script_log_warn "$(whoami) does not have sudo access. Manually install dependencies."
  132. fi
  133. fi
  134. # Add sudo dpkg --add-architecture i386 if using i386 packages.
  135. if [ "$(command -v dpkg-query 2>/dev/null)" ]; then
  136. if printf '%s\n' "${array_deps_required[@]}" | grep -q -P 'i386'; then
  137. i386installcommand="sudo dpkg --add-architecture i386; "
  138. fi
  139. fi
  140. # If automatic dependency install is available
  141. if [ "${autodepinstall}" == "0" ]; then
  142. fn_print_information_nl "Automatically installing missing dependencies."
  143. fn_script_log_info "Automatically installing missing dependencies."
  144. echo -en ".\r"
  145. sleep 1
  146. echo -en "..\r"
  147. sleep 1
  148. echo -en "...\r"
  149. sleep 1
  150. echo -en " \r"
  151. if [ "$(command -v dpkg-query 2>/dev/null)" ]; then
  152. cmd="echo steamcmd steam/question select \"I AGREE\" | sudo debconf-set-selections; echo steamcmd steam/license note '' | sudo debconf-set-selections; ${i386installcommand}sudo apt-get update; sudo apt-get -y install ${array_deps_missing[*]}"
  153. eval "${cmd}"
  154. elif [ "$(command -v dnf 2>/dev/null)" ]; then
  155. cmd="sudo dnf -y install ${array_deps_missing[*]}"
  156. eval "${cmd}"
  157. elif [ "$(command -v yum 2>/dev/null)" ]; then
  158. cmd="sudo yum -y install ${array_deps_missing[*]}"
  159. eval "${cmd}"
  160. fi
  161. autodepinstall="$?"
  162. fi
  163. # If automatic dependency install is unavailable.
  164. if [ "${autodepinstall}" != "0" ]; then
  165. if [ "$(command -v dpkg-query 2>/dev/null)" ]; then
  166. echo -e "${i386installcommand}sudo apt update; sudo apt install ${array_deps_missing[*]}"
  167. elif [ "$(command -v dnf 2>/dev/null)" ]; then
  168. echo -e "sudo dnf install ${array_deps_missing[*]}"
  169. elif [ "$(command -v yum 2>/dev/null)" ]; then
  170. echo -e "sudo yum install ${array_deps_missing[*]}"
  171. fi
  172. fi
  173. if [ "${steamcmdfail}" ]; then
  174. if [ "${commandname}" == "INSTALL" ]; then
  175. fn_print_failure_nl "Missing dependencies required to run SteamCMD."
  176. fn_script_log_fatal "Missing dependencies required to run SteamCMD."
  177. core_exit.sh
  178. else
  179. fn_print_error_nl "Missing dependencies required to run SteamCMD."
  180. fn_script_log_error "Missing dependencies required to run SteamCMD."
  181. fi
  182. fi
  183. else
  184. if [ "${commandname}" == "INSTALL" ]; then
  185. fn_print_information_nl "Required dependencies already installed."
  186. fn_script_log_info "Required dependencies already installed."
  187. fi
  188. fi
  189. }
  190. fn_check_loop(){
  191. # Loop though required depenencies checking if they are installed.
  192. for deptocheck in ${array_deps_required[*]}; do
  193. fn_deps_detector
  194. done
  195. # user will be informed of any missing dependencies.
  196. fn_install_missing_deps
  197. }
  198. # Checks if dependency is installed or not.
  199. fn_deps_detector(){
  200. # Java: Added for users using Oracle JRE to bypass check.
  201. if [[ ${deptocheck} == "openjdk"* ]]||[[ ${deptocheck} == "java"* ]]; then
  202. # Is java already installed?
  203. if [ -n "${javaversion}" ]; then
  204. # Added for users using Oracle JRE to bypass check.
  205. depstatus=0
  206. deptocheck="${javaversion}"
  207. else
  208. depstatus=1
  209. fi
  210. # Mono: A Mono repo needs to be installed.
  211. elif [ "${deptocheck}" == "mono-complete" ]; then
  212. if [ -n "${monoversion}" ]&&[ "${monoversion}" -ge "5" ]; then
  213. # Mono >= 5.0.0 already installed.
  214. depstatus=0
  215. monostatus=0
  216. else
  217. # Mono not installed or installed Mono < 5.0.0.
  218. depstatus=1
  219. monostatus=1
  220. fi
  221. elif [ "$(command -v dpkg-query 2>/dev/null)" ]; then
  222. dpkg-query -W -f='${Status}' "${deptocheck}" 2>/dev/null | grep -q -P '^install ok installed'
  223. depstatus=$?
  224. elif [ "$(command -v dnf 2>/dev/null)" ]; then
  225. dnf list installed "${deptocheck}" > /dev/null 2>&1
  226. depstatus=$?
  227. elif [ "$(command -v rpm 2>/dev/null)" ]; then
  228. rpm -q "${deptocheck}" > /dev/null 2>&1
  229. depstatus=$?
  230. fi
  231. if [ "${depstatus}" == "0" ]; then
  232. # If dependency is found.
  233. missingdep=0
  234. if [ "${commandname}" == "INSTALL" ]; then
  235. echo -e "${green}${deptocheck}${default}"
  236. sleep 0.1
  237. fi
  238. else
  239. # If dependency is not found.
  240. missingdep=1
  241. if [ "${commandname}" == "INSTALL" ]; then
  242. echo -e "${red}${deptocheck}${default}"
  243. sleep 0.1
  244. fi
  245. # Define required dependencies for SteamCMD.
  246. if [ -n "${appid}" ]; then
  247. array_steamcmd_deps_required=("${depsteamcmd}")
  248. for steamcmddeptocheck in ${array_steamcmd_deps_required[*]}; do
  249. if [ "${deptocheck}" == "${steamcmddeptocheck}" ]; then
  250. steamcmdfail=1
  251. fi
  252. done
  253. fi
  254. fi
  255. unset depstatus
  256. # Missing dependencies are added to array_deps_missing.
  257. if [ "${missingdep}" == "1" ]; then
  258. array_deps_missing+=("${deptocheck}")
  259. fi
  260. }
  261. if [ "${commandname}" == "INSTALL" ]; then
  262. if [ "$(whoami)" == "root" ]; then
  263. echo -e ""
  264. echo -e "${lightyellow}Checking Dependencies as root${default}"
  265. echo -e "================================="
  266. fn_print_information_nl "Checking any missing dependencies for ${gamename} server only."
  267. fn_print_information_nl "This will NOT install a ${gamename} server."
  268. fn_sleep_time
  269. else
  270. echo -e ""
  271. echo -e "${lightyellow}Checking Dependencies${default}"
  272. echo -e "================================="
  273. fi
  274. fi
  275. info_distro.sh
  276. # some RHEL based distros use 8.4 instead of just 8.
  277. if [[ "${distroidlike}" == *"rhel"* ]]||[ "${distroid}" == "rhel" ]; then
  278. distroversion="${distroversionrh}"
  279. fi
  280. if [ ! -f "${tmpdir}/dependency-no-check.tmp" ]&&[ ! -f "${datadir}/${distroid}-${distroversion}.csv" ]; then
  281. # Check that the disto dependency csv file exists.
  282. fn_check_file_github "lgsm/data" "${distroid}-${distroversion}.csv"
  283. if [ -n "${checkflag}" ]&&[ "${checkflag}" == "0" ]; then
  284. fn_fetch_file_github "lgsm/data" "${distroid}-${distroversion}.csv" "lgsm/data" "chmodx" "norun" "noforce" "nohash"
  285. fi
  286. fi
  287. # If the file successfully downloaded run the dependency check.
  288. if [ -f "${datadir}/${distroid}-${distroversion}.csv" ]; then
  289. depall=$(awk -F, '$1=="all" {$1=""; print $0}' "${datadir}/${distroid}-${distroversion}.csv")
  290. depsteamcmd=$(awk -F, '$1=="steamcmd" {$1=""; print $0}' "${datadir}/${distroid}-${distroversion}.csv")
  291. depshortname=$(awk -v shortname="$shortname" -F, '$1==shortname {$1=""; print $0}' "${datadir}/${distroid}-${distroversion}.csv")
  292. # Generate array of missing deps.
  293. array_deps_missing=()
  294. array_deps_required=("${depall} ${depsteamcmd} ${depshortname}")
  295. fn_deps_email
  296. # Unique sort dependency array.
  297. IFS=" " read -r -a array_deps_required <<< "$(echo "${array_deps_required[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')"
  298. fn_check_loop
  299. # Warn the user that dependency checking is unavailable for their distro.
  300. elif [ "${commandname}" == "INSTALL" ]||[ -n "${checkflag}" ]&&[ "${checkflag}" != "0" ]; then
  301. fn_print_warning_nl "LinuxGSM dependency checking currently unavailable for ${distroname}."
  302. # Prevent future dependency checking if unavailable for the distro.
  303. echo "${version}" > "${tmpdir}/dependency-no-check.tmp"
  304. elif [ -f "${tmpdir}/dependency-no-check.tmp" ]; then
  305. # Allow LinuxGSM to try a dependency check if LinuxGSM has been recently updated.
  306. nocheckversion=$(cat "${tmpdir}/dependency-no-check.tmp")
  307. if [ "${version}" != "${nocheckversion}" ]; then
  308. rm -f "${tmpdir:?}/dependency-no-check.tmp"
  309. fi
  310. fi