blob: 10effce375af39cc7826df9a385682668efae7ad [file] [log] [blame]
Joey Armstrong4048c1f2022-12-17 22:24:42 -05001#!/bin/bash
Joey Armstrong541aafc2024-02-28 09:54:53 -05002# -----------------------------------------------------------------------
3# Copyright 2017-2024 Open Networking Foundation Contributors
Matt Jeanneretc7b437c2019-05-13 12:33:08 -04004#
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 Armstrong541aafc2024-02-28 09:54:53 -050016# -----------------------------------------------------------------------
17# SPDX-FileCopyrightText: 2017-2024 Open Networking Foundation Contributors
18# SPDX-License-Identifier: Apache-2.0
19# -----------------------------------------------------------------------
Matt Jeanneretc7b437c2019-05-13 12:33:08 -040020
Joey Armstronga9deb692023-06-30 18:13:01 -040021##-------------------##
22##---] GLOBALS [---##
23##-------------------##
Joey Armstrong541aafc2024-02-28 09:54:53 -050024set -euo pipefail
Joey Armstronga9deb692023-06-30 18:13:01 -040025umask 022
Joey Armstronga9deb692023-06-30 18:13:01 -040026
27## -----------------------------------------------------------------------
Joey Armstrong541aafc2024-02-28 09:54:53 -050028## Intent: Display a stack trace on error
29## -----------------------------------------------------------------------
30function 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}
58trap 'errexit' ERR
59
60## -----------------------------------------------------------------------
Joey Armstronga9deb692023-06-30 18:13:01 -040061## Intent: Display an error mesage then exit with shell exit status
62## -----------------------------------------------------------------------
63function error()
64{
65 echo "${BASH_SOURCE[0]} ERROR: $*"
66 exit 1
67}
68
69## -----------------------------------------------------------------------
70## Intent: Verify required environment variables are set
71## -----------------------------------------------------------------------
Joey Armstrongc0668642024-02-06 17:00:39 -050072## TODO: Covert into command line args
73## -----------------------------------------------------------------------
74# shellcheck disable=SC2120
Joey Armstronga9deb692023-06-30 18:13:01 -040075function init()
76{
Joey Armstrongc0668642024-02-06 17:00:39 -050077 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 Armstrong541aafc2024-02-28 09:54:53 -050098** $(declare -p KARAF_M2)
99** -----------------------------------------------------------------------
Joey Armstrongc0668642024-02-06 17:00:39 -0500100EOM
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## -----------------------------------------------------------------------
112function 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 Armstronga9deb692023-06-30 18:13:01 -0400122 return
123}
124
Joey Armstrong541aafc2024-02-28 09:54:53 -0500125## -----------------------------------------------------------------------
126## Intent: Display program usage
127## -----------------------------------------------------------------------
128function usage()
129{
130 if [[ $# -gt 0 ]]; then
131 printf "\n** $@\n"
132 fi
133
134 cat <<__HELP__
135Usage: ${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 Armstronga9deb692023-06-30 18:13:01 -0400153
Joey Armstrongc0668642024-02-06 17:00:39 -0500154declare arg
155while [[ $# -gt 0 ]]; do
156 arg="$1"; shift
157 case "$arg" in
158 '--debug') declare -g -i debug=1 ;;
Joey Armstrong541aafc2024-02-28 09:54:53 -0500159 '--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 Armstrongc0668642024-02-06 17:00:39 -0500172 *) echo "[SKIP] Unknown argument [$arg]" ;;
173 esac
174done
175
Joey Armstrong541aafc2024-02-28 09:54:53 -0500176##----------------##
177##---] MAIN [---##
178##----------------##
Joey Armstronga9deb692023-06-30 18:13:01 -0400179init
180
Joey Armstrongc0668642024-02-06 17:00:39 -0500181declare -a oars
182get_oar_files oars "$DOWNLOAD_ROOT"
183
184for oar in "${oars[@]}"; do
Joey Armstronga9deb692023-06-30 18:13:01 -0400185
Joey Armstrong541aafc2024-02-28 09:54:53 -0500186 # app_xml="$APP_INSTALL_ROOT/app.xml"
187 app_xml='app.xml'
Joey Armstronga9deb692023-06-30 18:13:01 -0400188 oar_basename="${oar##*/}" # bash builtin
189
Joey Armstrongc0668642024-02-06 17:00:39 -0500190 cat <<EOF
191
192** -----------------------------------------------------------------------
Joey Armstrong541aafc2024-02-28 09:54:53 -0500193** Artifact: ${oar}
194** Basename: ${oar_basename}
Joey Armstrongc0668642024-02-06 17:00:39 -0500195** -----------------------------------------------------------------------
196EOF
197
198 echo "Installing application '${oar##*/}'"
Joey Armstronga9deb692023-06-30 18:13:01 -0400199 rm -rf "$APP_INSTALL_ROOT"
200 mkdir -p "$APP_INSTALL_ROOT"
Joey Armstronga9deb692023-06-30 18:13:01 -0400201
Joey Armstrong541aafc2024-02-28 09:54:53 -0500202 rsync --checksum "$oar" "$APP_INSTALL_ROOT/."
203
Joey Armstrongc0668642024-02-06 17:00:39 -0500204 ## 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 Armstrong541aafc2024-02-28 09:54:53 -0500210 unzip -oq -d . "${oar_basename}"
Joey Armstrongc0668642024-02-06 17:00:39 -0500211
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 Armstrong541aafc2024-02-28 09:54:53 -0500217 | sed 's/<app name="//g;s/".*//g')
Joey Armstrongc0668642024-02-06 17:00:39 -0500218
Joey Armstrong541aafc2024-02-28 09:54:53 -0500219 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** -----------------------------------------------------------------------
233ERR
234 exit 1
235 fi
Joey Armstrongc0668642024-02-06 17:00:39 -0500236
237 printf '** %s\n' "$(declare -p names)"
238 name="${names[0]}"
Joey Armstronga9deb692023-06-30 18:13:01 -0400239 apps_name="$APPS_ROOT/$name"
240
241 mkdir -p "$apps_name"
Joey Armstrongc0668642024-02-06 17:00:39 -0500242 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 Armstrong541aafc2024-02-28 09:54:53 -0500248 rsync -v "${oar_basename}" "${apps_name}/${name}.oar"
Joey Armstrongc0668642024-02-06 17:00:39 -0500249
250 popd >/dev/null || { error "popd failed: $APP_INSTALL_ROOT"; }
251
Joey Armstrong541aafc2024-02-28 09:54:53 -0500252 rsync -rv --checksum "$APP_INSTALL_ROOT/m2/." "$KARAF_M2/."
253 rm -rf "$APP_INSTALL_ROOT"
254
Matt Jeanneretc7b437c2019-05-13 12:33:08 -0400255done
Joey Armstronga9deb692023-06-30 18:13:01 -0400256
257# [EOF]