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