xci: files: Move Ansible installation to a library file 99/54099/3
authorMarkos Chandras <mchandras@suse.de>
Mon, 19 Mar 2018 09:37:29 +0000 (09:37 +0000)
committerMarkos Chandras <mchandras@suse.de>
Wed, 21 Mar 2018 15:19:14 +0000 (15:19 +0000)
Installing Ansible is just one of the functions we need during XCI
deployment, as such move it to a library file which can contain more
functions in the future.

Change-Id: I708a4e51b2b1d624d6827a8c99f3719b59227ec0
Signed-off-by: Markos Chandras <mchandras@suse.de>
xci/files/install-ansible.sh [deleted file]
xci/files/install-lib.sh [new file with mode: 0644]
xci/xci-deploy.sh

diff --git a/xci/files/install-ansible.sh b/xci/files/install-ansible.sh
deleted file mode 100644 (file)
index e370d14..0000000
+++ /dev/null
@@ -1,161 +0,0 @@
-#!/bin/bash
-# NOTE(hwoarang): Most parts of this this file were taken from the
-# bifrost repository (scripts/install-deps.sh). This script contains all
-# the necessary distro specific code to install ansible and it's dependencies.
-
-set -eu
-
-declare -A PKG_MAP
-
-# workaround: for latest bindep to work, it needs to use en_US local
-export LANG=c
-
-CHECK_CMD_PKGS=(
-    gcc
-    libffi
-    libopenssl
-    lsb-release
-    make
-    net-tools
-    python-devel
-    python
-    venv
-    wget
-)
-
-source /etc/os-release || source /usr/lib/os-release
-case ${ID,,} in
-    *suse)
-    OS_FAMILY="Suse"
-    INSTALLER_CMD="sudo -H -E zypper -q install -y --no-recommends"
-    CHECK_CMD="zypper search --match-exact --installed"
-    PKG_MAP=(
-        [gcc]=gcc
-        [libffi]=libffi-devel
-        [libopenssl]=libopenssl-devel
-        [lsb-release]=lsb-release
-        [make]=make
-        [net-tools]=net-tools
-        [python]=python
-        [python-devel]=python-devel
-        [venv]=python-virtualenv
-        [wget]=wget
-    )
-    EXTRA_PKG_DEPS=( python-xml )
-    sudo zypper -n ref
-    # NOTE (cinerama): we can't install python without removing this package
-    # if it exists
-    if $(${CHECK_CMD} patterns-openSUSE-minimal_base-conflicts &> /dev/null); then
-        sudo -H zypper remove -y patterns-openSUSE-minimal_base-conflicts
-    fi
-    ;;
-
-    ubuntu|debian)
-    OS_FAMILY="Debian"
-    export DEBIAN_FRONTEND=noninteractive
-    INSTALLER_CMD="sudo -H -E apt-get -y -q=3 install"
-    CHECK_CMD="dpkg -l"
-    PKG_MAP=(
-        [gcc]=gcc
-        [libffi]=libffi-dev
-        [libopenssl]=libssl-dev
-        [lsb-release]=lsb-release
-        [make]=make
-        [net-tools]=net-tools
-        [python]=python-minimal
-        [python-devel]=libpython-dev
-        [venv]=python-virtualenv
-        [wget]=wget
-    )
-    EXTRA_PKG_DEPS=()
-    sudo apt-get update
-    ;;
-
-    rhel|fedora|centos)
-    OS_FAMILY="RedHat"
-    PKG_MANAGER=$(which dnf || which yum)
-    INSTALLER_CMD="sudo -H -E ${PKG_MANAGER} -q -y install"
-    CHECK_CMD="rpm -q"
-    PKG_MAP=(
-        [gcc]=gcc
-        [libffi]=libffi-devel
-        [libopenssl]=openssl-devel
-        [lsb-release]=redhat-lsb
-        [make]=make
-        [net-tools]=net-tools
-        [python]=python
-        [python-devel]=python-devel
-        [venv]=python-virtualenv
-        [wget]=wget
-    )
-    sudo $PKG_MANAGER updateinfo
-    EXTRA_PKG_DEPS=( deltarpm )
-    ;;
-
-    *) echo "ERROR: Supported package manager not found.  Supported: apt, dnf, yum, zypper"; exit 1;;
-esac
-
-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 ! $(wget --version &>/dev/null); then
-    ${INSTALLER_CMD} ${PKG_MAP[wget]}
-fi
-
-if ! $(python -m virtualenv --version &>/dev/null); then
-    ${INSTALLER_CMD} ${PKG_MAP[venv]}
-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 we're using a venv, we need to work around sudo not
-# keeping the path even with -E.
-PYTHON=$(which python)
-
-# To install python packages, we need pip.
-#
-# We can't use the apt packaged version of pip since
-# older versions of pip are incompatible with
-# requests, one of our indirect dependencies (bug 1459947).
-#
-# Note(cinerama): We use pip to install an updated pip plus our
-# other python requirements. pip breakages can seriously impact us,
-# so we've chosen to install/upgrade pip here rather than in
-# requirements (which are synced automatically from the global ones)
-# so we can quickly and easily adjust version parameters.
-# 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
-    wget -O /tmp/get-pip.py https://bootstrap.pypa.io/get-pip.py
-    sudo -H -E ${PYTHON} /tmp/get-pip.py
-fi
-
-PIP=$(which pip)
-echo "Using pip: $(${PIP} --version)"
-sudo -H -E ${PIP} -q install --upgrade virtualenv
-sudo -H -E ${PIP} -q install --upgrade pip
-# upgrade setuptools, as latest version is needed to install some projects
-sudo -H -E ${PIP} -q install --upgrade setuptools
-${PIP} install -q --user --upgrade ansible==$XCI_ANSIBLE_PIP_VERSION
diff --git a/xci/files/install-lib.sh b/xci/files/install-lib.sh
new file mode 100644 (file)
index 0000000..35a7d62
--- /dev/null
@@ -0,0 +1,173 @@
+# SPDX-license-identifier: Apache-2.0
+##############################################################################
+# Copyright (c) 2018 SUSE LINUX GmbH.
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+# NOTE(hwoarang): Most parts of this this file were taken from the
+# bifrost repository (scripts/install-deps.sh). This script contains all
+# the necessary distro specific code to install ansible and it's dependencies.
+
+function install_ansible() {
+    set -eu
+
+    declare -A PKG_MAP
+
+    # workaround: for latest bindep to work, it needs to use en_US local
+    export LANG=c
+
+    CHECK_CMD_PKGS=(
+        gcc
+        libffi
+        libopenssl
+        lsb-release
+        make
+        net-tools
+        python-devel
+        python
+        venv
+        wget
+    )
+
+    source /etc/os-release || source /usr/lib/os-release
+    case ${ID,,} in
+      *suse)
+        OS_FAMILY="Suse"
+        INSTALLER_CMD="sudo -H -E zypper -q install -y --no-recommends"
+        CHECK_CMD="zypper search --match-exact --installed"
+        PKG_MAP=(
+            [gcc]=gcc
+            [libffi]=libffi-devel
+            [libopenssl]=libopenssl-devel
+            [lsb-release]=lsb-release
+            [make]=make
+            [net-tools]=net-tools
+            [python]=python
+            [python-devel]=python-devel
+            [venv]=python-virtualenv
+            [wget]=wget
+        )
+        EXTRA_PKG_DEPS=( python-xml )
+        sudo zypper -n ref
+        # NOTE (cinerama): we can't install python without removing this package
+        # if it exists
+        if $(${CHECK_CMD} patterns-openSUSE-minimal_base-conflicts &> /dev/null); then
+            sudo -H zypper remove -y patterns-openSUSE-minimal_base-conflicts
+        fi
+        ;;
+
+        ubuntu|debian)
+        OS_FAMILY="Debian"
+        export DEBIAN_FRONTEND=noninteractive
+        INSTALLER_CMD="sudo -H -E apt-get -y -q=3 install"
+        CHECK_CMD="dpkg -l"
+        PKG_MAP=(
+            [gcc]=gcc
+            [libffi]=libffi-dev
+            [libopenssl]=libssl-dev
+            [lsb-release]=lsb-release
+            [make]=make
+            [net-tools]=net-tools
+            [python]=python-minimal
+            [python-devel]=libpython-dev
+            [venv]=python-virtualenv
+            [wget]=wget
+        )
+        EXTRA_PKG_DEPS=()
+        sudo apt-get update
+        ;;
+
+        rhel|fedora|centos)
+        OS_FAMILY="RedHat"
+        PKG_MANAGER=$(which dnf || which yum)
+        INSTALLER_CMD="sudo -H -E ${PKG_MANAGER} -q -y install"
+        CHECK_CMD="rpm -q"
+        PKG_MAP=(
+            [gcc]=gcc
+            [libffi]=libffi-devel
+            [libopenssl]=openssl-devel
+            [lsb-release]=redhat-lsb
+            [make]=make
+            [net-tools]=net-tools
+            [python]=python
+            [python-devel]=python-devel
+            [venv]=python-virtualenv
+            [wget]=wget
+        )
+        sudo $PKG_MANAGER updateinfo
+        EXTRA_PKG_DEPS=( deltarpm )
+        ;;
+
+        *) echo "ERROR: Supported package manager not found.  Supported: apt, dnf, yum, zypper"; exit 1;;
+    esac
+
+    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 ! $(wget --version &>/dev/null); then
+        ${INSTALLER_CMD} ${PKG_MAP[wget]}
+    fi
+
+    if ! $(python -m virtualenv --version &>/dev/null); then
+        ${INSTALLER_CMD} ${PKG_MAP[venv]}
+    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 we're using a venv, we need to work around sudo not
+    # keeping the path even with -E.
+    PYTHON=$(which python)
+
+    # To install python packages, we need pip.
+    #
+    # We can't use the apt packaged version of pip since
+    # older versions of pip are incompatible with
+    # requests, one of our indirect dependencies (bug 1459947).
+    #
+    # Note(cinerama): We use pip to install an updated pip plus our
+    # other python requirements. pip breakages can seriously impact us,
+    # so we've chosen to install/upgrade pip here rather than in
+    # requirements (which are synced automatically from the global ones)
+    # so we can quickly and easily adjust version parameters.
+    # 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
+        wget -O /tmp/get-pip.py https://bootstrap.pypa.io/get-pip.py
+        sudo -H -E ${PYTHON} /tmp/get-pip.py
+    fi
+
+    PIP=$(which pip)
+    echo "Using pip: $(${PIP} --version)"
+    sudo -H -E ${PIP} -q install --upgrade virtualenv
+    sudo -H -E ${PIP} -q install --upgrade pip
+    # upgrade setuptools, as latest version is needed to install some projects
+    sudo -H -E ${PIP} -q install --upgrade setuptools
+    ${PIP} install -q --user --upgrade ansible==$XCI_ANSIBLE_PIP_VERSION
+}
+
+# vim: set ts=4 sw=4 expandtab:
index 04e52fe..19ae7c0 100755 (executable)
@@ -64,6 +64,9 @@ source "$XCI_PATH/xci/config/${XCI_FLAVOR}-vars"
 source "$XCI_PATH/xci/installer/${INSTALLER_TYPE}/env" &>/dev/null || true
 # source xci configuration
 source $XCI_PATH/xci/config/env-vars
+# source helpers library
+source ${XCI_PATH}/xci/files/install-lib.sh
+
 # Make sure we pass XCI_PATH everywhere
 export XCI_ANSIBLE_PARAMS+=" -e xci_path=${XCI_PATH}"
 # Make sure everybody knows where our global roles are
@@ -118,7 +121,7 @@ echo "-------------------------------------------------------------------------"
 #-------------------------------------------------------------------------------
 echo "Info: Installing Ansible from pip"
 echo "-------------------------------------------------------------------------"
-bash files/install-ansible.sh
+install_ansible
 echo "-------------------------------------------------------------------------"
 
 # Clone OPNFV scenario repositories