prototypes: xci: Make sure Ansible dependencies are installed
[releng.git] / prototypes / xci / file / install-ansible.sh
1 #!/bin/bash
2 # NOTE(hwoarang): Most parts of this this file were taken from the
3 # bifrost repository (scripts/install-deps.sh). This script contains all
4 # the necessary distro specific code to install ansible and it's dependencies.
5
6 set -eu
7
8 declare -A PKG_MAP
9
10 CHECK_CMD_PKGS=(
11     libffi
12     libopenssl
13     net-tools
14     python-devel
15 )
16
17 # Check zypper before apt-get in case zypper-aptitude
18 # is installed
19 if [ -x '/usr/bin/zypper' ]; then
20     OS_FAMILY="Suse"
21     INSTALLER_CMD="sudo -H -E zypper install -y"
22     CHECK_CMD="zypper search --match-exact --installed"
23     PKG_MAP=(
24         [gcc]=gcc
25         [git]=git
26         [libffi]=libffi-devel
27         [libopenssl]=libopenssl-devel
28         [net-tools]=net-tools
29         [python]=python
30         [python-devel]=python-devel
31         [venv]=python-virtualenv
32         [wget]=wget
33     )
34     EXTRA_PKG_DEPS=( python-xml )
35     # NOTE (cinerama): we can't install python without removing this package
36     # if it exists
37     if $(${CHECK_CMD} patterns-openSUSE-minimal_base-conflicts &> /dev/null); then
38         sudo -H zypper remove -y patterns-openSUSE-minimal_base-conflicts
39     fi
40 elif [ -x '/usr/bin/apt-get' ]; then
41     OS_FAMILY="Debian"
42     INSTALLER_CMD="sudo -H -E apt-get -y install"
43     CHECK_CMD="dpkg -l"
44     PKG_MAP=( [gcc]=gcc
45               [git]=git
46               [libffi]=libffi-dev
47               [libopenssl]=libssl-dev
48               [net-tools]=net-tools
49               [python]=python-minimal
50               [python-devel]=libpython-dev
51               [venv]=python-virtualenv
52               [wget]=wget
53             )
54     EXTRA_PKG_DEPS=()
55 elif [ -x '/usr/bin/dnf' ] || [ -x '/usr/bin/yum' ]; then
56     OS_FAMILY="RedHat"
57     PKG_MANAGER=$(which dnf || which yum)
58     INSTALLER_CMD="sudo -H -E ${PKG_MANAGER} -y install"
59     CHECK_CMD="rpm -q"
60     PKG_MAP=(
61         [gcc]=gcc
62         [git]=git
63         [libffi]=libffi-devel
64         [libopenssl]=openssl-devel
65         [net-tools]=net-tools
66         [python]=python
67         [python-devel]=python-devel
68         [venv]=python-virtualenv
69         [wget]=wget
70     )
71     EXTRA_PKG_DEPS=()
72 else
73     echo "ERROR: Supported package manager not found.  Supported: apt,yum,zypper"
74 fi
75
76 if ! $(python --version &>/dev/null); then
77     ${INSTALLER_CMD} ${PKG_MAP[python]}
78 fi
79 if ! $(gcc -v &>/dev/null); then
80     ${INSTALLER_CMD} ${PKG_MAP[gcc]}
81 fi
82 if ! $(git --version &>/dev/null); then
83     ${INSTALLER_CMD} ${PKG_MAP[git]}
84 fi
85 if ! $(wget --version &>/dev/null); then
86     ${INSTALLER_CMD} ${PKG_MAP[wget]}
87 fi
88
89 for pkg in ${CHECK_CMD_PKGS[@]}; do
90     if ! $(${CHECK_CMD} ${PKG_MAP[$pkg]} &>/dev/null); then
91         ${INSTALLER_CMD} ${PKG_MAP[$pkg]}
92     fi
93 done
94
95 if [ -n "${EXTRA_PKG_DEPS-}" ]; then
96     for pkg in ${EXTRA_PKG_DEPS}; do
97         if ! $(${CHECK_CMD} ${pkg} &>/dev/null); then
98             ${INSTALLER_CMD} ${pkg}
99         fi
100     done
101 fi
102
103 # If we're using a venv, we need to work around sudo not
104 # keeping the path even with -E.
105 PYTHON=$(which python)
106
107 # To install python packages, we need pip.
108 #
109 # We can't use the apt packaged version of pip since
110 # older versions of pip are incompatible with
111 # requests, one of our indirect dependencies (bug 1459947).
112 #
113 # Note(cinerama): We use pip to install an updated pip plus our
114 # other python requirements. pip breakages can seriously impact us,
115 # so we've chosen to install/upgrade pip here rather than in
116 # requirements (which are synced automatically from the global ones)
117 # so we can quickly and easily adjust version parameters.
118 # See bug 1536627.
119 #
120 # Note(cinerama): If pip is linked to pip3, the rest of the install
121 # won't work. Remove the alternatives. This is due to ansible's
122 # python 2.x requirement.
123 if [[ $(readlink -f /etc/alternatives/pip) =~ "pip3" ]]; then
124     sudo -H update-alternatives --remove pip $(readlink -f /etc/alternatives/pip)
125 fi
126
127 if ! which pip; then
128     wget -O /tmp/get-pip.py https://bootstrap.pypa.io/get-pip.py
129     sudo -H -E ${PYTHON} /tmp/get-pip.py
130 fi
131
132 PIP=$(which pip)
133
134 sudo -H -E ${PIP} install "pip>6.0"
135
136 pip install ansible==$XCI_ANSIBLE_PIP_VERSION