kayobe-config/kayobe-env
Mark Goddard 651b8be1a0 kayobe-env: Unstick KOLLA_SOURCE_PATH and KOLLA_VENV_PATH
The kayobe-env script does not update the KOLLA_SOURCE_PATH and
KOLLA_VENV_PATH variables if they are already set.  This can lead to
dangerous and difficult to diagnose issues where Kayobe uses a different
version of Kolla Ansible than expected.

This change updates these variables each time the kayobe-env script is
sourced.

Change-Id: I3b4b0b611750b9c7846ff5f74554aee2f14939e4
Closes-Bug: #2036711
2023-09-21 10:01:03 +00:00

72 lines
2.1 KiB
Plaintext

# Copyright (c) 2017 StackHPC Ltd.
#
# 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.
KAYOBE_CONFIG_ROOT=$(dirname $(realpath ${BASH_SOURCE[0]:-${(%):-%x}}))
echo "Using Kayobe config from $KAYOBE_CONFIG_ROOT"
export KAYOBE_CONFIG_PATH=$KAYOBE_CONFIG_ROOT/etc/kayobe
export KOLLA_CONFIG_PATH=$KAYOBE_CONFIG_ROOT/etc/kolla
# NOTE: These paths can be modified depending on the local environment.
# These defaults are set based on the following directory structure:
#
# ${base_path}/
# src/
# kayobe/
# kayobe-config/
# kolla-ansible/
# venvs/
# kayobe/
# kolla-ansible/
base_path=$(realpath $KAYOBE_CONFIG_ROOT/../../)
export KOLLA_SOURCE_PATH=${base_path}/src/kolla-ansible
export KOLLA_VENV_PATH=${base_path}/venvs/kolla-ansible
function check_and_export_env {
# Look for existing Kayobe environments
if [ -d "${KAYOBE_CONFIG_PATH}/environments" ]; then
if [ -d "${KAYOBE_CONFIG_PATH}/environments/${kayobe_env}" ]; then
export KAYOBE_ENVIRONMENT="${kayobe_env}"
echo "Using Kayobe environment ${KAYOBE_ENVIRONMENT}"
return 0
else
echo "Unable to find Kayobe environment ${kayobe_env} in ${KAYOBE_CONFIG_PATH}/environments"
return 1
fi
else
echo "Cannot find environments folder in ${KAYOBE_CONFIG_PATH}"
return 1
fi
}
function usage {
echo "usage: ${BASH_SOURCE[0]:-${(%):-%x}} [--environment <env-name>]"
return 1
}
if [ "$#" -ge 1 ]; then
if [ "$1" = "--environment" -a "$#" -eq 2 ]; then
kayobe_env="$2"
check_and_export_env
else
usage
fi
return $?
fi
if [[ -f "$KAYOBE_CONFIG_ROOT/.environment" ]]; then
kayobe_env=$(cat "$KAYOBE_CONFIG_ROOT/.environment")
check_and_export_env
fi