Joey Armstrong | 4048c1f | 2022-12-17 22:24:42 -0500 | [diff] [blame] | 1 | #!/bin/bash |
Joey Armstrong | 541aafc | 2024-02-28 09:54:53 -0500 | [diff] [blame^] | 2 | # ----------------------------------------------------------------------- |
| 3 | # Copyright 2017-2024 Open Networking Foundation Contributors |
Matt Jeanneret | c7b437c | 2019-05-13 12:33:08 -0400 | [diff] [blame] | 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
Joey Armstrong | 541aafc | 2024-02-28 09:54:53 -0500 | [diff] [blame^] | 16 | # ----------------------------------------------------------------------- |
| 17 | # SPDX-FileCopyrightText: 2017-2024 Open Networking Foundation Contributors |
| 18 | # SPDX-License-Identifier: Apache-2.0 |
| 19 | # ----------------------------------------------------------------------- |
Matt Jeanneret | c7b437c | 2019-05-13 12:33:08 -0400 | [diff] [blame] | 20 | |
Joey Armstrong | a9deb69 | 2023-06-30 18:13:01 -0400 | [diff] [blame] | 21 | ##-------------------## |
| 22 | ##---] GLOBALS [---## |
| 23 | ##-------------------## |
Joey Armstrong | 541aafc | 2024-02-28 09:54:53 -0500 | [diff] [blame^] | 24 | set -euo pipefail |
Joey Armstrong | a9deb69 | 2023-06-30 18:13:01 -0400 | [diff] [blame] | 25 | umask 022 |
Joey Armstrong | a9deb69 | 2023-06-30 18:13:01 -0400 | [diff] [blame] | 26 | |
| 27 | ## ----------------------------------------------------------------------- |
Joey Armstrong | 541aafc | 2024-02-28 09:54:53 -0500 | [diff] [blame^] | 28 | ## Intent: Display a stack trace on error |
| 29 | ## ----------------------------------------------------------------------- |
| 30 | function errexit() |
| 31 | { |
| 32 | local err=$? |
| 33 | set +o xtrace |
| 34 | local code="${1:-1}" |
| 35 | |
| 36 | local prefix="${BASH_SOURCE[1]}:${BASH_LINENO[1]}" |
| 37 | echo -e "\nOFFENDER: ${prefix}" |
| 38 | if [ $# -gt 0 ] && [ "$1" == '--stacktrace-quiet' ]; then |
| 39 | code=1 |
| 40 | else |
| 41 | echo "ERROR: '${BASH_COMMAND}' exited with status $err" |
| 42 | fi |
| 43 | |
| 44 | # Print out the stack trace described by $function_stack |
| 45 | if [ ${#FUNCNAME[@]} -gt 2 ] |
| 46 | then |
| 47 | echo "Call tree:" |
| 48 | for ((i=1;i<${#FUNCNAME[@]}-1;i++)) |
| 49 | do |
| 50 | echo " $i: ${BASH_SOURCE[$i+1]}:${BASH_LINENO[$i]} ${FUNCNAME[$i]}(...)" |
| 51 | done |
| 52 | fi |
| 53 | |
| 54 | echo "Exiting with status ${code}" |
| 55 | echo |
| 56 | exit "${code}" |
| 57 | } |
| 58 | trap 'errexit' ERR |
| 59 | |
| 60 | ## ----------------------------------------------------------------------- |
Joey Armstrong | a9deb69 | 2023-06-30 18:13:01 -0400 | [diff] [blame] | 61 | ## Intent: Display an error mesage then exit with shell exit status |
| 62 | ## ----------------------------------------------------------------------- |
| 63 | function error() |
| 64 | { |
| 65 | echo "${BASH_SOURCE[0]} ERROR: $*" |
| 66 | exit 1 |
| 67 | } |
| 68 | |
| 69 | ## ----------------------------------------------------------------------- |
| 70 | ## Intent: Verify required environment variables are set |
| 71 | ## ----------------------------------------------------------------------- |
Joey Armstrong | c066864 | 2024-02-06 17:00:39 -0500 | [diff] [blame] | 72 | ## TODO: Covert into command line args |
| 73 | ## ----------------------------------------------------------------------- |
| 74 | # shellcheck disable=SC2120 |
Joey Armstrong | a9deb69 | 2023-06-30 18:13:01 -0400 | [diff] [blame] | 75 | function init() |
| 76 | { |
Joey Armstrong | c066864 | 2024-02-06 17:00:39 -0500 | [diff] [blame] | 77 | declare -a vars=() |
| 78 | vars+=('APP_INSTALL_ROOT') |
| 79 | vars+=('APPS_ROOT') |
| 80 | vars+=('DOWNLOAD_ROOT') |
| 81 | vars+=('KARAF_M2') |
| 82 | |
| 83 | local var |
| 84 | for var in "${vars[@]}"; |
| 85 | do |
| 86 | [[ ! -v "$var" ]] && { error "EnvVar ${var}= is required"; } |
| 87 | done |
| 88 | |
| 89 | cat <<EOM |
| 90 | |
| 91 | ** ----------------------------------------------------------------------- |
| 92 | ** Running: $0 $@ |
| 93 | ** ----------------------------------------------------------------------- |
| 94 | ** $(declare -p APP_INSTALL_ROOT) |
| 95 | ** $(declare -p APPS_ROOT) |
| 96 | ** $(declare -p DOWNLOAD_ROOT) |
| 97 | ** ----------------------------------------------------------------------- |
Joey Armstrong | 541aafc | 2024-02-28 09:54:53 -0500 | [diff] [blame^] | 98 | ** $(declare -p KARAF_M2) |
| 99 | ** ----------------------------------------------------------------------- |
Joey Armstrong | c066864 | 2024-02-06 17:00:39 -0500 | [diff] [blame] | 100 | EOM |
| 101 | |
| 102 | return |
| 103 | } |
| 104 | |
| 105 | ## ----------------------------------------------------------------------- |
| 106 | ## Intent: Gather artifact *.oar files to unpack |
| 107 | ## ----------------------------------------------------------------------- |
| 108 | ## GIVEN |
| 109 | ## ref An indirect array variable to return values through. |
| 110 | ## dir Directory to glob *.oar files from |
| 111 | ## ----------------------------------------------------------------------- |
| 112 | function get_oar_files() |
| 113 | { |
| 114 | local -n ref=$1 ; shift |
| 115 | local dir="$1" ; shift |
| 116 | |
| 117 | readarray -t oars < <(find "$dir" -name '*.oar' -type f -print) |
| 118 | [[ ${#oars[@]} -eq 0 ]] && { error "No \*.oar files detected in $dir"; } |
| 119 | |
| 120 | # shellcheck disable=SC2034 |
| 121 | ref=("${oars[@]}") |
Joey Armstrong | a9deb69 | 2023-06-30 18:13:01 -0400 | [diff] [blame] | 122 | return |
| 123 | } |
| 124 | |
Joey Armstrong | 541aafc | 2024-02-28 09:54:53 -0500 | [diff] [blame^] | 125 | ## ----------------------------------------------------------------------- |
| 126 | ## Intent: Display program usage |
| 127 | ## ----------------------------------------------------------------------- |
| 128 | function usage() |
| 129 | { |
| 130 | if [[ $# -gt 0 ]]; then |
| 131 | printf "\n** $@\n" |
| 132 | fi |
| 133 | |
| 134 | cat <<__HELP__ |
| 135 | Usage: ${BASH_SOURCE[0]##*/} |
| 136 | --download Holding directory for downloaded oar files |
| 137 | --install Unpack and install app files into this directory |
| 138 | |
| 139 | [Test Helpers] |
| 140 | --fail Force script exit with status fail ($?==99) |
| 141 | --pass Force script exit with status pass ($?==0) |
| 142 | |
| 143 | --debug Enable debug mode. |
| 144 | --help Display program usage. |
| 145 | __HELP__ |
| 146 | |
| 147 | return |
| 148 | } |
| 149 | |
| 150 | ##-------------------## |
| 151 | ##---] GETOPTS [---## |
| 152 | ##-------------------## |
Joey Armstrong | a9deb69 | 2023-06-30 18:13:01 -0400 | [diff] [blame] | 153 | |
Joey Armstrong | c066864 | 2024-02-06 17:00:39 -0500 | [diff] [blame] | 154 | declare arg |
| 155 | while [[ $# -gt 0 ]]; do |
| 156 | arg="$1"; shift |
| 157 | case "$arg" in |
| 158 | '--debug') declare -g -i debug=1 ;; |
Joey Armstrong | 541aafc | 2024-02-28 09:54:53 -0500 | [diff] [blame^] | 159 | '--down'*) |
| 160 | arg="$1"; shift |
| 161 | [[ ! -e "$arg" ]] && { error "--download $arg does not exist"; } |
| 162 | export DOWNLOAD_ROOT="$arg" |
| 163 | ;; |
| 164 | '--inst'*) |
| 165 | arg="$1"; shift |
| 166 | [[ ! -e "$arg" ]] && { error "--install $arg does not exist"; } |
| 167 | export APP_INSTALL_ROOT="$arg" |
| 168 | ;; |
| 169 | '--fail') exit 99 ;; |
| 170 | '--pass') exit 0 ;; |
| 171 | '--help') usage; exit 0 ;; |
Joey Armstrong | c066864 | 2024-02-06 17:00:39 -0500 | [diff] [blame] | 172 | *) echo "[SKIP] Unknown argument [$arg]" ;; |
| 173 | esac |
| 174 | done |
| 175 | |
Joey Armstrong | 541aafc | 2024-02-28 09:54:53 -0500 | [diff] [blame^] | 176 | ##----------------## |
| 177 | ##---] MAIN [---## |
| 178 | ##----------------## |
Joey Armstrong | a9deb69 | 2023-06-30 18:13:01 -0400 | [diff] [blame] | 179 | init |
| 180 | |
Joey Armstrong | c066864 | 2024-02-06 17:00:39 -0500 | [diff] [blame] | 181 | declare -a oars |
| 182 | get_oar_files oars "$DOWNLOAD_ROOT" |
| 183 | |
| 184 | for oar in "${oars[@]}"; do |
Joey Armstrong | a9deb69 | 2023-06-30 18:13:01 -0400 | [diff] [blame] | 185 | |
Joey Armstrong | 541aafc | 2024-02-28 09:54:53 -0500 | [diff] [blame^] | 186 | # app_xml="$APP_INSTALL_ROOT/app.xml" |
| 187 | app_xml='app.xml' |
Joey Armstrong | a9deb69 | 2023-06-30 18:13:01 -0400 | [diff] [blame] | 188 | oar_basename="${oar##*/}" # bash builtin |
| 189 | |
Joey Armstrong | c066864 | 2024-02-06 17:00:39 -0500 | [diff] [blame] | 190 | cat <<EOF |
| 191 | |
| 192 | ** ----------------------------------------------------------------------- |
Joey Armstrong | 541aafc | 2024-02-28 09:54:53 -0500 | [diff] [blame^] | 193 | ** Artifact: ${oar} |
| 194 | ** Basename: ${oar_basename} |
Joey Armstrong | c066864 | 2024-02-06 17:00:39 -0500 | [diff] [blame] | 195 | ** ----------------------------------------------------------------------- |
| 196 | EOF |
| 197 | |
| 198 | echo "Installing application '${oar##*/}'" |
Joey Armstrong | a9deb69 | 2023-06-30 18:13:01 -0400 | [diff] [blame] | 199 | rm -rf "$APP_INSTALL_ROOT" |
| 200 | mkdir -p "$APP_INSTALL_ROOT" |
Joey Armstrong | a9deb69 | 2023-06-30 18:13:01 -0400 | [diff] [blame] | 201 | |
Joey Armstrong | 541aafc | 2024-02-28 09:54:53 -0500 | [diff] [blame^] | 202 | rsync --checksum "$oar" "$APP_INSTALL_ROOT/." |
| 203 | |
Joey Armstrong | c066864 | 2024-02-06 17:00:39 -0500 | [diff] [blame] | 204 | ## pushd()/popd(): cd $here && cd $root w/error checking |
| 205 | pushd "$APP_INSTALL_ROOT" >/dev/null \ |
| 206 | || { error "pushd failed: $APP_INSTALL_ROOT"; } |
| 207 | |
| 208 | [[ -v debug ]] && { echo "** Installing: $oar"; } |
| 209 | |
Joey Armstrong | 541aafc | 2024-02-28 09:54:53 -0500 | [diff] [blame^] | 210 | unzip -oq -d . "${oar_basename}" |
Joey Armstrong | c066864 | 2024-02-06 17:00:39 -0500 | [diff] [blame] | 211 | |
| 212 | # ------------------------------------------------------------ |
| 213 | # [IN] <app name="org.opencord.kafka" origin="ONF" version="2.13.2" |
| 214 | # [OUT] declare -a names=([0]="org.opencord.kafka") |
| 215 | # ------------------------------------------------------------ |
| 216 | readarray -t names < <(grep 'name=' "$app_xml" \ |
Joey Armstrong | 541aafc | 2024-02-28 09:54:53 -0500 | [diff] [blame^] | 217 | | sed 's/<app name="//g;s/".*//g') |
Joey Armstrong | c066864 | 2024-02-06 17:00:39 -0500 | [diff] [blame] | 218 | |
Joey Armstrong | 541aafc | 2024-02-28 09:54:53 -0500 | [diff] [blame^] | 219 | if [[ ${#names[@]} -eq 0 ]]; then |
| 220 | echo |
| 221 | echo "APP_XML FILE: $APP_INSTALL/${app_xml}" |
| 222 | cat "$app_xml" |
| 223 | |
| 224 | cat <<ERR |
| 225 | |
| 226 | ** ----------------------------------------------------------------------- |
| 227 | ** FILE: $APP_INSTALL/${app_xml} |
| 228 | ** GREP: $(grep 'name=' "$app_xml") |
| 229 | ** SED: $(grep 'name=' "$app_xml" | sed 's/<app name="//g;s/".*//g') |
| 230 | ** ERROR: Detected invalid app_xml=${app_xml} name gathering. |
| 231 | ** NAMES: $(declare -p names) |
| 232 | ** ----------------------------------------------------------------------- |
| 233 | ERR |
| 234 | exit 1 |
| 235 | fi |
Joey Armstrong | c066864 | 2024-02-06 17:00:39 -0500 | [diff] [blame] | 236 | |
| 237 | printf '** %s\n' "$(declare -p names)" |
| 238 | name="${names[0]}" |
Joey Armstrong | a9deb69 | 2023-06-30 18:13:01 -0400 | [diff] [blame] | 239 | apps_name="$APPS_ROOT/$name" |
| 240 | |
| 241 | mkdir -p "$apps_name" |
Joey Armstrong | c066864 | 2024-02-06 17:00:39 -0500 | [diff] [blame] | 242 | rsync -v --checksum "$app_xml" "${apps_name}/app.xml" |
| 243 | |
| 244 | touch "${apps_name}/active" # what is this used for (?) |
| 245 | |
| 246 | declare app_png="$APP_INSTALL_ROOT/app.png" |
| 247 | [ -f "$app_png" ] && { rsync -v --checksum "$app_png" "${apps_name}/."; } |
Joey Armstrong | 541aafc | 2024-02-28 09:54:53 -0500 | [diff] [blame^] | 248 | rsync -v "${oar_basename}" "${apps_name}/${name}.oar" |
Joey Armstrong | c066864 | 2024-02-06 17:00:39 -0500 | [diff] [blame] | 249 | |
| 250 | popd >/dev/null || { error "popd failed: $APP_INSTALL_ROOT"; } |
| 251 | |
Joey Armstrong | 541aafc | 2024-02-28 09:54:53 -0500 | [diff] [blame^] | 252 | rsync -rv --checksum "$APP_INSTALL_ROOT/m2/." "$KARAF_M2/." |
| 253 | rm -rf "$APP_INSTALL_ROOT" |
| 254 | |
Matt Jeanneret | c7b437c | 2019-05-13 12:33:08 -0400 | [diff] [blame] | 255 | done |
Joey Armstrong | a9deb69 | 2023-06-30 18:13:01 -0400 | [diff] [blame] | 256 | |
| 257 | # [EOF] |