Allow to build image with common from Gerrit

Story: 2001694
Task: 24226

Change-Id: I356c9fdde670433bef1f3c70e02f2f760521ee0a
This commit is contained in:
Dobroslaw Zybort 2018-07-27 11:53:16 +02:00
parent adfc935a62
commit 15c761a6af
3 changed files with 95 additions and 19 deletions

View File

@ -44,9 +44,11 @@ ONBUILD ARG GITHUB_REPO
ONBUILD ARG REPO_VERSION ONBUILD ARG REPO_VERSION
ONBUILD ARG GIT_COMMIT ONBUILD ARG GIT_COMMIT
ONBUILD ARG CONSTRAINTS_BRANCH ONBUILD ARG CONSTRAINTS_BRANCH
ONBUILD ARG CONSTRAINTS_FILE=http://git.openstack.org/cgit/openstack/requirements/plain/upper-constraints.txt ONBUILD ARG CONSTRAINTS_FILE
ONBUILD ARG EXTRA_DEPS ONBUILD ARG EXTRA_DEPS
ONBUILD ARG COMMON_REPO=https://git.openstack.org/openstack/monasca-common ONBUILD ARG COMMON_REPO
ONBUILD ARG COMMON_VERSION
ONBUILD ARG COMMON_GIT_COMMIT
# Build-time metadata as defined at # Build-time metadata as defined at
# https://github.com/opencontainers/image-spec/blob/master/annotations.md # https://github.com/opencontainers/image-spec/blob/master/annotations.md
@ -59,6 +61,9 @@ ONBUILD LABEL org.opencontainers.image.revision="$GIT_COMMIT"
ONBUILD LABEL org.opencontainers.image.licenses="Apache-2.0" ONBUILD LABEL org.opencontainers.image.licenses="Apache-2.0"
ONBUILD LABEL org.openstack.constraints_uri="$CONSTRAINTS_FILE?h=$CONSTRAINTS_BRANCH" ONBUILD LABEL org.openstack.constraints_uri="$CONSTRAINTS_FILE?h=$CONSTRAINTS_BRANCH"
ONBUILD LABEL org.openstack.monasca.python.extra_deps="$EXTRA_DEPS" ONBUILD LABEL org.openstack.monasca.python.extra_deps="$EXTRA_DEPS"
ONBUILD LABEL org.openstack.monasca.common.source="$COMMON_REPO"
ONBUILD LABEL org.openstack.monasca.common.version="$COMMON_VERSION"
ONBUILD LABEL org.openstack.monasca.common.revision="$COMMON_GIT_COMMIT"
# Every child image need to provide starting and health check script. # Every child image need to provide starting and health check script.
# If they're not provided build will fail. We want that for uniformity. # If they're not provided build will fail. We want that for uniformity.
@ -81,15 +86,30 @@ ONBUILD RUN \
mkdir -p /app && \ mkdir -p /app && \
git -C /app init && \ git -C /app init && \
git -C /app remote add origin "$APP_REPO" && \ git -C /app remote add origin "$APP_REPO" && \
echo "Cloning app in version: $REPO_VERSION" && \
git -C /app fetch origin "$REPO_VERSION" && \ git -C /app fetch origin "$REPO_VERSION" && \
git -C /app reset --hard FETCH_HEAD && \ git -C /app reset --hard FETCH_HEAD && \
wget --output-document /app/upper-constraints.txt \ wget --output-document /app/upper-constraints.txt \
"$CONSTRAINTS_FILE"?h="$CONSTRAINTS_BRANCH" && \ "$CONSTRAINTS_FILE"?h="$CONSTRAINTS_BRANCH" && \
# Download monasca-common
mkdir -p /monasca-common && \
git -C /monasca-common init && \
git -C /monasca-common remote add origin "$COMMON_REPO" && \
echo "Cloning monasca-common in version: $COMMON_VERSION" && \
git -C /monasca-common fetch origin "$COMMON_VERSION" && \
git -C /monasca-common reset --hard FETCH_HEAD && \
# When creating image from master, stable branch or commit use # When creating image from master, stable branch or commit use
# monasca-common from git repository. # monasca-common from git repository in /monasca-common,
[ ! "$(git -C /app tag -l ${REPO_VERSION})" ] && \ # otherwise use the version from upper-constraints.txt
sed -i "s|monasca-common.*|-e git+$COMMON_REPO@$CONSTRAINTS_BRANCH#egg=monasca-common|" \ [ ! "$(git -C /monasca-common tag -l ${COMMON_VERSION})" ] && \
sed -i "s|monasca-common.*||" \
/app/requirements.txt || true && \
[ ! "$(git -C /monasca-common tag -l ${COMMON_VERSION})" ] && \
sed -i "s|monasca-common.*||" \
/app/upper-constraints.txt || true && \ /app/upper-constraints.txt || true && \
pip3 install \
--editable /monasca-common \
--constraint /app/upper-constraints.txt && \
# Install packages needed by wait scripts and used for templating. # Install packages needed by wait scripts and used for templating.
pip3 install \ pip3 install \
pykafka \ pykafka \
@ -105,20 +125,24 @@ ONBUILD RUN \
printf "App: %s\\n" "$DOCKER_IMAGE" >> /VERSIONS && \ printf "App: %s\\n" "$DOCKER_IMAGE" >> /VERSIONS && \
printf "Repository: %s\\n" "$APP_REPO" >> /VERSIONS && \ printf "Repository: %s\\n" "$APP_REPO" >> /VERSIONS && \
printf "Version: %s\\n" "$REPO_VERSION" >> /VERSIONS && \ printf "Version: %s\\n" "$REPO_VERSION" >> /VERSIONS && \
printf "Revision: %s\\n" "$GIT_COMMIT" >> /VERSIONS && \
printf "Build date: %s\\n" "$CREATION_TIME" >> /VERSIONS && \ printf "Build date: %s\\n" "$CREATION_TIME" >> /VERSIONS && \
printf "Revision: %s\\n" \ printf "Revision: %s\\n" \
"$(git -C /app rev-parse FETCH_HEAD)" >> /VERSIONS && \ "$(git -C /app rev-parse FETCH_HEAD)" >> /VERSIONS && \
printf "Monasca-common version: %s\\n" \ printf "Monasca-common version: %s\\n" "$COMMON_VERSION" \
>> /VERSIONS && \
printf "Monasca-common pip version: %s\\n" \
"$(pip3 freeze 2>&1 | grep 'monasca-common')" >> /VERSIONS && \ "$(pip3 freeze 2>&1 | grep 'monasca-common')" >> /VERSIONS && \
printf "Monasca-common revision: %s\\n" \
"$COMMON_GIT_COMMIT" >> /VERSIONS && \
printf "Constraints file: %s\\n" \ printf "Constraints file: %s\\n" \
"$CONSTRAINTS_FILE"?h="$CONSTRAINTS_BRANCH" >> /VERSIONS && \ "$CONSTRAINTS_FILE"?h="$CONSTRAINTS_BRANCH" >> /VERSIONS && \
# Clean after instalation. # Clean after instalation.
apk del .build-deps && \ apk del .build-deps && \
# Pip is leaving monasca-common repo in /src so remove it.
rm -rf \ rm -rf \
/app \ /app \
/root/.cache/ \ /root/.cache/ \
/src/monasca-common/java/ \ /monasca-common/java/ \
/tmp/* \ /tmp/* \
/var/cache/apk/* \ /var/cache/apk/* \
/var/log/* && \ /var/log/* && \

View File

@ -5,13 +5,14 @@ ARG APP_REPO=https://git.openstack.org/openstack/monasca-api
# Branch, tag or git hash to build from. # Branch, tag or git hash to build from.
ARG REPO_VERSION=master ARG REPO_VERSION=master
ARG CONSTRAINTS_BRANCH=master ARG CONSTRAINTS_BRANCH=master
ARG COMMON_VERSION=master
# Extra Python3 dependencies. # Extra Python3 dependencies.
ARG EXTRA_DEPS="gunicorn influxdb python-memcached" ARG EXTRA_DEPS="gunicorn influxdb python-memcached"
# Always start from `monasca-base` image and use specific tag of it. # Always start from `monasca-base` image and use specific tag of it.
ARG BASE_TAG=1.0.0 ARG BASE_TAG=1.0.0
FROM monasca-base:$BASE_TAG FROM monasca/base:$BASE_TAG
# Environment variables used for our service or wait scripts. # Environment variables used for our service or wait scripts.
ENV \ ENV \

View File

@ -19,22 +19,30 @@
set -x # Print each script step. set -x # Print each script step.
set -eo pipefail # Exit the script if any statement returns error. set -eo pipefail # Exit the script if any statement returns error.
# This script is used for building Docker image with proper labels. # This script is used for building Docker image with proper labels
# and proper version of monasca-common.
# #
# Example usage: # Example usage:
# $ ./build_image.sh <repository_version> <upper_constains_branch> # $ ./build_image.sh <repository_version> <upper_constains_branch> <common_version>
#
# Everything after `./build_image.sh` is optional and by default configured
# to get versions from `Dockerfile`.
# #
# To build from master branch (default): # To build from master branch (default):
# $ ./build_image.sh # $ ./build_image.sh
# To build specific version run this script in the following way: # To build specific version run this script in the following way:
# $ ./build_image.sh stable/queens # $ ./build_image.sh stable/queens
# Building from specific commit: # Building from specific commit:
# $ ./build_image.sh cb7f226 # $ ./build_image.sh cb7f226
# When building from a tag monasca-common will be used in version available # When building from a tag monasca-common will be used in version available
# in upper constraint file: # in upper constraint file:
# $ ./build_image.sh 2.5.0 # $ ./build_image.sh 2.5.0
# To build image from Gerrit patch sets that is targeting branch stable/queens: # To build image from Gerrit patch sets that is targeting branch stable/queens:
# $ ./build_image.sh refs/changes/51/558751/1 stable/queens # $ ./build_image.sh refs/changes/51/558751/1 stable/queens
#
# If you want to build image with custom monasca-common version you need
# to provide it as in the following example:
# $ ./build_image.sh master master refs/changes/19/595719/3
[ -z "$DOCKER_IMAGE" ] && \ [ -z "$DOCKER_IMAGE" ] && \
DOCKER_IMAGE=$(\grep DOCKER_IMAGE Dockerfile | cut -f2 -d"=") DOCKER_IMAGE=$(\grep DOCKER_IMAGE Dockerfile | cut -f2 -d"=")
@ -50,19 +58,47 @@ REPO_VERSION_CLEAN=$(echo "$REPO_VERSION" | sed 's|/|-|g')
GITHUB_REPO=$(echo "$APP_REPO" | sed 's/git.openstack.org/github.com/' | \ GITHUB_REPO=$(echo "$APP_REPO" | sed 's/git.openstack.org/github.com/' | \
sed 's/ssh:/https:/') sed 's/ssh:/https:/')
if [ -z "$CONSTRAINTS_FILE" ]; then
CONSTRAINTS_FILE=$(\grep CONSTRAINTS_FILE Dockerfile | cut -f2 -d"=") || true
: "${CONSTRAINTS_FILE:=http://git.openstack.org/cgit/openstack/requirements/plain/upper-constraints.txt}"
fi
: "${CONSTRAINTS_BRANCH:=$2}" : "${CONSTRAINTS_BRANCH:=$2}"
[ -z "$CONSTRAINTS_BRANCH" ] && \ [ -z "$CONSTRAINTS_BRANCH" ] && \
CONSTRAINTS_BRANCH=$(\grep CONSTRAINTS_BRANCH Dockerfile | cut -f2 -d"=") CONSTRAINTS_BRANCH=$(\grep CONSTRAINTS_BRANCH Dockerfile | cut -f2 -d"=")
# When using stable version of repository use same stable constraints file. # When using stable version of repository use same stable constraints file.
case "$REPO_VERSION" in case "$REPO_VERSION" in
*stable*) *stable*)
CONSTRAINTS_BRANCH_CLEAN="$REPO_VERSION" CONSTRAINTS_BRANCH_CLEAN="$REPO_VERSION"
# Get monasca-common version from stable upper constraints file.
CONSTRAINTS_TMP_FILE=$(mktemp)
wget --output-document "$CONSTRAINTS_TMP_FILE" \
"$CONSTRAINTS_FILE"?h="$CONSTRAINTS_BRANCH_CLEAN"
UPPER_COMMON=$(\grep 'monasca-common' "$CONSTRAINTS_TMP_FILE")
# Get only version part from monasca-common.
UPPER_COMMON_VERSION="${UPPER_COMMON##*===}"
rm -rf "$CONSTRAINTS_TMP_FILE"
;; ;;
*) *)
CONSTRAINTS_BRANCH_CLEAN="$CONSTRAINTS_BRANCH" CONSTRAINTS_BRANCH_CLEAN="$CONSTRAINTS_BRANCH"
;; ;;
esac esac
# Monasca-common variables.
if [ -z "$COMMON_REPO" ]; then
COMMON_REPO=$(\grep COMMON_REPO Dockerfile | cut -f2 -d"=") || true
: "${COMMON_REPO:=https://git.openstack.org/openstack/monasca-common}"
fi
: "${COMMON_VERSION:=$3}"
if [ -z "$COMMON_VERSION" ]; then
COMMON_VERSION=$(\grep COMMON_VERSION Dockerfile | cut -f2 -d"=") || true
if [ "$UPPER_COMMON_VERSION" ]; then
# Common from upper constraints file.
COMMON_VERSION="$UPPER_COMMON_VERSION"
fi
fi
# Clone project to temporary directory for getting proper commit number from # Clone project to temporary directory for getting proper commit number from
# branches and tags. We need this for setting proper image labels. # branches and tags. We need this for setting proper image labels.
# Docker does not allow to get any data from inside of system when building # Docker does not allow to get any data from inside of system when building
@ -76,12 +112,23 @@ TMP_DIR=$(mktemp -d)
git fetch origin "$REPO_VERSION" git fetch origin "$REPO_VERSION"
git reset --hard FETCH_HEAD git reset --hard FETCH_HEAD
) )
GIT_COMMIT=$(git -C "$TMP_DIR" rev-parse FETCH_HEAD) GIT_COMMIT=$(git -C "$TMP_DIR" rev-parse HEAD)
[ -z "${GIT_COMMIT}" ] && echo "No git commit hash found" && exit 1 [ -z "${GIT_COMMIT}" ] && echo "No git commit hash found" && exit 1
rm -rf "$TMP_DIR" rm -rf "$TMP_DIR"
# TODO(Dobroslaw): find a way to set label monasca-common with version # Do the same for monasca-common.
# we will be using with app. COMMON_TMP_DIR=$(mktemp -d)
(
cd "$COMMON_TMP_DIR"
# This many steps are needed to support gerrit patch sets.
git init
git remote add origin "$COMMON_REPO"
git fetch origin "$COMMON_VERSION"
git reset --hard FETCH_HEAD
)
COMMON_GIT_COMMIT=$(git -C "$COMMON_TMP_DIR" rev-parse HEAD)
[ -z "${COMMON_GIT_COMMIT}" ] && echo "No git commit hash found" && exit 1
rm -rf "$COMMON_TMP_DIR"
CREATION_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ") CREATION_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
# Docker tags don't like colons so use shorter version of ISO 8601 for them. # Docker tags don't like colons so use shorter version of ISO 8601 for them.
@ -93,6 +140,10 @@ docker build --no-cache \
--build-arg APP_REPO="$APP_REPO" \ --build-arg APP_REPO="$APP_REPO" \
--build-arg REPO_VERSION="$REPO_VERSION" \ --build-arg REPO_VERSION="$REPO_VERSION" \
--build-arg GIT_COMMIT="$GIT_COMMIT" \ --build-arg GIT_COMMIT="$GIT_COMMIT" \
--build-arg CONSTRAINTS_FILE="$CONSTRAINTS_FILE" \
--build-arg CONSTRAINTS_BRANCH="$CONSTRAINTS_BRANCH_CLEAN" \ --build-arg CONSTRAINTS_BRANCH="$CONSTRAINTS_BRANCH_CLEAN" \
--build-arg COMMON_REPO="$COMMON_REPO" \
--build-arg COMMON_VERSION="$COMMON_VERSION" \
--build-arg COMMON_GIT_COMMIT="$COMMON_GIT_COMMIT" \
--tag "$DOCKER_IMAGE":"$REPO_VERSION_CLEAN" \ --tag "$DOCKER_IMAGE":"$REPO_VERSION_CLEAN" \
--tag "$DOCKER_IMAGE":"$REPO_VERSION_CLEAN"-"$CREATION_TIME_SHORT" . --tag "$DOCKER_IMAGE":"$REPO_VERSION_CLEAN"-"$CREATION_TIME_SHORT" .