Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / jenkins-init-scripts / lf-env.sh
1 #!/usr/bin/no-execute
2 # shellcheck shell=bash
3
4 # SPDX-License-Identifier: EPL-1.0
5 ##############################################################################
6 # Copyright (c) 2019 The Linux Foundation and others.
7 #
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 ##############################################################################
13 #
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.
20 #
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.
23 #
24 ################################################################################
25 #
26 # NAME
27 #       lf-echo-stderr
28 #
29 # SYNOPSIS
30 #   source ~/lf-env.sh
31 #
32 #   lf-echo-stderr "this entire" "string will be sent to stderr"
33 #
34 # DESCRIPTION
35 #   This function will echo all command line aruments to 'stderr'
36 #
37 # RETURN VALUE
38 #   None
39 #
40 ################################################################################
41
42 lf-echo-stderr () {
43     echo "$@" 1>&2
44 }
45
46 ################################################################################
47 #
48 # NAME
49 #       lf-boolean
50 #
51 # SYNOPSIS
52 #   # shellcheck disable=SC1090
53 #   source ~/lf-env.sh
54 #
55 #   if lf-boolean $VAR; then
56 #       echo "VAR is true"
57 #   fi
58 #
59 # DESCRIPTION
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.
64 #
65 # RETURN VALUES
66 #   OK: 0
67 #   Fail: 1 or 2
68 #
69 ################################################################################
70
71 lf-boolean () {
72     if (( $# != 1 )); then
73         echo "ERROR: ${FUNCNAME[0]}() line: ${BASH_LINENO[0]} :"\
74         " Missing Required Argument"
75         return 1
76     fi
77     local bool
78     bool=$(echo "$1" | tr '[:upper:]' '[:lower:]')
79     case $bool in
80         true)  return 0 ;;
81         false) return 1 ;;
82         '')
83             lf-echo-stderr "ERROR: ${FUNCNAME[0]}() line:{BASH_LINENO[0]} :"\
84             " A boolean cannot be a empty string" >&2
85             return 2
86             ;;
87         *)
88             lf-echo-stderr "ERROR: ${FUNCNAME[0]}() line: ${BASH_LINENO[0]} :"\
89             " Invalid value for a boolean: '$bool'"
90             return 2
91             ;;
92     esac
93 }
94
95 ################################################################################
96 #
97 # NAME
98 #   lf-activate-venv [-p|--python python] [-v|--venv-file] [--no-path]
99 #                    [--system-site-packages] [package]...
100 #
101 # SYNOPSIS
102 #   # shellcheck disable=SC1090
103 #   source ~/lf-env.sh
104 #
105 #   lf-activate-venv tox tox-pyenv
106 #   or
107 #   lf-activate-venv jenkins-job-builder
108 #   or
109 #   lf-activate-venv lftools
110 #   or
111 #   lf-activate-venv --python python3.8 git-review
112 #
113 #   lf-activate-venv --python python3.8 --venv-file /tmp/.myvenv git-review
114 #
115 # DESCRIPTION
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
119 #   to the PATH.
120 #
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.
124 #
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.
128 #
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'
131 #   already exists.
132 #
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.
135 #
136 #   By default all packages are installed with '--upgrade-strategy eager'.
137 #   The venv will always contain pip & virtualenv.
138 #
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:
143 #       Package                  Version
144 #       jenkins-job-builder      $JJB_VERSION
145 #
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-####).
149 #
150 #   If the --no-path flag is specified, $lf_venv/bin will not be prepended to
151 #   the PATH.
152 #
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).
155 #
156 # RETURN VALUES
157 #   OK: 0
158 #   Fail: 1
159 #
160 ################################################################################
161
162 lf-activate-venv () {
163     lf_venv=$(mktemp -d /tmp/venv-XXXX)
164     local venv_file="/tmp/.os_lf_venv"
165     local python=python3
166     local options
167     local set_path=true
168     local install_args=""
169     # set -x
170     options=$(getopt -o 'np:v:' -l 'no-path,system-site-packages,python:,venv-file:' \
171                 -n "${FUNCNAME[0]}" -- "$@" )
172     eval set -- "$options"
173     while true; do
174         case $1 in
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" ;
179                                     shift ;;
180             --) shift; break ;;
181             *)  lf-echo-stderr \
182                 "${FUNCNAME[0]}(): ERROR: Unknown switch '$1'." ;
183                 return 1 ;;
184         esac
185     done
186
187     case $python in
188     python2*)
189         local pkg_list="$*"
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"
195             return 0
196         fi
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
201         ;;
202     python3*)
203         local pkg_list=""
204         # Use pyenv for selecting the python version
205         if [[ -d "/opt/pyenv" ]]; then
206             echo "Setup pyenv:"
207             export PYENV_ROOT="/opt/pyenv"
208             export PATH="$PYENV_ROOT/bin:$PATH"
209             pyenv versions
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}")
214             fi
215         fi
216
217         # Add version specifier for some packages
218         for arg in "$@"; do
219             case $arg in
220                 jenkins-job-builder)
221                     pkg_list+="jenkins-job-builder==${JJB_VERSION:-6.2.0} " ;;
222                 *)                   pkg_list+="$arg " ;;
223             esac
224         done
225
226         # Precedence:
227         # - Re-use venv:
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" \
234                 "file:$venv_file"
235         elif [[ ! -f "$venv_file" ]]; then
236             if [[ -n "$install_args" ]]; then
237                 $python -m venv "$install_args" "$lf_venv" || return 1
238             else
239                 $python -m venv "$lf_venv" || return 1
240             fi
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"
244         fi
245
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"
250         else
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
256         fi
257         ;;
258     *)
259         lf-echo-stderr "${FUNCNAME[0]}(): ERROR: No support for: $python"
260         return 1
261         ;;
262     esac
263
264     if ! type "$python" > /dev/null; then
265         lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Unknown Python: $python"
266         return 1
267     fi
268
269     if $set_path; then
270         echo "${FUNCNAME[0]}(): INFO: Adding $lf_venv/bin to PATH"
271         PATH=$lf_venv/bin:$PATH
272         return 0
273     else
274         echo "${FUNCNAME[0]}(): INFO: Path not set, lf_venv set to: $lf_venv"
275     fi
276
277 }   # End lf-activate-venv
278
279 ################################################################################
280 #
281 # NAME
282 #   lf-git-validate-jira-urls
283 #
284 # SYNOPSIS
285 #   # shellcheck disable=SC1090
286 #   source ~/lf-env.sh
287 #
288 #   lf-git-validate-jira-urls
289 #
290 # DESCRIPTION
291 #   Check for JIRA URLS in the commit message
292 #
293 # RETURN VALUES
294 #   OK: 0
295 #   Fail: 1
296 #
297 ################################################################################
298
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
307             lf-echo-stderr \
308             "${FUNCNAME[0]}(): ERROR: JIRA URL found in commit message"
309             lf-echo-stderr \
310             'Add jira references as: Issue: <JIRAKEY>-<ISSUE#>,'\
311             ' instead of URLs'
312             return 1
313         fi
314     else
315         echo "${FUNCNAME[0]}(): WARNING: JIRA_URL not set, continuing anyway"
316     fi
317     return 0
318 }
319
320 ################################################################################
321 #
322 # NAME
323 #   lf-jjb-check-ascii
324 #
325 # SYNOPSIS
326 #   # shellcheck disable=SC1090
327 #   source ~/lf-env.sh
328 #
329 #   lf-jjb-check-ascii
330 #
331 # DESCRIPTION
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.
334 #
335 # RETURN VALUES
336 #   OK: 0
337 #   Fail: 1
338 #
339 ################################################################################
340
341 lf-jjb-check-ascii () {
342     if [[ ! -d "jjb" ]]; then
343         lf-echo-stderr "${FUNCNAME[0]}(): ERROR: missing jjb directory"
344         lf-echo-stderr \
345         "This function can only be run from top of global-jjb directory"
346         return 1
347     fi
348     if LC_ALL=C grep -I -r '[^[:print:][:space:]]' jjb/; then
349         lf-echo-stderr \
350         "${FUNCNAME[0]}(): ERROR: Found YAML files containing"\
351         " non-printable characters."
352         return 1
353     fi
354     echo "${FUNCNAME[0]}(): INFO: All JJB YAML files contain only printable"\
355     " ASCII characters"
356     return 0
357 }
358
359 ################################################################################
360 # Functions that assign Variables
361 ################################################################################
362
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
365 # 'used-before-set'.
366
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"
375 }
376
377 ################################################################################
378 #
379 # NAME
380 #   lf-pyver [python-version X.Y]
381 #
382 # SYNOPSIS
383 #   pyver 3.8 (outputs 3.8.13)
384 #   or
385 #   pyver 3.10 (outputs 3.10.6)
386 #   or
387 #   pyver 3 (outputs the most recent version 3.10.6)
388 #
389 # DESCRIPTION
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
392 #   with pyenv.
393 #
394 #   When the expected version is not installed, nothing is returned.
395 #
396 # RETURN VALUES
397 #   OK: 0
398 #   Fail: 1
399 #
400 ################################################################################
401
402 lf-pyver() {
403     local py_version_xy="${1:-python3}"
404     local py_version_xyz=""
405
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"
410         return 1
411     fi
412
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}"
419         return 1
420     fi
421     echo "${py_version_xyz}"
422     return 0
423 }