-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·136 lines (111 loc) · 3.7 KB
/
setup.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
#!/usr/bin/env bash
# A deployment bootstrap script for setting up Python environment tasks like
# installing setting up a virtualenv and importing requirements.txt into pip.
# Can be run multiple times, it is written to be idempotent.
#
# Version 1.5.2
this_file=`basename "$0"`
this_path=`cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd`
this_dir=`basename "$this_path"`
project_name="${this_dir}"
required_minimum_python_version_major=''
required_minimum_python_version_minor=''
required_minimum_python_version_minor_minor=''
virtualenvs_dir="/usr/lib/virtualenvs"
project_virtualenv_path="${virtualenvs_dir}/${project_name}"
individual_setup_files_dir="./setup"
set -o nounset
set -o errtrace
set -o errexit
set -o pipefail
log () {
printf "$*\n"
}
error () {
log "ERROR: " "$*\n"
exit 1
}
help () {
echo "Usage is './${this_file}'"
echo "No additional flags or arguments currently implemented."
}
# Application functions
before_exit () {
# Works like a finally statement
# Code that must always be run goes here
return
} ; trap before_exit EXIT
verify_python_version () {
python_version=`python -V 2>&1 | awk -F' ' '{print $2}'`
python_major_version=`echo $python_version | awk -F'.' '{print $1}'`
python_minor_version=`echo $python_version | awk -F'.' '{print $2}'`
python_minor_minor_version=`echo $python_version | awk -F'.' '{print $3}'`
if [[ -z "$required_minimum_python_version_major" ]]; then
return 0
else
if [[ "$python_major_version" -lt "$required_minimum_python_version_major" ]]; then
error "Python version below required threshold, ${python_major_version}<${required_minimum_python_version_major}."
fi
fi
if [[ -z "$required_minimum_python_version_minor" ]]; then
return 0
else
if [[ "$python_minor_version" -lt "$required_minimum_python_version_minor" ]]; then
error "Python version below required threshold, ${python_minor_version}<${required_minimum_python_version_minor}."
fi
fi
if [[ -z "$required_minimum_python_version_minor_minor" ]]; then
return 0
else
if [[ "$python_minor_minor_version" -lt "$required_minimum_python_version_minor_minor" ]]; then
error "Python version below required threshold, ${python_minor_minor_version}<${required_minimum_python_version_minor_minor}."
fi
fi
}
verify_virtualenv_binary_installed () {
if ! which virtualenv 1>/dev/null; then
error "Required python package virtualenv not installed."
fi
}
create_system_virtualenvs_dir_if_needed () {
if [[ -d "$virtualenvs_dir" ]]; then
return 0
fi
if ! mkdir "$virtualenvs_dir"; then
error "Could not create common virtualenv dir, 'mkdir ${$virtualenvs_dir}'."
fi
}
create_project_virtualenv_if_needed () {
if [[ -d "$project_virtualenv_path" ]]; then
return 0
fi
if ! virtualenv "$project_virtualenv_path"; then
error "Could not create virtualenv '${project_virtualenv_path}'."
fi
}
install_pip_packages_into_virtualenv_if_requires_file () {
if [[ ! -f "./requirements.txt" ]]; then
return 0
fi
if [[ ! -f "${project_virtualenv_path}/bin/pip" ]]; then
error "Could not find virtualenv's pip binary to install requirements.txt '${project_virtualenv_path}/bin/pip'."
fi
if ! ${project_virtualenv_path}/bin/pip install -r requirements.txt; then
error "Could not install pip packages inside requirements.txt."
fi
}
execute_individual_setup_files () {
if [[ ! -d "${individual_setup_files_dir}" ]]; then
return 0
fi
find "`pwd`/${individual_setup_files_dir}" -maxdepth 1 -type f | while read filename; do
source "${filename}"
done
}
# Application execution
verify_python_version
verify_virtualenv_binary_installed
create_system_virtualenvs_dir_if_needed
create_project_virtualenv_if_needed
install_pip_packages_into_virtualenv_if_requires_file
execute_individual_setup_files