|
| 1 | +#! /usr/bin/env bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +# PT_* variables are set by Bolt. |
| 6 | +# shellcheck disable=SC2154 |
| 7 | +installdir=$PT__installdir |
| 8 | +# shellcheck disable=SC2154 |
| 9 | +collection=$PT_collection |
| 10 | +# shellcheck disable=SC2154 |
| 11 | +yum_source=$PT_yum_source |
| 12 | +# shellcheck disable=SC2154 |
| 13 | +apt_source=$PT_apt_source |
| 14 | + |
| 15 | +tempdir=$(mktemp -d) |
| 16 | +trap 'rm -rf $tempdir' EXIT |
| 17 | + |
| 18 | +log() { |
| 19 | + local _level="$1" |
| 20 | + shift |
| 21 | + |
| 22 | + local ts |
| 23 | + ts=$(date '+%Y-%m-%dT%H:%M:%S') |
| 24 | + |
| 25 | + echo "${ts} [${_level}]: $*" |
| 26 | +} |
| 27 | + |
| 28 | +info() { |
| 29 | + log 'INFO' "$*" |
| 30 | +} |
| 31 | + |
| 32 | +err() { |
| 33 | + log 'ERROR' "$*" |
| 34 | +} |
| 35 | + |
| 36 | +fail() { |
| 37 | + err "$*" |
| 38 | + exit 1 |
| 39 | +} |
| 40 | + |
| 41 | +# Log the value of a variable. |
| 42 | +assigned() { |
| 43 | + local _var="$1" |
| 44 | + |
| 45 | + info "Assigned ${_var}=${!_var}" |
| 46 | +} |
| 47 | + |
| 48 | +# Check if a command exists. |
| 49 | +exists() { |
| 50 | + command -v "$1" > /dev/null 2>&1 |
| 51 | +} |
| 52 | + |
| 53 | +# Set platform and full_version variables by reaching out to the |
| 54 | +# puppetlabs-facts bash task as an executable. |
| 55 | +set_platform() { |
| 56 | + # PT__installdir is set by Bolt. |
| 57 | + local facts="${installdir}/facts/tasks/bash.sh" |
| 58 | + if [ -e "${facts}" ]; then |
| 59 | + platform=$(bash "${facts}" platform) |
| 60 | + assigned 'platform' |
| 61 | + full_version=$(bash "${facts}" release) |
| 62 | + assigned 'full_version' |
| 63 | + else |
| 64 | + fail "Unable to find the puppetlabs-facts bash task to determine platform." |
| 65 | + fi |
| 66 | +} |
| 67 | + |
| 68 | +# Based on platform family set: |
| 69 | +# repository - the package repository to download from |
| 70 | +set_repository() { |
| 71 | + local _family="$1" |
| 72 | + |
| 73 | + case $_family in |
| 74 | + amazon|fedora|rhel|sles) |
| 75 | + repository=$yum_source |
| 76 | + ;; |
| 77 | + debian|ubuntu) |
| 78 | + repository=$apt_source |
| 79 | + ;; |
| 80 | + esac |
| 81 | + assigned 'repository' |
| 82 | +} |
| 83 | + |
| 84 | +# Based on platform family set: |
| 85 | +# package_type - rpm or deb or... |
| 86 | +# package_file_suffix - the file extension for the release package name |
| 87 | +set_package_type() { |
| 88 | + local _family="$1" |
| 89 | + |
| 90 | + case $_family in |
| 91 | + amazon|fedora|rhel|sles) |
| 92 | + package_type='rpm' |
| 93 | + package_file_suffix='noarch.rpm' |
| 94 | + ;; |
| 95 | + debian|ubuntu) |
| 96 | + package_type='deb' |
| 97 | + package_file_suffix='deb' |
| 98 | + ;; |
| 99 | + esac |
| 100 | + assigned 'package_type' |
| 101 | + assigned 'package_file_suffix' |
| 102 | +} |
| 103 | + |
| 104 | +# Based on the platform set: |
| 105 | +# package_name - the name of the release package |
| 106 | +# package_url - the url to download the release package |
| 107 | +set_collection_url() { |
| 108 | + local _platform="$1" |
| 109 | + |
| 110 | + case $_platform in |
| 111 | + Amazon) |
| 112 | + family='amazon' |
| 113 | + ;; |
| 114 | + RHEL|RedHat|CentOS|Scientific|OracleLinux|Rocky|AlmaLinux) |
| 115 | + family='rhel' |
| 116 | + ;; |
| 117 | + Fedora) |
| 118 | + family='fedora' |
| 119 | + ;; |
| 120 | + SLES|Suse) |
| 121 | + family='sles' |
| 122 | + ;; |
| 123 | + Debian) |
| 124 | + family='debian' |
| 125 | + ;; |
| 126 | + Ubuntu) |
| 127 | + family='ubuntu' |
| 128 | + ;; |
| 129 | + *) |
| 130 | + fail "Unhandled platform: '${platform}'" |
| 131 | + ;; |
| 132 | + esac |
| 133 | + assigned 'family' |
| 134 | + |
| 135 | + set_repository $family |
| 136 | + set_package_type $family |
| 137 | + |
| 138 | + if [ "${package_type}" == 'rpm' ]; then |
| 139 | + package_name="${collection}-release-${family}-${full_version}.${package_file_suffix}" |
| 140 | + else |
| 141 | + package_name="${collection}-release-${family}${full_version}.${package_file_suffix}" |
| 142 | + fi |
| 143 | + package_url="https://${repository}/${package_name}" |
| 144 | + |
| 145 | + assigned 'package_name' |
| 146 | + assigned 'package_url' |
| 147 | +} |
| 148 | + |
| 149 | +# Download the given url to the given local file path. |
| 150 | +download() { |
| 151 | + local _url="$1" |
| 152 | + local _file="$2" |
| 153 | + |
| 154 | + if exists 'wget'; then |
| 155 | + wget -O "${_file}" "${_url}" |
| 156 | + elif exists 'curl'; then |
| 157 | + curl -sSL -o "${_file}" "${_url}" |
| 158 | + else |
| 159 | + fail "Unable to download ${_url}. Neither wget nor curl are installed." |
| 160 | + fi |
| 161 | +} |
| 162 | + |
| 163 | +# Download the release package to the tempdir. |
| 164 | +# Sets: |
| 165 | +# local_release_package - the path to the downloaded release package. |
| 166 | +download_release_package() { |
| 167 | + local _package_url="$1" |
| 168 | + local _package_name="$2" |
| 169 | + |
| 170 | + local_release_package="${tempdir}/${_package_name}" |
| 171 | + assigned 'local_release_package' |
| 172 | + |
| 173 | + download "${_package_url}" "${local_release_package}" |
| 174 | +} |
| 175 | + |
| 176 | +# Install the downloaded release package. |
| 177 | +# The release package has the repository metadata needed to install packages |
| 178 | +# from the collection using the platform package manager. |
| 179 | +install_release_package() { |
| 180 | + local _package_type="$1" |
| 181 | + local _package_file="$2" |
| 182 | + |
| 183 | + case $_package_type in |
| 184 | + rpm) |
| 185 | + rpm -Uvh "$_package_file" |
| 186 | + ;; |
| 187 | + deb) |
| 188 | + dpkg -i "$_package_file" |
| 189 | + apt update |
| 190 | + ;; |
| 191 | + *) |
| 192 | + fail "Unhandled package type: '${package_type}'" |
| 193 | + ;; |
| 194 | + esac |
| 195 | +} |
| 196 | + |
| 197 | +install_package() { |
| 198 | + local _package="$1" |
| 199 | + |
| 200 | + if exists 'dnf'; then |
| 201 | + dnf install -y "$_package" |
| 202 | + elif exists 'yum'; then |
| 203 | + yum install -y "$_package" |
| 204 | + elif exists 'zypper'; then |
| 205 | + zypper install -y "$_package" |
| 206 | + elif exists 'apt'; then |
| 207 | + apt install -y "$_package" |
| 208 | + else |
| 209 | + fail "Unable to install ${_package}. Neither dnf, yum, zypper, nor apt are installed." |
| 210 | + fi |
| 211 | +} |
| 212 | + |
| 213 | +# Get platform information |
| 214 | +set_platform |
| 215 | +# Set collection release package url based on platform |
| 216 | +set_collection_url "${platform}" |
| 217 | +# Download the release package to tempdir |
| 218 | +download_release_package "${package_url}" "${package_name}" |
| 219 | +# Install the release package |
| 220 | +install_release_package "${package_type}" "${local_release_package}" |
| 221 | +# Use the platform package manager to install openvox-agent |
| 222 | +install_package 'openvox-agent' |
0 commit comments