From a8c7722c19cfca9f0361c578ce63bf552093dd0e Mon Sep 17 00:00:00 2001 From: Anil Belur Date: Thu, 1 Sep 2022 13:03:24 +1000 Subject: [PATCH] Fix: Use lf-activate-venv to install openstack dep Using python-tools-install.sh for the pre/post build is not recommended approach for installing python dependencies since this installs the dependencies with `--user` option (removed in I821a86ac3b54f284e8). Instead use lf-activate-venv to setup an venv and pull in the required dependencies and save the path of the virtualenv in a temp file that can be checked before attempting to create a venv. Issue-ID: RELENG-4357 Change-Id: I00bbf08921a3ddc6349e74e9cd9d6316b0876749 Signed-off-by: Anil Belur --- jenkins-init-scripts/lf-env.sh | 2 +- .../fix-openstack-pre-reqs-014ed3db94365608.yaml | 15 +++++++++++++ shell/jenkins-verify-images.sh | 13 +++++++++++ shell/logs-deploy.sh | 2 +- shell/openstack-cleanup-old-images.sh | 25 ++++++++++++++++++++++ shell/openstack-cleanup-orphaned-k8s-clusters.sh | 18 ++++++++++++++++ shell/openstack-cleanup-orphaned-ports.sh | 17 +++++++++++++++ shell/openstack-cleanup-orphaned-servers.sh | 23 ++++++++++++++++++++ shell/openstack-cleanup-orphaned-stacks.sh | 25 ++++++++++++++++++++++ shell/openstack-cleanup-orphaned-volumes.sh | 22 +++++++++++++++++++ shell/openstack-kubernetes-create.sh | 24 +++++++++++++++++++++ shell/openstack-protect-in-use-images.sh | 17 +++++++++++++++ shell/openstack-stack-copy-ssh-keys.sh | 17 +++++++++++++++ shell/openstack-stack-create.sh | 23 ++++++++++++++++++++ shell/openstack-stack-delete.sh | 15 +++++++++++-- shell/python-tools-install.sh | 4 +++- 16 files changed, 257 insertions(+), 5 deletions(-) create mode 100644 releasenotes/notes/fix-openstack-pre-reqs-014ed3db94365608.yaml diff --git a/jenkins-init-scripts/lf-env.sh b/jenkins-init-scripts/lf-env.sh index a2b8cb2c..114f0c17 100644 --- a/jenkins-init-scripts/lf-env.sh +++ b/jenkins-init-scripts/lf-env.sh @@ -190,7 +190,7 @@ lf-activate-venv () { # Use pyenv for selecting the python version if [[ -d "/opt/pyenv" ]]; then # set_python_version = pyver "${python//[a-zA-Z]/}" - echo "---> Setting up pyenv" + echo "Setup pyenv:" export PYENV_ROOT="/opt/pyenv" export PATH="$PYENV_ROOT/bin:$PATH" pyenv versions diff --git a/releasenotes/notes/fix-openstack-pre-reqs-014ed3db94365608.yaml b/releasenotes/notes/fix-openstack-pre-reqs-014ed3db94365608.yaml new file mode 100644 index 00000000..a08afaa8 --- /dev/null +++ b/releasenotes/notes/fix-openstack-pre-reqs-014ed3db94365608.yaml @@ -0,0 +1,15 @@ +--- +issues: + - | + Error: openstack: command not found +fixes: + - | + Use lf-activate-venv to install openstack deps + + Using python-tools-install.sh for the pre/post build is not recommended + approach for installing python dependencies since this installs the + dependencies with `--user` option (removed in I821a86ac3b54f284e8). + + Instead use lf-activate-venv to setup an venv and pull in the required + dependencies and save the path of the virtualenv in a temp file that + can be checked before attempting to create a venv. diff --git a/shell/jenkins-verify-images.sh b/shell/jenkins-verify-images.sh index 483a51b7..5db7fec2 100755 --- a/shell/jenkins-verify-images.sh +++ b/shell/jenkins-verify-images.sh @@ -14,6 +14,19 @@ echo "---> jenkins-verify-images.sh" set -eux -o pipefail +# shellcheck disable=SC1090 +source ~/lf-env.sh + +if [ -f "/tmp/.os_lf_venv" ]; then + os_lf_venv=$(cat "/tmp/.os_lf_venv") +fi + +if [ -d "${os_lf_venv}" ] && [ -f "${os_lf_venv}/bin/openstack" ]; then + echo "Re-use existing venv: ${os_lf_venv}" + PATH=$os_lf_venv/bin:$PATH +else + lf-activate-venv --python python3 python-openstackclient +fi error=false verify_images() diff --git a/shell/logs-deploy.sh b/shell/logs-deploy.sh index d797fa7e..c28d92ef 100644 --- a/shell/logs-deploy.sh +++ b/shell/logs-deploy.sh @@ -12,7 +12,7 @@ echo "---> logs-deploy.sh" # shellcheck disable=SC1090 source ~/lf-env.sh -lf-activate-venv --python python3.8 lftools +lf-activate-venv --python python3 lftools # Ensure we fail the job if any steps fail # Disable 'globbing' diff --git a/shell/openstack-cleanup-old-images.sh b/shell/openstack-cleanup-old-images.sh index cbb2ef8e..8d1ddd5d 100644 --- a/shell/openstack-cleanup-old-images.sh +++ b/shell/openstack-cleanup-old-images.sh @@ -11,6 +11,31 @@ # Removes openstack images older than X days in the cloud echo "---> Cleanup old images" +# shellcheck disable=SC1090 +source ~/lf-env.sh + +set -x +# Check if openstack venv was previously created +if [ -f "/tmp/.os_lf_venv" ]; then + os_lf_venv=$(cat "/tmp/.os_lf_venv") +fi + +if [ -d "${os_lf_venv}" ] && [ -f "${os_lf_venv}/bin/openstack" ]; then + echo "Re-use existing venv: ${os_lf_venv}" + PATH=$os_lf_venv/bin:$PATH +else + lf-activate-venv --python python3 "cryptography<3.4" \ + "lftools[openstack]" \ + kubernetes \ + "niet~=1.4.2" \ + python-heatclient \ + python-openstackclient \ + python-magnumclient \ + setuptools \ + "openstacksdk<0.99" \ + yq +fi + os_cloud="${OS_CLOUD:-vex}" os_image_cleanup_age="${OS_IMAGE_CLEANUP_AGE:-30}" diff --git a/shell/openstack-cleanup-orphaned-k8s-clusters.sh b/shell/openstack-cleanup-orphaned-k8s-clusters.sh index b1f3923e..33fcaa65 100644 --- a/shell/openstack-cleanup-orphaned-k8s-clusters.sh +++ b/shell/openstack-cleanup-orphaned-k8s-clusters.sh @@ -58,6 +58,24 @@ cluster_in_jenkins() { return 1 } +# shellcheck disable=SC1090 +source ~/lf-env.sh +# Check if openstack venv was previously created +if [ -f "/tmp/.os_lf_venv" ]; then + os_lf_venv=$(cat "/tmp/.os_lf_venv") +fi + +if [ -d "${os_lf_venv}" ] && [ -f "${os_lf_venv}/bin/openstack" ]; then + echo "Re-use existing venv: ${os_lf_venv}" + PATH=$os_lf_venv/bin:$PATH +else + lf-activate-venv --python python3 \ + kubernetes \ + python-heatclient \ + python-openstackclient \ + python-magnumclient +fi + ######################### ## FETCH ACTIVE BUILDS ## ######################### diff --git a/shell/openstack-cleanup-orphaned-ports.sh b/shell/openstack-cleanup-orphaned-ports.sh index 080b5907..e1f84360 100644 --- a/shell/openstack-cleanup-orphaned-ports.sh +++ b/shell/openstack-cleanup-orphaned-ports.sh @@ -11,8 +11,25 @@ # Scans OpenStack for orphaned ports echo "---> Orphaned ports" +# shellcheck disable=SC1090 +source ~/lf-env.sh + os_cloud="${OS_CLOUD:-vex}" +# Check if openstack venv was previously created +if [ -f "/tmp/.os_lf_venv" ]; then + os_lf_venv=$(cat "/tmp/.os_lf_venv") +fi + +if [ -d "${os_lf_venv}" ] && [ -f "${os_lf_venv}/bin/openstack" ]; then + echo "Re-use existing venv: ${os_lf_venv}" + PATH=$os_lf_venv/bin:$PATH +else + lf-activate-venv --python python3 \ + python-heatclient \ + python-openstackclient +fi + set -eux -o pipefail mapfile -t os_ports_ts < <(openstack --os-cloud "$os_cloud" port list \ diff --git a/shell/openstack-cleanup-orphaned-servers.sh b/shell/openstack-cleanup-orphaned-servers.sh index 629a3a0f..4dad6ed0 100644 --- a/shell/openstack-cleanup-orphaned-servers.sh +++ b/shell/openstack-cleanup-orphaned-servers.sh @@ -47,6 +47,29 @@ minion_in_jenkins() { return 1 } +# shellcheck disable=SC1090 +source ~/lf-env.sh + +# Check if openstack venv was previously created +if [ -f "/tmp/.os_lf_venv" ]; then + os_lf_venv=$(cat "/tmp/.os_lf_venv") +fi + +if [ -d "${os_lf_venv}" ] && [ -f "${os_lf_venv}/bin/openstack" ]; then + echo "Re-use existing venv: ${os_lf_venv}" + PATH=$os_lf_venv/bin:$PATH +else + lf-activate-venv --python python3 "cryptography<3.4" \ + "lftools[openstack]" \ + kubernetes \ + "niet~=1.4.2" \ + python-heatclient \ + python-openstackclient \ + python-magnumclient \ + setuptools \ + "openstacksdk<0.99" \ + yq +fi ########################## ## FETCH ACTIVE MINIONS ## ########################## diff --git a/shell/openstack-cleanup-orphaned-stacks.sh b/shell/openstack-cleanup-orphaned-stacks.sh index cc150ac4..187a83d1 100644 --- a/shell/openstack-cleanup-orphaned-stacks.sh +++ b/shell/openstack-cleanup-orphaned-stacks.sh @@ -56,6 +56,31 @@ stack_in_jenkins() { return 1 } + +# shellcheck disable=SC1090 +source ~/lf-env.sh + +# Check if openstack venv was previously created +if [ -f "/tmp/.os_lf_venv" ]; then + os_lf_venv=$(cat "/tmp/.os_lf_venv") +fi + +if [ -d "${os_lf_venv}" ] && [ -f "${os_lf_venv}/bin/openstack" ]; then + echo "Re-use existing venv: ${os_lf_venv}" + PATH=$os_lf_venv/bin:$PATH +else + lf-activate-venv --python python3 "cryptography<3.4" \ + "lftools[openstack]" \ + kubernetes \ + "niet~=1.4.2" \ + python-heatclient \ + python-openstackclient \ + python-magnumclient \ + setuptools \ + "openstacksdk<0.99" \ + yq +fi + set -x ######################### ## FETCH ACTIVE BUILDS ## diff --git a/shell/openstack-cleanup-orphaned-volumes.sh b/shell/openstack-cleanup-orphaned-volumes.sh index 51b4e58a..c5cc0b2b 100644 --- a/shell/openstack-cleanup-orphaned-volumes.sh +++ b/shell/openstack-cleanup-orphaned-volumes.sh @@ -15,6 +15,28 @@ os_cloud="${OS_CLOUD:-vex}" set -eux -o pipefail +# shellcheck disable=SC1090 +source ~/lf-env.sh +# Check if openstack venv was previously created +if [ -f "/tmp/.os_lf_venv" ]; then + os_lf_venv=$(cat "/tmp/.os_lf_venv") +fi + +if [ -d "${os_lf_venv}" ] && [ -f "${os_lf_venv}/bin/openstack" ]; then + echo "Re-use existing venv: ${os_lf_venv}" + PATH=$os_lf_venv/bin:$PATH +else + lf-activate-venv --python python3 "cryptography<3.4" \ + "lftools[openstack]" \ + kubernetes \ + "niet~=1.4.2" \ + python-heatclient \ + python-openstackclient \ + python-magnumclient \ + setuptools \ + "openstacksdk<0.99" \ + yq +fi mapfile -t os_volumes < <(openstack --os-cloud "$os_cloud" volume list -f value -c ID --status Available) if [ ${#os_volumes[@]} -eq 0 ]; then diff --git a/shell/openstack-kubernetes-create.sh b/shell/openstack-kubernetes-create.sh index 2f1a2b7f..5aef45d1 100755 --- a/shell/openstack-kubernetes-create.sh +++ b/shell/openstack-kubernetes-create.sh @@ -13,6 +13,30 @@ echo "---> Creating kubernetes cluster" set -eux -o pipefail +# shellcheck disable=SC1090 +source ~/lf-env.sh + +# Check if openstack venv was previously created +if [ -f "/tmp/.os_lf_venv" ]; then + os_lf_venv=$(cat "/tmp/.os_lf_venv") +fi + +if [ -d "${os_lf_venv}" ] && [ -f "${os_lf_venv}/bin/openstack" ]; then + echo "Re-use existing venv: ${os_lf_venv}" + PATH=$os_lf_venv/bin:$PATH +else + lf-activate-venv --python python3 "cryptography<3.4" \ + "lftools[openstack]" \ + kubernetes \ + "niet~=1.4.2" \ + python-heatclient \ + python-openstackclient \ + python-magnumclient \ + setuptools \ + "openstacksdk<0.99" \ + yq +fi + os_cloud="${OS_CLOUD:-vex}" fixed_network="${FIXED_NETWORK}" fixed_subnet="${FIXED_SUBNET}" diff --git a/shell/openstack-protect-in-use-images.sh b/shell/openstack-protect-in-use-images.sh index 468ea0d8..07294407 100644 --- a/shell/openstack-protect-in-use-images.sh +++ b/shell/openstack-protect-in-use-images.sh @@ -19,6 +19,23 @@ set -eu -o pipefail echo "---> Protect in-use images" os_cloud="${OS_CLOUD:-vex}" +# shellcheck disable=SC1090 +source ~/lf-env.sh + +# Check if openstack venv was previously created +if [ -f "/tmp/.os_lf_venv" ]; then + os_lf_venv=$(cat "/tmp/.os_lf_venv") +fi + +if [ -d "${os_lf_venv}" ] && [ -f "${os_lf_venv}/bin/openstack" ]; then + echo "Re-use existing venv: ${os_lf_venv}" + PATH=$os_lf_venv/bin:$PATH +else + lf-activate-venv --python python3 \ + python-heatclient \ + python-openstackclient +fi + images=() while read -r -d $'\n' ; do images+=("$REPLY") diff --git a/shell/openstack-stack-copy-ssh-keys.sh b/shell/openstack-stack-copy-ssh-keys.sh index 79d75d91..0509bb8f 100644 --- a/shell/openstack-stack-copy-ssh-keys.sh +++ b/shell/openstack-stack-copy-ssh-keys.sh @@ -50,6 +50,23 @@ copy_ssh_keys () { done } +# shellcheck disable=SC1090 +source ~/lf-env.sh + +# Check if openstack venv was previously created +if [ -f "/tmp/.os_lf_venv" ]; then + os_lf_venv=$(cat "/tmp/.os_lf_venv") +fi + +if [ -d "${os_lf_venv}" ] && [ -f "${os_lf_venv}/bin/openstack" ]; then + echo "Re-use existing venv: ${os_lf_venv}" + PATH=$os_lf_venv/bin:$PATH +else + lf-activate-venv --python python3 \ + python-heatclient \ + python-openstackclient +fi + # IP Addresses are returned as a space separated list so word splitting is ok # shellcheck disable=SC2207 ip_addresses=($(openstack --os-cloud "$os_cloud" stack show -f json -c outputs "$stack_name" | diff --git a/shell/openstack-stack-create.sh b/shell/openstack-stack-create.sh index 478ac5be..0a2dc9ff 100644 --- a/shell/openstack-stack-create.sh +++ b/shell/openstack-stack-create.sh @@ -18,6 +18,29 @@ stack_parameters="$WORKSPACE/stack-parameters.yaml" set -eux -o pipefail +# shellcheck disable=SC1090 +source ~/lf-env.sh + +# Check if openstack venv was previously created +if [ -f "/tmp/.os_lf_venv" ]; then + os_lf_venv=$(cat "/tmp/.os_lf_venv") +fi + +if [ -d "${os_lf_venv}" ] && [ -f "${os_lf_venv}/bin/openstack" ]; then + echo "Re-use existing venv: ${os_lf_venv}" + PATH=$os_lf_venv/bin:$PATH +else + lf-activate-venv --python python3 "cryptography<3.4" \ + "lftools[openstack]" \ + kubernetes \ + "niet~=1.4.2" \ + python-heatclient \ + python-openstackclient \ + python-magnumclient \ + setuptools \ + "openstacksdk<0.99" \ + yq +fi openstack --os-cloud "$os_cloud" limits show --absolute pushd "$stack_template_dir" || exit 1 diff --git a/shell/openstack-stack-delete.sh b/shell/openstack-stack-delete.sh index 79da58af..512ea99a 100644 --- a/shell/openstack-stack-delete.sh +++ b/shell/openstack-stack-delete.sh @@ -15,7 +15,19 @@ set -eufo pipefail # shellcheck disable=SC1090 source ~/lf-env.sh -lf-activate-venv lftools[openstack] python-openstackclient +# Check if openstack venv was previously created +if [ -f "/tmp/.os_lf_venv" ]; then + os_lf_venv=$(cat "/tmp/.os_lf_venv") +fi + +if [ -d "${os_lf_venv}" ] && [ -f "${os_lf_venv}/bin/openstack" ]; then + echo "Re-use existing venv: ${os_lf_venv}" + PATH=$os_lf_venv/bin:$PATH +else + lf-activate-venv --python python3 lftools[openstack] \ + python-heatclient \ + python-openstackclient +fi echo "INFO: Retrieving stack cost for: $OS_STACK_NAME" if ! lftools openstack --os-cloud "$OS_CLOUD" stack cost "$OS_STACK_NAME" > stack-cost; then @@ -28,4 +40,3 @@ fi # Delete the stack even if the stack-cost script fails lftools openstack --os-cloud "$OS_CLOUD" stack delete "$OS_STACK_NAME" \ | echo "INFO: $(cat)" - diff --git a/shell/python-tools-install.sh b/shell/python-tools-install.sh index c8173609..e5d836bf 100644 --- a/shell/python-tools-install.sh +++ b/shell/python-tools-install.sh @@ -17,8 +17,10 @@ python="python3" if [[ -f ~/lf-env.sh ]]; then source ~/lf-env.sh lf-activate-venv --python "$python" lftools + # Save the virtualenv path + echo "$lf_venv" > "/tmp/.os_lf_venv" elif [[ -d /opt/pyenv ]]; then - echo "---> Setting up pyenv" + echo "Setup up pyenv" export PYENV_ROOT="/opt/pyenv" export PATH="$PYENV_ROOT/bin:$PATH" pyenv versions -- 2.16.6