From 2ca10ab06eedffdbc1852bcb0a34f620d25a42f2 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Evrard Date: Sat, 16 Feb 2019 08:07:25 +0100 Subject: [PATCH] Change the osa wrapper from a heredoc to a file The environment variable "OSA_WRAPPER_BIN" has been added to the bootstrap script so that deployers could, if they so choose, override the wrapper script with something else. While I can't imagine folks needing this all the time, there have been several occasions where it's been necessary to augment the wrapper tool locally within an environment. Every-time this has been needed it creates a maintenance burden on operators over the lifetime of the deployment and it'd be great if we had an official entry point into this tooling when needed. This will be required to simplify the releasing process in the same way across the branches. Change-Id: I6a05d3b73838fa646a635831d655f7470df3378f (cherry picked from commit 2b2447e72d955cf0816d34cac7258fbc11a6d5fb) (cherry picked from commit ecdd3822b6007eb804d9e3681959cc382859103f) --- scripts/bootstrap-ansible.sh | 86 ++---------------------------------- scripts/openstack-ansible.sh | 79 +++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 82 deletions(-) create mode 100644 scripts/openstack-ansible.sh diff --git a/scripts/bootstrap-ansible.sh b/scripts/bootstrap-ansible.sh index c759d0f777..ca0bd8b0b8 100755 --- a/scripts/bootstrap-ansible.sh +++ b/scripts/bootstrap-ansible.sh @@ -36,6 +36,8 @@ export PIP_OPTS=${PIP_OPTS:-""} # Set the role fetch mode to any option [galaxy, git-clone] export ANSIBLE_ROLE_FETCH_MODE=${ANSIBLE_ROLE_FETCH_MODE:-git-clone} +export OSA_WRAPPER_BIN="${OSA_WRAPPER_BIN:-scripts/openstack-ansible.sh}" + # This script should be executed from the root directory of the cloned repo cd "$(dirname "${0}")/.." @@ -203,89 +205,9 @@ popd sed "s|OSA_INVENTORY_PATH|${OSA_INVENTORY_PATH}|g" scripts/openstack-ansible.rc > /usr/local/bin/openstack-ansible.rc sed -i "s|OSA_PLAYBOOK_PATH|${OSA_PLAYBOOK_PATH}|g" /usr/local/bin/openstack-ansible.rc - # Create openstack ansible wrapper tool -cat > /usr/local/bin/openstack-ansible < - -# OpenStack wrapper tool to ease the use of ansible with multiple variable files. - -export PATH="/opt/ansible-runtime/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}" - -function info() { - if [ "\${ANSIBLE_NOCOLOR:-0}" -eq "1" ]; then - echo -e "\${@}" - else - echo -e "\e[0;35m\${@}\e[0m" - fi -} - -# Figure out which Ansible binary was executed -RUN_CMD=\$(basename \${0}) - -# Apply the OpenStack-Ansible configuration selectively. -if [[ "\${PWD}" == *"${OSA_CLONE_DIR}"* ]] || [ "\${RUN_CMD}" == "openstack-ansible" ]; then - - # Source the Ansible configuration. - . /usr/local/bin/openstack-ansible.rc - - # Load userspace group vars - if [[ -d /etc/openstack_deploy/group_vars || -d /etc/openstack_deploy/host_vars ]]; then - if [[ ! -f /etc/openstack_deploy/inventory.ini ]]; then - echo '[all]' > /etc/openstack_deploy/inventory.ini - fi - fi - - # Check whether there are any user configuration files - if ls -1 /etc/openstack_deploy/user_*.yml &> /dev/null; then - - # Discover the variable files. - VAR1="\$(for i in \$(ls /etc/openstack_deploy/user_*.yml); do echo -ne "-e @\$i "; done)" - - # Provide information on the discovered variables. - info "Variable files: \"\${VAR1}\"" - - fi - -else - - # If you're not executing 'openstack-ansible' and are - # not in the OSA git clone root, then do not source - # the configuration and do not add extra vars. - VAR1="" - -fi - -# Execute the Ansible command. -if [ "\${RUN_CMD}" == "openstack-ansible" ] || [ "\${RUN_CMD}" == "ansible-playbook" ]; then - ansible-playbook "\${@}" \${VAR1} - PLAYBOOK_RC="\$?" - if [[ "\${PLAYBOOK_RC}" -ne "0" ]]; then - echo -e "\nEXIT NOTICE [Playbook execution failure] **************************************" - else - echo -e "\nEXIT NOTICE [Playbook execution success] **************************************" - fi - echo "===============================================================================" - exit "\${PLAYBOOK_RC}" -else - \${RUN_CMD} "\${@}" -fi -EOF +cp -v ${OSA_WRAPPER_BIN} /usr/local/bin/openstack-ansible +sed -i "s|OSA_CLONE_DIR|${OSA_CLONE_DIR}|g" /usr/local/bin/openstack-ansible # Ensure wrapper tool is executable chmod +x /usr/local/bin/openstack-ansible diff --git a/scripts/openstack-ansible.sh b/scripts/openstack-ansible.sh new file mode 100644 index 0000000000..dd3d823a71 --- /dev/null +++ b/scripts/openstack-ansible.sh @@ -0,0 +1,79 @@ +#!/usr/bin/env bash +# Copyright 2014, Rackspace US, Inc. +# +# 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. +# +# (c) 2014, Kevin Carter + +# OpenStack wrapper tool to ease the use of ansible with multiple variable files. + +export PATH="/opt/ansible-runtime/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}" + +function info { + if [ "${ANSIBLE_NOCOLOR:-0}" -eq "1" ]; then + echo -e "${@}" + else + echo -e "\e[0;35m${@}\e[0m" + fi +} + +# Figure out which Ansible binary was executed +RUN_CMD=$(basename ${0}) + +# Apply the OpenStack-Ansible configuration selectively. +if [[ "${PWD}" == *"OSA_CLONE_DIR"* ]] || [ "${RUN_CMD}" == "openstack-ansible" ]; then + + # Source the Ansible configuration. + . /usr/local/bin/openstack-ansible.rc + + # Load userspace group vars + if [[ -d /etc/openstack_deploy/group_vars || -d /etc/openstack_deploy/host_vars ]]; then + if [[ ! -f /etc/openstack_deploy/inventory.ini ]]; then + echo '[all]' > /etc/openstack_deploy/inventory.ini + fi + fi + + # Check whether there are any user configuration files + if ls -1 /etc/openstack_deploy/user_*.yml &> /dev/null; then + + # Discover the variable files. + VAR1="$(for i in $(ls /etc/openstack_deploy/user_*.yml); do echo -ne "-e @$i "; done)" + + # Provide information on the discovered variables. + info "Variable files: \"${VAR1}\"" + + fi + +else + + # If you're not executing 'openstack-ansible' and are + # not in the OSA git clone root, then do not source + # the configuration and do not add extra vars. + VAR1="" + +fi + +# Execute the Ansible command. +if [ "${RUN_CMD}" == "openstack-ansible" ] || [ "${RUN_CMD}" == "ansible-playbook" ]; then + ansible-playbook "${@}" ${VAR1} + PLAYBOOK_RC="$?" + if [[ "${PLAYBOOK_RC}" -ne "0" ]]; then + echo -e "\nEXIT NOTICE [Playbook execution failure] **************************************" + else + echo -e "\nEXIT NOTICE [Playbook execution success] **************************************" + fi + echo "===============================================================================" + exit "${PLAYBOOK_RC}" +else + ${RUN_CMD} "${@}" +fi