3 # SPDX-License-Identifier: EPL-1.0
4 ##############################################################################
5 # Copyright (c) 2019 The Linux Foundation and others.
7 # All rights reserved. This program and the accompanying materials
8 # are made available under the terms of the Eclipse Public License v1.0
9 # which accompanies this distribution, and is available at
10 # http://www.eclipse.org/legal/epl-v10.html
11 ##############################################################################
13 # A library of functions for LF/Jenkins bash scripts. In the general case, these
14 # functions should only use 'local' variables, and should NOT set
15 # shell/environment variables. If you want to make a
16 # variable available, provide a function that sets the variable: 'function
17 # lf_set_foo() {foo=asdf;}'. Any scripts that need access to the variable can
18 # call the 'set' function. This keeps the name-space pollution to a minimum.
20 # Shell variables that are shared between functions
22 _lf_done_file=".lf-done"
24 ################################################################################
26 # Name: lf-echo-stderr
31 # lf-echo-stderr "this entire" "string will be sent to stderr"
34 # This function will echo all command line aruments to 'stderr'
39 ################################################################################
41 function lf-echo-stderr() { echo "$@" 1>&2; }
43 ################################################################################
49 # # shellcheck disable=SC1090
52 # if lf-boolean $VAR; then
57 # This function will return a BOOLEAN (true or false) based upon the value
58 # of VAR. The value of VAR will be mapped to lower case. If VAR maps to
59 # "true", return true(0). If VAR maps to "false", return false(1). Any
60 # other values will return false(2) and an error message.
63 # true(0), false(1) or false(2)
65 ################################################################################
69 if (( $# != 1 )); then
70 echo "ERROR: ${FUNCNAME[0]}() line: ${BASH_LINENO[0]} : Missing required argument"
74 bool=$(echo "$1" | tr '[:upper:]' '[:lower:]')
79 lf-echo-stderr "ERROR: ${FUNCNAME[0]}() line:{BASH_LINENO[0]} : A boolean cannot be a empty string" >&2
83 lf-echo-stderr "ERROR: ${FUNCNAME[0]}() line: ${BASH_LINENO[0]} : Invalid value for a boolean: '$bool'"
89 ################################################################################
95 # # shellcheck disable=SC1090
98 # lf-venv-activate python3
101 # This function will validate existance of 'python' venv. If it exists
102 # 'path-to-venv/bin' will be prepended to the PATH.
107 ################################################################################
109 function lf-venv-activate()
111 if (( $# != 1 )); then
112 echo "${FUNCNAME[0]}(): Missing path operand"
117 if [[ $arg =~ ^python ]] && type $arg > /dev/null ; then
118 venv=~/.venv${arg#python}
122 # Validate the path to a VENV
123 if [[ ! -f $venv/$_lf_done_file ]]; then
124 lf-echo-stderr "ERROR: Is '$venv' a Python Environment ?"
127 echo "${FUNCNAME[0]}(): Adding $venv/bin to PATH"
130 } # ENd lf-venv-activate()
132 ################################################################################
138 # # shellcheck disable=SC1090
141 # lf-venv-create python2
143 # lf-venv-create python3.6
146 # This function will create a Python Virtual Environment based on the
147 # python specified. The 'python' argument must be in the PATH. The venv
148 # will be located in ~/.venv## where ## comes from the 'python' argument.
149 # I.E. python3 -> ~/.venv3. The resulting venv will be left 'read-only' to
150 # discourage the installation of any other packages (except by
156 ################################################################################
158 function lf-venv-create()
160 if (( $# != 1 )); then
161 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Missing Python argument"
165 if ! type $python > /dev/null; then
166 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Unknown Python: $python"
169 local venv=~/.venv${python#python}
170 local suffix=$python-$$
171 local pip_log=/tmp/pip_log.$suffix
172 if [[ -f $venv/$_lf_done_file ]]; then
173 echo "Venv Already Exists: '$venv'"
176 # Make sure noting is left over
177 if [[ -d $venv ]]; then
182 echo "Creating '$python' venv ($venv)"
186 # For Python2, just create venv and install pip
187 virtualenv -p $python $venv > $pip_log || return 1
188 $venv/bin/pip install --upgrade pip > $pip_log || return 1
191 local pkg_list="git-review jenkins-job-builder lftools[openstack] "
192 pkg_list+="python-heatclient python-openstackclient "
193 pkg_list+="setuptools testresources tox yq"
194 $python -m venv $venv > $pip_log
195 $venv/bin/pip install --upgrade pip > $pip_log || return 1
196 # Redirect errors for now
197 $venv/bin/pip install --upgrade $pkg_list >> $pip_log 2> /dev/null || return 1
198 # Generate list of packages
199 pkg_list=$($venv/bin/pip freeze | awk -F '=' '{print $1}') || return 1
200 # Update all packages, usuaally need to run twice to get all versions
202 local upgrade_cmd="$venv/bin/pip install --upgrade $pkg_list"
203 if $upgrade_cmd >> $pip_log 2>&1 > /dev/null ; then
204 echo -n "Running 'pip --upgrade' to validate..."
205 $upgrade_cmd >> $pip_log || return 1
210 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: No support for: $python"
215 touch $venv/$_lf_done_file
216 # Once this venv is created, make it read-only
218 # Archive output of 'pip freeze'
219 mkdir -p $WORKSPACE/archives
220 $venv/bin/pip freeze > $WORKSPACE/archives/freeze-log-$python || return 1
223 } # End lf-venv-create()
225 ################################################################################
231 # # shellcheck disable=SC1090
234 # lf-venv-add python3 pkg
236 # lf-venv-add python2 pkg1 pkg2 pkg3
239 # This function will add one or more python packages to an existing venv.
240 # Attempts to add packages directly (pip) will result in errors because
241 # the venv does not have 'write' permission.
246 ################################################################################
248 function lf-venv-add()
250 if (( $# < 2 )); then
251 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Missing Package argument"
255 if ! type $python > /dev/null ; then
256 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Unknown Python: $python"
260 local venv=~/.venv${python#python}
261 if [[ ! -f $venv/$_lf_done_file ]]; then
262 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: '$venv' is not a valid venv"
266 local pip_log=/tmp/pip_log-$$
268 echo "Installing '$pkg_list' into $venv"
270 rm $venv/$_lf_done_file
271 $venv/bin/pip install --upgrade $pkg_list > $pip_log || return 1
272 pkg_list=$($venv/bin/pip freeze | awk -F '=' '{print $1}') || return 1
273 $venv/bin/pip install --upgrade $pkg_list > $pip_log || return 1
274 touch $venv/$_lf_done_file
276 # Archive output of 'pip freeze'
277 $venv/bin/pip freeze > $WORKSPACE/archives/freeze-log-$python || return 1
279 } # End lf-venv-add()
281 ################################################################################
282 # Functions that assign Variables
283 ################################################################################
285 # These variables are shell (local) variables and need to be lower-case so
286 # Shellcheck knows they are shell variables and will check for
289 function lf-set-maven-options()
291 # Disable 'unused-variable' check
292 # shellcheck disable=SC2034
293 maven_options="--show-version --batch-mode -Djenkins \
294 -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn \
295 -Dmaven.repo.local=/tmp/r -Dorg.ops4j.pax.url.mvn.localRepository=/tmp/r"