diff options
Diffstat (limited to 'resources/libraries/bash/function/common.sh')
-rw-r--r-- | resources/libraries/bash/function/common.sh | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/resources/libraries/bash/function/common.sh b/resources/libraries/bash/function/common.sh new file mode 100644 index 0000000..7f8aa0e --- /dev/null +++ b/resources/libraries/bash/function/common.sh @@ -0,0 +1,110 @@ +# Copyright (c) 2020 Cisco and/or its affiliates. +# Copyright (c) 2020 PANTHEON.tech and/or its affiliates. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -exuo pipefail + +# This library defines functions used by multiple entry scripts. +# Keep functions ordered alphabetically, please. + +function activate_virtualenv () { + + # Update virtualenv pip package, delete and create virtualenv directory, + # activate the virtualenv, install requirements, set PYTHONPATH. + + # Arguments: + # - ${1} - Path to existing directory for creating virtualenv in. + # If missing or empty, ${ROOT_DIR} is used. + # - ${2} - Path to requirements file, ${ROOT_DIR}/requirements.txt if empty. + # Variables read: + # - ROOT_DIR - Path to existing root of local git repository. + # Functions called: + # - die - Print to stderr and exit. + + set -exuo pipefail + + root_path="${1-$ROOT_DIR}" + env_dir="${root_path}/env" + req_path=${2-$ROOT_DIR/requirements.txt} + rm -rf "${env_dir}" || die "Failed to clean previous virtualenv." + pip3 install virtualenv==20.0.20 || { + die "Virtualenv package install failed." + } + virtualenv --no-download --python=$(which python3) "${env_dir}" || { + die "Virtualenv creation for $(which python3) failed." + } + set +u + source "${env_dir}/bin/activate" || die "Virtualenv activation failed." + set -u + pip3 install -r "${req_path}" || { + die "Requirements installation failed." + } +} + + +function common_dirs () { + + # Set global variables, create some directories (without touching content). + + # Variables set: + # - BASH_FUNCTION_DIR - Path to existing directory this file is located in. + # - ROOT_DIR - Path to existing root of local git repository. + # - RESOURCES_DIR - Path to existing CSIT subdirectory "resources". + # Functions called: + # - die - Print to stderr and exit. + + set -exuo pipefail + + this_file=$(readlink -e "${BASH_SOURCE[0]}") || { + die "Some error during locating of this source file." + } + BASH_FUNCTION_DIR=$(dirname "${this_file}") || { + die "Some error during dirname call." + } + # Current working directory could be in a different repo, e.g. VPP. + pushd "${BASH_FUNCTION_DIR}" || die "Pushd failed" + relative_csit_dir=$(git rev-parse --show-toplevel) || { + die "Git rev-parse failed." + } + ROOT_DIR=$(readlink -e "${relative_csit_dir}") || die "Readlink failed." + popd || die "Popd failed." +} + +function die () { + + # Print the message to standard error end exit with error code specified + # by the second argument. + # + # Hardcoded values: + # - The default error message. + # Arguments: + # - ${1} - The whole error message, be sure to quote. Optional + # - ${2} - the code to exit with, default: 1. + + set -x + set +eu + warn "${1:-Unspecified run-time error occurred!}" + exit "${2:-1}" +} + +function warn () { + + # Print the message to standard error. + # + # Arguments: + # - ${@} - The text of the message. + + set -exuo pipefail + + echo "$@" >&2 +} |