-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathinstall-ansible.sh
executable file
·216 lines (196 loc) · 6.03 KB
/
install-ansible.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/bin/bash
# This script installs the latest version of Ansible and the OpenStack
# client libraries in an isolated python virtual environment.
################################################################################
# Functions
################################################################################
help() {
echo "usage: $0 [-v version]"
echo ""
echo "optional arguments:"
echo "-v version, --version version valid versions: latest, stable"
echo "-n venv-name, --name venv-name override the default python virtual environment name"
echo "-h, --help prints help information"
}
################################################################################
# Main()
################################################################################
# Set Ansible python virtual environment name
ANSIBLE_VENV="ansible-venv"
# Parse command line arguments
VERSION="stable"
while [ $# -ge 1 ]; do
case $1 in
--)
# no more arguments
shift
break
;;
-v|--version)
VERSION="$2"
shift
;;
-n|--name)
ANSIBLE_VENV="$2"
shift
;;
-h|--help)
help
exit 0
;;
*)
echo "Unknown argument $1"
exit 1
;;
esac
shift
done
check_debian_packages() {
PACKAGES=$1
for package in $PACKAGES; do
dpkg-query -Wf'${db:Status-abbrev}' $package 2>/dev/null | grep -q '^i'
if [ $? != 0 ]; then
return 1;
fi
done
return 0;
}
check_python3_exists() {
if [ `python -c 'import sys; print(list(sys.version_info)[:1][0])'` != 2 ]; then
which python3
if [ $? != 0 ]; then
echo -e "\nError: This script requires python3 in order to run.\n"
exit 1
fi
fi
}
# check python3 is available or exit.
check_python3_exists
echo "Installing $VERSION version of Ansible to $ANSIBLE_VENV python virtual environment"
# Install the required packages
RUN_PACKAGE_MANAGER=true;
if [[ -f /etc/debian_version ]] || [[ -f /etc/lsb_release ]]; then
PKG_MANAGER="apt"
# PACKAGES="build-essential gcc git libffi-dev libssl-dev python-dev python-pip python-setuptools"
PACKAGES="build-essential gcc git libffi-dev libssl-dev python3-dev python3-pip python3-setuptools"
if check_debian_packages "$PACKAGES"; then
RUN_PACKAGE_MANAGER=false;
fi
elif [[ -f /etc/redhat-release ]] || [[ -f /etc/fedora-release ]]; then
PKG_MANAGER="yum"
PACKAGES="gcc git python3-devel python3-pip python3-setuptools"
elif [[ -f /etc/solus-release ]]; then
PKG_MANAGER="eopkg"
PACKAGES="gcc git pip3 python3-devel python3-setuptools"
else
echo "Unknown Linux distribution."
echo "Please ensure the packages $PACKAGES are installed before proceeding."
echo "Press ENTER to continue."
read
fi
if [ "$RUN_PACKAGE_MANAGER" = true ]; then
case "$PKG_MANAGER" in
"apt")
sudo $PKG_MANAGER update
sudo $PKG_MANAGER -y install $PACKAGES
;;
"yum")
sudo $PKG_MANAGER -y update
sudo $PKG_MANAGER -y groupinstall 'Development Tools'
sudo $PKG_MANAGER -y install $PACKAGES
;;
"eopkg")
sudo $PKG_MANAGER it -c system.devel
sudo $PKG_MANAGER install $PACKAGES
;;
esac
fi
# Ensure Python virtualenv and pip are installed.
if ! which pip; then
echo "Could not find python pip on \$PATH"
echo "Installing pip via easy_install (sudo password may be required)"
sudo easy_install pip
if ! which pip; then
echo "Could not install pip using easy_install."
echo "This script requires python pip installed."
exit 1
fi
fi
if ! which virtualenv; then
echo "Could not find virtualenv on \$PATH"
echo "Installing virtualenv via easy_install (sudo password may be required)"
sudo easy_install virtualenv
if ! which virtualenv; then
echo "Could not install virtualenv using easy_install."
echo "This script requires python virtualenv installed."
exit 1
fi
fi
# Create and activate a python3 virtual environment for Ansible.
if ! virtualenv --python=python3 "$ANSIBLE_VENV"; then
echo "Failed to create virtual environment for Ansible at current location."
exit 1
fi
source "$ANSIBLE_VENV/bin/activate"
# Update pip to latest version.
if ! pip install -U pip; then
echo "Could not update pip."
exit 1
fi
# Updating setuptools fixes this warning (see https://github.com/ansible/ansible/pull/16723)
# [WARNING]: Optional dependency 'cryptography' raised an exception, falling back to 'Crypto'
# Update setuptools to latest version.
if ! pip install -U setuptools; then
echo "Could not update setuptools."
exit 1
fi
# Install the selected version of Ansible.
if [[ "$VERSION" == "latest" ]]; then
if [[ -d "$ANSIBLE_VENV/ansible" ]]; then
rm -rf "$ANSIBLE_VENV/ansible"
fi
if ! git clone git://github.com/ansible/ansible.git --recursive "$ANSIBLE_VENV/ansible"; then
echo "Could not install the latest version of Ansible."
exit 1
fi
if ! pip install jinja2; then
echo "Could not install jinja2 as a dependency for Ansible."
exit 1
fi
elif [[ "$VERSION" == "stable" ]]; then
if ! pip install ansible; then
echo "Could not install the stable version of Ansible."
exit 1
fi
else
echo "Unknown version: $VERSION."
echo "Valid versions are stable and latest."
exit 1
fi
# Install the shade library and the OpenStack client libraries.
if ! pip install shade python-openstackclient openstacksdk; then
echo "Could not install the OpenStack client tools and shade"
exit 1
fi
# Install dnspython so we can do opendns lookups
# this lets us create better default security group rules
if ! pip install dnspython; then
echo "Could not install dnspython"
exit 1
fi
echo
echo "Ansible installed successfully!"
echo
echo "The following versions are installed in $PWD/$ANSIBLE_VENV:"
echo
"$PWD/$ANSIBLE_VENV/bin/pip" freeze | egrep 'ansible|shade|openstackclient|openstacksdk'
echo
echo "To activate run the following command:"
echo
if [[ "$VERSION" == "stable" ]]; then
echo "source $PWD/$ANSIBLE_VENV/bin/activate"
else
echo "source $PWD/$ANSIBLE_VENV/bin/activate && source $PWD/$ANSIBLE_VENV/ansible/hacking/env-setup"
fi
echo
exit 0