From a874c7ac738ab9b2317fb7c78e0e43e9686348f4 Mon Sep 17 00:00:00 2001 From: Dmitry Tantsur Date: Tue, 8 Sep 2020 12:41:35 +0200 Subject: [PATCH] Always setup environment in bifrost-cli and make it less verbose For upgrades we need bifrost-cli to always try to setup environment (e.g. in case of a different ansible version). To avoid polluting the output, make `set -x` conditional on a new variable BIFROST_TRACE. Change-Id: I8caea6be0962db0e7019d0aa1484ef2424b7629e --- bifrost/cli.py | 13 +++++-------- scripts/env-setup.sh | 13 ++++++++----- scripts/install-deps.sh | 35 ++++++++++++++++++++++++++++++----- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/bifrost/cli.py b/bifrost/cli.py index d7b2a7c09..dd1529d09 100644 --- a/bifrost/cli.py +++ b/bifrost/cli.py @@ -65,14 +65,11 @@ def ansible(playbook, inventory, verbose=False, env=None, extra_vars=None, def env_setup(args): - if os.path.exists(VENV): - log(VENV, 'exists, skipping environment preparation', - only_if=args.debug) - return - - log('Installing dependencies and preparing an environment in', VENV) - subprocess.check_call(["bash", "scripts/env-setup.sh"], - env=get_env(), cwd=BASE) + log('Installing dependencies and preparing an environment in', VENV, + only_if=args.debug) + env = get_env({'BIFROST_TRACE': str(args.debug).lower(), + 'BIFROST_HIDE_PROMPT': 'true'}) + subprocess.check_call(["bash", "scripts/env-setup.sh"], env=env, cwd=BASE) def get_release(release): diff --git a/scripts/env-setup.sh b/scripts/env-setup.sh index 515dcfc46..1c0909d1f 100755 --- a/scripts/env-setup.sh +++ b/scripts/env-setup.sh @@ -19,6 +19,7 @@ ANSIBLE_SOURCE_PATH=${ANSIBLE_SOURCE_PATH:-ansible${ANSIBLE_PIP_VERSION}} BIFROST_COLLECTIONS_PATHS=${ANSIBLE_COLLECTIONS_PATHS:-} PLAYBOOKS_LIBRARY_PATH=$(dirname $0)/../playbooks/library +echo "Installing/upgrading Ansible" ${PIP} install "${ANSIBLE_SOURCE_PATH}" ANSIBLE=${VENV}/bin/ansible ANSIBLE_GALAXY=${VENV}/bin/ansible-galaxy @@ -62,8 +63,10 @@ if [ ! -e "$(dirname $0)/../playbooks/collections" ]; then ln -s ${ANSIBLE_COLLECTIONS_PATHS} "$(dirname $0)/../playbooks/collections" fi -echo -echo "To use bifrost, do" -echo "source ${VENV}/bin/activate" -echo "Then run playbooks as normal." -echo +if [[ "${BIFROST_HIDE_PROMPT:-false}" != true ]]; then + echo + echo "To use bifrost, do" + echo "source ${VENV}/bin/activate" + echo "Then run playbooks as normal." + echo +fi diff --git a/scripts/install-deps.sh b/scripts/install-deps.sh index 236c5c0f6..d6f4a12fe 100755 --- a/scripts/install-deps.sh +++ b/scripts/install-deps.sh @@ -1,5 +1,10 @@ #!/bin/bash -set -xeu + +set -eu + +if [[ "${BIFROST_TRACE:-}" == true ]]; then + set -x +fi declare -A PKG_MAP @@ -14,6 +19,8 @@ CHECK_CMD_PKGS=( python3-pip ) +echo Detecting package manager + source /etc/os-release || source /usr/lib/os-release case ${ID,,} in *suse*) @@ -58,6 +65,9 @@ case ${ID,,} in rhel|fedora|centos) OS_FAMILY="RedHat" PKG_MANAGER=$(which dnf || which yum) + if [[ "${BIFROST_TRACE:-}" != true ]]; then + PKG_MANAGER="$PKG_MANAGER --quiet" + fi INSTALLER_CMD="sudo -H -E ${PKG_MANAGER} -y install" CHECK_CMD="rpm -q" PKG_MAP=( @@ -72,6 +82,8 @@ case ${ID,,} in *) echo "ERROR: Supported package manager not found. Supported: apt, dnf, yum, zypper"; exit 1;; esac +echo Installing Python and PIP + for pkg in ${CHECK_CMD_PKGS[@]}; do if ! $(${CHECK_CMD} ${PKG_MAP[$pkg]} &>/dev/null); then ${INSTALLER_CMD} ${PKG_MAP[$pkg]} @@ -86,23 +98,36 @@ if [ "${#EXTRA_PKG_DEPS[@]}" -ne 0 ]; then done fi -echo "NOTICE: Using virtualenv for this installation." if [ ! -f ${VENV}/bin/activate ]; then + echo "Creating a virtual environment" + # only create venv if one doesn't exist sudo -H -E python3 -m venv --system-site-packages ${VENV} sudo -H -E chown -R ${USER} ${VENV} +else + echo "Virtual environment exists, skipping creation" + + # NOTE(dtantsur): place here any actions required to upgrade existing + # virtual environments. fi + # Note(cinerama): activate is not compatible with "set -u"; # disable it just for this line. -set +u +set +ux . ${VENV}/bin/activate set -u +if [[ "${BIFROST_TRACE:-}" == true ]]; then + set -x +fi VIRTUAL_ENV=${VENV} # If we're using a venv, we need to work around sudo not # keeping the path even with -E. PYTHON="python3" PIP="${PYTHON} -m pip" +if [[ "${BIFROST_TRACE:-}" != true ]]; then + PIP="$PIP --quiet" +fi $PYTHON << EOF import pip @@ -112,10 +137,10 @@ EOF export PIP_OPTS="--upgrade-strategy only-if-needed" -# Install the rest of required packages using bindep +echo "Installing bindep" ${PIP} install bindep -echo "Using Bindep to install binary dependencies..." +echo "Using Bindep to install binary dependencies" # bindep returns 1 if packages are missing bindep -b &> /dev/null || ${INSTALLER_CMD} $(bindep -b)