check_deps.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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}" == "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. # If auto install passes remove steamcmd install failure.
  163. if [ "${autodepinstall}" == "0" ]; then
  164. unset steamcmdfail
  165. fi
  166. fi
  167. # If automatic dependency install is unavailable.
  168. if [ "${autodepinstall}" != "0" ]; then
  169. if [ "$(command -v dpkg-query 2> /dev/null)" ]; then
  170. echo -e "${i386installcommand}sudo apt update; sudo apt install ${array_deps_missing[*]}"
  171. elif [ "$(command -v dnf 2> /dev/null)" ]; then
  172. echo -e "sudo dnf install ${array_deps_missing[*]}"
  173. elif [ "$(command -v yum 2> /dev/null)" ]; then
  174. echo -e "sudo yum install ${array_deps_missing[*]}"
  175. fi
  176. fi
  177. if [ "${steamcmdfail}" ]; then
  178. if [ "${commandname}" == "INSTALL" ]; then
  179. fn_print_failure_nl "Missing dependencies required to run SteamCMD."
  180. fn_script_log_fatal "Missing dependencies required to run SteamCMD."
  181. core_exit.sh
  182. else
  183. fn_print_error_nl "Missing dependencies required to run SteamCMD."
  184. fn_script_log_error "Missing dependencies required to run SteamCMD."
  185. fi
  186. fi
  187. else
  188. if [ "${commandname}" == "INSTALL" ]; then
  189. fn_print_information_nl "Required dependencies already installed."
  190. fn_script_log_info "Required dependencies already installed."
  191. fi
  192. fi
  193. }
  194. fn_check_loop() {
  195. # Loop though required depenencies checking if they are installed.
  196. for deptocheck in ${array_deps_required[*]}; do
  197. fn_deps_detector
  198. done
  199. # user will be informed of any missing dependencies.
  200. fn_install_missing_deps
  201. }
  202. # Checks if dependency is installed or not.
  203. fn_deps_detector() {
  204. ## Check.
  205. # SteamCMD: Will be removed from required array if no appid is present or non-free repo is not available.
  206. # This will cause SteamCMD to be installed using tar.
  207. if [ "${deptocheck}" == "libsdl2-2.0-0:i386" ] && [ -z "${appid}" ]; then
  208. array_deps_required=("${array_deps_required[@]/libsdl2-2.0-0:i386/}")
  209. steamcmdstatus=1
  210. elif [ "${deptocheck}" == "steamcmd" ] && [ -z "${appid}" ]; then
  211. array_deps_required=("${array_deps_required[@]/steamcmd/}")
  212. steamcmdstatus=1
  213. elif [ "${deptocheck}" == "steamcmd" ] && [ "${distroid}" == "debian" ] && ! grep -qE "^deb .*non-free" /etc/apt/sources.list; then
  214. array_deps_required=("${array_deps_required[@]/steamcmd/}")
  215. steamcmdstatus=1
  216. # Java: Added for users using Oracle JRE to bypass check.
  217. elif [[ ${deptocheck} == "openjdk"* ]] || [[ ${deptocheck} == "java"* ]]; then
  218. # Is java already installed?
  219. if [ -n "${javaversion}" ]; then
  220. # Added for users using Oracle JRE to bypass check.
  221. depstatus=0
  222. deptocheck="${javaversion}"
  223. else
  224. depstatus=1
  225. fi
  226. # Mono: A Mono repo needs to be installed.
  227. elif [ "${deptocheck}" == "mono-complete" ]; then
  228. if [ -n "${monoversion}" ] && [ "${monoversion}" -ge "5" ]; then
  229. # Mono >= 5.0.0 already installed.
  230. depstatus=0
  231. monostatus=0
  232. else
  233. # Mono not installed or installed Mono < 5.0.0.
  234. depstatus=1
  235. monostatus=1
  236. fi
  237. elif [ "$(command -v dpkg-query 2> /dev/null)" ]; then
  238. dpkg-query -W -f='${Status}' "${deptocheck}" 2> /dev/null | grep -q -P '^install ok installed'
  239. depstatus=$?
  240. elif [ "$(command -v dnf 2> /dev/null)" ]; then
  241. dnf list installed "${deptocheck}" > /dev/null 2>&1
  242. depstatus=$?
  243. elif [ "$(command -v rpm 2> /dev/null)" ]; then
  244. rpm -q "${deptocheck}" > /dev/null 2>&1
  245. depstatus=$?
  246. fi
  247. # Outcome of Check.
  248. if [ "${steamcmdstatus}" == "1" ]; then
  249. # If SteamCMD is not available in repo dont check for it.
  250. unset steamcmdstatus
  251. elif [ "${depstatus}" == "0" ]; then
  252. # If dependency is found.
  253. missingdep=0
  254. if [ "${commandname}" == "INSTALL" ]; then
  255. echo -e "${green}${deptocheck}${default}"
  256. sleep 0.1
  257. fi
  258. elif [ "${depstatus}" != "0" ]; then
  259. # If dependency is not found.
  260. missingdep=1
  261. if [ "${commandname}" == "INSTALL" ]; then
  262. echo -e "${red}${deptocheck}${default}"
  263. sleep 0.1
  264. fi
  265. # If SteamCMD requirements are not met install will fail.
  266. if [ -n "${appid}" ]; then
  267. for steamcmddeptocheck in ${array_deps_required_steamcmd[*]}; do
  268. if [ "${deptocheck}" != "steamcmd" ] && [ "${deptocheck}" == "${steamcmddeptocheck}" ]; then
  269. steamcmdfail=1
  270. fi
  271. done
  272. fi
  273. fi
  274. unset depstatus
  275. # Missing dependencies are added to array_deps_missing.
  276. if [ "${missingdep}" == "1" ]; then
  277. array_deps_missing+=("${deptocheck}")
  278. fi
  279. }
  280. if [ "${commandname}" == "INSTALL" ]; then
  281. if [ "$(whoami)" == "root" ]; then
  282. echo -e ""
  283. echo -e "${lightyellow}Checking Dependencies as root${default}"
  284. echo -e "================================="
  285. fn_print_information_nl "Checking any missing dependencies for ${gamename} server only."
  286. fn_print_information_nl "This will NOT install a ${gamename} server."
  287. fn_sleep_time
  288. else
  289. echo -e ""
  290. echo -e "${lightyellow}Checking Dependencies${default}"
  291. echo -e "================================="
  292. fi
  293. fi
  294. # Will warn user if their distro is no longer supported by the vendor.
  295. if [ -n "${distrosupport}" ]; then
  296. if [ "${distrosupport}" == "unsupported" ]; then
  297. fn_print_warning_nl "${distroname} is no longer supported by the vendor. Upgrading is recommended."
  298. fn_script_log_warn "${distroname} is no longer supported by the vendor. Upgrading is recommended."
  299. fi
  300. fi
  301. info_distro.sh
  302. if [ ! -f "${tmpdir}/dependency-no-check.tmp" ] && [ ! -f "${datadir}/${distroid}-${distroversioncsv}.csv" ]; then
  303. # Check that the distro dependency csv file exists.
  304. fn_check_file_github "lgsm/data" "${distroid}-${distroversioncsv}.csv"
  305. if [ -n "${checkflag}" ] && [ "${checkflag}" == "0" ]; then
  306. fn_fetch_file_github "lgsm/data" "${distroid}-${distroversioncsv}.csv" "lgsm/data" "chmodx" "norun" "noforce" "nohash"
  307. fi
  308. fi
  309. # If the file successfully downloaded run the dependency check.
  310. if [ -f "${datadir}/${distroid}-${distroversioncsv}.csv" ]; then
  311. depall=$(awk -F, '$1=="all" {$1=""; print $0}' "${datadir}/${distroid}-${distroversioncsv}.csv")
  312. depsteamcmd=$(awk -F, '$1=="steamcmd" {$1=""; print $0}' "${datadir}/${distroid}-${distroversioncsv}.csv")
  313. depshortname=$(awk -v shortname="${shortname}" -F, '$1==shortname {$1=""; print $0}' "${datadir}/${distroid}-${distroversioncsv}.csv")
  314. # Generate array of missing deps.
  315. array_deps_missing=()
  316. array_deps_required=("${depall} ${depsteamcmd} ${depshortname}")
  317. array_deps_required_steamcmd=("${depsteamcmd}")
  318. fn_deps_email
  319. # Unique sort dependency array.
  320. IFS=" " read -r -a array_deps_required <<< "$(echo "${array_deps_required[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')"
  321. fn_check_loop
  322. # Warn the user that dependency checking is unavailable for their distro.
  323. elif [ "${commandname}" == "INSTALL" ] || [ -n "${checkflag}" ] && [ "${checkflag}" != "0" ]; then
  324. fn_print_warning_nl "LinuxGSM dependency checking currently unavailable for ${distroname}."
  325. # Prevent future dependency checking if unavailable for the distro.
  326. echo "${version}" > "${tmpdir}/dependency-no-check.tmp"
  327. elif [ -f "${tmpdir}/dependency-no-check.tmp" ]; then
  328. # Allow LinuxGSM to try a dependency check if LinuxGSM has been recently updated.
  329. nocheckversion=$(cat "${tmpdir}/dependency-no-check.tmp")
  330. if [ "${version}" != "${nocheckversion}" ]; then
  331. rm -f "${tmpdir:?}/dependency-no-check.tmp"
  332. fi
  333. fi