openstack-ansible-repo_build/templates/venv-build-script.sh.j2

120 lines
3.7 KiB
Django/Jinja

#!/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}"
# 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"