Skip to content

Instantly share code, notes, and snippets.

@a1git
Created March 28, 2018 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a1git/7351a3aa3e95fd43220781dab740d28d to your computer and use it in GitHub Desktop.
Save a1git/7351a3aa3e95fd43220781dab740d28d to your computer and use it in GitHub Desktop.
#!/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 "/var/www/repo/venvs/17.0.0/ubuntu-16.04-x86_64" &>/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"
# Install pip, setuptools, wheel into it
# Rather than allow virtualenv to decide which version
# of pip/setuptools/wheel we use, we create the virtualenv
# without them and use get-pip.py later to install them
# into the venv. This prevents messy upgrade/downgrade
# failures when trying to install the versions we want.
# We also inform get-pip.py using the --find-links option
# that there are local wheels available so that it uses
# them instead of reaching out to the internet unnecessarily.
${ROLE_VENV_PATH}/bin/python /var/www/repo/os-releases/17.0.0/ubuntu-16.04-x86_64/get-pip.py \
pip setuptools wheel \
--find-links /var/www/repo/os-releases/17.0.0/ubuntu-16.04-x86_64 \
${PIP_INSTALL_OPTIONS} &>${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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment