This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
/
venv-build-script.sh.j2
127 lines (96 loc) · 3.97 KB
/
venv-build-script.sh.j2
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
#!/bin/bash
# Copyright 2017, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
## Shell Opts ----------------------------------------------------------------
set -e
## Variables -----------------------------------------------------------------
# The options file for the venv
ROLE_VENV_REQUIREMENTS_FILE="${1}"
## Functions -----------------------------------------------------------------
usage() {
cat <<EOF
Usage:
${0} <path to configuration options file>
EOF
}
## Main ----------------------------------------------------------------------
# Validate that an options file as been provided
if [[ -z "${ROLE_VENV_REQUIREMENTS_FILE}" ]]; then
usage
exit 1
fi
# Source the options file
source "${ROLE_VENV_REQUIREMENTS_FILE}"
# Output the beginning of the build
echo -n "Building venv ${ROLE_VENV_FILE}..."
# Set the log file path
ROLE_VENV_LOG="/var/log/repo/venv_build_${ROLE_VENV_FILE}.log"
# Begin the venv build
pushd "{{ repo_build_venv_dir }}" &>/dev/null
# If the venv achive already exists, remove it
[[ -e "${ROLE_VENV_FILE}.tgz" ]] && rm -f "${ROLE_VENV_FILE}.tgz"
# If the venv checksum file already exists, remove it
[[ -e "${ROLE_VENV_FILE}.checksum" ]] && rm -f "${ROLE_VENV_FILE}.checksum"
# If the venv working directory already exists, remove it
[[ -d "${ROLE_VENV_PATH}" ]] && rm -rf "${ROLE_VENV_PATH}"
# If the pip build directory already exists, remove it
[[ -d "/tmp/${ROLE_VENV_FILE}" ]] && rm -rf "/tmp/${ROLE_VENV_FILE}"
# Create the virtualenv shell
${VENV_CREATE_COMMAND} "${ROLE_VENV_PATH}" &>${ROLE_VENV_LOG}
# Create the pip build directory
mkdir -p "/tmp/${ROLE_VENV_FILE}"
# Activate the python virtual environment for good measure
source "${ROLE_VENV_PATH}/bin/activate"
# Upgrade pip, setuptools and wheel to the version we want
${ROLE_VENV_PATH}/bin/pip install \
--disable-pip-version-check \
--quiet --quiet \
--build "/tmp/${ROLE_VENV_FILE}" \
${PIP_INSTALL_OPTIONS} \
--constraint {{ repo_build_release_path }}/requirements_constraints.txt \
--upgrade \
--index-url {{ repo_build_pip_default_index }} \
--trusted-host {{ repo_build_pip_default_index | netloc_no_port }} \
pip setuptools wheel \
--log "${ROLE_VENV_LOG}"
# Install the packages into the venv
${ROLE_VENV_PATH}/bin/pip install \
--disable-pip-version-check \
--quiet --quiet \
--build "/tmp/${ROLE_VENV_FILE}" \
${PIP_INSTALL_OPTIONS} \
${PIP_INDEX_OPTIONS} \
${ROLE_VENV_REQUIREMENTS} \
--log "${ROLE_VENV_LOG}"
# If the lib64 symlink exists, change it to a relative symlink
# so that it works when extracting the venv from the tarball
if [[ -L ${ROLE_VENV_PATH}/lib64 ]]; then
pushd ${ROLE_VENV_PATH} &>/dev/null
ln -sfn lib lib64
popd &>/dev/null
fi
# Deactivate the venv for good measure
deactivate
# Find and remove all of the python pyc files
find "${ROLE_VENV_PATH}" -type f -name '*.pyc' -delete 2>>${ROLE_VENV_LOG}
# Create the archive
tar czf "${ROLE_VENV_FILE}.tgz" -C "${ROLE_VENV_PATH}" . 2>>${ROLE_VENV_LOG}
# Create a checksum file for the archive
sha1sum "${ROLE_VENV_FILE}.tgz" | awk '{print $1}' > "${ROLE_VENV_FILE}.checksum" 2>>${ROLE_VENV_LOG}
# Delete working directories
rm -rf "${ROLE_VENV_PATH}"
rm -rf "/tmp/${ROLE_VENV_FILE}"
popd &>/dev/null
# Output the end of the build
echo "done"