-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_venv.sh
152 lines (138 loc) · 4.64 KB
/
create_venv.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
#!/bin/bash
#
# Script to create a virtual environment suitable for
# developing/installing NGAS, or to use the fabric scripts
#
# ICRAR - International Centre for Radio Astronomy Research
# (c) UWA - The University of Western Australia, 2016
# Copyright by UWA (in the framework of the ICRAR)
# All rights reserved
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#
#
# This script creates a virtual environment.
# This new venv can be then used both to install NGAS on it
# (either normally, or in development mode)
# or to locally support the fabric-based remote installation procedure.
#
error() {
echo "ERROR: $1" 1>&2
exit 1
}
warning() {
echo "WARNING: $1" 1>&2
}
function print_usage {
echo "$0 [-h | -?] [-f] [-p <python_exec>] <virtualenv_dir>"
echo
echo "-h, -?: Show this help"
echo "-p <python_exec>: Use <python_exec> as the python interpreted for this virtualenv"
echo "-f: Install Fabric into the virtual environment"
}
# Command-line option parsing
FABRIC_READY=
PYTHON_EXEC=
while getopts "fp:h?" opt
do
case "$opt" in
f)
FABRIC_READY=yes
;;
p)
PYTHON_EXEC="$OPTARG"
;;
[h?])
print_usage
exit 0
;;
:)
print_usage 1>&2
exit 1
esac
done
if [ $(($# - $OPTIND)) -lt 0 ]
then
print_usage 1>&2
exit 1
fi
veDir=${@:$OPTIND:1}
if [[ -d "$veDir" ]]
then
error "$veDir already exists"
fi
# First things first, check that we have python installed
# We default to whatever is in the path if not specified
PYTHON_EXEC=${PYTHON_EXEC:-python}
if [[ -z "$(command -v ${PYTHON_EXEC} 2> /dev/null)" ]]
then
error "No Python found in this system, install Python 2.7"
fi
# Check that the python version is correct
pythonVersion=$(${PYTHON_EXEC} -V 2>&1)
if [[ ! "$pythonVersion" == *"2.7"* ]]
then
error "Python 2.7 needed, found: $pythonVersion"
fi
# Check if we already have virtualenv
# If not download one and untar it
veCommand="virtualenv -p $PYTHON_EXEC"
sourceCommand="source -- $veDir/bin/activate"
if [[ -z "$(command -v virtualenv 2> /dev/null)" ]]
then
VIRTUALENV_URL='https://pypi.python.org/packages/8b/2c/c0d3e47709d0458816167002e1aa3d64d03bdeb2a9d57c5bd18448fd24cd/virtualenv-15.0.3.tar.gz#md5=a5a061ad8a37d973d27eb197d05d99bf'
if [[ ! -z "$(command -v wget 2> /dev/null)" ]]
then
wget "$VIRTUALENV_URL" || error "Failed to download virtualenv"
elif [[ ! -z "$(command -v curl 2> /dev/null)" ]]
then
curl "$VIRTUALENV_URL" -o virtualenv-15.0.3.tar.gz || error "Failed to download virtualenv"
else
error "Can't find a download tool (tried wget and curl), cannot download virtualenv"
fi
tar xf virtualenv-15.0.3.tar.gz || error "Failed to untar virtualenv"
veCommand="$PYTHON_EXEC virtualenv-15.0.3/virtualenv.py -p $PYTHON_EXEC"
REMOVE_VE="yes"
fi
# Create a virtual environment for the NGAMS installation procedure to begin
# and source it
$veCommand -- "$veDir" || error "Failed to create virtualenv"
if [[ "$REMOVE_VE" == "yes" ]]
then
rm -rf virtualenv-15.0.3 virtualenv-15.0.3.tar.gz || warning "Failed to remove temporary copy of virtualenv"
fi
# Install initial packages into the new venv
# Fabric is needed to allow using the fab scripts in the first place.
# pycrypto is needed by the SSH pubkey-related bits in the fab scripts.
# boto is needed to support the aws-related fab tasks.
if [[ "$FABRIC_READY" == "yes" ]]
then
$sourceCommand || error "Failed to source virtualenv"
pip install boto Fabric pycrypto || error "Failed to install fabric packages in virtualenv"
fi
echo
echo
echo "----------------------------------------------------------------------------"
echo "Virtual Environment successfully created!"
echo "Now run the following command on your shell to load the virtual environment:"
echo
echo "$sourceCommand"
echo
echo "You can now use this virtual environment to either locally install NGAS"
echo "(normally or in development mode, see ./build.sh -h), or to run the remote"
echo "installation procedures via fabric scripts (run fab -l for more information)"
echo "----------------------------------------------------------------------------"
echo