Update lf-env Bash Library
[releng/global-jjb.git] / jenkins-init-scripts / lf-env.sh
1 #!/usr/bin/no-execute
2
3 # SPDX-License-Identifier: EPL-1.0
4 ##############################################################################
5 # Copyright (c) 2019 The Linux Foundation and others.
6 #
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 ##############################################################################
12 #
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 variable available, provide
16 # a function that sets the variable: 'function lf_set_foo() {foo=asdf;}'. Any
17 # scripts that need access to the variable can call the 'set' function. This
18 # keeps the name-space pollution to a minimum.
19 #
20 # This script will be installed in ~jenkins by the Jenkins Init Script when the
21 # build agent boots. When the build starts it should already be installed.
22 #
23 ################################################################################
24 #
25 # Name:    lf-echo-stderr
26 #
27 # SYNOPSIS
28 #   source ~/lf-env.sh
29 #
30 #   lf-echo-stderr "this entire" "string will be sent to stderr"
31 #
32 # DESCRIPTION
33 #   This function will echo all command line aruments to 'stderr'
34 #
35 # RETURN VALUE
36 #   None
37 #
38 ################################################################################
39
40 function lf-echo-stderr() { echo "$@" 1>&2; }
41
42 ################################################################################
43 #
44 # NAME
45 #       lf-boolean()
46 #
47 # SYNOPSIS
48 #   # shellcheck disable=SC1090
49 #   source ~/lf-env.sh
50 #
51 #   if lf-boolean $VAR; then
52 #       echo "VAR is true"
53 #   fi
54 #
55 # DESCRIPTION
56 #   This function will return a BOOLEAN (true or false) based upon the value
57 #   of VAR. The value of VAR will be mapped to lower case. If VAR maps to
58 #   "true", return true(0). If VAR maps to "false", return false(1).  Any
59 #   other values will return false(2) and an error message.
60 #
61 # RETURN VALUES
62 #   OK: 0
63 #   Fail: 1 or 2
64 #
65 ################################################################################
66
67 function lf-boolean()
68 {
69     if (( $# != 1 )); then
70         echo "ERROR: ${FUNCNAME[0]}() line: ${BASH_LINENO[0]} : Missing Required Argument"
71         return 1
72     fi
73     local bool
74     bool=$(echo "$1" | tr '[:upper:]' '[:lower:]')
75     case $bool in
76         true)  return 0 ;;
77         false) return 1 ;;
78         '')
79            lf-echo-stderr "ERROR: ${FUNCNAME[0]}() line:{BASH_LINENO[0]} : A boolean cannot be a empty string" >&2
80            return 2
81            ;;
82         *)
83             lf-echo-stderr "ERROR: ${FUNCNAME[0]}() line: ${BASH_LINENO[0]} : Invalid value for a boolean: '$bool'"
84             return 2
85             ;;
86     esac
87 }
88
89 ################################################################################
90 #
91 # NAME
92 #   lf-activate-venv [-p|--python python] [--no-path]
93 #                    [--system-site-packages] [package]...
94 #
95 # SYNOPSIS
96 #   # shellcheck disable=SC1090
97 #   source ~/lf-env.sh
98 #
99 #   lf-activate-venv tox tox-pyenv
100 #   or
101 #   lf-activate-venv jenkins-job-builder
102 #   or
103 #   lf-activate-venv lftools
104 #   or
105 #   lf-activate-venv --python 3.6 git-review
106 #
107 # DESCRIPTION
108 #   This function will create a new Python Virtual Environment (venv) and
109 #   install the specified packages in the new venv.  The venv will be installed
110 #   in $lf_venv and by default, the $lf_venv/bin directory will be prepended
111 #   to the PATH.
112 #
113 #   The 'lf_venv' variable will be set so you can directly execute commands
114 #   in the venv with: $lf_venv/bin/command. Beware that subsequent calls to
115 #   lf-activate-venv() will overwrite 'lf_venv'.
116 #
117 #   By default all packages are installed with '--upgrade-strategy eager'.
118 #   The venv will always contain pip & virtualenv.
119 #
120 #   Some packages have a default version. If one of those packages is specified,
121 #   the 'version' specifier will be added for the install. If the version is
122 #   specified on the command line that version will be used.
123 #   The following packages have default versions:
124 #       Package                  Version
125 #       jenkins-job-builder      $JJB_VERSION
126 #
127 #   If the --python flag is specified, the specified python executable will be
128 #   used to create the venv. The --python option must be in the PATH. The venv
129 #   will be located in $lf_venv (/tmp/venv-####).
130 #
131 #   If the --no-path flag is specified, $lf_venv/bin will not be prepended to
132 #   the PATH.
133 #
134 #   If the --system-site-packages flag is specified, the --system-site-packages
135 #   flag will be passed to the inital 'pip install' (python3* only).
136 #
137 # RETURN VALUES
138 #   OK: 0
139 #   Fail: 1
140 #
141 ################################################################################
142
143 function lf-activate-venv()
144 {
145     lf_venv=$(mktemp -d /tmp/venv-XXXX)
146     local python=python3
147     local options
148     local set_path=true
149     local install_args=""
150     options=$(getopt -o 'n:p:' -l 'no-path,python:,system-site-packages' -n "${FUNCNAME[0]}" -- "$@" )
151     eval set -- "$options"
152     while true; do
153         case $1 in
154             -n|--no-path) set_path=false ; shift   ;;
155             -p|--python)  python=$2      ; shift 2 ;;
156             --system-site-packages) install_args="--system-site-packages" ; shift ;;
157             --) shift; break ;;
158             *)  lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Unknown switch '$1'." ; return 1 ;;
159         esac
160     done
161     if ! type $python > /dev/null; then
162         lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Unknown Python: $python"
163         return 1
164     fi
165
166     echo "${FUNCNAME[0]}(): INFO: Creating $python venv at $lf_venv"
167
168     case $python in
169     python2*)
170         local pkg_list="$*"
171         # For Python2, just create venv and install pip
172         virtualenv -p $python $lf_venv || return 1
173         $lf_venv/bin/pip install --upgrade --quiet pip || return 1
174         if [[ -z $pkg_list ]]; then
175             echo "${FUNCNAME[0]}(): WARNING: No packages to install"
176             return 0
177         fi
178         echo "${FUNCNAME[0]}(): INFO: Installing: $pkg_list"
179         $lf_venv/bin/pip install --upgrade --quiet $pkg_list || return 1
180         ;;
181     python3*)
182         local pkg_list=""
183         # Add version specifier for some packages
184         for arg in "$@"; do
185             case $arg in
186                 jenkins-job-builder) pkg_list+="jenkins-job-builder==${JJB_VERSION:-2.8.0} " ;;
187                 *)                   pkg_list+="$arg " ;;
188             esac
189         done
190         $python -m venv $install_args $lf_venv || return 1
191         $lf_venv/bin/pip install --upgrade --quiet pip virtualenv || return 1
192         if [[ -z $pkg_list ]]; then
193             echo "${FUNCNAME[0]}(): WARNING: No packages to install"
194             return 0
195         fi
196         echo "${FUNCNAME[0]}(): INFO: Installing: $pkg_list"
197         $lf_venv/bin/pip install --upgrade --quiet --upgrade-strategy eager \
198                              $pkg_list || return 1
199         ;;
200     *)
201         lf-echo-stderr "${FUNCNAME[0]}(): ERROR: No support for: $python"
202         return 1
203         ;;
204     esac
205     if $set_path; then
206         echo "${FUNCNAME[0]}(): INFO: Adding $lf_venv/bin to PATH"
207         PATH=$lf_venv/bin:$PATH
208         return 0
209     else
210         echo "${FUNCNAME[0]}(): INFO: Path not set, lf_venv set to: $lf_venv"
211     fi
212
213 }   # End lf-activate-venv()
214
215 ################################################################################
216 #
217 # NAME
218 #   lf-git-validate-jira-urls
219 #
220 # SYNOPSIS
221 #   # shellcheck disable=SC1090
222 #   source ~/lf-env.sh
223 #
224 #   lf-git-validate-jira-urls
225 #
226 # DESCRIPTION
227 #   Check for JIRA URLS in the commit message
228 #
229 # RETURN VALUES
230 #   OK: 0
231 #   Fail: 1
232 #
233 ################################################################################
234
235 function lf-git-validate-jira-urls()
236 {
237     echo "Checking for JIRA URLs in commit message..."
238     # if JIRA_URL is not defined, nothing to do
239     if [[ -v JIRA_URL ]]; then
240         base_url=$(echo "$JIRA_URL" | awk -F'/' '{print $3}')
241         jira_link=$(git rev-list --format=%B --max-count=1 HEAD | grep -io "http[s]*://$base_url/" || true)
242         if [[ -n $jira_link ]]; then
243             lf-echo-stderr "${FUNCNAME[0]}(): ERROR: JIRA URL found in commit message"
244             lf-echo-stderr 'Add jira references as: Issue: <JIRAKEY>-<ISSUE#>, instead of URLs'
245             return 1
246         fi
247     else
248         echo "${FUNCNAME[0]}(): WARNING: JIRA_URL not set, continuing anyway"
249     fi
250     return 0
251 }
252
253 ################################################################################
254 #
255 # NAME
256 #   lf-jjb-check-ascii()
257 #
258 # SYNOPSIS
259 #   # shellcheck disable=SC1090
260 #   source ~/lf-env.sh
261 #
262 #   lf-jjb-check-ascii
263 #
264 # DESCRIPTION
265 #   Check for JJB YAML files containing non-printable ascii characters. This
266 #   function must be run from the top of the global-jjb repo.
267 #
268 # RETURN VALUES
269 #   OK: 0
270 #   Fail: 1
271 #
272 ################################################################################
273
274 function lf-jjb-check-ascii()
275 {
276     if [[ ! -d "jjb" ]]; then
277         lf-echo-stderr "${FUNCNAME[0]}(): ERROR: missing jjb directory"
278         lf-echo-stderr "This function can only be run from top of global-jjb directory"
279         return 1
280     fi
281     if LC_ALL=C grep -I -r '[^[:print:][:space:]]' jjb/; then
282         lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Found YAML files containing non-printable characters."
283         return 1
284     fi
285     echo "${FUNCNAME[0]}(): INFO: All JJB YAML files contain only printable ASCII characters"
286     return 0
287 }
288
289 ################################################################################
290 # Functions that assign Variables
291 ################################################################################
292
293 # These variables are shell (local) variables and need to be lower-case so
294 # Shellcheck knows they are shell variables and will check for
295 # 'used-before-set'.
296
297 function lf-set-maven-options()
298 {
299     # Disable 'unused-variable' check
300     # shellcheck disable=SC2034
301     maven_options="--show-version --batch-mode -Djenkins \
302         -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn \
303         -Dmaven.repo.local=/tmp/r -Dorg.ops4j.pax.url.mvn.localRepository=/tmp/r"
304 }