centos.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #!/usr/bin/env bash
  2. # Package tree used for installing netdata on distribution:
  3. # << CentOS: [7] [8] [9] >>
  4. set -e
  5. declare -a package_tree=(
  6. cmake
  7. curl
  8. elfutils-libelf-devel
  9. findutils
  10. gcc
  11. gcc-c++
  12. git
  13. gzip
  14. json-c-devel
  15. libatomic
  16. libmnl-devel
  17. libuuid-devel
  18. libuv-devel
  19. libyaml-devel
  20. lz4-devel
  21. make
  22. openssl-devel
  23. pkgconfig
  24. python3
  25. systemd-devel
  26. tar
  27. zlib-devel
  28. )
  29. os_version() {
  30. if [[ -f /etc/os-release ]]; then
  31. # shellcheck disable=SC2002
  32. cat /etc/os-release | grep VERSION_ID | cut -d'=' -f2 | cut -d'"' -f2
  33. else
  34. echo "Error: Cannot determine OS version!"
  35. exit 1
  36. fi
  37. }
  38. prompt() {
  39. if [[ "${NON_INTERACTIVE}" == "1" ]]; then
  40. echo >&2 "Running in non-interactive mode, assuming yes (y)"
  41. echo >&2 " > Would have prompted for ${1} ..."
  42. return 0
  43. fi
  44. while true; do
  45. read -r -p "${1} [y/n] " yn
  46. case $yn in
  47. [Yy]*) return 0 ;;
  48. [Nn]*) return 1 ;;
  49. *) echo >&2 "Please answer with yes (y) or no (n)." ;;
  50. esac
  51. done
  52. }
  53. usage() {
  54. cat << EOF
  55. OPTIONS:
  56. [--dont-wait] [--non-interactive] [ ]
  57. EOF
  58. }
  59. check_flags() {
  60. while [ -n "${1}" ]; do
  61. case "${1}" in
  62. dont-wait | --dont-wait | -n)
  63. DONT_WAIT=1
  64. ;;
  65. non-interactive | --non-interactive | -y)
  66. NON_INTERACTIVE=1
  67. ;;
  68. help | -h | --help)
  69. usage
  70. exit 1
  71. ;;
  72. *)
  73. echo >&2 "ERROR: Cannot understand option '${1}'"
  74. echo >&2
  75. usage
  76. exit 1
  77. ;;
  78. esac
  79. shift
  80. done
  81. if [[ "${DONT_WAIT}" == "0" ]] && [[ "${NON_INTERACTIVE}" == "0" ]]; then
  82. read -r -p "Press ENTER to run it > " || exit 1
  83. fi
  84. }
  85. validate_tree_centos() {
  86. local opts=
  87. package_manager=
  88. if [[ "${NON_INTERACTIVE}" == "1" ]]; then
  89. echo >&2 "Running in non-interactive mode"
  90. opts="-y"
  91. fi
  92. echo >&2 " > CentOS Version: $(os_version) ..."
  93. if [[ $(os_version) =~ ^9(\..*)?$ ]]; then
  94. package_manager=dnf
  95. echo >&2 " > Checking for config-manager ..."
  96. if ! dnf config-manager --help &> /dev/null; then
  97. if prompt "config-manager not found, shall I install it?"; then
  98. dnf ${opts} install 'dnf-command(config-manager)'
  99. fi
  100. fi
  101. echo >&2 " > Checking for CRB ..."
  102. if ! dnf repolist | grep CRB; then
  103. if prompt "CRB not found, shall I install it?"; then
  104. dnf ${opts} config-manager --set-enabled crb
  105. fi
  106. fi
  107. elif [[ $(os_version) =~ ^8(\..*)?$ ]]; then
  108. package_manager=dnf
  109. echo >&2 " > Checking for config-manager ..."
  110. if ! dnf config-manager --help &> /dev/null; then
  111. if prompt "config-manager not found, shall I install it?"; then
  112. dnf ${opts} install 'dnf-command(config-manager)'
  113. fi
  114. fi
  115. echo >&2 " > Checking for PowerTools ..."
  116. if ! dnf repolist | grep PowerTools; then
  117. if prompt "PowerTools not found, shall I install it?"; then
  118. dnf ${opts} config-manager --set-enabled powertools || enable_powertools_repo
  119. fi
  120. fi
  121. echo >&2 " > Updating libarchive ..."
  122. dnf ${opts} install libarchive
  123. elif [[ $(os_version) =~ ^7(\..*)?$ ]]; then
  124. package_manager=yum
  125. echo >&2 " > Checking for EPEL ..."
  126. if ! rpm -qa | grep epel-release > /dev/null; then
  127. if prompt "EPEL not found, shall I install it?"; then
  128. yum ${opts} install epel-release
  129. fi
  130. fi
  131. yum makecache
  132. fi
  133. }
  134. enable_powertools_repo() {
  135. if ! dnf repolist | grep -q powertools; then
  136. cat > /etc/yum.repos.d/powertools.repo <<-EOF
  137. [powertools]
  138. name=CentOS Linux \$releasever - PowerTools
  139. mirrorlist=http://mirrorlist.centos.org/?release=\$releasever&arch=\$basearch&repo=PowerTools&infra=\$infra
  140. #baseurl=http://mirror.centos.org/\$contentdir/\$releasever/PowerTools/\$basearch/os/
  141. gpgcheck=1
  142. enabled=1
  143. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
  144. EOF
  145. else
  146. echo "Something went wrong!"
  147. exit 1
  148. fi
  149. }
  150. # shellcheck disable=SC2068
  151. check_flags ${@}
  152. validate_tree_centos
  153. packages_to_install=
  154. for package in "${package_tree[@]}"; do
  155. if rpm -q "$package" &> /dev/null; then
  156. echo "Package '${package}' is installed"
  157. else
  158. echo "Package '$package' is NOT installed"
  159. packages_to_install="$packages_to_install $package"
  160. fi
  161. done
  162. if [[ -z $packages_to_install ]]; then
  163. echo "All required packages are already installed. Skipping .."
  164. else
  165. echo "packages_to_install:" "${packages_to_install[@]}"
  166. opts=
  167. if [[ "${NON_INTERACTIVE}" == "1" ]]; then
  168. echo >&2 "Running in non-interactive mode"
  169. opts="-y"
  170. fi
  171. # shellcheck disable=SC2068
  172. ${package_manager} install ${opts} ${packages_to_install[@]}
  173. fi