bifrost/scripts/env-setup.sh
Pavlo Shchelokovskyy 7a7f858ae2 Always install Ansible with pip
Instaling and using Ansible from source for bifrost has several
drawbacks, mainly due to how Ansible's 'ansible/hacking/env-setup'
script mangles with PATH and PYTHONPATH, which complicates running it as
part of other scripts. Besides, cloning the whole repo and it's
submodules is somewhat longer.

The main reason why we were doing that at all was a necessity to install
some additional Ansible modules from newer Ansible versions, which we
dropped right into the source of Ansible code - but this does not have to
be so.

Luckily for us, all Ansible versions we target to support can load
modules from 'library' directory next to playbooks/roles,
and we already use that for 'os_ironic_facts' module.
The need to install a particular module can be assessed by running
ad-hoc 'ansible' command against localhost with the module in question
and without any arguments ('ansible localhost -m <module>'):
- if the module is available in Ansible, the stderr will contain
  "changed" substring (as part of the standard module output)
- if the module is absent form Ansible, "changed" string will be absent
  from stderr too, in which case we can download the module from github
  directly into 'playbooks/library' directory.

This patch removes possibility of installing Ansible from source, and
always installs a released Ansible version via pip.
If not installed into venv, Ansible will be installed in user's ~/.local
directory via 'pip install --user'.
The missing but needed modules are downloaded as described above.

Some level of backward compatibility is provided:
- when the ANSIBLE_GIT_BRANCH has form of 'stable-X.Y', the
  env-setup.sh script will do the next best thing and install latest
  available Ansible version of X.Y.w.z

Also, ANSIBLE_PIP_VERSION can now accept a full pip version specifier:
- if ANSIBLE_PIP_VERSION starts with a digit, this exact version will be
  installed (as 'ansible==X.Y.W.Z')
- otherwize this whole variable is assigned as Ansible version specifier
  for pip, e.g

    env ANSIBLE_PIP_VERSION="<2.2" env-setup.sh

  will result in pip being called as

    pip install -U "ansible<2.2"

Closes-Bug: #1663562
Change-Id: I2c9f47abbbb6740d03978f684ad2c876749655b7
2017-02-13 13:10:54 +02:00

74 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
set -eu
source $(dirname $0)/install-deps.sh
# NOTE(pas-ha) the above exports some useful variables like
# $PYTHON , $PIP and $VENV depending on venv install or not
ANSIBLE_PIP_VERSION=${ANSIBLE_PIP_VERSION:-${ANSIBLE_GIT_BRANCH:-stable-2.1}}
ANSIBLE_PIP_STRING=$(${PYTHON} $(dirname $0)/ansible-pip-str.py ${ANSIBLE_PIP_VERSION})
if [ -n "${VENV-}" ]; then
${PIP} install --upgrade "${ANSIBLE_PIP_STRING}"
ANSIBLE=${VENV}/bin/ansible
else
${PIP} install --user --upgrade "${ANSIBLE_PIP_STRING}"
ANSIBLE=${HOME}/.local/bin/ansible
fi
PLAYBOOKS_LIBRARY_PATH=$(dirname $0)/../playbooks/library
function check_get_module () {
local module=${1}
local module_url_base=${2}
${ANSIBLE} localhost -m ${module} | grep "changed" || \
wget "${module_url_base}/${module}.py" -O "${PLAYBOOKS_LIBRARY_PATH}/${module}.py"
}
# Note(TheJulia): These files should be in the ansible library folder
# and this functionality exists for a level of ansible 1.9.x
# backwards compatability although the modules were developed
# for Ansible 2.0.
check_get_module os_ironic \
https://raw.githubusercontent.com/ansible/ansible-modules-core/stable-2.0/cloud/openstack
check_get_module os_ironic_node \
https://raw.githubusercontent.com/ansible/ansible-modules-core/stable-2.0/cloud/openstack
# os_ironic_inspect has appeared in Ansible 2.1
check_get_module os_ironic_inspect \
https://raw.githubusercontent.com/ansible/ansible-modules-extras/stable-2.1/cloud/openstack
# os_keystone_service has appeared in Ansible 2.2
check_get_module os_keystone_service \
https://raw.githubusercontent.com/ansible/ansible-modules-extras/stable-2.2/cloud/openstack
# NOTE(pas-ha) the following is a temporary workaround for third-party CI
# scripts that try to source Ansible's hacking/env-setup
# after running this very script
# TODO(pas-ha) remove after deprecation (in Pike?) and when third-party CIs
# (in particular OPNFV) are fixed
ANSIBLE_INSTALL_ROOT=${ANSIBLE_INSTALL_ROOT:-/opt/stack}
u=$(whoami)
g=$(groups | awk '{print $1}')
if [ ! -d ${ANSIBLE_INSTALL_ROOT} ]; then
mkdir -p ${ANSIBLE_INSTALL_ROOT} || (sudo mkdir -p ${ANSIBLE_INSTALL_ROOT})
fi
sudo -H chown -R $u:$g ${ANSIBLE_INSTALL_ROOT}
mkdir -p ${ANSIBLE_INSTALL_ROOT}/ansible/hacking
echo "echo Sourcing this file is no longer needed! Ansible is always installed from PyPI" > ${ANSIBLE_INSTALL_ROOT}/ansible/hacking/env-setup
echo
echo "To use bifrost, do"
if [ -n "${VENV-}" ]; then
echo "source ${VENV}/bin/activate"
else
echo "Prepend ~/.local/bin to your PATH if it is not that way already.."
echo ".. or use full path to local Ansible at ~/.local/bin/ansible-playbook"
fi
echo "source env-vars"
echo "Then run playbooks as normal."
echo