Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 3 | # update_repos() - Function that updates linux repositories |
| 4 | function update_repos { |
Victor Morales | 7abf1a8 | 2017-11-06 09:20:27 -0800 | [diff] [blame] | 5 | echo "Updating repositories list..." |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 6 | if [ -f /var/onap/files/sources.list ]; then |
| 7 | cp /var/onap/files/sources.list /etc/apt/sources.list |
| 8 | fi |
Victor Morales | 9f1c94b | 2017-11-07 07:56:04 -0800 | [diff] [blame] | 9 | source /etc/os-release || source /usr/lib/os-release |
| 10 | case ${ID,,} in |
| 11 | *suse) |
| 12 | zypper -n ref |
| 13 | ;; |
| 14 | ubuntu|debian) |
| 15 | if [[ "$debug" == "False" ]]; then |
| 16 | apt-get update > /dev/null |
| 17 | else |
| 18 | apt-get update |
| 19 | fi |
| 20 | ;; |
| 21 | rhel|centos|fedora) |
| 22 | yum updateinfo |
| 23 | ;; |
| 24 | esac |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | # is_package_installed() - Function to tell if a package is installed |
| 28 | function is_package_installed { |
| 29 | if [[ -z "$@" ]]; then |
| 30 | return 1 |
| 31 | fi |
Victor Morales | 9f1c94b | 2017-11-07 07:56:04 -0800 | [diff] [blame] | 32 | source /etc/os-release || source /usr/lib/os-release |
| 33 | case ${ID,,} in |
| 34 | *suse) |
Victor Morales | 96fe388 | 2018-03-02 14:08:39 -0800 | [diff] [blame^] | 35 | CHECK_CMD="zypper search --match-exact --installed" |
Victor Morales | 9f1c94b | 2017-11-07 07:56:04 -0800 | [diff] [blame] | 36 | ;; |
| 37 | ubuntu|debian) |
Victor Morales | 96fe388 | 2018-03-02 14:08:39 -0800 | [diff] [blame^] | 38 | CHECK_CMD="dpkg -l" |
Victor Morales | 9f1c94b | 2017-11-07 07:56:04 -0800 | [diff] [blame] | 39 | ;; |
| 40 | rhel|centos|fedora) |
Victor Morales | 96fe388 | 2018-03-02 14:08:39 -0800 | [diff] [blame^] | 41 | CHECK_CMD="rpm -q" |
Victor Morales | 9f1c94b | 2017-11-07 07:56:04 -0800 | [diff] [blame] | 42 | ;; |
| 43 | esac |
Victor Morales | 96fe388 | 2018-03-02 14:08:39 -0800 | [diff] [blame^] | 44 | ${CHECK_CMD} "$@" &> /dev/null |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 45 | } |
| 46 | |
Victor Morales | 7ae05a4 | 2017-08-22 14:39:00 -0500 | [diff] [blame] | 47 | # install_packages() - Install a list of packages |
| 48 | function install_packages { |
| 49 | local package=$@ |
Victor Morales | 9f1c94b | 2017-11-07 07:56:04 -0800 | [diff] [blame] | 50 | source /etc/os-release || source /usr/lib/os-release |
| 51 | case ${ID,,} in |
| 52 | *suse) |
| 53 | ;; |
| 54 | ubuntu|debian) |
| 55 | apt-get install -y -qq $package |
| 56 | ;; |
| 57 | rhel|centos|fedora) |
| 58 | ;; |
| 59 | esac |
Victor Morales | 7ae05a4 | 2017-08-22 14:39:00 -0500 | [diff] [blame] | 60 | } |
| 61 | |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 62 | # install_package() - Install specific package if doesn't exist |
| 63 | function install_package { |
| 64 | local package=$1 |
Victor Morales | 7abf1a8 | 2017-11-06 09:20:27 -0800 | [diff] [blame] | 65 | |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 66 | if ! is_package_installed $package; then |
Victor Morales | 7abf1a8 | 2017-11-06 09:20:27 -0800 | [diff] [blame] | 67 | echo "Installing $package..." |
| 68 | |
Victor Morales | 9f1c94b | 2017-11-07 07:56:04 -0800 | [diff] [blame] | 69 | source /etc/os-release || source /usr/lib/os-release |
| 70 | case ${ID,,} in |
| 71 | *suse) |
| 72 | zypper install -y $package |
| 73 | ;; |
| 74 | ubuntu|debian) |
| 75 | if [[ "$debug" == "False" ]]; then |
| 76 | apt-get install -y -qq -o=Dpkg::Use-Pty=0 $package |
| 77 | else |
| 78 | apt-get install -y $package |
| 79 | fi |
| 80 | ;; |
| 81 | rhel|centos|fedora) |
| 82 | PKG_MANAGER=$(which dnf || which yum) |
| 83 | ${PKG_MANAGER} -y install $package |
| 84 | ;; |
| 85 | esac |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 86 | fi |
| 87 | } |
Victor Morales | 7ae05a4 | 2017-08-22 14:39:00 -0500 | [diff] [blame] | 88 | |
| 89 | # uninstall_packages() - Uninstall a list of packages |
| 90 | function uninstall_packages { |
| 91 | local packages=$@ |
Victor Morales | 9f1c94b | 2017-11-07 07:56:04 -0800 | [diff] [blame] | 92 | source /etc/os-release || source /usr/lib/os-release |
| 93 | case ${ID,,} in |
| 94 | *suse) |
| 95 | ;; |
| 96 | ubuntu|debian) |
| 97 | apt-get purge -y -qq $packages |
| 98 | ;; |
| 99 | rhel|centos|fedora) |
| 100 | ;; |
| 101 | esac |
Victor Morales | 7ae05a4 | 2017-08-22 14:39:00 -0500 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | # uninstall_package() - Uninstall specific package if exists |
| 105 | function uninstall_package { |
| 106 | local package=$1 |
| 107 | if is_package_installed $package; then |
Victor Morales | 9f1c94b | 2017-11-07 07:56:04 -0800 | [diff] [blame] | 108 | source /etc/os-release || source /usr/lib/os-release |
| 109 | case ${ID,,} in |
| 110 | *suse) |
| 111 | ;; |
| 112 | ubuntu|debian) |
| 113 | apt-get purge -y -qq $package |
| 114 | ;; |
| 115 | rhel|centos|fedora) |
| 116 | ;; |
| 117 | esac |
Victor Morales | 7ae05a4 | 2017-08-22 14:39:00 -0500 | [diff] [blame] | 118 | fi |
| 119 | } |