a1bd350c391d1181e15ee57037470b0334f92a50
[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] [--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 # DESCRIPTION
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
117 #   to the PATH.
118 #
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'.
122 #
123 #   By default all packages are installed with '--upgrade-strategy eager'.
124 #   The venv will always contain pip & virtualenv.
125 #
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:
130 #       Package                  Version
131 #       jenkins-job-builder      $JJB_VERSION
132 #
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-####).
136 #
137 #   If the --no-path flag is specified, $lf_venv/bin will not be prepended to
138 #   the PATH.
139 #
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).
142 #
143 # RETURN VALUES
144 #   OK: 0
145 #   Fail: 1
146 #
147 ################################################################################
148
149 lf-activate-venv () {
150     lf_venv=$(mktemp -d /tmp/venv-XXXX)
151     local python=python3
152     local options
153     local set_path=true
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"
158     while true; do
159         case $1 in
160             -n|--no-path) set_path=false ; shift   ;;
161             -p|--python)  python=$2      ; shift 2 ;;
162             --system-site-packages) install_args="--system-site-packages" ;
163                                     shift ;;
164             --) shift; break ;;
165             *)  lf-echo-stderr \
166                 "${FUNCNAME[0]}(): ERROR: Unknown switch '$1'." ;
167                 return 1 ;;
168         esac
169     done
170     if ! type "$python" > /dev/null; then
171         lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Unknown Python: $python"
172         return 1
173     fi
174
175     echo "${FUNCNAME[0]}(): INFO: Creating $python venv at $lf_venv"
176
177     case $python in
178     python2*)
179         local pkg_list="$*"
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"
185             return 0
186         fi
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
191         ;;
192     python3*)
193         local pkg_list=""
194         # Add version specifier for some packages
195         for arg in "$@"; do
196             case $arg in
197                 jenkins-job-builder)
198                     pkg_list+="jenkins-job-builder==${JJB_VERSION:-2.8.0} " ;;
199                 *)                   pkg_list+="$arg " ;;
200             esac
201         done
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"
206             return 0
207         fi
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
213         ;;
214     *)
215         lf-echo-stderr "${FUNCNAME[0]}(): ERROR: No support for: $python"
216         return 1
217         ;;
218     esac
219     if $set_path; then
220         echo "${FUNCNAME[0]}(): INFO: Adding $lf_venv/bin to PATH"
221         PATH=$lf_venv/bin:$PATH
222         return 0
223     else
224         echo "${FUNCNAME[0]}(): INFO: Path not set, lf_venv set to: $lf_venv"
225     fi
226
227 }   # End lf-activate-venv
228
229 ################################################################################
230 #
231 # NAME
232 #   lf-git-validate-jira-urls
233 #
234 # SYNOPSIS
235 #   # shellcheck disable=SC1090
236 #   source ~/lf-env.sh
237 #
238 #   lf-git-validate-jira-urls
239 #
240 # DESCRIPTION
241 #   Check for JIRA URLS in the commit message
242 #
243 # RETURN VALUES
244 #   OK: 0
245 #   Fail: 1
246 #
247 ################################################################################
248
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
257             lf-echo-stderr \
258             "${FUNCNAME[0]}(): ERROR: JIRA URL found in commit message"
259             lf-echo-stderr \
260             'Add jira references as: Issue: <JIRAKEY>-<ISSUE#>,'\
261             ' instead of URLs'
262             return 1
263         fi
264     else
265         echo "${FUNCNAME[0]}(): WARNING: JIRA_URL not set, continuing anyway"
266     fi
267     return 0
268 }
269
270 ################################################################################
271 #
272 # NAME
273 #   lf-jjb-check-ascii
274 #
275 # SYNOPSIS
276 #   # shellcheck disable=SC1090
277 #   source ~/lf-env.sh
278 #
279 #   lf-jjb-check-ascii
280 #
281 # DESCRIPTION
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.
284 #
285 # RETURN VALUES
286 #   OK: 0
287 #   Fail: 1
288 #
289 ################################################################################
290
291 lf-jjb-check-ascii () {
292     if [[ ! -d "jjb" ]]; then
293         lf-echo-stderr "${FUNCNAME[0]}(): ERROR: missing jjb directory"
294         lf-echo-stderr \
295         "This function can only be run from top of global-jjb directory"
296         return 1
297     fi
298     if LC_ALL=C grep -I -r '[^[:print:][:space:]]' jjb/; then
299         lf-echo-stderr \
300         "${FUNCNAME[0]}(): ERROR: Found YAML files containing"\
301         " non-printable characters."
302         return 1
303     fi
304     echo "${FUNCNAME[0]}(): INFO: All JJB YAML files contain only printable"\
305     " ASCII characters"
306     return 0
307 }
308
309 ################################################################################
310 # Functions that assign Variables
311 ################################################################################
312
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
315 # 'used-before-set'.
316
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"
325 }