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] [--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
114 # This function will create a new Python Virtual Environment (venv) and
115 # install the specified packages in the new venv. The venv will be installed
116 # in $lf_venv and by default, the $lf_venv/bin directory will be prepended
119 # The 'lf_venv' variable will be set so you can directly execute commands
120 # in the venv with: $lf_venv/bin/command. Beware that subsequent calls to
121 # lf-activate-venv() will overwrite 'lf_venv'.
123 # By default all packages are installed with '--upgrade-strategy eager'.
124 # The venv will always contain pip & virtualenv.
126 # Some packages have a default version. If one of those packages is specified,
127 # the 'version' specifier will be added for the install. If the version is
128 # specified on the command line that version will be used.
129 # The following packages have default versions:
131 # jenkins-job-builder $JJB_VERSION
133 # If the --python flag is specified, the specified python executable will be
134 # used to create the venv. The --python option must be in the PATH. The venv
135 # will be located in $lf_venv (/tmp/venv-####).
137 # If the --no-path flag is specified, $lf_venv/bin will not be prepended to
140 # If the --system-site-packages flag is specified, the --system-site-packages
141 # flag will be passed to the inital 'pip install' (python3* only).
147 ################################################################################
149 lf-activate-venv () {
150 lf_venv=$(mktemp -d /tmp/venv-XXXX)
154 local install_args=""
155 options=$(getopt -o 'n:p:' -l 'no-path,python:,system-site-packages' \
156 -n "${FUNCNAME[0]}" -- "$@" )
157 eval set -- "$options"
160 -n|--no-path) set_path=false ; shift ;;
161 -p|--python) python=$2 ; shift 2 ;;
162 --system-site-packages) install_args="--system-site-packages" ;
166 "${FUNCNAME[0]}(): ERROR: Unknown switch '$1'." ;
171 echo "${FUNCNAME[0]}(): INFO: Creating $python venv at $lf_venv"
176 # For Python2, just create venv and install pip
177 virtualenv -p "$python" "$lf_venv" || return 1
178 "$lf_venv/bin/pip" install --upgrade --quiet pip || return 1
179 if [[ -z $pkg_list ]]; then
180 echo "${FUNCNAME[0]}(): WARNING: No packages to install"
183 echo "${FUNCNAME[0]}(): INFO: Installing: $pkg_list"
184 # $pkg_list is expected to be unquoted
185 # shellcheck disable=SC2086
186 "$lf_venv/bin/pip" install --upgrade --quiet $pkg_list || return 1
190 # Use pyenv for selecting the python version
191 if [[ -d "/opt/pyenv" ]]; then
192 # set_python_version = pyver "${python//[a-zA-Z]/}"
193 echo "---> Setting up pyenv"
194 export PYENV_ROOT="/opt/pyenv"
195 export PATH="$PYENV_ROOT/bin:$PATH"
197 if command -v pyenv 1>/dev/null 2>&1; then
198 eval "$(pyenv init - --no-rehash)"
199 pyenv local $(lf-pyver "${python}")
202 # Add version specifier for some packages
206 pkg_list+="jenkins-job-builder==${JJB_VERSION:-2.8.0} " ;;
207 *) pkg_list+="$arg " ;;
210 $python -m venv "$install_args" "$lf_venv" || return 1
211 "$lf_venv/bin/pip" install --upgrade --quiet pip virtualenv || return 1
212 if [[ -z $pkg_list ]]; then
213 echo "${FUNCNAME[0]}(): WARNING: No packages to install"
216 echo "${FUNCNAME[0]}(): INFO: Installing: $pkg_list"
217 # $pkg_list is expected to be unquoted
218 # shellcheck disable=SC2086
219 "$lf_venv/bin/pip" install --upgrade --quiet --upgrade-strategy eager \
220 $pkg_list || return 1
223 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: No support for: $python"
228 if ! type "$python" > /dev/null; then
229 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Unknown Python: $python"
234 echo "${FUNCNAME[0]}(): INFO: Adding $lf_venv/bin to PATH"
235 PATH=$lf_venv/bin:$PATH
238 echo "${FUNCNAME[0]}(): INFO: Path not set, lf_venv set to: $lf_venv"
241 } # End lf-activate-venv
243 ################################################################################
246 # lf-git-validate-jira-urls
249 # # shellcheck disable=SC1090
252 # lf-git-validate-jira-urls
255 # Check for JIRA URLS in the commit message
261 ################################################################################
263 lf-git-validate-jira-urls () {
264 echo "Checking for JIRA URLs in commit message..."
265 # if JIRA_URL is not defined, nothing to do
266 if [[ -v JIRA_URL ]]; then
267 base_url=$(echo "$JIRA_URL" | awk -F'/' '{print $3}')
268 jira_link=$(git rev-list --format=%B --max-count=1 HEAD | \
269 grep -io "http[s]*://$base_url/" || true)
270 if [[ -n $jira_link ]]; then
272 "${FUNCNAME[0]}(): ERROR: JIRA URL found in commit message"
274 'Add jira references as: Issue: <JIRAKEY>-<ISSUE#>,'\
279 echo "${FUNCNAME[0]}(): WARNING: JIRA_URL not set, continuing anyway"
284 ################################################################################
290 # # shellcheck disable=SC1090
296 # Check for JJB YAML files containing non-printable ascii characters. This
297 # function must be run from the top of the global-jjb repo.
303 ################################################################################
305 lf-jjb-check-ascii () {
306 if [[ ! -d "jjb" ]]; then
307 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: missing jjb directory"
309 "This function can only be run from top of global-jjb directory"
312 if LC_ALL=C grep -I -r '[^[:print:][:space:]]' jjb/; then
314 "${FUNCNAME[0]}(): ERROR: Found YAML files containing"\
315 " non-printable characters."
318 echo "${FUNCNAME[0]}(): INFO: All JJB YAML files contain only printable"\
323 ################################################################################
324 # Functions that assign Variables
325 ################################################################################
327 # These variables are shell (local) variables and need to be lower-case so
328 # Shellcheck knows they are shell variables and will check for
331 lf-set-maven-options () {
332 # Disable 'unused-variable' check
333 # shellcheck disable=SC2034
334 maven_options="--show-version --batch-mode -Djenkins \
335 -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.\
336 transfer.Slf4jMavenTransferListener=warn \
337 -Dmaven.repo.local=/tmp/r \
338 -Dorg.ops4j.pax.url.mvn.localRepository=/tmp/r"
341 ################################################################################
344 # lf-pyver [python-version X.Y]
347 # pyver 3.8 (outputs 3.8.13)
349 # pyver 3.10 (outputs 3.10.6)
351 # pyver 3 (outputs the most recent version 3.10.6)
354 # The function takes short python version in the format and X.Y and prints
355 # the semver format (X.Y.Z) of the version that has been installed on the system
358 # When the expected version is not installed, nothing is returned.
364 ################################################################################
367 local py_version_xy="${1:-python3}"
368 local py_version_xyz=""
370 pyenv versions | sed 's/[[:alpha:]|(|)|/||*|[:space:]]//g'| tr -d ' ' \
371 > "/tmp/.pyenv_versions"
372 if [[ ! -s "/tmp/.pyenv_versions" ]]; then
373 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: pyenv not available"
377 # strip out any prefix for (ex: 'python3.8' or 'v3.8') and match regex
378 # to the output return by pyenv
379 py_version_xyz=$(grep "^${py_version_xy//[a-zA-Z]/}\(\..*\)\?\.[0-9]\+$" \
380 "/tmp/.pyenv_versions" | sort -V | tail -n 1;)
381 if [[ ! -n ${py_version_xyz} ]]; then
382 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Not installed on host: ${py_version_xy}"
385 echo "${py_version_xyz}"