Refactor env-setup.sh

env-setup.sh got kind of unwieldy and out of sync
with itself. Refactor so we have relatively consistent
package lists, installation checks, etc. for each OS family.
This should be easier to maintain than what we have now
as it's easier to see where additional requirements should
be added.

Change-Id: I8a620a177efd8303bf30ebeb0f6a719a49eedf07
Closes-Bug: #1583803
This commit is contained in:
stephane 2016-09-16 19:48:43 -07:00 committed by Stephanie Miller
parent 767276b6f0
commit 5644b68a54

View File

@ -13,109 +13,105 @@ function check_get_module () {
fi fi
} }
declare -A PKG_MAP
CHECK_CMD_PKGS=(
libffi
libopenssl
net-tools
python-devel
)
# Check zypper before apt-get in case zypper-aptitude # Check zypper before apt-get in case zypper-aptitude
# is installed # is installed
if [ -x '/usr/bin/zypper' ]; then if [ -x '/usr/bin/zypper' ]; then
if ! $(python --version &>/dev/null); then OS_FAMILY="Suse"
sudo -H zypper install -y python INSTALLER_CMD="sudo -H zypper install -y"
fi CHECK_CMD="zypper search --match-exact --installed"
if ! zypper search --match-exact --installed python-devel &>/dev/null; then PKG_MAP=(
sudo -H zypper install -y python-devel [gcc]=gcc
fi [git]=git
if ! $(gcc -v &>/dev/null); then [libffi]=libffi-devel
sudo -H zypper install -y gcc [libopenssl]=libopenssl-devel
fi [net-tools]=net-tools
if ! $(git --version &>/dev/null); then [python]=python
sudo -H zypper install -y git [python-devel]=python-devel
fi [venv]=python-virtualenv
if ! $(wget --version &>/dev/null); then [wget]=wget
sudo -H zypper install -y wget )
fi EXTRA_PKG_DEPS=( python-xml )
if [ -n "${VENV-}" ]; then # NOTE (cinerama): we can't install python without removing this package
if $(virtualenv --version &>/dev/null); then # if it exists
sudo -H zypper install -y python-virtualenv if $(${CHECK_CMD} patterns-openSUSE-minimal_base-conflicts &> /dev/null); then
fi sudo -H zypper remove -y patterns-openSUSE-minimal_base-conflicts
fi
if ! zypper search --match-exact --installed libopenssl-devel &>/dev/null; then
sudo -H zypper install -y libopenssl-devel
fi
if ! zypper search --installed libffi-devel &>/dev/null; then
sudo -H zypper install -y libffi-devel
fi
if ! zypper search --installed net-tools &>/dev/null; then
sudo -H zypper install -y net-tools
fi
if ! zypper search --match-exact --installed python-pip &>/dev/null; then
sudo -H zypper install -y python-pip
fi
# Make sure python-pip is the preferred one
if readlink -f /etc/alternatives/pip | grep -q "3."; then
sudo -H update-alternatives --set pip /usr/bin/pip2.*
fi fi
elif [ -x '/usr/bin/apt-get' ]; then elif [ -x '/usr/bin/apt-get' ]; then
if ! $(gcc -v &>/dev/null); then OS_FAMILY="Debian"
sudo -H apt-get -y install gcc INSTALLER_CMD="sudo -H apt-get -y install"
fi CHECK_CMD="dpkg -l"
if ! $(git --version &>/dev/null) ; then PKG_MAP=( [gcc]=gcc
sudo -H apt-get -y install git [git]=git
fi [libffi]=libffi-dev
if ! $(python --version &>/dev/null); then [libopenssl]=libssl-dev
sudo -H apt-get -y install python-minimal [net-tools]=net-tools
fi [python]=python-minimal
if ! $(dpkg -l libpython-dev &>/dev/null); then [python-devel]=libpython-dev
sudo -H apt-get -y install libpython-dev [venv]=python-virtualenv
fi [wget]=wget
if ! $(dpkg -l wget &>/dev/null); then )
sudo -H apt-get -y install wget EXTRA_PKG_DEPS=()
fi
if [ -n "${VENV-}" ]; then
if ! $(virtualenv --version &>/dev/null); then
sudo -H apt-get -y install python-virtualenv
fi
fi
if ! $(dpkg -l libssl-dev &>/dev/null); then
sudo -H apt-get -y install libssl-dev
fi
if ! $(dpkg -l libffi-dev &>/dev/null); then
sudo -H apt-get -y install libffi-dev
fi
if ! $(dpkg -l net-tools &>/dev/null); then
sudo -H apt-get -y install net-tools
fi
elif [ -x '/usr/bin/yum' ]; then elif [ -x '/usr/bin/yum' ]; then
if ! $(python --version &>/dev/null); then OS_FAMILY="RedHat"
sudo -H yum -y install python INSTALLER_CMD="sudo -H yum -y install"
fi CHECK_CMD="rpm -q"
if ! yum -q list installed python-devel; then PKG_MAP=(
sudo -H yum -y install python-devel [gcc]=gcc
fi [git]=git
if ! $(gcc -v &>/dev/null); then [libffi]=libffi-devel
sudo -H yum -y install gcc [libopenssl]=openssl-devel
fi [net-tools]=net-tools
if ! $(git --version &>/dev/null); then [python]=python
sudo -H yum -y install git [python-devel]=python-devel
fi [venv]=python-virtualenv
if ! $(wget --version &>/dev/null); then [wget]=wget
sudo -H yum -y install wget )
fi EXTRA_PKG_DEPS=()
if [ -n "${VENV-}" ]; then
if $(virtualenv --version &>/dev/null); then
sudo -H yum -y install python-virtualenv
fi
fi
if ! $(rpm -q openssl-devel &>/dev/null); then
sudo -H yum -y install openssl-devel
fi
if ! $(rpm -q libffi-devel &>/dev/null); then
sudo -H yum -y install libffi-devel
fi
if ! $(rpm -q net-tools &>/dev/null); then
sudo -H yum -y install net-tools
fi
else else
echo "ERROR: Supported package manager not found. Supported: apt,yum,zypper" echo "ERROR: Supported package manager not found. Supported: apt,yum,zypper"
fi fi
if ! $(python --version &>/dev/null); then
${INSTALLER_CMD} ${PKG_MAP[python]}
fi
if ! $(gcc -v &>/dev/null); then
${INSTALLER_CMD} ${PKG_MAP[gcc]}
fi
if ! $(git --version &>/dev/null); then
${INSTALLER_CMD} ${PKG_MAP[git]}
fi
if ! $(wget --version &>/dev/null); then
${INSTALLER_CMD} ${PKG_MAP[wget]}
fi
if [ -n "${VENV-}" ]; then
if $(virtualenv --version &>/dev/null); then
${INSTALLER_CMD} ${PKG_MAP[venv]}
fi
fi
for pkg in ${CHECK_CMD_PKGS[@]}; do
if ! $(${CHECK_CMD} ${PKG_MAP[$pkg]} &>/dev/null); then
${INSTALLER_CMD} ${PKG_MAP[$pkg]}
fi
done
if [ -n "${EXTRA_PKG_DEPS-}" ]; then
for pkg in ${EXTRA_PKG_DEPS}; do
if ! $(${CHECK_CMD} ${pkg} &>/dev/null); then
${INSTALLER_CMD} ${pkg}
fi
done
fi
if [ -n "${VENV-}" ]; then if [ -n "${VENV-}" ]; then
echo "NOTICE: Using virtualenv for this installation." echo "NOTICE: Using virtualenv for this installation."
if [ ! -f ${VENV}/bin/activate ]; then if [ ! -f ${VENV}/bin/activate ]; then
@ -148,6 +144,13 @@ PYTHON=$(which python)
# requirements (which are synced automatically from the global ones) # requirements (which are synced automatically from the global ones)
# so we can quickly and easily adjust version parameters. # so we can quickly and easily adjust version parameters.
# See bug 1536627. # See bug 1536627.
#
# Note(cinerama): If pip is linked to pip3, the rest of the install
# won't work. Remove the alternatives. This is due to ansible's
# python 2.x requirement.
if [[ $(readlink -f /etc/alternatives/pip) =~ "pip3" ]]; then
sudo -H update-alternatives --remove pip $(readlink -f /etc/alternatives/pip)
fi
if ! which pip; then if ! which pip; then
wget -O /tmp/get-pip.py https://bootstrap.pypa.io/get-pip.py wget -O /tmp/get-pip.py https://bootstrap.pypa.io/get-pip.py
@ -155,6 +158,7 @@ if ! which pip; then
fi fi
PIP=$(which pip) PIP=$(which pip)
sudo -H -E ${PIP} install "pip>6.0" sudo -H -E ${PIP} install "pip>6.0"
sudo -H -E ${PIP} install -r "$(dirname $0)/../requirements.txt" sudo -H -E ${PIP} install -r "$(dirname $0)/../requirements.txt"
u=$(whoami) u=$(whoami)