2 # shellcheck shell=bash
4 # SPDX-License-Identifier: EPL-1.0
5 ##############################################################################
6 # Copyright (c) 2019 The Linux Foundation and others.
8 # All rights reserved. This program and the accompanying materials
9 # are made available under the terms of the Eclipse Public License v1.0
10 # which accompanies this distribution, and is available at
11 # http://www.eclipse.org/legal/epl-v10.html
12 ##############################################################################
14 # A library of functions for LF/Jenkins bash scripts. In the general case, these
15 # functions should only use 'local' variables, and should NOT set
16 # shell/environment variables. If you want to make a variable available, provide
17 # a function that sets the variable: 'function lf_set_foo() {foo=asdf;}'. Any
18 # scripts that need access to the variable can call the 'set' function. This
19 # keeps the name-space pollution to a minimum.
21 # This script will be installed in ~jenkins by the Jenkins Init Script when the
22 # build agent boots. When the build starts it should already be installed.
24 ################################################################################
32 # lf-echo-stderr "this entire" "string will be sent to stderr"
35 # This function will echo all command line aruments to 'stderr'
40 ################################################################################
46 ################################################################################
52 # # shellcheck disable=SC1090
55 # if lf-boolean $VAR; then
60 # This function will return a BOOLEAN (true or false) based upon the value
61 # of VAR. The value of VAR will be mapped to lower case. If VAR maps to
62 # "true", return true(0). If VAR maps to "false", return false(1). Any
63 # other values will return false(2) and an error message.
69 ################################################################################
72 if (( $# != 1 )); then
73 echo "ERROR: ${FUNCNAME[0]}() line: ${BASH_LINENO[0]} :"\
74 " Missing Required Argument"
78 bool=$(echo "$1" | tr '[:upper:]' '[:lower:]')
83 lf-echo-stderr "ERROR: ${FUNCNAME[0]}() line:{BASH_LINENO[0]} :"\
84 " A boolean cannot be a empty string" >&2
88 lf-echo-stderr "ERROR: ${FUNCNAME[0]}() line: ${BASH_LINENO[0]} :"\
89 " Invalid value for a boolean: '$bool'"
95 ################################################################################
98 # lf-activate-venv [-p|--python python] [-v|--venv-file] [--no-path]
99 # [--system-site-packages] [package]...
102 # # shellcheck disable=SC1090
105 # lf-activate-venv tox tox-pyenv
107 # lf-activate-venv jenkins-job-builder
109 # lf-activate-venv lftools
111 # lf-activate-venv --python python3.8 git-review
113 # lf-activate-venv --python python3.8 --venv-file /tmp/.myvenv git-review
116 # This function will create a new Python Virtual Environment (venv) and
117 # install the specified packages in the new venv. The venv will be installed
118 # in $lf_venv and by default, the $lf_venv/bin directory will be prepended
121 # The 'lf_venv' variable will be set so you can directly execute commands
122 # in the venv with: $lf_venv/bin/command. lf-activate-venv() will check for
123 # existing file '/tmp/.os_lf_venv' and set 'lf_venv' if the file exists.
125 # The function provides a --venv-file path for saving the value of the 'lf_env'
126 # that can re-used later. By default '/tmp/.os_lf_venv' venv file is created
127 # when the --venv-file option is not specified.
129 # Subsequent calls to lf-activate-venv() will re-use the existing venv
130 # throught and will NOT overwrite 'lf_venv', if the '/tmp/.os_lf_venv'
133 # If a new venv is required delete the file '/tmp/.os_lf_venv' before
134 # calling lf-activate-venv() will create a fresh venv.
136 # By default all packages are installed with '--upgrade-strategy eager'.
137 # The venv will always contain pip & virtualenv.
139 # Some packages have a default version. If one of those packages is specified,
140 # the 'version' specifier will be added for the install. If the version is
141 # specified on the command line that version will be used.
142 # The following packages have default versions:
144 # jenkins-job-builder $JJB_VERSION
146 # If the --python flag is specified, the specified python executable will be
147 # used to create the venv. The --python option must be in the PATH. The venv
148 # will be located in $lf_venv (/tmp/venv-####).
150 # If the --no-path flag is specified, $lf_venv/bin will not be prepended to
153 # If the --system-site-packages flag is specified, the --system-site-packages
154 # flag will be passed to the inital 'pip install' (python3* only).
160 ################################################################################
162 lf-activate-venv () {
163 lf_venv=$(mktemp -d /tmp/venv-XXXX)
164 local venv_file="/tmp/.os_lf_venv"
168 local install_args=""
170 options=$(getopt -o 'np:v:' -l 'no-path,system-site-packages,python:,venv-file:' \
171 -n "${FUNCNAME[0]}" -- "$@" )
172 eval set -- "$options"
175 -n|--no-path) set_path=false ; shift ;;
176 -p|--python) python="$2" ; shift 2 ;;
177 -v|--venv-file) venv_file="$2" ; shift 2 ;;
178 --system-site-packages) install_args="--system-site-packages" ;
182 "${FUNCNAME[0]}(): ERROR: Unknown switch '$1'." ;
190 # For Python2, just create venv and install pip
191 virtualenv -p "$python" "$lf_venv" || return 1
192 "$lf_venv/bin/pip" install --upgrade --quiet pip || return 1
193 if [[ -z $pkg_list ]]; then
194 echo "${FUNCNAME[0]}(): WARNING: No packages to install"
197 echo "${FUNCNAME[0]}(): INFO: Installing: $pkg_list"
198 # $pkg_list is expected to be unquoted
199 # shellcheck disable=SC2086
200 "$lf_venv/bin/pip" install --upgrade --quiet $pkg_list || return 1
204 # Use pyenv for selecting the python version
205 if [[ -d "/opt/pyenv" ]]; then
207 export PYENV_ROOT="/opt/pyenv"
208 export PATH="$PYENV_ROOT/bin:$PATH"
210 if command -v pyenv 1>/dev/null 2>&1; then
211 eval "$(pyenv init - --no-rehash)"
212 # shellcheck disable=SC2046
213 pyenv local $(lf-pyver "${python}")
217 # Add version specifier for some packages
221 pkg_list+="jenkins-job-builder==${JJB_VERSION:-6.3.0} " ;;
222 *) pkg_list+="$arg " ;;
228 # 1. --venv-file <path/to/file> as lf_venv
229 # 2. default: "/tmp/.os_lf_venv"
230 # - Create new venv when 1. and 2. is absent
231 if [[ -f "$venv_file" ]]; then
232 lf_venv=$(cat "$venv_file")
233 echo "${FUNCNAME[0]}(): INFO: Reuse venv:$lf_venv from" \
235 elif [[ ! -f "$venv_file" ]]; then
236 if [[ -n "$install_args" ]]; then
237 $python -m venv "$install_args" "$lf_venv" || return 1
239 $python -m venv "$lf_venv" || return 1
241 echo "${FUNCNAME[0]}(): INFO: Creating $python venv at $lf_venv"
242 echo "$lf_venv" > "$venv_file"
243 echo "${FUNCNAME[0]}(): INFO: Save venv in file: $venv_file"
246 "$lf_venv/bin/python3" -m pip install --upgrade --quiet pip \
247 virtualenv || return 1
248 if [[ -z $pkg_list ]]; then
249 echo "${FUNCNAME[0]}(): WARNING: No packages to install"
251 echo "${FUNCNAME[0]}(): INFO: Installing: $pkg_list"
252 # $pkg_list is expected to be unquoted
253 # shellcheck disable=SC2086
254 "$lf_venv/bin/python3" -m pip install --upgrade --quiet \
255 --upgrade-strategy eager $pkg_list || return 1
259 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: No support for: $python"
264 if ! type "$python" > /dev/null; then
265 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Unknown Python: $python"
270 echo "${FUNCNAME[0]}(): INFO: Adding $lf_venv/bin to PATH"
271 PATH=$lf_venv/bin:$PATH
274 echo "${FUNCNAME[0]}(): INFO: Path not set, lf_venv set to: $lf_venv"
277 } # End lf-activate-venv
279 ################################################################################
282 # lf-git-validate-jira-urls
285 # # shellcheck disable=SC1090
288 # lf-git-validate-jira-urls
291 # Check for JIRA URLS in the commit message
297 ################################################################################
299 lf-git-validate-jira-urls () {
300 echo "Checking for JIRA URLs in commit message..."
301 # if JIRA_URL is not defined, nothing to do
302 if [[ -v JIRA_URL ]]; then
303 base_url=$(echo "$JIRA_URL" | awk -F'/' '{print $3}')
304 jira_link=$(git rev-list --format=%B --max-count=1 HEAD | \
305 grep -io "http[s]*://$base_url/" || true)
306 if [[ -n $jira_link ]]; then
308 "${FUNCNAME[0]}(): ERROR: JIRA URL found in commit message"
310 'Add jira references as: Issue: <JIRAKEY>-<ISSUE#>,'\
315 echo "${FUNCNAME[0]}(): WARNING: JIRA_URL not set, continuing anyway"
320 ################################################################################
326 # # shellcheck disable=SC1090
332 # Check for JJB YAML files containing non-printable ascii characters. This
333 # function must be run from the top of the global-jjb repo.
339 ################################################################################
341 lf-jjb-check-ascii () {
342 if [[ ! -d "jjb" ]]; then
343 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: missing jjb directory"
345 "This function can only be run from top of global-jjb directory"
348 if LC_ALL=C grep -I -r '[^[:print:][:space:]]' jjb/; then
350 "${FUNCNAME[0]}(): ERROR: Found YAML files containing"\
351 " non-printable characters."
354 echo "${FUNCNAME[0]}(): INFO: All JJB YAML files contain only printable"\
359 ################################################################################
360 # Functions that assign Variables
361 ################################################################################
363 # These variables are shell (local) variables and need to be lower-case so
364 # Shellcheck knows they are shell variables and will check for
367 lf-set-maven-options () {
368 # Disable 'unused-variable' check
369 # shellcheck disable=SC2034
370 maven_options="--show-version --batch-mode -Djenkins \
371 -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.\
372 transfer.Slf4jMavenTransferListener=warn \
373 -Dmaven.repo.local=/tmp/r \
374 -Dorg.ops4j.pax.url.mvn.localRepository=/tmp/r"
377 ################################################################################
380 # lf-pyver [python-version X.Y]
383 # pyver 3.8 (outputs 3.8.13)
385 # pyver 3.10 (outputs 3.10.6)
387 # pyver 3 (outputs the most recent version 3.10.6)
390 # The function takes short python version in the format and X.Y and prints
391 # the semver format (X.Y.Z) of the version that has been installed on the system
394 # When the expected version is not installed, nothing is returned.
400 ################################################################################
403 local py_version_xy="${1:-python3}"
404 local py_version_xyz=""
406 pyenv versions | sed 's/^[ *]* //' | awk '{ print $1 }' | grep -E '^[0-9.]*[0-9]$' \
407 > "/tmp/.pyenv_versions"
408 if [[ ! -s "/tmp/.pyenv_versions" ]]; then
409 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: pyenv not available"
413 # strip out any prefix for (ex: 'python3.8' or 'v3.8') and match regex
414 # to the output return by pyenv
415 py_version_xyz=$(grep "^${py_version_xy//[a-zA-Z]/}" "/tmp/.pyenv_versions" |
416 sort -V | tail -n 1;)
417 if [[ -z ${py_version_xyz} ]]; then
418 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Not installed on host: ${py_version_xy}"
421 echo "${py_version_xyz}"