check_deps.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. moduleselfname="$(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}" == "22.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-jammy main' | sudo tee /etc/apt/sources.list.d/mono-official-stable.list;sudo apt update"
  22. elif [ "${distroversion}" == "20.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-focal main' | sudo tee /etc/apt/sources.list.d/mono-official-stable.list;sudo apt update"
  24. elif [ "${distroversion}" == "18.04" ]; then
  25. 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"
  26. elif [ "${distroversion}" == "16.04" ]; then
  27. 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"
  28. else
  29. monoautoinstall="1"
  30. fi
  31. elif [ "${distroid}" == "debian" ]; then
  32. if [ "${distroversion}" == "12" ]; 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-bookworm main' | sudo tee /etc/apt/sources.list.d/mono-official-stable.list;sudo apt update"
  34. elif [ "${distroversion}" == "11" ]; then
  35. 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-bullseye main' | sudo tee /etc/apt/sources.list.d/mono-official-stable.list;sudo apt update"
  36. elif [ "${distroversion}" == "10" ]; then
  37. 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"
  38. elif [ "${distroversion}" == "9" ]; then
  39. 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"
  40. else
  41. monoautoinstall="1"
  42. fi
  43. elif [ "${distroid}" == "centos" ] || [ "${distroid}" == "almalinux" ] || [ "${distroid}" == "rocky" ]; then
  44. if [ "${distroversion}" == "8" ]; then
  45. 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'"
  46. elif [ "${distroversion}" == "7" ]; then
  47. 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'"
  48. else
  49. monoautoinstall="1"
  50. fi
  51. elif [ "${distroid}" == "fedora" ]; then
  52. if [ "${distroversion}" -ge "29" ]; then
  53. 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"
  54. else
  55. 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"
  56. fi
  57. else
  58. monoautoinstall="1"
  59. fi
  60. # Run the mono repo install.
  61. eval "${cmd}"
  62. # Did Mono repo install correctly?
  63. if [ "${monoautoinstall}" != "1" ]; then
  64. if [ $? != 0 ]; then
  65. fn_print_failure_nl "Unable to install Mono repository."
  66. fn_script_log_fatal "Unable to install Mono repository."
  67. else
  68. fn_print_complete_nl "Installing Mono repository completed."
  69. fn_script_log_pass "Installing Mono repository completed."
  70. fi
  71. fi
  72. # Mono can not be auto installed with this distro.
  73. if [ "${monoautoinstall}" == "1" ]; then
  74. fn_print_warning_nl "Mono auto install not available for ${distroname}."
  75. echo -e "Follow instructions on Mono website to install."
  76. echo -e "https://www.mono-project.com/download/stable/#download-lin"
  77. fn_script_log_warn "Unable to install Mono repository. Mono auto install not available for ${distroname}."
  78. fi
  79. else
  80. fn_print_information_nl "Installing Mono repository."
  81. fn_print_warning_nl "$(whoami) does not have sudo access."
  82. echo -e "Follow instructions on Mono website to install."
  83. echo -e "https://www.mono-project.com/download/stable/#download-lin"
  84. fn_script_log_warn "Unable to install Mono repository. $(whoami) does not have sudo access."
  85. fi
  86. }
  87. fn_deps_email() {
  88. # Adds postfix to required dependencies if email alert is enabled.
  89. if [ "${emailalert}" == "on" ]; then
  90. if [ -f /usr/bin/mailx ]; then
  91. if [ -d /etc/exim4 ]; then
  92. array_deps_required+=(exim4)
  93. elif [ -d /etc/sendmail ]; then
  94. array_deps_required+=(sendmail)
  95. elif [ "$(command -v yum 2> /dev/null)" ] || [ "$(command -v dnf 2> /dev/null)" ]; then
  96. array_deps_required+=(mailx postfix)
  97. elif [ "$(command -v apt 2> /dev/null)" ]; then
  98. array_deps_required+=(mailutils postfix)
  99. fi
  100. else
  101. if [ "$(command -v yum 2> /dev/null)" ] || [ "$(command -v dnf 2> /dev/null)" ]; then
  102. array_deps_required+=(mailx postfix)
  103. elif [ "$(command -v apt 2> /dev/null)" ]; then
  104. array_deps_required+=(mailutils postfix)
  105. fi
  106. fi
  107. fi
  108. }
  109. fn_install_missing_deps() {
  110. # If any dependencies are not installed.
  111. if [ "${#array_deps_missing[*]}" != "0" ]; then
  112. if [ "${commandname}" == "INSTALL" ]; then
  113. fn_print_warning_nl "Missing dependencies: ${red}${array_deps_missing[*]}${default}"
  114. fn_script_log_warn "Missing dependencies: ${array_deps_missing[*]}"
  115. else
  116. fn_print_dots "Missing dependencies"
  117. fn_print_warn "Missing dependencies: ${red}${array_deps_missing[*]}${default}"
  118. fn_script_log_warn "Missing dependencies: ${array_deps_missing[*]}"
  119. fi
  120. fn_sleep_time
  121. # Attempt automatic dependency installation
  122. if [ "${autoinstall}" == "1" ]; then
  123. sudo -n true > /dev/null 2>&1
  124. else
  125. sudo -v > /dev/null 2>&1
  126. fi
  127. autodepinstall="$?"
  128. if [ "${monostatus}" == "1" ]; then
  129. fn_install_mono_repo
  130. fi
  131. if [ "${commandname}" == "INSTALL" ]; then
  132. if [ "${autodepinstall}" == "0" ]; then
  133. fn_print_information_nl "$(whoami) has sudo access."
  134. fn_script_log_info "$(whoami) has sudo access."
  135. else
  136. fn_print_warning_nl "$(whoami) does not have sudo access. Manually install dependencies."
  137. fn_script_log_warn "$(whoami) does not have sudo access. Manually install dependencies."
  138. fi
  139. fi
  140. # Add sudo dpkg --add-architecture i386 if using i386 packages.
  141. if [ "$(command -v apt 2> /dev/null)" ]; then
  142. if printf '%s\n' "${array_deps_required[@]}" | grep -q -P 'i386'; then
  143. i386installcommand="sudo dpkg --add-architecture i386; "
  144. fi
  145. fi
  146. # If automatic dependency install is available
  147. if [ "${autodepinstall}" == "0" ]; then
  148. fn_print_information_nl "Automatically installing missing dependencies."
  149. fn_script_log_info "Automatically installing missing dependencies."
  150. echo -en ".\r"
  151. sleep 1
  152. echo -en "..\r"
  153. sleep 1
  154. echo -en "...\r"
  155. sleep 1
  156. echo -en " \r"
  157. if [ "$(command -v apt 2> /dev/null)" ]; then
  158. 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[*]}"
  159. eval "${cmd}"
  160. elif [ "$(command -v dnf 2> /dev/null)" ]; then
  161. cmd="sudo dnf -y install ${array_deps_missing[*]}"
  162. eval "${cmd}"
  163. elif [ "$(command -v yum 2> /dev/null)" ]; then
  164. cmd="sudo yum -y install ${array_deps_missing[*]}"
  165. eval "${cmd}"
  166. fi
  167. autodepinstall="$?"
  168. # If auto install passes remove steamcmd install failure.
  169. if [ "${autodepinstall}" == "0" ]; then
  170. unset steamcmdfail
  171. fi
  172. fi
  173. # If automatic dependency install is unavailable.
  174. if [ "${autodepinstall}" != "0" ]; then
  175. if [ "$(command -v apt 2> /dev/null)" ]; then
  176. echo -e "${i386installcommand}sudo apt update; sudo apt install ${array_deps_missing[*]}"
  177. elif [ "$(command -v dnf 2> /dev/null)" ]; then
  178. echo -e "sudo dnf install ${array_deps_missing[*]}"
  179. elif [ "$(command -v yum 2> /dev/null)" ]; then
  180. echo -e "sudo yum install ${array_deps_missing[*]}"
  181. fi
  182. fi
  183. if [ "${steamcmdfail}" ]; then
  184. if [ "${commandname}" == "INSTALL" ]; then
  185. fn_print_failure_nl "Missing dependencies required to run SteamCMD."
  186. fn_script_log_fatal "Missing dependencies required to run SteamCMD."
  187. core_exit.sh
  188. else
  189. fn_print_error_nl "Missing dependencies required to run SteamCMD."
  190. fn_script_log_error "Missing dependencies required to run SteamCMD."
  191. fi
  192. fi
  193. else
  194. if [ "${commandname}" == "INSTALL" ]; then
  195. fn_print_information_nl "Required dependencies already installed."
  196. fn_script_log_info "Required dependencies already installed."
  197. fi
  198. fi
  199. }
  200. fn_check_loop() {
  201. # Loop though required depenencies checking if they are installed.
  202. for deptocheck in ${array_deps_required[*]}; do
  203. fn_deps_detector
  204. done
  205. # user will be informed of any missing dependencies.
  206. fn_install_missing_deps
  207. }
  208. # Checks if dependency is installed or not.
  209. fn_deps_detector() {
  210. ## Check.
  211. # SteamCMD: Will be removed from required array if no appid is present or non-free repo is not available.
  212. # This will cause SteamCMD to be installed using tar.
  213. if [ "${deptocheck}" == "libsdl2-2.0-0:i386" ] && [ -z "${appid}" ]; then
  214. array_deps_required=("${array_deps_required[@]/libsdl2-2.0-0:i386/}")
  215. steamcmdstatus=1
  216. elif [ "${deptocheck}" == "steamcmd" ] && [ -z "${appid}" ]; then
  217. array_deps_required=("${array_deps_required[@]/steamcmd/}")
  218. steamcmdstatus=1
  219. elif [ "${deptocheck}" == "steamcmd" ] && [ "${distroid}" == "debian" ] && ! grep -qE "^deb .*non-free" /etc/apt/sources.list; then
  220. array_deps_required=("${array_deps_required[@]/steamcmd/}")
  221. steamcmdstatus=1
  222. # Java: Added for users using Oracle JRE to bypass check.
  223. elif [[ ${deptocheck} == "openjdk"* ]] || [[ ${deptocheck} == "java"* ]]; then
  224. # Is java already installed?
  225. if [ -n "${javaversion}" ]; then
  226. # Added for users using Oracle JRE to bypass check.
  227. depstatus=0
  228. deptocheck="${javaversion}"
  229. else
  230. depstatus=1
  231. fi
  232. # Mono: A Mono repo needs to be installed.
  233. elif [ "${deptocheck}" == "mono-complete" ]; then
  234. if [ -n "${monoversion}" ] && [ "${monoversion}" -ge "5" ]; then
  235. # Mono >= 5.0.0 already installed.
  236. depstatus=0
  237. monostatus=0
  238. else
  239. # Mono not installed or installed Mono < 5.0.0.
  240. depstatus=1
  241. monostatus=1
  242. fi
  243. elif [ "$(command -v apt 2> /dev/null)" ]; then
  244. dpkg-query -W -f='${Status}' "${deptocheck}" 2> /dev/null | grep -q -P '^install ok installed'
  245. depstatus=$?
  246. elif [ "$(command -v dnf 2> /dev/null)" ]; then
  247. dnf list installed "${deptocheck}" > /dev/null 2>&1
  248. depstatus=$?
  249. elif [ "$(command -v rpm 2> /dev/null)" ]; then
  250. rpm -q "${deptocheck}" > /dev/null 2>&1
  251. depstatus=$?
  252. fi
  253. # Outcome of Check.
  254. if [ "${steamcmdstatus}" == "1" ]; then
  255. # If SteamCMD is not available in repo dont check for it.
  256. unset steamcmdstatus
  257. elif [ "${depstatus}" == "0" ]; then
  258. # If dependency is found.
  259. missingdep=0
  260. if [ "${commandname}" == "INSTALL" ]; then
  261. echo -e "${green}${deptocheck}${default}"
  262. sleep 0.1
  263. fi
  264. elif [ "${depstatus}" != "0" ]; then
  265. # If dependency is not found.
  266. missingdep=1
  267. if [ "${commandname}" == "INSTALL" ]; then
  268. echo -e "${red}${deptocheck}${default}"
  269. sleep 0.1
  270. fi
  271. # If SteamCMD requirements are not met install will fail.
  272. if [ -n "${appid}" ]; then
  273. for steamcmddeptocheck in ${array_deps_required_steamcmd[*]}; do
  274. if [ "${deptocheck}" != "steamcmd" ] && [ "${deptocheck}" == "${steamcmddeptocheck}" ]; then
  275. steamcmdfail=1
  276. fi
  277. done
  278. fi
  279. fi
  280. unset depstatus
  281. # Missing dependencies are added to array_deps_missing.
  282. if [ "${missingdep}" == "1" ]; then
  283. array_deps_missing+=("${deptocheck}")
  284. fi
  285. }
  286. if [ "${commandname}" == "INSTALL" ]; then
  287. if [ "$(whoami)" == "root" ]; then
  288. echo -e ""
  289. echo -e "${lightyellow}Checking Dependencies as root${default}"
  290. echo -e "================================="
  291. fn_print_information_nl "Checking any missing dependencies for ${gamename} server only."
  292. fn_print_information_nl "This will NOT install a ${gamename} server."
  293. fn_sleep_time
  294. else
  295. echo -e ""
  296. echo -e "${lightyellow}Checking Dependencies${default}"
  297. echo -e "================================="
  298. fi
  299. fi
  300. # Will warn user if their distro is no longer supported by the vendor.
  301. if [ -n "${distrosupport}" ]; then
  302. if [ "${distrosupport}" == "unsupported" ]; then
  303. fn_print_warning_nl "${distroname} is no longer supported by the vendor. Upgrading is recommended."
  304. fn_script_log_warn "${distroname} is no longer supported by the vendor. Upgrading is recommended."
  305. fi
  306. fi
  307. info_distro.sh
  308. if [ ! -f "${tmpdir}/dependency-no-check.tmp" ] && [ ! -f "${datadir}/${distroid}-${distroversioncsv}.csv" ]; then
  309. # Check that the distro dependency csv file exists.
  310. fn_check_file_github "lgsm/data" "${distroid}-${distroversioncsv}.csv"
  311. if [ -n "${checkflag}" ] && [ "${checkflag}" == "0" ]; then
  312. fn_fetch_file_github "lgsm/data" "${distroid}-${distroversioncsv}.csv" "lgsm/data" "chmodx" "norun" "noforce" "nohash"
  313. fi
  314. fi
  315. # If the file successfully downloaded run the dependency check.
  316. if [ -f "${datadir}/${distroid}-${distroversioncsv}.csv" ]; then
  317. depall=$(awk -F, '$1=="all" {$1=""; print $0}' "${datadir}/${distroid}-${distroversioncsv}.csv")
  318. depsteamcmd=$(awk -F, '$1=="steamcmd" {$1=""; print $0}' "${datadir}/${distroid}-${distroversioncsv}.csv")
  319. depshortname=$(awk -v shortname="${shortname}" -F, '$1==shortname {$1=""; print $0}' "${datadir}/${distroid}-${distroversioncsv}.csv")
  320. # Generate array of missing deps.
  321. array_deps_missing=()
  322. array_deps_required=("${depall} ${depsteamcmd} ${depshortname}")
  323. array_deps_required_steamcmd=("${depsteamcmd}")
  324. fn_deps_email
  325. # Unique sort dependency array.
  326. IFS=" " read -r -a array_deps_required <<< "$(echo "${array_deps_required[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')"
  327. fn_check_loop
  328. # Warn the user that dependency checking is unavailable for their distro.
  329. elif [ "${commandname}" == "INSTALL" ] || [ -n "${checkflag}" ] && [ "${checkflag}" != "0" ]; then
  330. fn_print_warning_nl "LinuxGSM dependency checking currently unavailable for ${distroname}."
  331. # Prevent future dependency checking if unavailable for the distro.
  332. echo "${version}" > "${tmpdir}/dependency-no-check.tmp"
  333. elif [ -f "${tmpdir}/dependency-no-check.tmp" ]; then
  334. # Allow LinuxGSM to try a dependency check if LinuxGSM has been recently updated.
  335. nocheckversion=$(cat "${tmpdir}/dependency-no-check.tmp")
  336. if [ "${version}" != "${nocheckversion}" ]; then
  337. rm -f "${tmpdir:?}/dependency-no-check.tmp"
  338. fi
  339. fi