| #!/bin/bash |
| |
| # ============LICENSE_START======================================================= |
| # Copyright (C) 2019 The Nordix Foundation. All rights reserved. |
| # ================================================================================ |
| # 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. |
| # |
| # SPDX-License-Identifier: Apache-2.0 |
| # ============LICENSE_END========================================================= |
| |
| set -o pipefail |
| |
| # TODO: ignoring SC2015 for the timebeing so we don't break things |
| # shellcheck disable=SC2015 |
| # Avoid double sourcing the file |
| [[ -n ${PACKAGE_LIB_SOURCED:-} ]] && return 0 || export PACKAGE_LIB_SOURCED=1 |
| |
| #------------------------------------------------------------------------------- |
| # Print the help message which includes the usage, the expected parameters |
| # and their default values if they are not specified |
| #------------------------------------------------------------------------------- |
| function usage() { |
| |
| # NOTE: shellcheck complains quoting in the example so SC2086 is disabled |
| # shellcheck disable=SC2086 |
| cat <<EOF |
| |
| Usage: $(basename ${0}) [-s <stack to package>] [-b <stack version/branch>] [-p <packaging profile>] [-d <distro>] [-v] [-h] |
| |
| -s: Stack to package. (Default kubernetes) |
| -b: Stack version. (Default master) |
| -p: Packaging profile. (Default full) |
| -d: Distro. (Default ubuntu1804) |
| -v: Increase verbosity and keep logs for troubleshooting. (Default false) |
| -h: This message. |
| |
| EOF |
| exit 0 |
| |
| } |
| |
| #------------------------------------------------------------------------------- |
| # Parse the arguments that are passed to the script |
| # If an argument is not specified, default values for those are set |
| # |
| # The priority order is |
| # - arguments: overrides the default values and values set as environment |
| # values. highest prio. |
| # - env vars: overrides the default values but not the values set from command |
| # line. |
| # - default values: only takes effect if the user doesn't specify the value |
| # of an argument either as an env var or from command line. lowest prio. |
| #------------------------------------------------------------------------------- |
| function parse_cmdline_opts() { |
| |
| # set variables to the values set in env - otherwise, set them to defaults |
| STACK_TYPE=${STACK_TYPE:-kubernetes} |
| STACK_VERSION=${STACK_VERSION:-master} |
| PACKAGING_PROFILE=${PACKAGING_PROFILE:-full} |
| DISTRO=${DISTRO:-ubuntu1804} |
| VERBOSITY=${VERBOSITY:-false} |
| |
| # TODO (fdegir): This function is left here so we can introduce additional |
| # arguments when we introduce support for other stacks than Kubernetes only |
| # get values passed as command line arguments, overriding the defaults or |
| # the ones set by using env variables |
| while getopts ":hs:b:p:d::v" o; do |
| case "${o}" in |
| h) usage ;; |
| s) STACK_TYPE="${OPTARG}" ;; |
| b) STACK_VERSION="${OPTARG}" ;; |
| p) PACKAGING_PROFILE="${OPTARG}" ;; |
| d) DISTRO="${OPTARG}" ;; |
| v) VERBOSITY="true" ;; |
| *) echo "ERROR: Invalid option '-${OPTARG}'"; usage ;; |
| esac |
| done |
| |
| # Do all the exports |
| export STACK_TYPE="${STACK_TYPE}" |
| export STACK_VERSION="${STACK_VERSION}" |
| export PACKAGING_PROFILE="${PACKAGING_PROFILE}" |
| export DISTRO="${DISTRO}" |
| export VERBOSITY="${VERBOSITY}" |
| export EXECUTION_MODE="packaging" |
| |
| log_summary |
| |
| } |
| |
| #------------------------------------------------------------------------------- |
| # Remove leftovers of previous runs |
| # Leftovers of previous run might result in failures, especially within CI/CD |
| # so this function is important to use but it is not executed by default. |
| #------------------------------------------------------------------------------- |
| function cleanup() { |
| |
| echo "Info : Remove leftovers of previous run" |
| |
| # remove engine venv, cache and .ansible |
| sudo /bin/rm -rf "$ENGINE_VENV" "$ENGINE_CACHE" "$HOME/.ansible" \ |
| "$OFFLINE_PKG_FOLDER" "$OFFLINE_PKG_FILE" |
| |
| # stop docker service since docker registry keeps creating |
| # $ENGINE_CACHE/certs folder, making engine prep to fail due |
| # to ownership issus |
| redirect_cmd sudo systemctl stop docker || true |
| |
| } |
| |
| #------------------------------------------------------------------------------- |
| # Log summary & parameters to console |
| #------------------------------------------------------------------------------- |
| function log_summary() { |
| |
| echo |
| echo "#---------------------------------------------------#" |
| echo "# Environment #" |
| echo "#---------------------------------------------------#" |
| echo "User : $USER" |
| echo "Hostname : $HOSTNAME" |
| echo "Host OS : $(source /etc/os-release &> /dev/null || source /usr/lib/os-release &> /dev/null; echo "${PRETTY_NAME}")" |
| echo "IP : $(hostname -I | cut -d' ' -f1)" |
| echo "#---------------------------------------------------#" |
| echo "# Execution Started #" |
| echo "#---------------------------------------------------#" |
| echo "Date & Time : $(date -u '+%F %T UTC')" |
| echo "Execution Mode : $EXECUTION_MODE" |
| echo "Stack Type : $STACK_TYPE" |
| echo "Stack Version : $STACK_VERSION" |
| echo "Distro : $DISTRO" |
| echo "Packaging Profile: $PACKAGING_PROFILE" |
| echo "Verbosity : $VERBOSITY" |
| echo "#---------------------------------------------------#" |
| echo |
| |
| } |
| |
| #------------------------------------------------------------------------------- |
| # Log elapsed time to console |
| #------------------------------------------------------------------------------- |
| function log_elapsed_time() { |
| |
| elapsed_time=$SECONDS |
| echo "#---------------------------------------------------#" |
| echo "# Execution Completed #" |
| echo "#---------------------------------------------------#" |
| echo "Date & Time : $(date -u '+%F %T UTC')" |
| echo "Elapsed : $((elapsed_time / 60)) minutes and $((elapsed_time % 60)) seconds" |
| echo "#---------------------------------------------------#" |
| |
| } |
| |
| # vim: set ts=2 sw=2 expandtab: |