24a7637b24f5f995b11c48ae24a5af726405b175
[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]} : Missing Required Argument"
74         return 1
75     fi
76     local bool
77     bool=$(echo "$1" | tr '[:upper:]' '[:lower:]')
78     case $bool in
79         true)  return 0 ;;
80         false) return 1 ;;
81         '')
82             lf-echo-stderr "ERROR: ${FUNCNAME[0]}() line:{BASH_LINENO[0]} : A boolean cannot be a empty string" >&2
83             return 2
84             ;;
85         *)
86             lf-echo-stderr "ERROR: ${FUNCNAME[0]}() line: ${BASH_LINENO[0]} : Invalid value for a boolean: '$bool'"
87             return 2
88             ;;
89     esac
90 }
91
92 ################################################################################
93 #
94 # NAME
95 #   lf-activate-venv [-p|--python python] [--no-path]
96 #                    [--system-site-packages] [package]...
97 #
98 # SYNOPSIS
99 #   # shellcheck disable=SC1090
100 #   source ~/lf-env.sh
101 #
102 #   lf-activate-venv tox tox-pyenv
103 #   or
104 #   lf-activate-venv jenkins-job-builder
105 #   or
106 #   lf-activate-venv lftools
107 #   or
108 #   lf-activate-venv --python 3.6 git-review
109 #
110 # DESCRIPTION
111 #   This function will create a new Python Virtual Environment (venv) and
112 #   install the specified packages in the new venv.  The venv will be installed
113 #   in $lf_venv and by default, the $lf_venv/bin directory will be prepended
114 #   to the PATH.
115 #
116 #   The 'lf_venv' variable will be set so you can directly execute commands
117 #   in the venv with: $lf_venv/bin/command. Beware that subsequent calls to
118 #   lf-activate-venv() will overwrite 'lf_venv'.
119 #
120 #   By default all packages are installed with '--upgrade-strategy eager'.
121 #   The venv will always contain pip & virtualenv.
122 #
123 #   Some packages have a default version. If one of those packages is specified,
124 #   the 'version' specifier will be added for the install. If the version is
125 #   specified on the command line that version will be used.
126 #   The following packages have default versions:
127 #       Package                  Version
128 #       jenkins-job-builder      $JJB_VERSION
129 #
130 #   If the --python flag is specified, the specified python executable will be
131 #   used to create the venv. The --python option must be in the PATH. The venv
132 #   will be located in $lf_venv (/tmp/venv-####).
133 #
134 #   If the --no-path flag is specified, $lf_venv/bin will not be prepended to
135 #   the PATH.
136 #
137 #   If the --system-site-packages flag is specified, the --system-site-packages
138 #   flag will be passed to the inital 'pip install' (python3* only).
139 #
140 # RETURN VALUES
141 #   OK: 0
142 #   Fail: 1
143 #
144 ################################################################################
145
146 lf-activate-venv () {
147     lf_venv=$(mktemp -d /tmp/venv-XXXX)
148     local python=python3
149     local options
150     local set_path=true
151     local install_args=""
152     options=$(getopt -o 'n:p:' -l 'no-path,python:,system-site-packages' -n "${FUNCNAME[0]}" -- "$@" )
153     eval set -- "$options"
154     while true; do
155         case $1 in
156             -n|--no-path) set_path=false ; shift   ;;
157             -p|--python)  python=$2      ; shift 2 ;;
158             --system-site-packages) install_args="--system-site-packages" ; shift ;;
159             --) shift; break ;;
160             *)  lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Unknown switch '$1'." ; return 1 ;;
161         esac
162     done
163     if ! type "$python" > /dev/null; then
164         lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Unknown Python: $python"
165         return 1
166     fi
167
168     echo "${FUNCNAME[0]}(): INFO: Creating $python venv at $lf_venv"
169
170     case $python in
171     python2*)
172         local pkg_list="$*"
173         # For Python2, just create venv and install pip
174         virtualenv -p "$python" "$lf_venv" || return 1
175         "$lf_venv/bin/pip" install --upgrade --quiet pip || return 1
176         if [[ -z $pkg_list ]]; then
177             echo "${FUNCNAME[0]}(): WARNING: No packages to install"
178             return 0
179         fi
180         echo "${FUNCNAME[0]}(): INFO: Installing: $pkg_list"
181         # $pkg_list is expected to be unquoted
182         # shellcheck disable=SC2086
183         "$lf_venv/bin/pip" install --upgrade --quiet $pkg_list || return 1
184         ;;
185     python3*)
186         local pkg_list=""
187         # Add version specifier for some packages
188         for arg in "$@"; do
189             case $arg in
190                 jenkins-job-builder) pkg_list+="jenkins-job-builder==${JJB_VERSION:-2.8.0} " ;;
191                 *)                   pkg_list+="$arg " ;;
192             esac
193         done
194         $python -m venv "$install_args" "$lf_venv" || return 1
195         "$lf_venv/bin/pip" install --upgrade --quiet pip virtualenv || return 1
196         if [[ -z $pkg_list ]]; then
197             echo "${FUNCNAME[0]}(): WARNING: No packages to install"
198             return 0
199         fi
200         echo "${FUNCNAME[0]}(): INFO: Installing: $pkg_list"
201         # $pkg_list is expected to be unquoted
202         # shellcheck disable=SC2086
203         "$lf_venv/bin/pip" install --upgrade --quiet --upgrade-strategy eager \
204                                 $pkg_list || return 1
205         ;;
206     *)
207         lf-echo-stderr "${FUNCNAME[0]}(): ERROR: No support for: $python"
208         return 1
209         ;;
210     esac
211     if $set_path; then
212         echo "${FUNCNAME[0]}(): INFO: Adding $lf_venv/bin to PATH"
213         PATH=$lf_venv/bin:$PATH
214         return 0
215     else
216         echo "${FUNCNAME[0]}(): INFO: Path not set, lf_venv set to: $lf_venv"
217     fi
218
219 }   # End lf-activate-venv
220
221 ################################################################################
222 #
223 # NAME
224 #   lf-git-validate-jira-urls
225 #
226 # SYNOPSIS
227 #   # shellcheck disable=SC1090
228 #   source ~/lf-env.sh
229 #
230 #   lf-git-validate-jira-urls
231 #
232 # DESCRIPTION
233 #   Check for JIRA URLS in the commit message
234 #
235 # RETURN VALUES
236 #   OK: 0
237 #   Fail: 1
238 #
239 ################################################################################
240
241 lf-git-validate-jira-urls () {
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 lf-jjb-check-ascii () {
280     if [[ ! -d "jjb" ]]; then
281         lf-echo-stderr "${FUNCNAME[0]}(): ERROR: missing jjb directory"
282         lf-echo-stderr "This function can only be run from top of global-jjb directory"
283         return 1
284     fi
285     if LC_ALL=C grep -I -r '[^[:print:][:space:]]' jjb/; then
286         lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Found YAML files containing non-printable characters."
287         return 1
288     fi
289     echo "${FUNCNAME[0]}(): INFO: All JJB YAML files contain only printable ASCII characters"
290     return 0
291 }
292
293 ################################################################################
294 # Functions that assign Variables
295 ################################################################################
296
297 # These variables are shell (local) variables and need to be lower-case so
298 # Shellcheck knows they are shell variables and will check for
299 # 'used-before-set'.
300
301 lf-set-maven-options () {
302     # Disable 'unused-variable' check
303     # shellcheck disable=SC2034
304     maven_options="--show-version --batch-mode -Djenkins \
305         -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn \
306         -Dmaven.repo.local=/tmp/r -Dorg.ops4j.pax.url.mvn.localRepository=/tmp/r"
307 }