forked from Azure/batch-shipyard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·359 lines (334 loc) · 10.4 KB
/
install.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env bash
# shellcheck disable=SC1090
# shellcheck disable=SC1091
set -e
set -o pipefail
# vars
PYTHON=python3
PIP=pip3
SUDO=sudo
VENV_NAME=.shipyard
# process options
while getopts "h?23ce:u" opt; do
case "$opt" in
h|\?)
echo "install.sh parameters"
echo ""
echo "-2 install for Python 2.7"
echo "-3 install for Python 3.4+ [default]"
echo "-c install for Cloud Shell (via Dockerfile)"
echo "-e [environment name] install to a virtual environment"
echo "-u force install into user python environment instead of a virtual enviornment"
echo ""
exit 1
;;
2)
PYTHON=python
PIP=pip
;;
3)
PYTHON=python3
PIP=pip3
;;
c)
PYTHON=python3
PIP=pip3
VENV_NAME=cloudshell
SUDO=
;;
e)
VENV_NAME=$OPTARG
;;
u)
VENV_NAME=
;;
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
# non-cloud shell environment checks
if [ -n "$SUDO" ]; then
# check to ensure this is not being run directly as root
if [ "$(id -u)" -eq 0 ]; then
echo "Installation cannot be performed as root or via sudo."
echo "Please install as a regular user."
exit 1
fi
# check for sudo
if hash sudo 2> /dev/null; then
echo "sudo found."
else
echo "sudo not found. Please install sudo first before proceeding."
exit 1
fi
fi
# check that shipyard.py is in cwd
if [ ! -f "${PWD}"/shipyard.py ]; then
echo "shipyard.py not found in $PWD."
echo "Please run install.sh from the same directory as shipyard.py."
exit 1
fi
# check for python
if hash $PYTHON 2> /dev/null; then
echo "Installing for $PYTHON."
else
echo "$PYTHON not found, please install $PYTHON first with your system software installer."
exit 1
fi
# check for anaconda
set +e
ANACONDA=0
if $PYTHON -c "from __future__ import print_function; import sys; print(sys.version)" | grep -Ei 'anaconda|continuum|conda-forge'; then
# check for conda
if hash conda 2> /dev/null; then
echo "Anaconda environment detected."
else
echo "Anaconda environment detected, but conda command not found."
exit 1
fi
if [ -z "$VENV_NAME" ]; then
echo "Virtual environment name must be supplied for Anaconda installations."
exit 1
fi
ANACONDA=1
PIP=pip
fi
set -e
# perform some virtual env parameter checks
INSTALL_VENV_BIN=0
if [ -n "$VENV_NAME" ]; then
# check if virtual env, env is not named shipyard
if [ "$VENV_NAME" == "shipyard" ]; then
echo "Virtual environment name cannot be shipyard. Please use a different virtual environment name."
exit 1
fi
# check for virtualenv executable
if [ $ANACONDA -eq 0 ]; then
if hash virtualenv 2> /dev/null; then
echo "virtualenv found."
else
echo "virtualenv not found."
INSTALL_VENV_BIN=1
fi
fi
echo "Installing into virtualenv: $VENV_NAME"
else
echo "Installing into user environment instead of virtualenv"
fi
# try to get /etc/lsb-release
if [ -e /etc/lsb-release ]; then
. /etc/lsb-release
else
if [ -e /etc/os-release ]; then
. /etc/os-release
DISTRIB_ID=$ID
DISTRIB_RELEASE=$VERSION_ID
fi
# check for OS X
if [ -z "${DISTRIB_ID+x}" ] && [ "$(uname)" == "Darwin" ]; then
DISTRIB_ID=$(uname)
DISTRIB_RELEASE=$(uname -a | cut -d' ' -f3)
fi
fi
if [ -z "${DISTRIB_ID+x}" ] || [ -z "${DISTRIB_RELEASE+x}" ]; then
echo "Unknown DISTRIB_ID or DISTRIB_RELEASE."
echo "Please refer to the Installation documentation for manual installation steps."
exit 1
fi
# lowercase vars
if [ "$DISTRIB_ID" != "Darwin" ]; then
DISTRIB_ID=${DISTRIB_ID,,}
DISTRIB_RELEASE=${DISTRIB_RELEASE,,}
fi
echo "Detected OS: $DISTRIB_ID $DISTRIB_RELEASE"
# install requisite packages from distro repo
if [ -n "$SUDO" ] || [ "$(id -u)" -eq 0 ]; then
if [ "$DISTRIB_ID" == "ubuntu" ] || [ "$DISTRIB_ID" == "debian" ]; then
$SUDO apt-get update
if [ $ANACONDA -eq 1 ]; then
PYTHON_PKGS=
else
if [ $PYTHON == "python" ]; then
PYTHON_PKGS="libpython-dev python-dev"
if [ $ANACONDA -eq 0 ]; then
PYTHON_PKGS="$PYTHON_PKGS python-pip"
fi
else
PYTHON_PKGS="libpython3-dev python3-dev"
if [ $ANACONDA -eq 0 ]; then
PYTHON_PKGS="$PYTHON_PKGS python3-pip"
fi
fi
fi
# shellcheck disable=SC2086
$SUDO apt-get install -y --no-install-recommends \
build-essential libssl-dev libffi-dev openssl \
openssh-client rsync $PYTHON_PKGS
elif [ "$DISTRIB_ID" == "centos" ] || [ "$DISTRIB_ID" == "rhel" ]; then
$SUDO yum makecache fast
if [ $ANACONDA -eq 1 ]; then
PYTHON_PKGS=
else
if [ $PYTHON == "python" ]; then
PYTHON_PKGS="python-devel"
else
if ! yum list installed epel-release; then
echo "epel-release package not installed."
echo "Please install the epel-release package or refer to the Installation documentation for manual installation steps".
exit 1
fi
if ! yum list installed python34; then
echo "python34 epel package not installed."
echo "Please install the python34 epel package or refer to the Installation documentation for manual installation steps."
exit 1
fi
PYTHON_PKGS="python34-devel"
fi
fi
# shellcheck disable=SC2086
$SUDO yum install -y gcc openssl-devel libffi-devel openssl \
openssh-clients rsync $PYTHON_PKGS
if [ $ANACONDA -eq 0 ]; then
curl -fSsL --tlsv1 https://bootstrap.pypa.io/get-pip.py | $SUDO $PYTHON
fi
elif [ "$DISTRIB_ID" == "opensuse" ] || [ "$DISTRIB_ID" == "sles" ]; then
$SUDO zypper ref
if [ $ANACONDA -eq 1 ]; then
PYTHON_PKGS=
else
if [ $PYTHON == "python" ]; then
PYTHON_PKGS="python-devel"
else
PYTHON_PKGS="python3-devel"
fi
fi
# shellcheck disable=SC2086
$SUDO zypper -n in gcc libopenssl-devel libffi48-devel openssl \
openssh rsync $PYTHON_PKGS
if [ $ANACONDA -eq 0 ]; then
curl -fSsL --tlsv1 https://bootstrap.pypa.io/get-pip.py | $SUDO $PYTHON
fi
elif [ "$DISTRIB_ID" == "Darwin" ]; then
# check for pip, otherwise install it
if hash $PIP 2> /dev/null; then
echo "$PIP detected."
else
echo "$PIP not found, installing for Python"
$SUDO $PYTHON -m ensurepip
fi
else
echo "Unsupported distribution."
echo "Please refer to the Installation documentation for manual installation steps."
exit 1
fi
fi
# create virtual env if required and install required python packages
if [ -n "$VENV_NAME" ]; then
# install virtual env if required
if [ $INSTALL_VENV_BIN -eq 1 ]; then
if [ -n "$SUDO" ] || [ "$(id -u)" -eq 0 ]; then
$SUDO $PIP install virtualenv
else
$PIP install --user virtualenv
fi
fi
if [ $ANACONDA -eq 0 ]; then
# create venv if it doesn't exist
if [ -n "$SUDO" ] || [ "$(id -u)" -eq 0 ]; then
virtualenv -p $PYTHON "$VENV_NAME"
else
"${HOME}"/.local/bin/virtualenv -p $PYTHON "$VENV_NAME"
fi
source "${VENV_NAME}"/bin/activate
$PYTHON -m pip install --upgrade pip
$PIP install --upgrade setuptools
set +e
$PIP uninstall -y azure-storage
set -e
$PIP install --upgrade -r requirements.txt
$PIP install --upgrade --no-deps -r req_nodeps.txt
deactivate
else
# set python version
pyver=$($PYTHON -c "import sys;a=sys.version_info;print('{}.{}'.format(a[0],a[1]));")
echo "Creating conda env for Python $pyver"
# create conda env
set +e
conda create --yes --name "$VENV_NAME" python="${pyver}"
set -e
source activate "$VENV_NAME"
conda install --yes pip
# temporary workaround with pip requirements upgrading setuptools and
# conda pip failing to reference the old setuptools version
set +e
$PIP install --upgrade setuptools
$PIP uninstall -y azure-storage
set -e
$PIP install --upgrade -r requirements.txt
$PIP install --upgrade --no-deps -r req_nodeps.txt
source deactivate "$VENV_NAME"
fi
else
$PYTHON -m pip install --upgrade --user pip
$PIP install --upgrade --user setuptools
set +e
$PIP uninstall -y azure-storage
set -e
$PIP install --upgrade --user -r requirements.txt
$PIP install --upgrade --no-deps --user -r req_nodeps.txt
fi
# create shipyard script
cat > shipyard << EOF
#!/usr/bin/env bash
set -e
set -f
BATCH_SHIPYARD_ROOT_DIR=$PWD
VENV_NAME=$VENV_NAME
EOF
cat >> shipyard << 'EOF'
if [ -z $BATCH_SHIPYARD_ROOT_DIR ]; then
echo Batch Shipyard root directory not set.
echo Please rerun the install.sh script.
exit 1
fi
EOF
if [ -n "$VENV_NAME" ]; then
if [ $ANACONDA -eq 0 ]; then
cat >> shipyard << 'EOF'
source $BATCH_SHIPYARD_ROOT_DIR/$VENV_NAME/bin/activate
EOF
else
cat >> shipyard << 'EOF'
source activate $VENV_NAME
EOF
fi
fi
if [ $PYTHON == "python" ]; then
cat >> shipyard << 'EOF'
python $BATCH_SHIPYARD_ROOT_DIR/shipyard.py $*
EOF
else
cat >> shipyard << 'EOF'
python3 $BATCH_SHIPYARD_ROOT_DIR/shipyard.py $*
EOF
fi
if [ -n "$VENV_NAME" ]; then
if [ $ANACONDA -eq 0 ]; then
cat >> shipyard << 'EOF'
deactivate
EOF
else
cat >> shipyard << 'EOF'
source deactivate $VENV_NAME
EOF
fi
fi
chmod 755 shipyard
echo ""
if [ -z "$VENV_NAME" ]; then
# shellcheck disable=SC2016
echo '>> Please add $HOME/.local/bin to your $PATH. You can do this '
echo '>> permanently in your shell rc script, e.g., .bashrc for bash shells.'
echo ""
fi
echo ">> Install complete for $PYTHON. Please run Batch Shipyard as: $PWD/shipyard"