forked from Azure/batch-shipyard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·316 lines (293 loc) · 8.8 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
#!/usr/bin/env bash
set -e
set -o pipefail
# vars
PYTHON=python
PIP=pip
SUDO=sudo
VENV_NAME=
# process options
while getopts "h?23ce:" opt; do
case "$opt" in
h|\?)
echo "install.sh parameters"
echo ""
echo "-2 install for Python 2.7"
echo "-3 install for Python 3.3+"
echo "-c install for Cloud Shell"
echo "-e [environment name] install to a virtual environment"
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
;;
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
# non-cloud shell environment checks
if [ ! -z $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
$PYTHON -c "from __future__ import print_function; import sys; print(sys.version)" | grep -Ei 'anaconda|continuum|conda-forge'
if [ $? -eq 0 ]; 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 [ ! -z $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
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
# install requisite packages from distro repo
if [ ! -z $SUDO ] || [ $(id -u) -eq 0 ]; then
if [ $DISTRIB_ID == "ubuntu" ] || [ $DISTRIB_ID == "debian" ]; then
$SUDO apt-get update
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
$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
if [ $PYTHON == "python" ]; then
PYTHON_PKGS="python-devel"
else
if [ $(yum list installed epel-release) -ne 0 ]; 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) -ne 0 ]; 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
$SUDO yum install -y gcc openssl-devel libffi-devel openssl \
openssh-clients rsync $PYTHON_PKGS
if [ $ANACONDA -eq 0 ]; then
curl -fSsL https://bootstrap.pypa.io/get-pip.py | $SUDO $PYTHON
fi
elif [ $DISTRIB_ID == "opensuse" ] || [ $DISTRIB_ID == "sles" ]; then
$SUDO zypper ref
if [ $PYTHON == "python" ]; then
PYTHON_PKGS="python-devel"
else
PYTHON_PKGS="python3-devel"
fi
$SUDO zypper -n in gcc libopenssl-devel libffi48-devel openssl \
openssh rsync $PYTHON_PKGS
if [ $ANACONDA -eq 0 ]; then
curl -fSsL 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 [ ! -z $VENV_NAME ]; then
# install virtual env if required
if [ $INSTALL_VENV_BIN -eq 1 ]; then
if [ ! -z $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 [ ! -z $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
$PIP install --upgrade pip setuptools
$PIP install --upgrade -r requirements.txt
deactivate
else
# create conda env
set +e
conda create --yes --name $VENV_NAME
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
set -e
$PIP install --upgrade -r requirements.txt
source deactivate $VENV_NAME
fi
else
$SUDO $PIP install --upgrade pip setuptools
$PIP install --upgrade --user -r requirements.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 [ ! -z $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 [ ! -z $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
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"