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 3.6 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'." ;
170 if ! type "$python" > /dev/null; then
171 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Unknown Python: $python"
175 echo "${FUNCNAME[0]}(): INFO: Creating $python venv at $lf_venv"
180 # For Python2, just create venv and install pip
181 virtualenv -p "$python" "$lf_venv" || return 1
182 "$lf_venv/bin/pip" install --upgrade --quiet pip || return 1
183 if [[ -z $pkg_list ]]; then
184 echo "${FUNCNAME[0]}(): WARNING: No packages to install"
187 echo "${FUNCNAME[0]}(): INFO: Installing: $pkg_list"
188 # $pkg_list is expected to be unquoted
189 # shellcheck disable=SC2086
190 "$lf_venv/bin/pip" install --upgrade --quiet $pkg_list || return 1
194 # Add version specifier for some packages
198 pkg_list+="jenkins-job-builder==${JJB_VERSION:-2.8.0} " ;;
199 *) pkg_list+="$arg " ;;
202 $python -m venv "$install_args" "$lf_venv" || return 1
203 "$lf_venv/bin/pip" install --upgrade --quiet pip virtualenv || return 1
204 if [[ -z $pkg_list ]]; then
205 echo "${FUNCNAME[0]}(): WARNING: No packages to install"
208 echo "${FUNCNAME[0]}(): INFO: Installing: $pkg_list"
209 # $pkg_list is expected to be unquoted
210 # shellcheck disable=SC2086
211 "$lf_venv/bin/pip" install --upgrade --quiet --upgrade-strategy eager \
212 $pkg_list || return 1
215 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: No support for: $python"
220 echo "${FUNCNAME[0]}(): INFO: Adding $lf_venv/bin to PATH"
221 PATH=$lf_venv/bin:$PATH
224 echo "${FUNCNAME[0]}(): INFO: Path not set, lf_venv set to: $lf_venv"
227 } # End lf-activate-venv
229 ################################################################################
232 # lf-git-validate-jira-urls
235 # # shellcheck disable=SC1090
238 # lf-git-validate-jira-urls
241 # Check for JIRA URLS in the commit message
247 ################################################################################
249 lf-git-validate-jira-urls () {
250 echo "Checking for JIRA URLs in commit message..."
251 # if JIRA_URL is not defined, nothing to do
252 if [[ -v JIRA_URL ]]; then
253 base_url=$(echo "$JIRA_URL" | awk -F'/' '{print $3}')
254 jira_link=$(git rev-list --format=%B --max-count=1 HEAD | \
255 grep -io "http[s]*://$base_url/" || true)
256 if [[ -n $jira_link ]]; then
258 "${FUNCNAME[0]}(): ERROR: JIRA URL found in commit message"
260 'Add jira references as: Issue: <JIRAKEY>-<ISSUE#>,'\
265 echo "${FUNCNAME[0]}(): WARNING: JIRA_URL not set, continuing anyway"
270 ################################################################################
276 # # shellcheck disable=SC1090
282 # Check for JJB YAML files containing non-printable ascii characters. This
283 # function must be run from the top of the global-jjb repo.
289 ################################################################################
291 lf-jjb-check-ascii () {
292 if [[ ! -d "jjb" ]]; then
293 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: missing jjb directory"
295 "This function can only be run from top of global-jjb directory"
298 if LC_ALL=C grep -I -r '[^[:print:][:space:]]' jjb/; then
300 "${FUNCNAME[0]}(): ERROR: Found YAML files containing"\
301 " non-printable characters."
304 echo "${FUNCNAME[0]}(): INFO: All JJB YAML files contain only printable"\
309 ################################################################################
310 # Functions that assign Variables
311 ################################################################################
313 # These variables are shell (local) variables and need to be lower-case so
314 # Shellcheck knows they are shell variables and will check for
317 lf-set-maven-options () {
318 # Disable 'unused-variable' check
319 # shellcheck disable=SC2034
320 maven_options="--show-version --batch-mode -Djenkins \
321 -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.\
322 transfer.Slf4jMavenTransferListener=warn \
323 -Dmaven.repo.local=/tmp/r \
324 -Dorg.ops4j.pax.url.mvn.localRepository=/tmp/r"