From: Tim Johnson Date: Mon, 30 Sep 2019 16:08:39 +0000 (-0700) Subject: Global JJB use python3 versions of lftools and jjb X-Git-Tag: v0.45.0~5 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=6d856ad13d33ad912145f7c2b90ccaad919590e4;p=releng%2Fglobal-jjb.git Global JJB use python3 versions of lftools and jjb Create LF bash 'library' and install in ~jenkins Issue: RELENG-2411 Change-Id: I211c85b9823ca5582a263ec145797d4de9424e10 Signed-off-by: Tim Johnson --- diff --git a/jenkins-init-scripts/init.sh b/jenkins-init-scripts/init.sh index 0ae7654e..66053b0d 100755 --- a/jenkins-init-scripts/init.sh +++ b/jenkins-init-scripts/init.sh @@ -25,4 +25,8 @@ fi # Create the jenkins user last so that hopefully we DO NOT have to deal with # guard files -"$jjb_init_scripts/create-jenkins-user.sh" +$jjb_init_scripts/create-jenkins-user.sh + +cp $jjb_init_scripts/lf-env.sh ~jenkins/ +chmod 644 ~jenkins/lf-env.sh +chown jenkins:jenkins ~jenkins/lf-env.sh diff --git a/jenkins-init-scripts/lf-env.sh b/jenkins-init-scripts/lf-env.sh new file mode 100644 index 00000000..68e6cf03 --- /dev/null +++ b/jenkins-init-scripts/lf-env.sh @@ -0,0 +1,80 @@ +#!/usr/bin/no-execute + +# SPDX-License-Identifier: EPL-1.0 +############################################################################## +# Copyright (c) 2019 The Linux Foundation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +############################################################################## + +# A library of functions for LF/Jenkins bash scripts. In the general case, do +# NOT set shell variables here. If you want to make a variable available, +# provide a function that sets the variable: 'function lf_set_foo() +# {foo=asdf;}'. Any scripts that need access to the variable can call the 'set' +# function. This keeps the name-space polution to a minimum. + +################################################################################ +# Functions +################################################################################ + +# Execute pip from 'user' venv +function lf-pip() { /home/jenkins/.local/bin/pip "$@" ; } + +# Echo all arguments to stderr +function lf-echoerr() { echo "$@" 1>&2; } + +# Function to safely evaluate booleans +# If the boolean equal '' it is an Fatal Error +# Note the case of boolean will be ignored (always mapped to lower-case) +function lf-boolean() +{ + if [[ $# != 1 ]]; then + echo "lf-boolean(): Missing boolean operand" + exit 1 + fi + local bool + bool=$(echo "$1" | tr '[:upper:]' '[:lower:]') + if [[ -z $bool ]] ; then + # Output script name & line number of call to function + lf-echoerr "ERROR: $(basename $0) line: ${BASH_LINENO[0]} : A boolean cannot be a empty string" >&2 + exit 1 + fi + $bool +} + +# Function to activate a Python Virtual Environment +# Just validate the path and add 'bin' to the path +function lf-activate() +{ + if [[ $# != 1 ]]; then + echo "lf-activate(): Missing path operand" + exit 1 + fi + local venv_path=$1 + # Validate the path to a VENV + if [[ ! -d $venv_path/bin ]]; then + lf-echoerr "ERROR: Is '$venv_path' a Python Environment ?" + return 1 + fi + PATH=$venv_path/bin:$PATH +} + +################################################################################ +# Functions that assign Variables +################################################################################ + +# Although these variables are 'shell' variables, they need to be upper-case so +# 'shellcheck' does check for 'used-before-set' when they are referenced. So if +# forget to 'source' this file, you will get a 'run-time' error (if you have -u +# set), otherwise you will get "MAVEN_OPIONS=''. + +function lf-set-maven-options() +{ + # shellcheck disable=SC2034 # Disable 'unused-variable' check + MAVEN_OPTIONS=$(echo --show-version --batch-mode -Djenkins \ + -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn \ + -Dmaven.repo.local=/tmp/r -Dorg.ops4j.pax.url.mvn.localRepository=/tmp/r) +} diff --git a/releasenotes/notes/add-lf-bash-library-b60dcce181c87d5a.yaml b/releasenotes/notes/add-lf-bash-library-b60dcce181c87d5a.yaml new file mode 100644 index 00000000..0ff94e6f --- /dev/null +++ b/releasenotes/notes/add-lf-bash-library-b60dcce181c87d5a.yaml @@ -0,0 +1,8 @@ +--- +features: + - | + Add lf-env.sh 'library' script in ~jenkins. Sourcing this 'library' script + from a bash script provides access to a number of functions. The script is + installed by the 'init' script at boot time so that is is accessible from + any Jenkins build script. Hopefully over time other functions will be added + to this library.