check_deps.sh 18 KB

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