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